]> Creatis software - gdcm.git/blob - src/gdcmElementSet.cxx
ENH: Removed all FILE* ref and replace by ifstream/ofstream. For now I use a temp...
[gdcm.git] / src / gdcmElementSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmElementSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/10/22 03:05:41 $
7   Version:   $Revision: 1.25 $
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(); i != TagHT.end(); ++i)
94    {
95       i->second->Write(fp, filetype);
96    } 
97 }
98 //-----------------------------------------------------------------------------
99 // Protected
100
101 //-----------------------------------------------------------------------------
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    TagKey key = newEntry->GetKey();
113
114    if( TagHT.count(key) == 1 )
115    {
116       dbg.Verbose(1, "ElementSet::AddEntry key already present: ",
117                   key.c_str());
118       return(false);
119    } 
120    else 
121    {
122       TagHT[newEntry->GetKey()] = newEntry;
123       return true;
124    }   
125 }
126
127 /**
128  * \brief   Clear the hash table from given entry BUT keep the entry.
129  * @param   entryToRemove Entry to remove.
130  */
131 bool ElementSet::RemoveEntryNoDestroy( DocEntry* entryToRemove)
132 {
133    TagKey key = entryToRemove->GetKey();
134    if( TagHT.count(key) == 1 )
135    {
136       TagHT.erase(key);
137       dbg.Verbose(0, "ElementSet::RemoveEntry: one element erased.");
138       return true;
139    }
140
141    dbg.Verbose(0, "ElementSet::RemoveEntry: key not present: ");
142    return false ;
143 }
144
145 /**
146  * \brief   Clear the hash table from given entry AND delete the entry.
147  * @param   entryToRemove Entry to remove AND delete.
148  */
149 bool ElementSet::RemoveEntry( DocEntry* entryToRemove)
150 {
151    TagKey key = entryToRemove->GetKey();
152    if( TagHT.count(key) == 1 )
153    {
154       TagHT.erase(key);
155       dbg.Verbose(0, "ElementSet::RemoveEntry: one element erased.");
156       delete entryToRemove;
157       return true;
158    }
159
160    dbg.Verbose(0, "ElementSet::RemoveEntry: key not present: ");
161    return false ;
162 }
163 } // end namespace gdcm