2 //-----------------------------------------------------------------------------
11 //-----------------------------------------------------------------------------
12 // Constructor / Destructor
15 * @param FileName from which to build the dictionary.
17 gdcmDict::gdcmDict(std::string & FileName) {
18 guint16 group, element;
24 std::ifstream from(FileName.c_str());
25 dbg.Error(!from, "gdcmDict::gdcmDict: can't open dictionary",
30 from >> group; /// MEMORY LEAK in std::istream::operator>>
34 getline(from, name); /// MEMORY LEAK in std::getline<>
36 gdcmDictEntry * newEntry = new gdcmDictEntry(group, element,
38 AddNewEntry(newEntry);
48 gdcmDict::~gdcmDict() {
49 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag) {
50 gdcmDictEntry* EntryToDelete = tag->second;
54 // Since AddNewEntry adds symetrical in both KeyHt and NameHT we can
55 // assume all the pointed gdcmDictEntries are already cleaned-up when
61 //-----------------------------------------------------------------------------
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.
68 void gdcmDict::Print(std::ostream &os) {
69 os<<"Dict file name : "<<filename<<std::endl;
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.
78 void gdcmDict::PrintByKey(std::ostream &os) {
81 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag){
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;
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.
99 void gdcmDict::PrintByName(std::ostream& os) {
100 std::ostringstream s;
102 for (TagNameHT::iterator tag = NameHt.begin(); tag != NameHt.end(); ++tag){
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;
114 //-----------------------------------------------------------------------------
118 * \brief adds a new Dicom Dictionary Entry
119 * @param NewEntry entry to add
120 * @return false if Dicom Element already exists
122 bool gdcmDict::AddNewEntry(gdcmDictEntry *NewEntry)
125 key = NewEntry->GetKey();
127 if(KeyHt.count(key) == 1)
129 dbg.Verbose(1, "gdcmDict::AddNewEntry already present", key.c_str());
134 KeyHt[NewEntry->GetKey()] = NewEntry;
135 NameHt[NewEntry->GetName()] = NewEntry; /// MEMORY LEAK in
136 /// std::map<>::operator[]
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
147 bool gdcmDict::ReplaceEntry(gdcmDictEntry *NewEntry) {
148 if ( RemoveEntry(NewEntry->gdcmDictEntry::GetKey()) ) {
149 KeyHt[NewEntry->GetKey()] = NewEntry;
150 NameHt[NewEntry->GetName()] = NewEntry;
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
163 bool gdcmDict::RemoveEntry(TagKey key)
165 if(KeyHt.count(key) == 1)
167 gdcmDictEntry* EntryToDelete = KeyHt.find(key)->second;
171 NameHt.erase(EntryToDelete->GetName());
172 delete EntryToDelete;
180 dbg.Verbose(1, "gdcmDict::RemoveEntry unfound entry", key.c_str());
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
193 bool gdcmDict::RemoveEntry (guint16 group, guint16 element) {
194 return( RemoveEntry(gdcmDictEntry::TranslateToKey(group, element)) );
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
206 gdcmDictEntry *gdcmDict::GetDictEntryByName(TagName name) {
207 if ( ! NameHt.count(name))
209 return NameHt.find(name)->second;
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
219 gdcmDictEntry *gdcmDict::GetDictEntryByNumber(guint16 group, guint16 element) {
220 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
221 if ( ! KeyHt.count(key))
223 return KeyHt.find(key)->second;
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.
233 std::list<std::string> *gdcmDict::GetDictEntryNames(void)
235 std::list<std::string> *Result = new std::list<std::string>;
236 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
238 Result->push_back( tag->second->GetName() );
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 :-(
263 * @return An hashtable: whose keys are the names of the groups and whose
264 * corresponding values are lists of all the dictionnary entries
267 std::map<std::string, std::list<std::string> > *gdcmDict::GetDictEntryNamesByCategory(void)
269 std::map<std::string, std::list<std::string> > *Result = new std::map<std::string, std::list<std::string> >;
271 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
273 (*Result)[tag->second->GetFourth()].push_back(tag->second->GetName());
278 //-----------------------------------------------------------------------------
281 //-----------------------------------------------------------------------------
284 //-----------------------------------------------------------------------------