]> Creatis software - gdcm.git/blob - src/gdcmDictSet.cxx
* src/gdcmDicSet.[h|cxx] : add virtual entries to have a reference of
[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::GetPubDictTagNames(void) 
82 {
83    std::list<std::string> *Result = new std::list<std::string>;
84    TagKeyHT entries = GetDefaultPubDict()->GetEntries();
85    
86    for (TagKeyHT::iterator tag = entries.begin(); tag != entries.end(); ++tag)
87    {
88       Result->push_back( tag->second->GetName() );
89    }
90    return Result;
91 }
92
93 /** 
94  * \ingroup gdcmDictSet
95  * \brief   Consider all the entries of the public dicom dictionnary.
96  *          Build an hashtable whose keys are the names of the groups
97  *          (fourth field in each line of dictionary) and whose corresponding
98  *          values are lists of all the dictionnary entries among that
99  *          group. Note that apparently the Dicom standard doesn't explicitely
100  *          define a name (as a string) for each group.
101  *          A typical usage of this method would be to enable a dynamic
102  *          configuration of a Dicom file browser: the admin/user can
103  *          select in the interface which Dicom tags should be displayed.
104  * \warning Dicom *doesn't* define any name for any 'categorie'
105  *          (the dictionnary fourth field was formerly NIH defined
106  *           - and no longer he is-
107  *           and will be removed when Dicom provides us a text file
108  *           with the 'official' Dictionnary, that would be more friendly
109  *           than asking us to perform a line by line check of the dictionnary
110  *           at the beginning of each year to -try to- guess the changes)
111  *           Therefore : please NEVER use that fourth field :-(
112  * *
113  * @return  An hashtable: whose keys are the names of the groups and whose
114  *          corresponding values are lists of all the dictionnary entries
115  *          among that group.
116  */
117 std::map<std::string, std::list<std::string> > *gdcmDictSet::GetPubDictTagNamesByCategory(void) 
118 {
119    std::map<std::string, std::list<std::string> > *Result = new std::map<std::string, std::list<std::string> >;
120    TagKeyHT entries = GetDefaultPubDict()->GetEntries();
121
122    for (TagKeyHT::iterator tag = entries.begin(); tag != entries.end(); ++tag)
123    {
124       (*Result)[tag->second->GetFourth()].push_back(tag->second->GetName());
125    }
126    return Result;
127 }
128
129 /**
130  * \ingroup gdcmDictSet
131  * \brief   Loads a dictionary from a specified file, and add it
132  *          to already the existing ones contained in this gdcmDictSet.
133  * @param   FileName Absolute or relative filename containing the
134  *          dictionary to load.
135  * @param   Name Symbolic name that be used as identifier of the newly 
136  *          created dictionary.
137  */
138 void gdcmDictSet::LoadDictFromFile(std::string FileName, DictKey Name) 
139 {
140    gdcmDict *NewDict = new gdcmDict(FileName);
141    AppendDict(NewDict,Name);
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  * \ingroup gdcmDictSet
161  * \brief   Retrieve the default reference DICOM V3 public dictionary.
162  * \result  The retrieved default dictionary.
163  */
164 gdcmDict *gdcmDictSet::GetDefaultPubDict() 
165 {
166    return GetDict(PUB_DICT_NAME);
167 }
168
169 /**
170  * \ingroup gdcmDictSet
171  * \brief   Create a gdcmDictEntry which will be reference 
172  *          in no dictionnary
173  * @return  virtual entry
174  */
175 gdcmDictEntry *gdcmDictSet::NewVirtualDictEntry(guint16 group, guint16 element,
176                                                 std::string vr,std::string fourth,
177                                                 std::string name)
178 {
179    gdcmDictEntry *entry;
180    std::string tag=gdcmDictEntry::TranslateToKey(group,element)+vr;
181    std::map<std::string,gdcmDictEntry *>::iterator it;
182    
183    it=virtualEntry.find(tag);
184    if(it!=virtualEntry.end())
185    {
186       entry=it->second;
187    }
188    else
189    {
190       entry=new gdcmDictEntry(group,element,vr,fourth,name);
191       virtualEntry[tag]=entry;
192    }
193    return(entry);
194 }
195
196 /**
197  * \ingroup gdcmDictSet
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