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