]> Creatis software - gdcm.git/blob - src/gdcmElementSet.cxx
* src/gdcmFile.[h|cxx] : add the Print method
[gdcm.git] / src / gdcmElementSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmElementSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/12/16 11:37:02 $
7   Version:   $Revision: 1.34 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmElementSet.h"
20 #include "gdcmDebug.h"
21 #include "gdcmValEntry.h"
22 #include "gdcmBinEntry.h"
23 #include "gdcmSeqEntry.h"
24
25 namespace gdcm 
26 {
27
28 //-----------------------------------------------------------------------------
29 // Constructor / Destructor
30 /**
31  * \ingroup ElementSet
32  * \brief   Constructor from a given ElementSet
33  */
34 //BOZ depthLevel is not usefull anymore
35 ElementSet::ElementSet(int depthLevel) 
36               : DocEntrySet()
37 {
38   (void)depthLevel;
39 }
40
41 /**
42  * \ingroup ElementSet
43  * \brief   Canonical destructor.
44  */
45 ElementSet::~ElementSet() 
46 {
47    for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
48    {
49       if ( cc->second )
50       {
51          delete cc->second;
52       }
53    }
54    TagHT.clear();
55 }
56
57 //-----------------------------------------------------------------------------
58 // Print
59 /**
60   * \brief   Prints the Header Entries (Dicom Elements)
61   *          from the H Table
62   * @return
63   */ 
64 void ElementSet::Print(std::ostream& os)
65 {
66    for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
67    {
68       DocEntry* entry = i->second;
69       entry->Print(os);   
70       if ( SeqEntry* seqEntry = dynamic_cast<SeqEntry*>(entry) )
71       {
72          (void)seqEntry;
73          // Avoid the newline for a sequence:
74          continue;
75       }
76       os << std::endl;
77    }
78 }
79
80 //-----------------------------------------------------------------------------
81 // Public
82 /**
83   * \brief   Writes the Header Entries (Dicom Elements)
84   *          from the H Table
85   * @return
86   */ 
87 void ElementSet::WriteContent(std::ofstream* fp, FileType filetype)
88 {
89    for (TagDocEntryHT::const_iterator i = TagHT.begin(); 
90                                      i != TagHT.end(); 
91                                     ++i)
92    {
93       i->second->WriteContent(fp, filetype);
94    } 
95 }
96
97 //-----------------------------------------------------------------------------
98 // Protected
99
100 //-----------------------------------------------------------------------------
101 // Private
102
103 /**
104  * \brief   add a new Dicom Element pointer to the H Table
105  * @param   newEntry entry to add
106  */
107 bool ElementSet::AddEntry(DocEntry* newEntry)
108 {
109    const TagKey& key = newEntry->GetKey();
110
111    if( TagHT.count(key) == 1 )
112    {
113       dbg.Verbose(1, "ElementSet::AddEntry key already present: ",
114                   key.c_str());
115       return false;
116    }
117    else
118    {
119       TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
120       return true;
121    }
122 }
123
124 /**
125  * \brief   Clear the hash table from given entry AND delete the entry.
126  * @param   entryToRemove Entry to remove AND delete.
127  * \warning Some problems when using under Windows... prefer the use of
128  *          Initialize / GetNext methods
129  */
130 bool ElementSet::RemoveEntry( DocEntry* entryToRemove)
131 {
132    const TagKey& key = entryToRemove->GetKey();
133    if( TagHT.count(key) == 1 )
134    {
135       TagHT.erase(key);
136       dbg.Verbose(0, "ElementSet::RemoveEntry: one element erased.");
137       delete entryToRemove;
138       return true;
139    }
140
141    dbg.Verbose(0, "ElementSet::RemoveEntry: key not present");
142    return false ;
143 }
144
145 /**
146  * \brief   Clear the hash table from given entry BUT keep the entry.
147  * @param   entryToRemove Entry to remove.
148  */
149 bool ElementSet::RemoveEntryNoDestroy(DocEntry* entryToRemove)
150 {
151    const TagKey& key = entryToRemove->GetKey();
152    if( TagHT.count(key) == 1 )
153    {
154       TagHT.erase(key);
155       dbg.Verbose(0, "ElementSet::RemoveEntry: one element erased.");
156       return true;
157    }
158
159    dbg.Verbose(0, "ElementSet::RemoveEntry: key not present");
160    return false ;
161 }
162
163 /**
164  * \brief   Initialise the visit of the Hash table (TagHT)
165  */
166 void ElementSet::Initialize()
167 {
168    ItTagHT = TagHT.begin();
169 }
170
171 /**
172  * \brief   Get the next entry whil visiting the Hash table (TagHT)
173  * \return  The next DocEntry if found, otherwhise NULL
174  */
175 DocEntry *ElementSet::GetNextEntry()
176 {
177    if (ItTagHT != TagHT.end())
178    {
179       DocEntry *tmp = ItTagHT->second;
180       ++ItTagHT;
181
182       return(tmp);
183    }
184    else
185    {
186       return(NULL);
187    }
188 }
189
190 //-----------------------------------------------------------------------------
191 } // end namespace gdcm