]> Creatis software - gdcm.git/blob - src/gdcmElementSet.cxx
* Minor coding-style clean up
[gdcm.git] / src / gdcmElementSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmElementSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/18 08:35:50 $
7   Version:   $Revision: 1.65 $
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 "gdcmSeqEntry.h"
22 #include "gdcmDataEntry.h"
23
24 namespace gdcm 
25 {
26 //-----------------------------------------------------------------------------
27 // Constructor / Destructor
28 /**
29  * \brief   Constructor for a given ElementSet
30  */
31 //BOZ depthLevel is not usefull anymore
32 ElementSet::ElementSet(int depthLevel) 
33           : DocEntrySet()
34 {
35   (void)depthLevel;
36 }
37
38 /**
39  * \brief   Canonical destructor.
40  */
41 ElementSet::~ElementSet() 
42 {
43    ClearEntry();
44 }
45
46 //-----------------------------------------------------------------------------
47 // Public
48 /**
49   * \brief   Writes the Header Entries (Dicom Elements)
50   *          from the H Table
51   * @param fp ofstream to write to  
52   * @param filetype filetype
53   */ 
54 void ElementSet::WriteContent(std::ofstream *fp, FileType filetype)
55 {
56    for (TagDocEntryHT::const_iterator i = TagHT.begin(); 
57                                      i != TagHT.end(); 
58                                     ++i)
59    {
60       i->second->WriteContent(fp, filetype);
61    } 
62 }
63
64 /**
65  * \brief   add a new Dicom Element pointer to the H Table
66  * @param   newEntry entry to add
67  */
68 bool ElementSet::AddEntry(DocEntry *newEntry)
69 {
70    const TagKey &key = newEntry->GetKey();
71
72    if ( TagHT.count(key) == 1 )
73    {
74       gdcmWarningMacro( "Key already present: " << key );
75       return false;
76    }
77    else
78    {
79       TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
80       return true;
81    }
82 }
83
84 /**
85  * \brief   Clear the hash table from given entry AND delete the entry.
86  * @param   entryToRemove Entry to remove AND delete.
87  */
88 bool ElementSet::RemoveEntry( DocEntry *entryToRemove)
89 {
90    const TagKey &key = entryToRemove->GetKey();
91    if ( TagHT.count(key) == 1 )
92    {
93       TagHT.erase(key);
94       //gdcmWarningMacro( "One element erased.");
95       delete entryToRemove;
96       return true;
97    }
98
99    gdcmWarningMacro( "Key not present : " << key);
100    return false ;
101 }
102
103 /**
104  * \brief   Clear the hash table from given entry BUT keep the entry.
105  * @param   entryToRemove Entry to remove.
106  */
107 bool ElementSet::RemoveEntryNoDestroy(DocEntry *entryToRemove)
108 {
109    const TagKey &key = entryToRemove->GetKey();
110    if ( TagHT.count(key) == 1 )
111    {
112       TagHT.erase(key);
113       //gdcmWarningMacro( "One element erased.");
114       return true;
115    }
116
117    gdcmWarningMacro( "Key not present " << key);
118    return false ;
119 }
120
121 /**
122  * \brief   delete all entries in the ElementSet
123  */
124 void ElementSet::ClearEntry()
125 {
126    for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
127    {
128       if ( cc->second )
129       {
130          delete cc->second;
131       }
132    }
133    TagHT.clear();
134 }
135
136 /**
137  * \brief   Get the first entry while visiting *the* 'zero level' DocEntrySet
138  *              (DocEntries out of any Sequence)
139  * \return  The first DocEntry if found, otherwhise NULL
140  */
141 DocEntry *ElementSet::GetFirstEntry()
142 {
143    ItTagHT = TagHT.begin();
144    if (ItTagHT != TagHT.end())
145       return  ItTagHT->second;
146    return NULL;
147 }
148
149 /**
150  * \brief   Get the next entry while visiting *the* 'zero level' DocEntrySet
151  *              (DocEntries out of any Sequence) 
152  * \note : meaningfull only if GetFirstEntry already called 
153  * \return  The next DocEntry if found, otherwhise NULL
154  */
155 DocEntry *ElementSet::GetNextEntry()
156 {
157    gdcmAssertMacro (ItTagHT != TagHT.end());
158
159    ++ItTagHT;
160    if (ItTagHT != TagHT.end())
161       return  ItTagHT->second;
162    return NULL;
163 }
164
165 /**
166  * \brief  retrieves a Dicom Element using (group, element)
167  * @param   group  Group number of the searched Dicom Element 
168  * @param   elem Element number of the searched Dicom Element 
169  * @return  
170  */
171 DocEntry *ElementSet::GetDocEntry(uint16_t group, uint16_t elem) 
172 {
173    TagKey key = DictEntry::TranslateToKey(group, elem);
174    TagDocEntryHT::iterator it = TagHT.find(key);
175
176    if ( it!=TagHT.end() )
177       return it->second;
178    return NULL;
179 }
180
181 //-----------------------------------------------------------------------------
182 // Protected
183
184 //-----------------------------------------------------------------------------
185 // Private
186
187 //-----------------------------------------------------------------------------
188 // Print
189 /**
190   * \brief   Prints the Header Entries (Dicom Elements) from the H Table
191   * @param os ostream to write to  
192   * @param indent Indentation string to be prepended during printing
193   */ 
194 void ElementSet::Print(std::ostream &os, std::string const & )
195 {
196    // Let's change the 'warning value' for Pixel Data,
197    // to avoid human reader to be confused by 'gdcm::NotLoaded'.   
198    DataEntry *pixelElement = GetDataEntry(0x7fe0,0x0010);
199    if ( pixelElement != 0 )
200    {
201       pixelElement->SetFlag( DataEntry::FLAG_PIXELDATA );
202    }
203
204    for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
205    {
206       DocEntry *entry = i->second;
207
208       entry->SetPrintLevel(PrintLevel);
209       entry->Print(os);   
210
211       if ( dynamic_cast<SeqEntry *>(entry) )
212       {
213          // Avoid the newline for a sequence:
214          continue;
215       }
216       os << std::endl;
217    }
218 }
219
220 //-----------------------------------------------------------------------------
221 } // end namespace gdcm