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