]> Creatis software - gdcm.git/blob - src/gdcmDocEntryArchive.cxx
ENH: * Huge cleanup:
[gdcm.git] / src / gdcmDocEntryArchive.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntryArchive.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/12/03 20:16:58 $
7   Version:   $Revision: 1.4 $
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 archieve
68  * @param   newEntry New entry to substitute to an other entry of the Header
69  * @return  FALSE when an other DocEntry is already archieved 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   newEntry New entry to substitute to an other entry of the Header
106  * @return  FALSE when an other DocEntry is already archieved with the same
107  *          generalized key, TRUE otherwise
108  */
109 bool DocEntryArchive::Push(uint16_t group,uint16_t element)
110 {
111    std::string key = DictEntry::TranslateToKey(group,element);
112
113    if( Archive.find(key)==Archive.end() )
114    {
115       // Save the old DocEntry if any
116       TagDocEntryHT::iterator it = HeaderHT.find(key);
117       if( it!=HeaderHT.end() )
118       {
119          Archive[key] = it->second;
120          HeaderHT.erase(it);
121       }
122
123       return(true);
124    }
125    return(false);
126 }
127
128 /**
129  * \brief   Restore in the Header the DocEntry that have the generalized key. 
130  *          The old entry is destroyed.
131  * @param   key Key of the DocEntry to restore
132  * @return  FALSE when the generalized key isn't in the archieve, 
133  *          TRUE otherwise
134  */
135 bool DocEntryArchive::Restore(uint16_t group,uint16_t element)
136 {
137    std::string key=DictEntry::TranslateToKey(group,element);
138
139    TagDocEntryHT::iterator restoreIt=Archive.find(key);
140    if( restoreIt!=Archive.end() )
141    {
142       TagDocEntryHT::iterator restorePos = HeaderHT.find(key);
143       if( restorePos!=HeaderHT.end() )
144       {
145          delete restorePos->second;
146       }
147
148       if( Archive[key] )
149       {
150          HeaderHT[key] = Archive[key];
151       }
152       else
153       {
154          HeaderHT.erase(restorePos);
155       }
156
157       Archive.erase(restoreIt);
158
159       return(true);
160    }
161    return(false);
162 }
163
164 /**
165  * \brief   Remove all DocEntry that are in the archive. The entries aren't 
166  *          restored but only destroyed.
167  */
168 void DocEntryArchive::ClearArchive(void)
169 {
170    for(TagDocEntryHT::iterator it = Archive.begin();
171        it!=Archive.end();
172        ++it)
173    {
174       delete it->second;
175    }
176    Archive.clear();
177 }
178
179 //-----------------------------------------------------------------------------
180 // Protected
181
182 //-----------------------------------------------------------------------------
183 // Private
184
185 //-----------------------------------------------------------------------------
186
187 } // end namespace gdcm