]> Creatis software - gdcm.git/blob - src/gdcmElementSet.cxx
Fix bug on DicomDir utilities
[gdcm.git] / src / gdcmElementSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmElementSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/19 15:24:28 $
7   Version:   $Revision: 1.46 $
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  * \brief   Constructor from a given ElementSet
32  */
33 //BOZ depthLevel is not usefull anymore
34 ElementSet::ElementSet(int depthLevel) 
35               : DocEntrySet()
36 {
37   (void)depthLevel;
38 }
39
40 /**
41  * \brief   Canonical destructor.
42  */
43 ElementSet::~ElementSet() 
44 {
45    for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
46    {
47       if ( cc->second )
48       {
49          delete cc->second;
50       }
51    }
52    TagHT.clear();
53 }
54
55 //-----------------------------------------------------------------------------
56 // Print
57 /**
58   * \brief   Prints the Header Entries (Dicom Elements)
59   *          from the H Table
60   * @param os ostream to write to  
61   * @param indent Indentation string to be prepended during printing
62   */ 
63 void ElementSet::Print(std::ostream &os, std::string const & )
64 {
65    for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
66    {
67       DocEntry* entry = i->second;
68
69       entry->SetPrintLevel(PrintLevel);
70       entry->Print(os);   
71
72       if ( dynamic_cast<SeqEntry*>(entry) )
73       {
74          // Avoid the newline for a sequence:
75          continue;
76       }
77       os << std::endl;
78    }
79 }
80
81 //-----------------------------------------------------------------------------
82 // Public
83 /**
84   * \brief   Writes the Header Entries (Dicom Elements)
85   *          from the H Table
86   * @param fp ofstream to write to  
87   * @param filetype filetype
88   */ 
89 void ElementSet::WriteContent(std::ofstream *fp, FileType filetype)
90 {
91    for (TagDocEntryHT::const_iterator i = TagHT.begin(); 
92                                      i != TagHT.end(); 
93                                     ++i)
94    {
95       i->second->WriteContent(fp, filetype);
96    } 
97 }
98
99 //-----------------------------------------------------------------------------
100 // Protected
101
102 //-----------------------------------------------------------------------------
103 // Private
104
105 /**
106  * \brief   add a new Dicom Element pointer to the H Table
107  * @param   newEntry entry to add
108  */
109 bool ElementSet::AddEntry(DocEntry *newEntry)
110 {
111    const TagKey &key = newEntry->GetKey();
112
113    if( TagHT.count(key) == 1 )
114    {
115       gdcmVerboseMacro( "Key already present: " << key.c_str());
116       return false;
117    }
118    else
119    {
120       TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
121       return true;
122    }
123 }
124
125 /**
126  * \brief   Clear the hash table from given entry AND delete the entry.
127  * @param   entryToRemove Entry to remove AND delete.
128  */
129 bool ElementSet::RemoveEntry( DocEntry *entryToRemove)
130 {
131    const TagKey &key = entryToRemove->GetKey();
132    if( TagHT.count(key) == 1 )
133    {
134       TagHT.erase(key);
135       //gdcmVerboseMacro( "One element erased.");
136       delete entryToRemove;
137       return true;
138    }
139
140    gdcmVerboseMacro( "Key not present");
141    return false ;
142 }
143
144 /**
145  * \brief   Clear the hash table from given entry BUT keep the entry.
146  * @param   entryToRemove Entry to remove.
147  */
148 bool ElementSet::RemoveEntryNoDestroy(DocEntry *entryToRemove)
149 {
150    const TagKey &key = entryToRemove->GetKey();
151    if( TagHT.count(key) == 1 )
152    {
153       TagHT.erase(key);
154       //gdcmVerboseMacro( "One element erased.");
155       return true;
156    }
157
158    gdcmVerboseMacro( "Key not present");
159    return false ;
160 }
161
162 /**
163  * \brief   Get the first entry while visiting the DocEntrySet
164  * \return  The first DocEntry if found, otherwhise NULL
165  */
166 DocEntry *ElementSet::GetFirstEntry()
167 {
168    ItTagHT = TagHT.begin();
169    if (ItTagHT != TagHT.end())
170       return  ItTagHT->second;
171    return NULL;
172 }
173
174 /**
175  * \brief   Get the next entry while visiting the Hash table (TagHT)
176  * \note : meaningfull only if GetFirstEntry already called 
177  * \return  The next DocEntry if found, otherwhise NULL
178  */
179 DocEntry *ElementSet::GetNextEntry()
180 {
181    gdcmAssertMacro (ItTagHT != TagHT.end());
182
183    ++ItTagHT;
184    if (ItTagHT != TagHT.end())
185       return  ItTagHT->second;
186    return NULL;
187 }
188
189 //-----------------------------------------------------------------------------
190 } // end namespace gdcm