]> Creatis software - gdcm.git/blob - src/gdcmDocEntryArchive.cxx
* src/gdcmDocEntry.cxx : remove the copy of the DictEntry... there isn't
[gdcm.git] / src / gdcmDocEntryArchive.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntryArchive.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/11/19 18:49:39 $
7   Version:   $Revision: 1.1 $
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
22 #include <string>
23
24 namespace gdcm 
25 {
26 //-----------------------------------------------------------------------------
27 /**
28  * \brief Constructor
29  */
30 DocEntryArchive::DocEntryArchive(Header *header):
31    HeaderHT(header->TagHT)
32 {
33 }
34
35 //-----------------------------------------------------------------------------
36 /**
37  * \brief Destructor
38  */
39 DocEntryArchive::~DocEntryArchive()
40 {
41    ClearArchive();
42 }
43
44 //-----------------------------------------------------------------------------
45 // Print
46 /**
47  * \brief   Print all 
48  * @param   os The output stream to be written to.
49  */
50 void DocEntryArchive::Print(std::ostream &os) 
51 {
52 }
53
54 //-----------------------------------------------------------------------------
55 // Public
56 /**
57  * \brief   Replace in the Header a DocEntry by the new DocEntry. The last
58  *          DocEntry is kept in archieve
59  * @param   newEntry New entry to substitute to an other entry of the Header
60  * @return  FALSE when an other DocEntry is already archieved with the same
61  *          generalized key, TRUE otherwise
62  */
63 bool DocEntryArchive::Push(DocEntry *newEntry)
64 {
65    if(!newEntry)
66       return(false);
67
68    uint16_t gr = newEntry->GetDictEntry()->GetGroup();
69    uint16_t elt = newEntry->GetDictEntry()->GetElement();
70    std::string key = DictEntry::TranslateToKey(gr,elt);
71
72    if( Archive.find(key)==Archive.end() )
73    {
74       // Save the old DocEntry if any
75       TagDocEntryHT::iterator it = HeaderHT.find(key);
76       if( it!=HeaderHT.end() )
77          Archive[key] = it->second;
78       else
79          Archive[key] = NULL;
80
81       // Set the new DocEntry
82       HeaderHT[key] = newEntry;
83
84       return(true);
85    }
86    return(false);
87 }
88
89 /**
90  * \brief   Restore in the Header the DocEntry that have the generalized key. 
91  *          The old entry is destroyed.
92  * @param   key Key of the DocEntry to restore
93  * @return  FALSE when the generalized key isn't in the archieve, 
94  *          TRUE otherwise
95  */
96 bool DocEntryArchive::Restore(uint16_t group,uint16_t element)
97 {
98    std::string key=DictEntry::TranslateToKey(group,element);
99
100    TagDocEntryHT::iterator restoreIt=Archive.find(key);
101    if( restoreIt!=Archive.end() )
102    {
103       TagDocEntryHT::iterator restorePos = HeaderHT.find(key);
104       if( restoreIt!=HeaderHT.end() )
105          delete restorePos->second;
106
107       if( Archive[key] )
108          HeaderHT[key] = Archive[key];
109       else
110          HeaderHT.erase(restorePos);
111
112       Archive.erase(restoreIt);
113
114       return(true);
115    }
116    return(false);
117 }
118
119 /**
120  * \brief   Remove all DocEntry that are in the archive. The entries aren't 
121  *          restored but only destroyed.
122  */
123 void DocEntryArchive::ClearArchive(void)
124 {
125    for(TagDocEntryHT::iterator it = Archive.begin();
126        it!=Archive.end();
127        ++it)
128    {
129       delete it->second;
130    }
131    Archive.clear();
132 }
133
134 //-----------------------------------------------------------------------------
135 // Protected
136
137 //-----------------------------------------------------------------------------
138 // Private
139
140 //-----------------------------------------------------------------------------
141
142 } // end namespace gdcm