]> Creatis software - gdcm.git/blob - src/gdcmDictSet.cxx
doxygenation
[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 
98  *          - Dicom *doesn't* define any name for any 'categorie'
99  *          (the dictionnary fourth field was formerly NIH defined
100  *           -and no longer he is-
101  *           and will be removed when Dicom provides us a text file
102  *           with the 'official' Dictionnary, that would be more friendly
103  *           than asking us to perform a line by line check of the dictionnary
104  *           at the beginning of each year to -try to- guess the changes)
105  *          - Therefore : please NEVER use that fourth field :-(
106  * *
107  * @return  An hashtable: whose keys are the names of the groups and whose
108  *          corresponding values are lists of all the dictionnary entries
109  *          among that group.
110  */
111 std::map<std::string, std::list<std::string> > *gdcmDictSet::GetPubDictEntryNamesByCategory(void) 
112 {
113    return(GetDefaultPubDict()->GetDictEntryNamesByCategory());
114 }
115
116 /**
117  * \ingroup gdcmDictSet
118  * \brief   Loads a dictionary from a specified file, and add it
119  *          to already the existing ones contained in this gdcmDictSet.
120  * @param   FileName Absolute or relative filename containing the
121  *          dictionary to load.
122  * @param   Name Symbolic name that be used as identifier of the newly 
123  *          created dictionary.
124  */
125 gdcmDict *gdcmDictSet::LoadDictFromFile(std::string FileName, DictKey Name) 
126 {
127    gdcmDict *NewDict = new gdcmDict(FileName);
128    AppendDict(NewDict,Name);
129    return(NewDict);
130 }
131
132 /**
133  * \ingroup gdcmDictSet
134  * \brief   Retrieve the specified dictionary (when existing) from this
135  *          gdcmDictSet.
136  * @param   DictName The symbolic name of the searched dictionary.
137  * \result  The retrieved dictionary.
138  */
139 gdcmDict *gdcmDictSet::GetDict(DictKey DictName) 
140 {
141    DictSetHT::iterator dict = Dicts.find(DictName);
142    if(dict!=Dicts.end())
143       return dict->second;
144    return NULL;
145 }
146
147 /**
148  * \ingroup gdcmDictSet
149  * \brief   Retrieve the default reference DICOM V3 public dictionary.
150  * \result  The retrieved default dictionary.
151  */
152 gdcmDict *gdcmDictSet::GetDefaultPubDict() 
153 {
154    return GetDict(PUB_DICT_NAME);
155 }
156
157 /**
158  * \ingroup gdcmDictSet
159  * \brief   Create a gdcmDictEntry which will be reference 
160  *          in no dictionnary
161  * @return  virtual entry
162  */
163 gdcmDictEntry *gdcmDictSet::NewVirtualDictEntry(guint16 group, guint16 element,
164                                                 std::string vr,std::string fourth,
165                                                 std::string name)
166 {
167    gdcmDictEntry *entry;
168    std::string tag=gdcmDictEntry::TranslateToKey(group,element)+"#"+vr+"#"+fourth+"#"+name;
169    std::map<std::string,gdcmDictEntry *>::iterator it;
170    
171    it=virtualEntry.find(tag);
172    if(it!=virtualEntry.end())
173    {
174       entry=it->second;
175    }
176    else
177    {
178       entry=new gdcmDictEntry(group,element,vr,fourth,name);
179       virtualEntry[tag]=entry;
180    }
181    return(entry);
182 }
183
184 /**
185  * \ingroup gdcmDictSet
186  * \brief   Obtain from the GDCM_DICT_PATH environnement variable the
187  *          path to directory containing the dictionnaries. When
188  *          the environnement variable is absent the path is defaulted
189  *          to "../Dicts/".
190  * @return  path to directory containing the dictionnaries
191  */
192 std::string gdcmDictSet::BuildDictPath(void) 
193 {
194    std::string ResultPath;
195    const char *EnvPath = (char*)0;
196    EnvPath = getenv("GDCM_DICT_PATH");
197    if (EnvPath && (strlen(EnvPath) != 0)) 
198    {
199       ResultPath = EnvPath;
200       if (ResultPath[ResultPath.length() -1] != '/' )
201          ResultPath += '/';
202       dbg.Verbose(1, "gdcmDictSet::BuildDictPath:",
203                      "Dictionary path set from environnement");
204    } 
205    else
206       ResultPath = PUB_DICT_PATH;
207    return ResultPath;
208 }
209
210 //-----------------------------------------------------------------------------
211 // Protected
212 bool gdcmDictSet::AppendDict(gdcmDict *NewDict,DictKey Name)
213 {
214    Dicts[Name] = NewDict;
215    return(true);
216 }
217
218 //-----------------------------------------------------------------------------
219 // Private
220
221 //-----------------------------------------------------------------------------
222
223