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