]> Creatis software - gdcm.git/blob - src/gdcmDictSet.cxx
* Remove missed FASTTAGKEY (due to the integration of the
[gdcm.git] / src / gdcmDictSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDictSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/20 09:23:24 $
7   Version:   $Revision: 1.70 $
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 "gdcmDictSet.h"
20 #include "gdcmDebug.h"
21 #include <fstream>
22 #include <stdlib.h>  // For getenv
23 #include <stdio.h>   // For sprintf
24
25 namespace gdcm 
26 {
27
28 //-----------------------------------------------------------------------------
29 // Constructor / Destructor
30 /** 
31  * \brief   The Dictionary Set obtained with this constructor simply
32  *          contains the Default Public dictionary.
33  */
34 DictSet::DictSet() 
35 {
36    DictPath = BuildDictPath();
37    std::string pubDictFile(DictPath);
38    pubDictFile += PUB_DICT_FILENAME;
39    Dicts[PUB_DICT_NAME] = new Dict(pubDictFile);
40 }
41
42 /**
43  * \brief  Destructor 
44  */
45 DictSet::~DictSet() 
46 {
47    // Remove dictionaries
48    for (DictSetHT::iterator tag = Dicts.begin(); tag != Dicts.end(); ++tag) 
49    {
50       Dict *entryToDelete = tag->second;
51       if ( entryToDelete )
52       {
53          delete entryToDelete;
54       }
55       tag->second = NULL;
56    }
57    Dicts.clear();
58
59    // Remove virtual dictionary entries
60    VirtualEntries.clear();
61 }
62
63 //-----------------------------------------------------------------------------
64 // Public
65 /**
66  * \brief   Loads a dictionary from a specified file, and add it
67  *          to already the existing ones contained in this DictSet.
68  * @param   filename Absolute or relative filename containing the
69  *          dictionary to load.
70  * @param   name Symbolic name that be used as identifier of the newly 
71  *          created dictionary.
72  */
73 Dict *DictSet::LoadDictFromFile(std::string const &filename, 
74                                 DictKey const &name) 
75 {
76    Dict *newDict = new Dict(filename);
77    AppendDict(newDict, name);
78
79    return newDict;
80 }
81
82 /**
83  * \brief   Retrieve the specified dictionary (when existing) from this
84  *          DictSet.
85  * @param   dictName The symbolic name of the searched dictionary.
86  * \result  The retrieved dictionary.
87  */
88 Dict *DictSet::GetDict(DictKey const &dictName) 
89 {
90    DictSetHT::iterator dict = Dicts.find(dictName);
91    if ( dict != Dicts.end() )
92    {
93       return dict->second;
94    }
95    return NULL;
96 }
97
98 /**
99  * \brief   Create a DictEntry which will be referenced in no dictionary
100  * @param   group   Group number of the Entry 
101  * @param   elem  Element number of the Entry
102  * @param   vr  Value Representation of the Entry
103  * @param   vm  Value Multiplicity of the Entry
104  * @param   name  English name of the Entry
105  * @return  virtual entry
106  */
107 DictEntry *DictSet::NewVirtualDictEntry( uint16_t group,
108                                          uint16_t elem,
109                                          const VRKey &vr,
110                                          const TagName &vm,
111                                          const TagName &name)
112 {
113    DictEntry *entry;
114
115    // Let's follow 'Purify' advice
116    // const std::string tag = DictEntry::TranslateToKey(group,elem)
117    //                         + "#" + vr + "#" + vm + "#" + name;
118    ExtendedTagKey tag = DictEntry::TranslateToKey(group,elem).str();
119    tag += "#" + vr.str() + "#" + vm + "#" + name;  
120
121    ExtendedTagKeyHT::iterator it;
122    
123    it = VirtualEntries.find(tag);
124    if ( it != VirtualEntries.end() )
125    {
126       entry = &(it->second);
127    }
128    else
129    {
130       DictEntry ent(group, elem, vr, vm, name);
131       VirtualEntries.insert(
132          ExtendedTagKeyHT::value_type(tag, ent) );
133       entry = &(VirtualEntries.find(tag)->second);
134    }
135
136    return entry;
137 }
138
139 /**
140  * \brief   Get the first entry while visiting the DictSet
141  * \return  The first Dict if found, otherwhise NULL
142  */
143 Dict *DictSet::GetFirstEntry()
144 {
145    ItDictHt = Dicts.begin();
146    if ( ItDictHt != Dicts.end() )
147       return ItDictHt->second;
148    return NULL;
149 }
150
151 /**
152  * \brief   Get the next entry while visiting the Hash table (DictSetHT)
153  * \note : meaningfull only if GetFirstEntry already called
154  * \return  The next Dict if found, otherwhise NULL
155  */
156 Dict *DictSet::GetNextEntry()
157 {
158    gdcmAssertMacro (ItDictHt != Dicts.end());
159   
160    ++ItDictHt;
161    if ( ItDictHt != Dicts.end() )
162       return ItDictHt->second;
163    return NULL;
164 }
165
166 /**
167  * \brief   Obtain from the GDCM_DICT_PATH environnement variable the
168  *          path to directory containing the dictionaries. When
169  *          the environnement variable is absent the path is defaulted
170  *          to "../Dicts/".
171  * @return  path to directory containing the dictionaries
172  */
173 std::string DictSet::BuildDictPath() 
174 {
175    std::string resultPath;
176    const char *envPath;
177    envPath = getenv("GDCM_DICT_PATH");
178
179    if (envPath && (strlen(envPath) != 0)) 
180    {
181       resultPath = envPath;
182       gdcmWarningMacro( "Dictionary path set from environnement");
183    } 
184    else
185    {
186       resultPath = PUB_DICT_PATH;
187    }
188    if ( resultPath[resultPath.length()-1] != '/' )
189    {
190       resultPath += '/';
191    }
192
193    return resultPath;
194 }
195
196 //-----------------------------------------------------------------------------
197 // Protected
198 /**
199  * \brief   Adds a Dictionary to a DictSet
200  * \return  always true
201  */
202 bool DictSet::AppendDict(Dict *newDict, DictKey const &name)
203 {
204    Dicts[name] = newDict;
205
206    return true;
207 }
208
209 //-----------------------------------------------------------------------------
210 // Private
211
212 //-----------------------------------------------------------------------------
213 // Print
214 /**
215  * \brief   Print, in an informal fashion, the list of all the dictionaries
216  *          contained is this DictSet, along with their respective content.
217  * @param   os Output stream used for printing.
218  * @param indent Indentation string to be prepended during printing
219  */
220 void DictSet::Print(std::ostream &os, std::string const & )
221 {
222    for (DictSetHT::iterator dict = Dicts.begin(); dict != Dicts.end(); ++dict)
223    {
224       os << "Printing dictionary " << dict->first << std::endl;
225       dict->second->Print(os);
226    }
227 }
228
229 //-----------------------------------------------------------------------------
230 } // end namespace gdcm