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