]> Creatis software - gdcm.git/blob - src/gdcmElementSet.cxx
* FIX : now, the DocEntries are all deleted in the gdcmElementSet.
[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 16:20:23 $
7   Version:   $Revision: 1.30 $
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       if ( cc->second )
50       {
51          delete cc->second;
52       }
53    }
54    TagHT.clear();
55 }
56
57
58 //-----------------------------------------------------------------------------
59 // Public
60
61
62 //-----------------------------------------------------------------------------
63 // Print
64 /**
65   * \brief   Prints the Header Entries (Dicom Elements)
66   *          from the H Table
67   * @return
68   */ 
69 void ElementSet::Print(std::ostream& os)
70 {
71    for( TagDocEntryHT::const_iterator i = TagHT.begin(); i != TagHT.end(); ++i)
72    {
73       DocEntry* entry = i->second;
74       entry->Print(os);   
75       if ( SeqEntry* seqEntry = dynamic_cast<SeqEntry*>(entry) )
76       {
77          (void)seqEntry;
78          // Avoid the newline for a sequence:
79          continue;
80       }
81       os << std::endl;
82    }
83 }
84
85 /**
86   * \brief   Writes the Header Entries (Dicom Elements)
87   *          from the H Table
88   * @return
89   */ 
90 void ElementSet::Write(std::ofstream* fp, FileType filetype)
91 {
92    for (TagDocEntryHT::const_iterator i = TagHT.begin(); 
93                                      i != TagHT.end(); 
94                                     ++i)
95    {
96       i->second->Write(fp, filetype);
97    } 
98 }
99 //-----------------------------------------------------------------------------
100 // Protected
101
102 //-----------------------------------------------------------------------------
103
104 //-----------------------------------------------------------------------------
105 // Private
106
107 /**
108  * \brief   add a new Dicom Element pointer to the H Table
109  * @param   newEntry entry to add
110  */
111 bool ElementSet::AddEntry(DocEntry* newEntry)
112 {
113    const TagKey& key = newEntry->GetKey();
114
115    if( TagHT.count(key) == 1 )
116    {
117       dbg.Verbose(1, "ElementSet::AddEntry key already present: ",
118                   key.c_str());
119       return false;
120    }
121    else
122    {
123       TagHT.insert(TagDocEntryHT::value_type(newEntry->GetKey(), newEntry));
124       return true;
125    }
126 }
127
128 /**
129  * \brief   Clear the hash table from given entry BUT keep the entry.
130  * @param   entryToRemove Entry to remove.
131  */
132 bool ElementSet::RemoveEntryNoDestroy(DocEntry* entryToRemove)
133 {
134    const TagKey& key = entryToRemove->GetKey();
135    if( TagHT.count(key) == 1 )
136    {
137       TagHT.erase(key);
138       dbg.Verbose(0, "ElementSet::RemoveEntry: one element erased.");
139       return true;
140    }
141
142    dbg.Verbose(0, "ElementSet::RemoveEntry: key not present: ");
143    return false ;
144 }
145
146 /**
147  * \brief   Clear the hash table from given entry AND delete the entry.
148  * @param   entryToRemove Entry to remove AND delete.
149  */
150 bool ElementSet::RemoveEntry( DocEntry* entryToRemove)
151 {
152    const TagKey& key = entryToRemove->GetKey();
153    if( TagHT.count(key) == 1 )
154    {
155       TagHT.erase(key);
156       dbg.Verbose(0, "ElementSet::RemoveEntry: one element erased.");
157       delete entryToRemove;
158       return true;
159    }
160
161    dbg.Verbose(0, "ElementSet::RemoveEntry: key not present: ");
162    return false ;
163 }
164 } // end namespace gdcm