]> Creatis software - gdcm.git/blob - src/gdcmDictSet.cxx
* src/gdcmDictGroupName.[h|cxx] : add a correlation between a group (number)
[gdcm.git] / src / gdcmDictSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDictSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/04/05 10:56:25 $
7   Version:   $Revision: 1.62 $
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   Get the first entry while visiting the DictSet
132  * \return  The first Dict if found, otherwhise NULL
133  */
134 Dict *DictSet::GetFirstEntry()
135 {
136    ItDictHt = Dicts.begin();
137    if( ItDictHt != Dicts.end() )
138       return ItDictHt->second;
139    return NULL;
140 }
141
142 /**
143  * \brief   Get the next entry while visiting the Hash table (DictSetHT)
144  * \note : meaningfull only if GetFirstEntry already called
145  * \return  The next Dict if found, otherwhise NULL
146  */
147 Dict *DictSet::GetNextEntry()
148 {
149    gdcmAssertMacro (ItDictHt != Dicts.end());
150   
151    ++ItDictHt;
152    if ( ItDictHt != Dicts.end() )
153       return ItDictHt->second;
154    return NULL;
155 }
156
157 /**
158  * \brief   Obtain from the GDCM_DICT_PATH environnement variable the
159  *          path to directory containing the dictionaries. When
160  *          the environnement variable is absent the path is defaulted
161  *          to "../Dicts/".
162  * @return  path to directory containing the dictionaries
163  */
164 std::string DictSet::BuildDictPath() 
165 {
166    std::string resultPath;
167    const char *envPath;
168    envPath = getenv("GDCM_DICT_PATH");
169
170    if (envPath && (strlen(envPath) != 0)) 
171    {
172       resultPath = envPath;
173       gdcmWarningMacro( "Dictionary path set from environnement");
174    } 
175    else
176    {
177       resultPath = PUB_DICT_PATH;
178    }
179    if ( resultPath[resultPath.length()-1] != '/' )
180    {
181       resultPath += '/';
182    }
183
184    return resultPath;
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