]> Creatis software - gdcm.git/blob - src/gdcmDictSet.cxx
ENH: Minor cleanup, mostly remove comments
[gdcm.git] / src / gdcmDictSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDictSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/10/27 22:47:20 $
7   Version:   $Revision: 1.43 $
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  * \ingroup DictSet
31  * \brief   The Dictionnary Set obtained with this constructor simply
32  *          contains the Default Public dictionnary.
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  * \ingroup DictSet
44  * \brief  Destructor 
45  */
46 DictSet::~DictSet() 
47 {
48    // Remove dictionnaries
49    for (DictSetHT::iterator tag = Dicts.begin(); tag != Dicts.end(); ++tag) 
50    {
51       Dict *entryToDelete = tag->second;
52       if ( entryToDelete )
53       {
54          delete entryToDelete;
55       }
56       tag->second = NULL;
57    }
58    Dicts.clear();
59
60    // Remove virtual dictionnary entries
61    VirtualEntry.clear();
62 }
63
64 //-----------------------------------------------------------------------------
65 // Print
66 /**
67  * \ingroup DictSet
68  * \brief   Print, in an informal fashion, the list of all the dictionaries
69  *          contained is this DictSet, along with their respective content.
70  * @param   os Output stream used for printing.
71  */
72 void DictSet::Print(std::ostream& os) 
73 {
74    for (DictSetHT::iterator dict = Dicts.begin(); dict != Dicts.end(); ++dict)
75    {
76       os << "Printing dictionary " << dict->first << std::endl;
77       dict->second->Print(os);
78    }
79 }
80
81 //-----------------------------------------------------------------------------
82 // Public
83 /** 
84  * \ingroup DictSet
85  * \brief   Consider all the entries of the public dicom dictionnary. 
86  *          Build all list of all the tag names of all those entries.
87  * \sa DictSet::GetPubDictTagNamesByCategory
88  * @return  A list of all entries of the public dicom dictionnary.
89  */
90 EntryNamesList * DictSet::GetPubDictEntryNames() 
91 {
92    return GetDefaultPubDict()->GetDictEntryNames();
93 }
94
95 /** 
96  * \ingroup DictSet
97  * \brief   
98  *          - Consider all the entries of the public dicom dictionnary.
99  *          - Build an hashtable whose keys are the names of the groups
100  *           (fourth field in each line of dictionary) and whose corresponding
101  *           values are lists of all the dictionnary entries among that
102  *           group. Note that apparently the Dicom standard doesn't explicitely
103  *           define a name (as a string) for each 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 dictionnary 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' Dictionnary, that would be more friendly
113  *           than asking us to perform a line by line check of the dictionnary
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 dictionnary entries
119  *          among that group.
120  */
121 EntryNamesByCatMap * DictSet::GetPubDictEntryNamesByCategory() 
122 {
123    return GetDefaultPubDict()->GetDictEntryNamesByCategory();
124 }
125
126 /**
127  * \ingroup DictSet
128  * \brief   Loads a dictionary from a specified file, and add it
129  *          to already the existing ones contained in this DictSet.
130  * @param   filename Absolute or relative filename containing the
131  *          dictionary to load.
132  * @param   name Symbolic name that be used as identifier of the newly 
133  *          created dictionary.
134  */
135 Dict *DictSet::LoadDictFromFile(std::string const & filename, 
136                                 DictKey const & name) 
137 {
138    Dict *newDict = new Dict(filename);
139    AppendDict(newDict, name);
140
141    return newDict;
142 }
143
144 /**
145  * \ingroup DictSet
146  * \brief   Retrieve the specified dictionary (when existing) from this
147  *          DictSet.
148  * @param   dictName The symbolic name of the searched dictionary.
149  * \result  The retrieved dictionary.
150  */
151 Dict *DictSet::GetDict(DictKey const & dictName) 
152 {
153    DictSetHT::iterator dict = Dicts.find(dictName);
154    if(dict != Dicts.end())
155    {
156       return dict->second;
157    }
158    return NULL;
159 }
160
161 /**
162  * \brief   Create a DictEntry which will be reference 
163  *          in no dictionnary
164  * @return  virtual entry
165  */
166 DictEntry *DictSet::NewVirtualDictEntry( uint16_t group,
167                                          uint16_t element,
168                                          TagName vr,
169                                          TagName fourth,
170                                          TagName name)
171 {
172    DictEntry *entry;
173    const std::string tag = DictEntry::TranslateToKey(group,element)
174                            + "#" + vr + "#" + fourth + "#" + name;
175    TagKeyHT::iterator it;
176    
177    it = VirtualEntry.find(tag);
178    if(it != VirtualEntry.end())
179    {
180       entry = &(it->second);
181    }
182    else
183    {
184       DictEntry ent(group, element, vr, fourth, name);
185       VirtualEntry.insert(
186          TagKeyHT::value_type<TagKey, DictEntry>
187             (tag, ent));
188       entry = &(VirtualEntry.find(tag)->second);
189    }
190
191    return entry;
192 }
193
194 /**
195  * \brief   Obtain from the GDCM_DICT_PATH environnement variable the
196  *          path to directory containing the dictionnaries. When
197  *          the environnement variable is absent the path is defaulted
198  *          to "../Dicts/".
199  * @return  path to directory containing the dictionnaries
200  */
201 std::string DictSet::BuildDictPath() 
202 {
203    std::string resultPath;
204    const char *envPath = 0;
205    envPath = getenv("GDCM_DICT_PATH");
206
207    if (envPath && (strlen(envPath) != 0)) 
208    {
209       resultPath = envPath;
210       if ( resultPath[resultPath.length()-1] != '/' )
211       {
212          resultPath += '/';
213       }
214       dbg.Verbose(1, "DictSet::BuildDictPath:",
215                      "Dictionary path set from environnement");
216    } 
217    else
218    {
219       resultPath = PUB_DICT_PATH;
220    }
221
222    return resultPath;
223 }
224
225 //-----------------------------------------------------------------------------
226 // Protected
227 bool DictSet::AppendDict(Dict *newDict, DictKey const & name)
228 {
229    Dicts[name] = newDict;
230
231    return true;
232 }
233
234 //-----------------------------------------------------------------------------
235 // Private
236
237 //-----------------------------------------------------------------------------
238
239 } // end namespace gdcm