]> Creatis software - gdcm.git/blob - src/gdcmElementSet.cxx
2005-01-18 Jean-Pierre Roux <jpr@creatis.univ-lyon1.fr>
[gdcm.git] / src / gdcmElementSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmElementSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/18 08:01:41 $
7   Version:   $Revision: 1.43 $
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   * @return
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 ( /*SeqEntry *seqEntry = */dynamic_cast<SeqEntry*>(entry) )
73       {
74          //(void)seqEntry;
75          // Avoid the newline for a sequence:
76          continue;
77       }
78       os << std::endl;
79    }
80 }
81
82 //-----------------------------------------------------------------------------
83 // Public
84 /**
85   * \brief   Writes the Header Entries (Dicom Elements)
86   *          from the H Table
87   * @param os ostream to write to  
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  */
130 bool ElementSet::RemoveEntry( DocEntry *entryToRemove)
131 {
132    const TagKey &key = entryToRemove->GetKey();
133    if( TagHT.count(key) == 1 )
134    {
135       TagHT.erase(key);
136       //gdcmVerboseMacro( "One element erased.");
137       delete entryToRemove;
138       return true;
139    }
140
141    gdcmVerboseMacro( "Key not present");
142    return false ;
143 }
144
145 /**
146  * \brief   Clear the hash table from given entry BUT keep the entry.
147  * @param   entryToRemove Entry to remove.
148  */
149 bool ElementSet::RemoveEntryNoDestroy(DocEntry *entryToRemove)
150 {
151    const TagKey &key = entryToRemove->GetKey();
152    if( TagHT.count(key) == 1 )
153    {
154       TagHT.erase(key);
155       //gdcmVerboseMacro( "One element erased.");
156       return true;
157    }
158
159    gdcmVerboseMacro( "Key not present");
160    return false ;
161 }
162
163 /**
164  * \brief   Get the first entry while visiting the DocEntrySet
165  * \return  The first DocEntry if found, otherwhise NULL
166  */
167 DocEntry *ElementSet::GetFirstEntry()
168 {
169    ItTagHT = TagHT.begin();
170    return ItTagHT->second;
171 }
172
173 /**
174  * \brief   Get the next entry while visiting the Hash table (TagHT)
175  * \note : meaningfull only if GetFirstEntry already called 
176  * \return  The next DocEntry if found, otherwhise NULL
177  */
178 DocEntry *ElementSet::GetNextEntry()
179 {
180    if (ItTagHT != TagHT.end())
181    {
182       DocEntry *tmp = ItTagHT->second;
183       ++ItTagHT;
184
185       return tmp;
186    }
187    else
188    {
189       return NULL;
190    }
191 }
192
193 //-----------------------------------------------------------------------------
194 } // end namespace gdcm