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