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