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