]> Creatis software - gdcm.git/blob - src/gdcmDictSet.cxx
* src/*.cxx *.h Reference to License.htm fixed to License.html.
[gdcm.git] / src / gdcmDictSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDictSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/09/27 08:39:06 $
7   Version:   $Revision: 1.37 $
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 //-----------------------------------------------------------------------------
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   Create a gdcmDictEntry which will be reference 
170  *          in no dictionnary
171  * @return  virtual entry
172  */
173 gdcmDictEntry *gdcmDictSet::NewVirtualDictEntry(uint16_t group,
174                                                 uint16_t element,
175                                                 std::string vr,
176                                                 std::string fourth,
177                                                 std::string name)
178 {
179    gdcmDictEntry* entry;
180    const std::string tag = gdcmDictEntry::TranslateToKey(group,element)
181                            + "#" + vr + "#" + fourth + "#" + name;
182    std::map<std::string,gdcmDictEntry *>::iterator it;
183    
184    it = VirtualEntry.find(tag);
185    if(it != VirtualEntry.end())
186    {
187       entry = it->second;
188    }
189    else
190    {
191       entry = new gdcmDictEntry(group, element, vr, fourth, name);
192       VirtualEntry[tag] = entry;
193    }
194
195    return entry;
196 }
197
198 /**
199  * \brief   Obtain from the GDCM_DICT_PATH environnement variable the
200  *          path to directory containing the dictionnaries. When
201  *          the environnement variable is absent the path is defaulted
202  *          to "../Dicts/".
203  * @return  path to directory containing the dictionnaries
204  */
205 std::string gdcmDictSet::BuildDictPath() 
206 {
207    std::string resultPath;
208    const char *envPath = 0;
209    envPath = getenv("GDCM_DICT_PATH");
210
211    if (envPath && (strlen(envPath) != 0)) 
212    {
213       resultPath = envPath;
214       if ( resultPath[resultPath.length()-1] != '/' )
215       {
216          resultPath += '/';
217       }
218       dbg.Verbose(1, "gdcmDictSet::BuildDictPath:",
219                      "Dictionary path set from environnement");
220    } 
221    else
222    {
223       resultPath = PUB_DICT_PATH;
224    }
225
226    return resultPath;
227 }
228
229 //-----------------------------------------------------------------------------
230 // Protected
231 bool gdcmDictSet::AppendDict(gdcmDict *newDict, DictKey const & name)
232 {
233    Dicts[name] = newDict;
234
235    return true;   //FIXME
236 }
237
238 //-----------------------------------------------------------------------------
239 // Private
240
241 //-----------------------------------------------------------------------------
242
243