]> Creatis software - gdcm.git/blob - src/gdcmDictSet.cxx
-add method GetFirstEntry to replace call to InitTraversal+GetNextEntry
[gdcm.git] / src / gdcmDictSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDictSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/18 07:50:58 $
7   Version:   $Revision: 1.52 $
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 dictionnaries
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 // Print
64 /**
65  * \brief   Print, in an informal fashion, the list of all the dictionaries
66  *          contained is this DictSet, along with their respective content.
67  * @param   os Output stream used for printing.
68  */
69 void DictSet::Print(std::ostream &os, std::string const & )
70 {
71    for (DictSetHT::iterator dict = Dicts.begin(); dict != Dicts.end(); ++dict)
72    {
73       os << "Printing dictionary " << dict->first << std::endl;
74       dict->second->Print(os);
75    }
76 }
77
78 //-----------------------------------------------------------------------------
79 // Public
80 /** 
81  * \brief   Consider all the entries of the public dicom dictionary. 
82  *          Build all list of all the tag names of all those entries.
83  * \sa DictSet::GetPubDictTagNamesByCategory
84  * @return  A list of all entries of the public dicom dictionary.
85  */
86
87 // Probabely useless!
88  
89 //EntryNamesList *DictSet::GetPubDictEntryNames() 
90 //{
91 //   return GetDefaultPubDict()->GetDictEntryNames();
92 //}
93
94 /** 
95  * \brief   
96  *          - Consider all the entries of the public dicom dictionary.
97  *          - Build an hashtable whose keys are the names of the groups
98  *           (fourth field in each line of dictionary) and whose corresponding
99  *           values are lists of all the dictionary entries among that
100  *           group. Note that apparently the Dicom standard doesn't explicitely
101  *           define a name (as a string) for each group.
102  *           NO ! Dicom Standard explicitely doesn't define 
103  *                any name, for any group !
104  *          - A typical usage of this method would be to enable a dynamic
105  *           configuration of a Dicom file browser: the admin/user can
106  *           select in the interface which Dicom tags should be displayed.
107  * \warning 
108  *          - Dicom *doesn't* define any name for any 'categorie'
109  *          (the dictionary fourth field was formerly NIH defined
110  *           -and no longer he is-
111  *           and will be removed when Dicom provides us a text file
112  *           with the 'official' Dictionary, that would be more friendly
113  *           than asking us to perform a line by line check of the dictionary
114  *           at the beginning of each year to -try to- guess the changes)
115  *          - Therefore : please NEVER use that fourth field :-(
116  * *
117  * @return  An hashtable: whose keys are the names of the groups and whose
118  *          corresponding values are lists of all the dictionary entries
119  *          among that group.
120  */
121
122
123 // Probabely useless!
124  
125 //EntryNamesByCatMap *DictSet::GetPubDictEntryNamesByCategory() 
126 //{
127 //   return GetDefaultPubDict()->GetDictEntryNamesByCategory();
128 //}
129
130 /**
131  * \brief   Loads a dictionary from a specified file, and add it
132  *          to already the existing ones contained in this DictSet.
133  * @param   filename Absolute or relative filename containing the
134  *          dictionary to load.
135  * @param   name Symbolic name that be used as identifier of the newly 
136  *          created dictionary.
137  */
138 Dict *DictSet::LoadDictFromFile(std::string const & filename, 
139                                 DictKey const & name) 
140 {
141    Dict *newDict = new Dict(filename);
142    AppendDict(newDict, name);
143
144    return newDict;
145 }
146
147 /**
148  * \brief   Retrieve the specified dictionary (when existing) from this
149  *          DictSet.
150  * @param   dictName The symbolic name of the searched dictionary.
151  * \result  The retrieved dictionary.
152  */
153 Dict *DictSet::GetDict(DictKey const &dictName) 
154 {
155    DictSetHT::iterator dict = Dicts.find(dictName);
156    if(dict != Dicts.end())
157    {
158       return dict->second;
159    }
160    return NULL;
161 }
162
163 /**
164  * \brief   Create a DictEntry which will be referenced 
165  *          in no dictionary
166  * @return  virtual entry
167  */
168 DictEntry *DictSet::NewVirtualDictEntry( uint16_t group,
169                                          uint16_t element,
170                                          TagName vr,
171                                          TagName vm,
172                                          TagName name)
173 {
174    DictEntry *entry;
175    const std::string tag = DictEntry::TranslateToKey(group,element)
176                            + "#" + vr + "#" + vm + "#" + name;
177    TagKeyHT::iterator it;
178    
179    it = VirtualEntry.find(tag);
180    if(it != VirtualEntry.end())
181    {
182       entry = &(it->second);
183    }
184    else
185    {
186       DictEntry ent(group, element, vr, vm, name);
187       VirtualEntry.insert(
188          std::map<TagKey, DictEntry>::value_type
189             (tag, ent));
190       entry = &(VirtualEntry.find(tag)->second);
191    }
192
193    return entry;
194 }
195
196 /**
197  * \brief   Obtain from the GDCM_DICT_PATH environnement variable the
198  *          path to directory containing the dictionnaries. When
199  *          the environnement variable is absent the path is defaulted
200  *          to "../Dicts/".
201  * @return  path to directory containing the dictionnaries
202  */
203 std::string DictSet::BuildDictPath() 
204 {
205    std::string resultPath;
206    const char *envPath = 0;
207    envPath = getenv("GDCM_DICT_PATH");
208
209    if (envPath && (strlen(envPath) != 0)) 
210    {
211       resultPath = envPath;
212       if ( resultPath[resultPath.length()-1] != '/' )
213       {
214          resultPath += '/';
215       }
216       gdcmVerboseMacro( "Dictionary path set from environnement");
217    } 
218    else
219    {
220       resultPath = PUB_DICT_PATH;
221    }
222
223    return resultPath;
224 }
225
226
227 /**
228  * \brief   Get the first entry while visiting the DictSet
229  * \return  The first Dict if found, otherwhise NULL
230  */
231 Dict *DictSet::GetFirstEntry()
232 {
233    ItDictHt = Dicts.begin();
234    return ItDictHt->second;
235 }
236
237 /**
238  * \brief   Get the next entry while visiting the Hash table (DictSetHT)
239  * \note : meaningfull only if GetFirstEntry already called
240  * \return  The next Dict if found, otherwhise NULL
241  */
242 Dict *DictSet::GetNextEntry()
243 {
244    if (ItDictHt != Dicts.end())
245    {
246       Dict *tmp = ItDictHt->second;
247       ++ItDictHt;
248
249       return tmp;
250    }
251    else
252    {
253       return NULL;
254    }
255 }
256
257 //-----------------------------------------------------------------------------
258 // Protected
259
260 /**
261  * \brief   Adds a Dictionary to a DictSet
262  * \return  always true
263  */
264 bool DictSet::AppendDict(Dict *newDict, DictKey const &name)
265 {
266    Dicts[name] = newDict;
267
268    return true;
269 }
270
271 //-----------------------------------------------------------------------------
272 // Private
273
274 //-----------------------------------------------------------------------------
275
276 } // end namespace gdcm