]> Creatis software - gdcm.git/blob - src/gdcmDict.cxx
* Memory leak hunt with the following command:
[gdcm.git] / src / gdcmDict.cxx
1 // gdcmDict.cxx
2 //-----------------------------------------------------------------------------
3 #include "gdcmDict.h"
4 #include "gdcmUtil.h"
5 #include "gdcmDebug.h"
6
7 #include <fstream>
8 #include <iostream>
9 #include <iomanip>
10
11 //-----------------------------------------------------------------------------
12 // Constructor / Destructor
13 /**
14  * \brief   Construtor
15  * @param   FileName from which to build the dictionary.
16  */
17 gdcmDict::gdcmDict(std::string & FileName) {
18    guint16 group, element;
19    char buff[1024];
20    TagName vr;
21    TagName fourth;
22    TagName name;
23
24    std::ifstream from(FileName.c_str());
25    dbg.Error(!from, "gdcmDict::gdcmDict: can't open dictionary",
26                     FileName.c_str());
27
28    while (!from.eof()) {
29       from >> std::hex;
30       from >> group;          /// MEMORY LEAK in std::istream::operator>>
31       from >> element;
32       from >> vr;
33       from >> fourth;
34       getline(from, name);    /// MEMORY LEAK in std::getline<>
35
36       gdcmDictEntry * newEntry = new gdcmDictEntry(group, element,
37                                                    vr, fourth, name);
38       AddNewEntry(newEntry);
39    }
40    from.close();
41
42    filename=FileName;
43 }
44
45 /**
46  * \brief  Destructor 
47  */
48 gdcmDict::~gdcmDict() {
49    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag) {
50       gdcmDictEntry* EntryToDelete = tag->second;
51       if ( EntryToDelete )
52          delete EntryToDelete;
53    }
54    // Since AddNewEntry adds symetrical in both KeyHt and NameHT we can
55    // assume all the pointed gdcmDictEntries are already cleaned-up when
56    // we cleaned KeyHt.
57    KeyHt.clear();
58    NameHt.clear();
59 }
60
61 //-----------------------------------------------------------------------------
62 // Print
63 /**
64  * \brief   Print all the dictionary entries contained in this dictionary.
65  *          Entries will be sorted by tag i.e. the couple (group, element).
66  * @param   os The output stream to be written to.
67  */
68 void gdcmDict::Print(std::ostream &os) {
69    os<<"Dict file name : "<<filename<<std::endl;
70    PrintByKey(os);
71 }
72
73 /**
74  * \brief   Print all the dictionary entries contained in this dictionary.
75  *          Entries will be sorted by tag i.e. the couple (group, element).
76  * @param   os The output stream to be written to.
77  */
78 void gdcmDict::PrintByKey(std::ostream &os) {
79    std::ostringstream s;
80
81    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag){
82       s << "Entry : ";
83       s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ',';
84       s << std::hex << std::setw(4) << tag->second->GetElement() << ") = " << std::dec;
85       s << tag->second->GetVR() << ", ";
86       s << tag->second->GetFourth() << ", ";
87       s << tag->second->GetName() << "."  << std::endl;
88    }
89    os << s.str();
90 }
91
92 /**
93  * \brief   Print all the dictionary entries contained in this dictionary.
94  *          Entries will be sorted by the name of the dictionary entries.
95  * \warning AVOID USING IT : the name IS NOT an identifier; 
96  *                           unpredictable result
97  * @param   os The output stream to be written to.
98  */
99 void gdcmDict::PrintByName(std::ostream& os) {
100    std::ostringstream s;
101
102    for (TagNameHT::iterator tag = NameHt.begin(); tag != NameHt.end(); ++tag){
103       s << "Entry : ";
104       s << tag->second->GetName() << ",";
105       s << tag->second->GetVR() << ", ";
106       s << tag->second->GetFourth() << ", ";
107       s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ',';
108       s << std::hex << std::setw(4) << tag->second->GetElement() << ") = ";
109       s << std::dec << std::endl;
110    }
111    os << s.str();
112 }
113
114 //-----------------------------------------------------------------------------
115 // Public
116 /**
117  * \ingroup gdcmDict
118  * \brief  adds a new Dicom Dictionary Entry 
119  * @param   NewEntry entry to add 
120  * @return  false if Dicom Element already exists
121  */
122 bool gdcmDict::AddNewEntry(gdcmDictEntry *NewEntry) 
123 {
124    TagKey key;
125    key = NewEntry->GetKey();
126
127    if(KeyHt.count(key) == 1)
128    {
129       dbg.Verbose(1, "gdcmDict::AddNewEntry already present", key.c_str());
130       return(false);
131    } 
132    else 
133    {
134       KeyHt[NewEntry->GetKey()] = NewEntry;
135       NameHt[NewEntry->GetName()] = NewEntry;  /// MEMORY LEAK in
136                                                /// std::map<>::operator[]
137       return(true);
138    }
139 }
140
141 /**
142  * \ingroup gdcmDict
143  * \brief  replaces an already existing Dicom Element by a new one
144  * @param   NewEntry new entry (overwrites any previous one with same tag)
145  * @return  false if Dicom Element doesn't exist
146  */
147 bool gdcmDict::ReplaceEntry(gdcmDictEntry *NewEntry) {
148    if ( RemoveEntry(NewEntry->gdcmDictEntry::GetKey()) ) {
149        KeyHt[NewEntry->GetKey()] = NewEntry;
150        NameHt[NewEntry->GetName()] = NewEntry;
151        return (true);
152    } 
153    return (false);
154 }
155
156 /**
157  * \ingroup gdcmDict
158  * \brief  removes an already existing Dicom Dictionary Entry,
159  *         identified by its Tag
160  * @param   key (group|element)
161  * @return  false if Dicom Dictionary Entry doesn't exist
162  */
163 bool gdcmDict::RemoveEntry(TagKey key) 
164 {
165    if(KeyHt.count(key) == 1) 
166    {
167       gdcmDictEntry* EntryToDelete = KeyHt.find(key)->second;
168
169       if ( EntryToDelete )
170       {
171          NameHt.erase(EntryToDelete->GetName());
172          delete EntryToDelete;
173       }
174
175       KeyHt.erase(key);
176       return (true);
177    } 
178    else 
179    {
180       dbg.Verbose(1, "gdcmDict::RemoveEntry unfound entry", key.c_str());
181       return (false);
182   }
183 }
184
185 /**
186  * \ingroup gdcmDict
187  * \brief  removes an already existing Dicom Dictionary Entry, 
188  *          identified by its group,element number
189  * @param   group   Dicom group number of the Dicom Element
190  * @param   element Dicom element number of the Dicom Element
191  * @return  false if Dicom Dictionary Entry doesn't exist
192  */
193 bool gdcmDict::RemoveEntry (guint16 group, guint16 element) {
194         return( RemoveEntry(gdcmDictEntry::TranslateToKey(group, element)) );
195 }
196
197 /**
198  * \ingroup gdcmDict
199  * \brief   Get the dictionnary entry identified by it's name.
200  * @param   name element of the ElVal to modify
201  * \warning : NEVER use it !
202  *            the 'name' IS NOT an identifier within the Dicom Dicom Dictionary
203  *            the name MAY CHANGE between two versions !
204  * @return  the corresponding dictionnary entry when existing, NULL otherwise
205  */
206 gdcmDictEntry *gdcmDict::GetDictEntryByName(TagName name) {
207    if ( ! NameHt.count(name))
208       return NULL; 
209    return NameHt.find(name)->second;
210 }
211
212 /**
213  * \ingroup gdcmDict
214  * \brief   Get the dictionnary entry identified by a given tag (group,element)
215  * @param   group   group of the entry to be found
216  * @param   element element of the entry to be found
217  * @return  the corresponding dictionnary entry when existing, NULL otherwise
218  */
219 gdcmDictEntry *gdcmDict::GetDictEntryByNumber(guint16 group, guint16 element) {
220    TagKey key = gdcmDictEntry::TranslateToKey(group, element);
221    if ( ! KeyHt.count(key))
222       return NULL; 
223    return KeyHt.find(key)->second;
224 }
225
226 /** 
227  * \ingroup gdcmDict
228  * \brief   Consider all the entries of the public dicom dictionnary. 
229  *          Build all list of all the tag names of all those entries.
230  * \sa      gdcmDictSet::GetPubDictTagNamesByCategory
231  * @return  A list of all entries of the public dicom dictionnary.
232  */
233 std::list<std::string> *gdcmDict::GetDictEntryNames(void) 
234 {
235    std::list<std::string> *Result = new std::list<std::string>;
236    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
237    {
238       Result->push_back( tag->second->GetName() );
239    }
240    return Result;
241 }
242
243 /** 
244  * \ingroup gdcmDict
245  * \brief   Consider all the entries of the public dicom dictionnary.
246  *          Build an hashtable whose keys are the names of the groups
247  *          (fourth field in each line of dictionary) and whose corresponding
248  *          values are lists of all the dictionnary entries among that
249  *          group. Note that apparently the Dicom standard doesn't explicitely
250  *          define a name (as a string) for each group.
251  *          A typical usage of this method would be to enable a dynamic
252  *          configuration of a Dicom file browser: the admin/user can
253  *          select in the interface which Dicom tags should be displayed.
254  * \warning Dicom *doesn't* define any name for any 'categorie'
255  *          (the dictionnary fourth field was formerly NIH defined
256  *           - and no longer he is-
257  *           and will be removed when Dicom provides us a text file
258  *           with the 'official' Dictionnary, that would be more friendly
259  *           than asking us to perform a line by line check of the dictionnary
260  *           at the beginning of each year to -try to- guess the changes)
261  *           Therefore : please NEVER use that fourth field :-(
262  *
263  * @return  An hashtable: whose keys are the names of the groups and whose
264  *          corresponding values are lists of all the dictionnary entries
265  *          among that group.
266  */
267 std::map<std::string, std::list<std::string> > *gdcmDict::GetDictEntryNamesByCategory(void) 
268 {
269    std::map<std::string, std::list<std::string> > *Result = new std::map<std::string, std::list<std::string> >;
270
271    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
272    {
273       (*Result)[tag->second->GetFourth()].push_back(tag->second->GetName());
274    }
275    return Result;
276 }
277
278 //-----------------------------------------------------------------------------
279 // Protected
280
281 //-----------------------------------------------------------------------------
282 // Private
283
284 //-----------------------------------------------------------------------------