]> Creatis software - gdcm.git/blob - src/gdcmDictSet.cxx
ENH: Guess what ! Still some cosmetic cleanup
[gdcm.git] / src / gdcmDictSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDictSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/08/01 02:39:09 $
7   Version:   $Revision: 1.34 $
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.htm 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 //-----------------------------------------------------------------------------
25 // Constructor / Destructor
26 /** 
27  * \ingroup gdcmDictSet
28  * \brief   The Dictionnary Set obtained with this constructor simply
29  *          contains the Default Public dictionnary.
30  */
31 gdcmDictSet::gdcmDictSet() 
32 {
33    DictPath = BuildDictPath();
34    std::string pubDictFile(DictPath);
35    pubDictFile += PUB_DICT_FILENAME;
36    Dicts[PUB_DICT_NAME] = new gdcmDict(pubDictFile);
37 }
38
39 /**
40  * \ingroup gdcmDictSet
41  * \brief  Destructor 
42  */
43 gdcmDictSet::~gdcmDictSet() 
44 {
45    // Remove dictionnaries
46    for (DictSetHT::iterator tag = Dicts.begin(); tag != Dicts.end(); ++tag) 
47    {
48       gdcmDict *entryToDelete = tag->second;
49       if ( entryToDelete )
50       {
51          delete entryToDelete;
52       }
53       tag->second = NULL;
54    }
55    Dicts.clear();
56
57    // Remove virtual dictionnary entries
58    std::map<std::string,gdcmDictEntry *>::iterator it;
59    for(it = VirtualEntry.begin(); it != VirtualEntry.end(); ++it)
60    {
61       gdcmDictEntry *entry = it->second;
62       if ( entry )
63       {
64          delete entry;
65       }
66       it->second = NULL;
67    }
68 }
69
70 //-----------------------------------------------------------------------------
71 // Print
72 /**
73  * \ingroup gdcmDictSet
74  * \brief   Print, in an informal fashion, the list of all the dictionaries
75  *          contained is this gdcmDictSet, along with their respective content.
76  * @param   os Output stream used for printing.
77  */
78 void gdcmDictSet::Print(std::ostream& os) 
79 {
80    for (DictSetHT::iterator dict = Dicts.begin(); dict != Dicts.end(); ++dict)
81    {
82       os << "Printing dictionary " << dict->first << std::endl;
83       dict->second->Print(os);
84    }
85 }
86
87 //-----------------------------------------------------------------------------
88 // Public
89 /** 
90  * \ingroup gdcmDictSet
91  * \brief   Consider all the entries of the public dicom dictionnary. 
92  *          Build all list of all the tag names of all those entries.
93  * \sa gdcmDictSet::GetPubDictTagNamesByCategory
94  * @return  A list of all entries of the public dicom dictionnary.
95  */
96 std::list<std::string> *gdcmDictSet::GetPubDictEntryNames() 
97 {
98    return GetDefaultPubDict()->GetDictEntryNames();
99 }
100
101 /** 
102  * \ingroup gdcmDictSet
103  * \brief   
104  *          - Consider all the entries of the public dicom dictionnary.
105  *          - Build an hashtable whose keys are the names of the groups
106  *           (fourth field in each line of dictionary) and whose corresponding
107  *           values are lists of all the dictionnary entries among that
108  *           group. Note that apparently the Dicom standard doesn't explicitely
109  *           define a name (as a string) for each group.
110  *          - A typical usage of this method would be to enable a dynamic
111  *           configuration of a Dicom file browser: the admin/user can
112  *           select in the interface which Dicom tags should be displayed.
113  * \warning 
114  *          - Dicom *doesn't* define any name for any 'categorie'
115  *          (the dictionnary fourth field was formerly NIH defined
116  *           -and no longer he is-
117  *           and will be removed when Dicom provides us a text file
118  *           with the 'official' Dictionnary, that would be more friendly
119  *           than asking us to perform a line by line check of the dictionnary
120  *           at the beginning of each year to -try to- guess the changes)
121  *          - Therefore : please NEVER use that fourth field :-(
122  * *
123  * @return  An hashtable: whose keys are the names of the groups and whose
124  *          corresponding values are lists of all the dictionnary entries
125  *          among that group.
126  */
127 std::map<std::string, std::list<std::string> > *
128    gdcmDictSet::GetPubDictEntryNamesByCategory() 
129 {
130    return GetDefaultPubDict()->GetDictEntryNamesByCategory();
131 }
132
133 /**
134  * \ingroup gdcmDictSet
135  * \brief   Loads a dictionary from a specified file, and add it
136  *          to already the existing ones contained in this gdcmDictSet.
137  * @param   FileName Absolute or relative filename containing the
138  *          dictionary to load.
139  * @param   Name Symbolic name that be used as identifier of the newly 
140  *          created dictionary.
141  */
142 gdcmDict *gdcmDictSet::LoadDictFromFile(std::string const & fileName, 
143                                         DictKey const & name) 
144 {
145    gdcmDict *newDict = new gdcmDict(fileName);
146    AppendDict(newDict, name);
147
148    return newDict;
149 }
150
151 /**
152  * \ingroup gdcmDictSet
153  * \brief   Retrieve the specified dictionary (when existing) from this
154  *          gdcmDictSet.
155  * @param   DictName The symbolic name of the searched dictionary.
156  * \result  The retrieved dictionary.
157  */
158 gdcmDict *gdcmDictSet::GetDict(DictKey const & dictName) 
159 {
160    DictSetHT::iterator dict = Dicts.find(dictName);
161    if(dict != Dicts.end())
162    {
163       return dict->second;
164    }
165    return NULL;
166 }
167
168 /**
169  * \brief   Retrieve the default reference DICOM V3 public dictionary.
170  * \result  The retrieved default dictionary.
171  */
172 gdcmDict *gdcmDictSet::GetDefaultPubDict() 
173 {
174    return GetDict(PUB_DICT_NAME);
175 }
176
177 /**
178  * \brief   Create a gdcmDictEntry which will be reference 
179  *          in no dictionnary
180  * @return  virtual entry
181  */
182 gdcmDictEntry *gdcmDictSet::NewVirtualDictEntry(uint16_t group,
183                                                 uint16_t element,
184                                                 std::string vr,
185                                                 std::string fourth,
186                                                 std::string name)
187 {
188    gdcmDictEntry* entry;
189    const std::string tag = gdcmDictEntry::TranslateToKey(group,element)
190                            + "#" + vr + "#" + fourth + "#" + name;
191    std::map<std::string,gdcmDictEntry *>::iterator it;
192    
193    it = VirtualEntry.find(tag);
194    if(it != VirtualEntry.end())
195    {
196       entry = it->second;
197    }
198    else
199    {
200       entry = new gdcmDictEntry(group, element, vr, fourth, name);
201       VirtualEntry[tag] = entry;
202    }
203
204    return entry;
205 }
206
207 /**
208  * \brief   Obtain from the GDCM_DICT_PATH environnement variable the
209  *          path to directory containing the dictionnaries. When
210  *          the environnement variable is absent the path is defaulted
211  *          to "../Dicts/".
212  * @return  path to directory containing the dictionnaries
213  */
214 std::string gdcmDictSet::BuildDictPath() 
215 {
216    std::string resultPath;
217    const char *envPath = 0;
218    envPath = getenv("GDCM_DICT_PATH");
219
220    if (envPath && (strlen(envPath) != 0)) 
221    {
222       resultPath = envPath;
223       if ( resultPath[resultPath.length()-1] != '/' )
224       {
225          resultPath += '/';
226       }
227       dbg.Verbose(1, "gdcmDictSet::BuildDictPath:",
228                      "Dictionary path set from environnement");
229    } 
230    else
231    {
232       resultPath = PUB_DICT_PATH;
233    }
234
235    return resultPath;
236 }
237
238 //-----------------------------------------------------------------------------
239 // Protected
240 bool gdcmDictSet::AppendDict(gdcmDict *newDict, DictKey const & name)
241 {
242    Dicts[name] = newDict;
243
244    return true;   //FIXME
245 }
246
247 //-----------------------------------------------------------------------------
248 // Private
249
250 //-----------------------------------------------------------------------------
251
252