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