summaryrefslogtreecommitdiffstats
path: root/Source/cmCableData.cxx
blob: a1a811a4647cc0a16f5feed3dedee3a62c814808 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) 2000 National Library of Medicine
  All rights reserved.

  See COPYRIGHT.txt for copyright details.

=========================================================================*/
#include "cmCabilData.h"
#include "cmCacheManager.h"


/**
 * Free all data that was stored here.
 */
cmCabilData::~cmCabilData()
{
  for(OutputFiles::iterator i = m_OutputFiles.begin();
      i != m_OutputFiles.end(); ++i)
    {
    delete i->second;
    }
}


/**
 * The constructor attempts to open the file for writing.
 */
cmCabilData::OutputFile
::OutputFile(std::string file, const cmCabilCommand* command):
  m_FileStream(file.c_str()),
  m_FirstReferencingCommand(command),
  m_LastReferencingCommand(command)
{
  if(!m_FileStream)
    {
    cmSystemTools::Error("Error can not open for write: ", file.c_str());
    }
}


/**
 * Destructor closes the file, if it was open.
 */
cmCabilData::OutputFile
::~OutputFile()
{
  if(m_FileStream)
    m_FileStream.close();
}


/**
 * Get the output stream associated with this OutputFile.
 */
std::ostream&
cmCabilData::OutputFile
::GetStream()
{
  return m_FileStream;
}


void
cmCabilData::OutputFile
::SetLastReferencingCommand(const cmCabilCommand* command)
{
  m_LastReferencingCommand = command;
}


bool
cmCabilData::OutputFile
::FirstReferencingCommandIs(const cmCabilCommand* command) const
{
  return (m_FirstReferencingCommand == command);
}


bool
cmCabilData::OutputFile
::LastReferencingCommandIs(const cmCabilCommand* command) const
{
  return (m_LastReferencingCommand == command);
}


/**
 * Get the OutputFile for the file with the given name.  Automatically
 * maintains first and last referencing commands.
 */
cmCabilData::OutputFile*
cmCabilData::GetOutputFile(const std::string& name,
                           const cmCabilCommand* command)
{
  OutputFiles::iterator f = m_OutputFiles.find(name);
  // If the file hasn't yet been opened, create an entry for it.
  if(f == m_OutputFiles.end())
    {
    OutputFile* outputFile = new OutputFile(name, command);
    m_OutputFiles[name] = outputFile;
    
    return outputFile;
    }
  
  // The file has already been opened.  Set the command as the last
  // referencing command.
  f->second->SetLastReferencingCommand(command);
  
  return f->second;
}