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