]> Creatis software - gdcm.git/blob - src/gdcmDictSet.cxx
ENH: A TagKey is a TagKey and not a string or TagName... #2
[gdcm.git] / src / gdcmDictSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDictSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/07/11 14:53:16 $
7   Version:   $Revision: 1.66 $
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                                          TagName vr,
110                                          TagName vm,
111                                          TagName name)
112 {
113    DictEntry *entry;
114
115   // Let's follow 'Purify' advice
116   //
117   // const std::string tag = DictEntry::TranslateToKey(group,elem)
118   //                         + "#" + vr + "#" + vm + "#" + name;
119    char res[10];
120    sprintf(res,"%04x|%04x", group, elem);
121    TagKey tag = res;
122    tag += "#" + vr + "#" + vm + "#" + name;  
123   
124    TagKeyHT::iterator it;
125    
126    it = VirtualEntries.find(tag);
127    if ( it != VirtualEntries.end() )
128    {
129       entry = &(it->second);
130    }
131    else
132    {
133       DictEntry ent(group, elem, vr, vm, name);
134       VirtualEntries.insert(
135          std::map<TagKey, DictEntry>::value_type(tag, ent) );
136       entry = &(VirtualEntries.find(tag)->second);
137    }
138
139    return entry;
140 }
141
142 /**
143  * \brief   Get the first entry while visiting the DictSet
144  * \return  The first Dict if found, otherwhise NULL
145  */
146 Dict *DictSet::GetFirstEntry()
147 {
148    ItDictHt = Dicts.begin();
149    if ( ItDictHt != Dicts.end() )
150       return ItDictHt->second;
151    return NULL;
152 }
153
154 /**
155  * \brief   Get the next entry while visiting the Hash table (DictSetHT)
156  * \note : meaningfull only if GetFirstEntry already called
157  * \return  The next Dict if found, otherwhise NULL
158  */
159 Dict *DictSet::GetNextEntry()
160 {
161    gdcmAssertMacro (ItDictHt != Dicts.end());
162   
163    ++ItDictHt;
164    if ( ItDictHt != Dicts.end() )
165       return ItDictHt->second;
166    return NULL;
167 }
168
169 /**
170  * \brief   Obtain from the GDCM_DICT_PATH environnement variable the
171  *          path to directory containing the dictionaries. When
172  *          the environnement variable is absent the path is defaulted
173  *          to "../Dicts/".
174  * @return  path to directory containing the dictionaries
175  */
176 std::string DictSet::BuildDictPath() 
177 {
178    std::string resultPath;
179    const char *envPath;
180    envPath = getenv("GDCM_DICT_PATH");
181
182    if (envPath && (strlen(envPath) != 0)) 
183    {
184       resultPath = envPath;
185       gdcmWarningMacro( "Dictionary path set from environnement");
186    } 
187    else
188    {
189       resultPath = PUB_DICT_PATH;
190    }
191    if ( resultPath[resultPath.length()-1] != '/' )
192    {
193       resultPath += '/';
194    }
195
196    return resultPath;
197 }
198
199 //-----------------------------------------------------------------------------
200 // Protected
201 /**
202  * \brief   Adds a Dictionary to a DictSet
203  * \return  always true
204  */
205 bool DictSet::AppendDict(Dict *newDict, DictKey const &name)
206 {
207    Dicts[name] = newDict;
208
209    return true;
210 }
211
212 //-----------------------------------------------------------------------------
213 // Private
214
215 //-----------------------------------------------------------------------------
216 // Print
217 /**
218  * \brief   Print, in an informal fashion, the list of all the dictionaries
219  *          contained is this DictSet, along with their respective content.
220  * @param   os Output stream used for printing.
221  * @param indent Indentation string to be prepended during printing
222  */
223 void DictSet::Print(std::ostream &os, std::string const & )
224 {
225    for (DictSetHT::iterator dict = Dicts.begin(); dict != Dicts.end(); ++dict)
226    {
227       os << "Printing dictionary " << dict->first << std::endl;
228       dict->second->Print(os);
229    }
230 }
231
232 //-----------------------------------------------------------------------------
233 } // end namespace gdcm