]> Creatis software - gdcm.git/blob - src/gdcmElementSet.cxx
dade9a061cfea8cd34aaa9d72212676560a977b3
[gdcm.git] / src / gdcmElementSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmElementSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/11 00:21:48 $
7   Version:   $Revision: 1.40 $
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       gdcmVerboseMacro( "Key already present: " << key.c_str());
117       return false;
118    }
119    else
120    {
121       TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
122       return true;
123    }
124 }
125
126 /**
127  * \brief   Clear the hash table from given entry AND delete the entry.
128  * @param   entryToRemove Entry to remove AND delete.
129  * \warning Some problems when using under Windows... prefer the use of
130  *          Initialize / GetNext methods
131  */
132 bool ElementSet::RemoveEntry( DocEntry *entryToRemove)
133 {
134    const TagKey &key = entryToRemove->GetKey();
135    if( TagHT.count(key) == 1 )
136    {
137       TagHT.erase(key);
138       gdcmVerboseMacro( "One element erased.");
139       delete entryToRemove;
140       return true;
141    }
142
143    gdcmVerboseMacro( "Key not present");
144    return false ;
145 }
146
147 /**
148  * \brief   Clear the hash table from given entry BUT keep the entry.
149  * @param   entryToRemove Entry to remove.
150  */
151 bool ElementSet::RemoveEntryNoDestroy(DocEntry *entryToRemove)
152 {
153    const TagKey &key = entryToRemove->GetKey();
154    if( TagHT.count(key) == 1 )
155    {
156       TagHT.erase(key);
157       gdcmVerboseMacro( "One element erased.");
158       return true;
159    }
160
161    gdcmVerboseMacro( "Key not present");
162    return false ;
163 }
164
165 /**
166  * \brief   Initialise the visit of the Hash table (TagHT)
167  */
168 void ElementSet::Initialize()
169 {
170    ItTagHT = TagHT.begin();
171 }
172
173 /**
174  * \brief   Get the next entry whil visiting the Hash table (TagHT)
175  * \return  The next DocEntry if found, otherwhise NULL
176  */
177 DocEntry *ElementSet::GetNextEntry()
178 {
179    if (ItTagHT != TagHT.end())
180    {
181       DocEntry *tmp = ItTagHT->second;
182       ++ItTagHT;
183
184       return tmp;
185    }
186    else
187    {
188       return NULL;
189    }
190 }
191
192 //-----------------------------------------------------------------------------
193 } // end namespace gdcm