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