]> Creatis software - gdcm.git/blob - src/gdcmDocEntryArchive.cxx
* src/gdcmDocEntryArchive.[h|cxx] : bug fix and add a method to temporary
[gdcm.git] / src / gdcmDocEntryArchive.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntryArchive.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/11/24 10:23:47 $
7   Version:   $Revision: 1.3 $
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 "gdcmDocEntryArchive.h"
20 #include "gdcmDebug.h"
21
22 #include <string>
23
24 namespace gdcm 
25 {
26 //-----------------------------------------------------------------------------
27 /**
28  * \brief Constructor
29  */
30 DocEntryArchive::DocEntryArchive(Header *header):
31    HeaderHT(header->TagHT)
32 {
33 }
34
35 //-----------------------------------------------------------------------------
36 /**
37  * \brief Destructor
38  */
39 DocEntryArchive::~DocEntryArchive()
40 {
41    ClearArchive();
42 }
43
44 //-----------------------------------------------------------------------------
45 // Print
46 /**
47  * \brief   Print all 
48  * @param   os The output stream to be written to.
49  */
50 void DocEntryArchive::Print(std::ostream &os) 
51 {
52    os << "Elements in archives :" << std::endl;
53    for(TagDocEntryHT::iterator it = Archive.begin();
54        it!=Archive.end();
55        ++it)
56    {
57       if(it->second)
58          it->second->Print(os);
59    }
60 }
61
62 //-----------------------------------------------------------------------------
63 // Public
64 /**
65  * \brief   Replace in the Header a DocEntry by the new DocEntry. The last
66  *          DocEntry is kept in archieve
67  * @param   newEntry New entry to substitute to an other entry of the Header
68  * @return  FALSE when an other DocEntry is already archieved with the same
69  *          generalized key, TRUE otherwise
70  */
71 bool DocEntryArchive::Push(DocEntry *newEntry)
72 {
73    if(!newEntry)
74       return(false);
75
76    uint16_t gr = newEntry->GetDictEntry()->GetGroup();
77    uint16_t elt = newEntry->GetDictEntry()->GetElement();
78    std::string key = DictEntry::TranslateToKey(gr,elt);
79
80    if( Archive.find(key)==Archive.end() )
81    {
82       // Save the old DocEntry if any
83       TagDocEntryHT::iterator it = HeaderHT.find(key);
84       if( it!=HeaderHT.end() )
85       {
86          Archive[key] = it->second;
87       }
88       else
89       {
90          Archive[key] = NULL;
91       }
92
93       // Set the new DocEntry
94       HeaderHT[key] = newEntry;
95
96       return(true);
97    }
98    return(false);
99 }
100
101 /**
102  * \brief   Replace in the Header a DocEntry by the new DocEntry. The last
103  *          DocEntry is kept in archieve
104  * @param   newEntry New entry to substitute to an other entry of the Header
105  * @return  FALSE when an other DocEntry is already archieved with the same
106  *          generalized key, TRUE otherwise
107  */
108 bool DocEntryArchive::Push(uint16_t group,uint16_t element)
109 {
110    std::string key = DictEntry::TranslateToKey(group,element);
111
112    if( Archive.find(key)==Archive.end() )
113    {
114       // Save the old DocEntry if any
115       TagDocEntryHT::iterator it = HeaderHT.find(key);
116       if( it!=HeaderHT.end() )
117       {
118          Archive[key] = it->second;
119          HeaderHT.erase(it);
120       }
121
122       return(true);
123    }
124    return(false);
125 }
126
127 /**
128  * \brief   Restore in the Header the DocEntry that have the generalized key. 
129  *          The old entry is destroyed.
130  * @param   key Key of the DocEntry to restore
131  * @return  FALSE when the generalized key isn't in the archieve, 
132  *          TRUE otherwise
133  */
134 bool DocEntryArchive::Restore(uint16_t group,uint16_t element)
135 {
136    std::string key=DictEntry::TranslateToKey(group,element);
137
138    TagDocEntryHT::iterator restoreIt=Archive.find(key);
139    if( restoreIt!=Archive.end() )
140    {
141       TagDocEntryHT::iterator restorePos = HeaderHT.find(key);
142       if( restorePos!=HeaderHT.end() )
143       {
144          delete restorePos->second;
145       }
146
147       if( Archive[key] )
148       {
149          HeaderHT[key] = Archive[key];
150       }
151       else
152       {
153          HeaderHT.erase(restorePos);
154       }
155
156       Archive.erase(restoreIt);
157
158       return(true);
159    }
160    return(false);
161 }
162
163 /**
164  * \brief   Remove all DocEntry that are in the archive. The entries aren't 
165  *          restored but only destroyed.
166  */
167 void DocEntryArchive::ClearArchive(void)
168 {
169    for(TagDocEntryHT::iterator it = Archive.begin();
170        it!=Archive.end();
171        ++it)
172    {
173       delete it->second;
174    }
175    Archive.clear();
176 }
177
178 //-----------------------------------------------------------------------------
179 // Protected
180
181 //-----------------------------------------------------------------------------
182 // Private
183
184 //-----------------------------------------------------------------------------
185
186 } // end namespace gdcm