]> Creatis software - gdcm.git/blob - src/gdcmDocEntryArchive.cxx
60e0f2d90b7e3c8edeec0cffd20db7ab8035963b
[gdcm.git] / src / gdcmDocEntryArchive.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntryArchive.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/17 11:13:21 $
7   Version:   $Revision: 1.7 $
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(Header *header):
32    HeaderHT(header->TagHT)
33 {
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 gr = newEntry->GetDictEntry()->GetGroup();
78    uint16_t elt = newEntry->GetDictEntry()->GetElement();
79    std::string key = DictEntry::TranslateToKey(gr,elt);
80
81    if( Archive.find(key)==Archive.end() )
82    {
83       // Save the old DocEntry if any
84       TagDocEntryHT::iterator it = HeaderHT.find(key);
85       if( it!=HeaderHT.end() )
86       {
87          Archive[key] = it->second;
88       }
89       else
90       {
91          Archive[key] = NULL;
92       }
93
94       // Set the new DocEntry
95       HeaderHT[key] = newEntry;
96
97       return true;
98    }
99    return false;
100 }
101
102 /**
103  * \brief   Replace in the Header a DocEntry by the new DocEntry. The last
104  *          DocEntry is kept in archieve
105  * @param   group   Group number of the Entry 
106  * @param   elem  Element number of the Entry
107  * @return  FALSE when an other DocEntry is already archived with the same
108  *          generalized key, TRUE otherwise
109  */
110 bool DocEntryArchive::Push(uint16_t group,uint16_t elem)
111 {
112    std::string key = DictEntry::TranslateToKey(group,elem);
113
114    if( Archive.find(key)==Archive.end() )
115    {
116       // Save the old DocEntry if any
117       TagDocEntryHT::iterator it = HeaderHT.find(key);
118       if( it!=HeaderHT.end() )
119       {
120          Archive[key] = it->second;
121          HeaderHT.erase(it);
122       }
123
124       return true;
125    }
126    return false;
127 }
128
129 /**
130  * \brief   Restore in the Header the DocEntry that have the generalized key. 
131  *          The old entry is destroyed.
132  * @param   group   Group number of the Entry 
133  * @param   elem  Element number of the Entry
134  * @return  FALSE when the generalized key isn't in the archive, 
135  *          TRUE otherwise
136  */
137 bool DocEntryArchive::Restore(uint16_t group,uint16_t elem)
138 {
139    std::string key=DictEntry::TranslateToKey(group,elem);
140
141    TagDocEntryHT::iterator restoreIt=Archive.find(key);
142    if( restoreIt!=Archive.end() )
143    {
144       TagDocEntryHT::iterator restorePos = HeaderHT.find(key);
145       if( restorePos!=HeaderHT.end() )
146       {
147          delete restorePos->second;
148       }
149
150       if( Archive[key] )
151       {
152          HeaderHT[key] = Archive[key];
153       }
154       else
155       {
156          HeaderHT.erase(restorePos);
157       }
158
159       Archive.erase(restoreIt);
160
161       return true;
162    }
163    return false;
164 }
165
166 /**
167  * \brief   Remove all DocEntry that are in the archive.  
168  *          The entries aren't restored but only destroyed.
169  */
170 void DocEntryArchive::ClearArchive( )
171 {
172    for(TagDocEntryHT::iterator it = Archive.begin();
173        it!=Archive.end();
174        ++it)
175    {
176       delete it->second;
177    }
178    Archive.clear();
179 }
180
181 //-----------------------------------------------------------------------------
182 // Protected
183
184 //-----------------------------------------------------------------------------
185 // Private
186
187 //-----------------------------------------------------------------------------
188
189 } // end namespace gdcm