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