]> Creatis software - gdcm.git/blob - src/gdcmElementSet.cxx
* src/ : fix compilation warnings for the Write method (2 different
[gdcm.git] / src / gdcmElementSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmElementSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/11/25 15:46:11 $
7   Version:   $Revision: 1.32 $
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 //-----------------------------------------------------------------------------
59 // Public
60
61
62 //-----------------------------------------------------------------------------
63 // Print
64 /**
65   * \brief   Prints the Header Entries (Dicom Elements)
66   *          from the H Table
67   * @return
68   */ 
69 void ElementSet::Print(std::ostream& os)
70 {
71    for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
72    {
73       DocEntry* entry = i->second;
74       entry->Print(os);   
75       if ( SeqEntry* seqEntry = dynamic_cast<SeqEntry*>(entry) )
76       {
77          (void)seqEntry;
78          // Avoid the newline for a sequence:
79          continue;
80       }
81       os << std::endl;
82    }
83 }
84
85 /**
86   * \brief   Writes the Header Entries (Dicom Elements)
87   *          from the H Table
88   * @return
89   */ 
90 void ElementSet::WriteContent(std::ofstream* fp, FileType filetype)
91 {
92    for (TagDocEntryHT::const_iterator i = TagHT.begin(); 
93                                      i != TagHT.end(); 
94                                     ++i)
95    {
96       i->second->WriteContent(fp, filetype);
97    } 
98 }
99 //-----------------------------------------------------------------------------
100 // Protected
101
102 //-----------------------------------------------------------------------------
103
104 //-----------------------------------------------------------------------------
105 // Private
106
107 /**
108  * \brief   add a new Dicom Element pointer to the H Table
109  * @param   newEntry entry to add
110  */
111 bool ElementSet::AddEntry(DocEntry* newEntry)
112 {
113    const TagKey& key = newEntry->GetKey();
114
115    if( TagHT.count(key) == 1 )
116    {
117       dbg.Verbose(1, "ElementSet::AddEntry key already present: ",
118                   key.c_str());
119       return false;
120    }
121    else
122    {
123       TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
124       return true;
125    }
126 }
127
128 /**
129  * \brief   Clear the hash table from given entry BUT keep the entry.
130  * @param   entryToRemove Entry to remove.
131  */
132 bool ElementSet::RemoveEntryNoDestroy(DocEntry* entryToRemove)
133 {
134    const TagKey& key = entryToRemove->GetKey();
135    if( TagHT.count(key) == 1 )
136    {
137       TagHT.erase(key);
138       dbg.Verbose(0, "ElementSet::RemoveEntry: one element erased.");
139       return true;
140    }
141
142    dbg.Verbose(0, "ElementSet::RemoveEntry: key not present: ");
143    return false ;
144 }
145
146 /**
147  * \brief   Clear the hash table from given entry AND delete the entry.
148  * @param   entryToRemove Entry to remove AND delete.
149  * \warning Some problems when using under Windows... prefer the use of
150  *          Initialize / GetNext methods
151  */
152 bool ElementSet::RemoveEntry( DocEntry* entryToRemove)
153 {
154    const TagKey& key = entryToRemove->GetKey();
155    if( TagHT.count(key) == 1 )
156    {
157       TagHT.erase(key);
158       dbg.Verbose(0, "ElementSet::RemoveEntry: one element erased.");
159       delete entryToRemove;
160       return true;
161    }
162
163    dbg.Verbose(0, "ElementSet::RemoveEntry: key not present: ");
164    return false ;
165 }
166
167 /**
168  * \brief   Initialise the visit of the Hash table (TagHT)
169  */
170 void ElementSet::Initialize()
171 {
172    ItTagHT = TagHT.begin();
173 }
174
175 /**
176  * \brief   Get the next entry whil visiting the Hash table (TagHT)
177  * \return  The next DocEntry if found, otherwhise NULL
178  */
179 DocEntry *ElementSet::GetNextEntry()
180 {
181    if (ItTagHT != TagHT.end())
182    {
183       DocEntry *tmp = ItTagHT->second;
184       ++ItTagHT;
185
186       return(tmp);
187    }
188    else
189    {
190       return(NULL);
191    }
192 }
193
194 //-----------------------------------------------------------------------------
195 } // end namespace gdcm