]> Creatis software - gdcm.git/blob - src/gdcmDictSet.cxx
- guint16 and guint32 removed. Use ISO C uint16_t, uint32_t instead.
[gdcm.git] / src / gdcmDictSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDictSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/07/02 13:55:27 $
7   Version:   $Revision: 1.32 $
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(void) 
32 {
33    DictPath = BuildDictPath();
34    std::string PubDictFile(DictPath);
35    PubDictFile += PUB_DICT_FILENAME;  /// MEMORY LEAK std::string::operator+=
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          delete EntryToDelete;
51       tag->second=NULL;
52    }
53    Dicts.clear();
54
55    // Remove virtual dictionnary entries
56    std::map<std::string,gdcmDictEntry *>::iterator it;
57    for(it=virtualEntry.begin(); it!=virtualEntry.end(); ++it)
58    {
59       gdcmDictEntry *Entry = it->second;
60       if ( Entry )
61          delete Entry;
62       it->second=NULL;
63    }
64 }
65
66 //-----------------------------------------------------------------------------
67 // Print
68 /**
69  * \ingroup gdcmDictSet
70  * \brief   Print, in an informal fashion, the list of all the dictionaries
71  *          contained is this gdcmDictSet, along with their respective content.
72  * @param   os Output stream used for printing.
73  */
74 void gdcmDictSet::Print(std::ostream& os) 
75 {
76    for (DictSetHT::iterator dict = Dicts.begin(); dict != Dicts.end(); ++dict)
77    {
78       os << "Printing dictionary " << dict->first << std::endl;
79       dict->second->Print(os);
80    }
81 }
82
83 //-----------------------------------------------------------------------------
84 // Public
85 /** 
86  * \ingroup gdcmDictSet
87  * \brief   Consider all the entries of the public dicom dictionnary. 
88  *          Build all list of all the tag names of all those entries.
89  * \sa gdcmDictSet::GetPubDictTagNamesByCategory
90  * @return  A list of all entries of the public dicom dictionnary.
91  */
92 std::list<std::string> *gdcmDictSet::GetPubDictEntryNames(void) 
93 {
94    return(GetDefaultPubDict()->GetDictEntryNames());
95 }
96
97 /** 
98  * \ingroup gdcmDictSet
99  * \brief   
100  *          - Consider all the entries of the public dicom dictionnary.
101  *          - Build an hashtable whose keys are the names of the groups
102  *           (fourth field in each line of dictionary) and whose corresponding
103  *           values are lists of all the dictionnary entries among that
104  *           group. Note that apparently the Dicom standard doesn't explicitely
105  *           define a name (as a string) for each group.
106  *          - A typical usage of this method would be to enable a dynamic
107  *           configuration of a Dicom file browser: the admin/user can
108  *           select in the interface which Dicom tags should be displayed.
109  * \warning 
110  *          - Dicom *doesn't* define any name for any 'categorie'
111  *          (the dictionnary fourth field was formerly NIH defined
112  *           -and no longer he is-
113  *           and will be removed when Dicom provides us a text file
114  *           with the 'official' Dictionnary, that would be more friendly
115  *           than asking us to perform a line by line check of the dictionnary
116  *           at the beginning of each year to -try to- guess the changes)
117  *          - Therefore : please NEVER use that fourth field :-(
118  * *
119  * @return  An hashtable: whose keys are the names of the groups and whose
120  *          corresponding values are lists of all the dictionnary entries
121  *          among that group.
122  */
123 std::map<std::string, std::list<std::string> > *gdcmDictSet::GetPubDictEntryNamesByCategory(void) 
124 {
125    return(GetDefaultPubDict()->GetDictEntryNamesByCategory());
126 }
127
128 /**
129  * \ingroup gdcmDictSet
130  * \brief   Loads a dictionary from a specified file, and add it
131  *          to already the existing ones contained in this gdcmDictSet.
132  * @param   FileName Absolute or relative filename containing the
133  *          dictionary to load.
134  * @param   Name Symbolic name that be used as identifier of the newly 
135  *          created dictionary.
136  */
137 gdcmDict *gdcmDictSet::LoadDictFromFile(std::string FileName, DictKey Name) 
138 {
139    gdcmDict *NewDict = new gdcmDict(FileName);
140    AppendDict(NewDict,Name);
141    return(NewDict);
142 }
143
144 /**
145  * \ingroup gdcmDictSet
146  * \brief   Retrieve the specified dictionary (when existing) from this
147  *          gdcmDictSet.
148  * @param   DictName The symbolic name of the searched dictionary.
149  * \result  The retrieved dictionary.
150  */
151 gdcmDict *gdcmDictSet::GetDict(DictKey DictName) 
152 {
153    DictSetHT::iterator dict = Dicts.find(DictName);
154    if(dict!=Dicts.end())
155       return dict->second;
156    return NULL;
157 }
158
159 /**
160  * \brief   Retrieve the default reference DICOM V3 public dictionary.
161  * \result  The retrieved default dictionary.
162  */
163 gdcmDict *gdcmDictSet::GetDefaultPubDict() 
164 {
165    return GetDict(PUB_DICT_NAME);
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    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    return(entry);
195 }
196
197 /**
198  * \brief   Obtain from the GDCM_DICT_PATH environnement variable the
199  *          path to directory containing the dictionnaries. When
200  *          the environnement variable is absent the path is defaulted
201  *          to "../Dicts/".
202  * @return  path to directory containing the dictionnaries
203  */
204 std::string gdcmDictSet::BuildDictPath(void) 
205 {
206    std::string ResultPath;
207    const char *EnvPath = (char*)0;
208    EnvPath = getenv("GDCM_DICT_PATH");
209    if (EnvPath && (strlen(EnvPath) != 0)) 
210    {
211       ResultPath = EnvPath;
212       if (ResultPath[ResultPath.length() -1] != '/' )
213          ResultPath += '/';
214       dbg.Verbose(1, "gdcmDictSet::BuildDictPath:",
215                      "Dictionary path set from environnement");
216    } 
217    else
218       ResultPath = PUB_DICT_PATH;
219    return ResultPath;
220 }
221
222 //-----------------------------------------------------------------------------
223 // Protected
224 bool gdcmDictSet::AppendDict(gdcmDict *NewDict,DictKey Name)
225 {
226    Dicts[Name] = NewDict;
227    return(true);
228 }
229
230 //-----------------------------------------------------------------------------
231 // Private
232
233 //-----------------------------------------------------------------------------
234
235