]> Creatis software - gdcm.git/blob - src/gdcmElementSet.cxx
ENH: Slightly bigger patch:
[gdcm.git] / src / gdcmElementSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmElementSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/11/16 02:54:35 $
7   Version:   $Revision: 1.27 $
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  * \ingroup ElementSet
32  * \brief   Constructor from a given ElementSet
33  */
34 //BOZ depthLevel is not usefull anymore
35 ElementSet::ElementSet(int depthLevel) 
36               : DocEntrySet()
37 {
38   (void)depthLevel;
39 }
40
41 /**
42  * \ingroup ElementSet
43  * \brief   Canonical destructor.
44  */
45 ElementSet::~ElementSet() 
46 {
47   for(TagDocEntryHT::iterator cc = TagHT.begin();cc != TagHT.end(); ++cc)
48    {
49       DocEntry* entryToDelete = cc->second;
50       if ( entryToDelete )
51       {
52          delete entryToDelete;
53       }
54    }
55    TagHT.clear();
56 }
57
58
59 //-----------------------------------------------------------------------------
60 // Public
61
62
63 //-----------------------------------------------------------------------------
64 // Print
65 /**
66   * \brief   Prints the Header Entries (Dicom Elements)
67   *          from the H Table
68   * @return
69   */ 
70 void ElementSet::Print(std::ostream& os)
71 {
72    for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
73    {
74       DocEntry* entry = i->second;
75       entry->Print(os);   
76       if ( SeqEntry* seqEntry = dynamic_cast<SeqEntry*>(entry) )
77       {
78          (void)seqEntry;
79          // Avoid the newline for a sequence:
80          continue;
81       }
82       os << std::endl;
83    }
84 }
85
86 /**
87   * \brief   Writes the Header Entries (Dicom Elements)
88   *          from the H Table
89   * @return
90   */ 
91 void ElementSet::Write(std::ofstream* fp, FileType filetype)
92 {
93    for (TagDocEntryHT::const_iterator i = TagHT.begin(); 
94                                      i != TagHT.end(); 
95                                     ++i)
96    {
97       i->second->Write(fp, filetype);
98    } 
99 }
100 //-----------------------------------------------------------------------------
101 // Protected
102
103 //-----------------------------------------------------------------------------
104
105 //-----------------------------------------------------------------------------
106 // Private
107
108 /**
109  * \brief   add a new Dicom Element pointer to the H Table
110  * @param   newEntry entry to add
111  */
112 bool ElementSet::AddEntry(DocEntry* newEntry)
113 {
114    const TagKey& key = newEntry->GetKey();
115
116    if( TagHT.count(key) == 1 )
117    {
118       dbg.Verbose(1, "ElementSet::AddEntry key already present: ",
119                   key.c_str());
120       return false;
121    } 
122    else 
123    {
124       TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
125       return true;
126    }   
127 }
128
129 /**
130  * \brief   Clear the hash table from given entry BUT keep the entry.
131  * @param   entryToRemove Entry to remove.
132  */
133 bool ElementSet::RemoveEntryNoDestroy(DocEntry* entryToRemove)
134 {
135    const TagKey& key = entryToRemove->GetKey();
136    if( TagHT.count(key) == 1 )
137    {
138       TagHT.erase(key);
139       dbg.Verbose(0, "ElementSet::RemoveEntry: one element erased.");
140       return true;
141    }
142
143    dbg.Verbose(0, "ElementSet::RemoveEntry: key not present: ");
144    return false ;
145 }
146
147 /**
148  * \brief   Clear the hash table from given entry AND delete the entry.
149  * @param   entryToRemove Entry to remove AND delete.
150  */
151 bool ElementSet::RemoveEntry( DocEntry* entryToRemove)
152 {
153    const TagKey& key = entryToRemove->GetKey();
154    if( TagHT.count(key) == 1 )
155    {
156       TagHT.erase(key);
157       dbg.Verbose(0, "ElementSet::RemoveEntry: one element erased.");
158       delete entryToRemove;
159       return true;
160    }
161
162    dbg.Verbose(0, "ElementSet::RemoveEntry: key not present: ");
163    return false ;
164 }
165 } // end namespace gdcm