]> Creatis software - gdcm.git/blob - src/gdcmDocEntryArchive.cxx
Fix mistypings
[gdcm.git] / src / gdcmDocEntryArchive.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntryArchive.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/05/23 14:18:09 $
7   Version:   $Revision: 1.19 $
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_NAME_SPACE 
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    TagKey key = newEntry->GetKey();
64    
65    if ( Archive.find(key) == Archive.end() )
66    {
67       uint16_t group = newEntry->GetGroup();
68       uint16_t elem  = newEntry->GetElement();
69       
70       // Save the old DocEntry if any
71       DocEntry *old = ArchFile->GetDocEntry(group, elem);
72       Archive[key]  = old;
73       if ( old )
74       {
75          old->Register();
76          ArchFile->RemoveEntry(old);
77       }
78
79       // Set the new DocEntry
80       ArchFile->AddEntry(newEntry);
81
82       return true;
83    }
84    return false;
85 }
86
87 /**
88  * \brief   Removes out of the Header a DocEntry.
89  *          (it's kept in archive).
90  * @param   group   Group number of the Entry to remove
91  * @param   elem  Element number of the Entry to remove
92  * @return  FALSE when an other DocEntry is already archived with the same key
93  *          TRUE otherwise
94  */
95 bool DocEntryArchive::Push(uint16_t group, uint16_t elem)
96 {
97    //TagKey key = DictEntry::TranslateToKey(group, elem);
98    TagKey key(group, elem);
99    if ( Archive.find(key)==Archive.end() )
100    {
101       // Save the old DocEntry if any
102       DocEntry *old = ArchFile->GetDocEntry(group, elem);
103       Archive[key] = old;
104       if ( old )
105       {
106          old->Register();
107          ArchFile->RemoveEntry(old);
108       }
109
110       return true;
111    }
112    return false;
113 }
114
115 /**
116  * \brief   Restore in the Header the DocEntry specified by (group,element). 
117  *          The archive entry is destroyed.
118  * @param   group   Group number of the Entry to restore
119  * @param   elem  Element number of the Entry to restore
120  * @return  FALSE when the key isn't in the archive, 
121  *          TRUE otherwise
122  */
123 bool DocEntryArchive::Restore(uint16_t group, uint16_t elem)
124 {
125    //TagKey key=DictEntry::TranslateToKey(group, elem);
126    TagKey key(group, elem);
127    TagDocEntryHT::iterator restoreIt=Archive.find(key);
128    if ( restoreIt!=Archive.end() )
129    {
130       // Delete the new value
131       DocEntry *rem = ArchFile->GetDocEntry(group, elem);
132       if ( rem )
133       {
134          ArchFile->RemoveEntry(rem);
135       }
136
137       // Restore the old value
138       if ( restoreIt->second )
139       {
140          ArchFile->AddEntry(restoreIt->second);
141          restoreIt->second->Unregister();
142       }
143
144       Archive.erase(restoreIt);
145
146       return true;
147    }
148    return false;
149 }
150
151 /**
152  * \brief   Removes all DocEntry from the archive, and destroy them.  
153  *          The archives entries aren't restored.
154  */
155 void DocEntryArchive::ClearArchive( )
156 {
157    for(TagDocEntryHT::iterator it = Archive.begin();
158        it!=Archive.end();
159        ++it)
160    {
161       if(it->second)
162          it->second->Unregister();
163    }
164    Archive.clear();
165 }
166
167 //-----------------------------------------------------------------------------
168 // Protected
169
170 //-----------------------------------------------------------------------------
171 // Private
172
173 //-----------------------------------------------------------------------------
174 // Print
175 /**
176  * \brief   Print all 
177  * @param   os The output stream to be written to.
178  */
179 void DocEntryArchive::Print(std::ostream &os) 
180 {
181    os << "Elements in archives :" << std::endl;
182    for(TagDocEntryHT::iterator it = Archive.begin();
183        it!=Archive.end();
184        ++it)
185    {
186       if ( it->second )
187          it->second->Print(os);
188    }
189 }
190
191 //-----------------------------------------------------------------------------
192 } // end namespace gdcm