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