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