]> Creatis software - gdcm.git/blob - src/gdcmElementSet.cxx
ENH: Change the gdcmDebug approach. Remov the global static debug 'dbg'. And now...
[gdcm.git] / src / gdcmElementSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmElementSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/07 16:26:12 $
7   Version:   $Revision: 1.37 $
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
70       entry->SetPrintLevel(PrintLevel);
71       entry->Print(os);   
72
73       if ( SeqEntry *seqEntry = dynamic_cast<SeqEntry*>(entry) )
74       {
75          (void)seqEntry;
76          // Avoid the newline for a sequence:
77          continue;
78       }
79       os << std::endl;
80    }
81 }
82
83 //-----------------------------------------------------------------------------
84 // Public
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 //-----------------------------------------------------------------------------
101 // Protected
102
103 //-----------------------------------------------------------------------------
104 // Private
105
106 /**
107  * \brief   add a new Dicom Element pointer to the H Table
108  * @param   newEntry entry to add
109  */
110 bool ElementSet::AddEntry(DocEntry *newEntry)
111 {
112    const TagKey &key = newEntry->GetKey();
113
114    if( TagHT.count(key) == 1 )
115    {
116       Debug::Verbose(1, "ElementSet::AddEntry key already present: ",
117                   key.c_str());
118       return false;
119    }
120    else
121    {
122       TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
123       return true;
124    }
125 }
126
127 /**
128  * \brief   Clear the hash table from given entry AND delete the entry.
129  * @param   entryToRemove Entry to remove AND delete.
130  * \warning Some problems when using under Windows... prefer the use of
131  *          Initialize / GetNext methods
132  */
133 bool ElementSet::RemoveEntry( DocEntry *entryToRemove)
134 {
135    const TagKey &key = entryToRemove->GetKey();
136    if( TagHT.count(key) == 1 )
137    {
138       TagHT.erase(key);
139       Debug::Verbose(0, "ElementSet::RemoveEntry: one element erased.");
140       delete entryToRemove;
141       return true;
142    }
143
144    Debug::Verbose(0, "ElementSet::RemoveEntry: key not present");
145    return false ;
146 }
147
148 /**
149  * \brief   Clear the hash table from given entry BUT keep the entry.
150  * @param   entryToRemove Entry to remove.
151  */
152 bool ElementSet::RemoveEntryNoDestroy(DocEntry *entryToRemove)
153 {
154    const TagKey &key = entryToRemove->GetKey();
155    if( TagHT.count(key) == 1 )
156    {
157       TagHT.erase(key);
158       Debug::Verbose(0, "ElementSet::RemoveEntry: one element erased.");
159       return true;
160    }
161
162    Debug::Verbose(0, "ElementSet::RemoveEntry: key not present");
163    return false ;
164 }
165
166 /**
167  * \brief   Initialise the visit of the Hash table (TagHT)
168  */
169 void ElementSet::Initialize()
170 {
171    ItTagHT = TagHT.begin();
172 }
173
174 /**
175  * \brief   Get the next entry whil visiting the Hash table (TagHT)
176  * \return  The next DocEntry if found, otherwhise NULL
177  */
178 DocEntry *ElementSet::GetNextEntry()
179 {
180    if (ItTagHT != TagHT.end())
181    {
182       DocEntry *tmp = ItTagHT->second;
183       ++ItTagHT;
184
185       return(tmp);
186    }
187    else
188    {
189       return(NULL);
190    }
191 }
192
193 //-----------------------------------------------------------------------------
194 } // end namespace gdcm