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