]> Creatis software - gdcm.git/blob - src/gdcmDocEntryArchive.cxx
ENH: A TagKey is a TagKey and not a string or TagName...
[gdcm.git] / src / gdcmDocEntryArchive.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntryArchive.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/11 14:40:40 $
7   Version:   $Revision: 1.16 $
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 // Constructor / Destructor
29 /**
30  * \brief Constructor
31  */
32 DocEntryArchive::DocEntryArchive(File *file)
33 {
34    ArchFile = file;
35 }
36
37 /**
38  * \brief Destructor
39  */
40 DocEntryArchive::~DocEntryArchive()
41 {
42    ClearArchive();
43 }
44
45 //-----------------------------------------------------------------------------
46 // Public
47 /**
48  * \brief   Replaces in the Header a DocEntry by the new DocEntry. 
49  *          The initial DocEntry is kept in archive.
50  * @param   newEntry New entry to substitute to an other entry of the Header
51  * @return  FALSE when an other DocEntry is already archived with the same key
52  *          TRUE otherwise
53  */
54 bool DocEntryArchive::Push(DocEntry *newEntry)
55 {
56    if ( !newEntry )
57       return false;
58
59    uint16_t group = newEntry->GetDictEntry()->GetGroup();
60    uint16_t elem  = newEntry->GetDictEntry()->GetElement();
61    TagKey key = DictEntry::TranslateToKey(group,elem);
62
63    if ( Archive.find(key) == Archive.end() )
64    {
65       // Save the old DocEntry if any
66       DocEntry *old = ArchFile->GetDocEntry(group, elem);
67       Archive[key]  = old;
68       if ( old )
69          ArchFile->RemoveEntryNoDestroy(old);
70
71       // Set the new DocEntry
72       ArchFile->AddEntry(newEntry);
73
74       return true;
75    }
76    return false;
77 }
78
79 /**
80  * \brief   Removes out of the Header a DocEntry.
81  *          (it's kept in archive).
82  * @param   group   Group number of the Entry to remove
83  * @param   elem  Element number of the Entry to remove
84  * @return  FALSE when an other DocEntry is already archived with the same key
85  *          TRUE otherwise
86  */
87 bool DocEntryArchive::Push(uint16_t group, uint16_t elem)
88 {
89    TagKey key = DictEntry::TranslateToKey(group, elem);
90
91    if ( Archive.find(key)==Archive.end() )
92    {
93       // Save the old DocEntry if any
94       DocEntry *old = ArchFile->GetDocEntry(group, elem);
95       Archive[key] = old;
96       if ( old )
97          ArchFile->RemoveEntryNoDestroy(old);
98
99       return true;
100    }
101    return false;
102 }
103
104 /**
105  * \brief   Restore in the Header the DocEntry specified by (group,element). 
106  *          The archive entry is destroyed.
107  * @param   group   Group number of the Entry to restore
108  * @param   elem  Element number of the Entry to restore
109  * @return  FALSE when the key isn't in the archive, 
110  *          TRUE otherwise
111  */
112 bool DocEntryArchive::Restore(uint16_t group, uint16_t elem)
113 {
114    TagKey key=DictEntry::TranslateToKey(group, elem);
115
116    TagDocEntryHT::iterator restoreIt=Archive.find(key);
117    if ( restoreIt!=Archive.end() )
118    {
119       // Delete the new value
120       DocEntry *rem = ArchFile->GetDocEntry(group, elem);
121       if ( rem )
122          ArchFile->RemoveEntry(rem);
123
124       // Restore the old value
125       if ( Archive[key] )
126          ArchFile->AddEntry(Archive[key]);
127
128       Archive.erase(restoreIt);
129
130       return true;
131    }
132    return false;
133 }
134
135 /**
136  * \brief   Removes all DocEntry from the archive, and destroy them.  
137  *          The archives entries aren't restored.
138  */
139 void DocEntryArchive::ClearArchive( )
140 {
141    for(TagDocEntryHT::iterator it = Archive.begin();
142        it!=Archive.end();
143        ++it)
144    {
145       delete it->second;
146    }
147    Archive.clear();
148 }
149
150 //-----------------------------------------------------------------------------
151 // Protected
152
153 //-----------------------------------------------------------------------------
154 // Private
155
156 //-----------------------------------------------------------------------------
157 // Print
158 /**
159  * \brief   Print all 
160  * @param   os The output stream to be written to.
161  */
162 void DocEntryArchive::Print(std::ostream &os) 
163 {
164    os << "Elements in archives :" << std::endl;
165    for(TagDocEntryHT::iterator it = Archive.begin();
166        it!=Archive.end();
167        ++it)
168    {
169       if ( it->second )
170          it->second->Print(os);
171    }
172 }
173
174 //-----------------------------------------------------------------------------
175 } // end namespace gdcm