2 //-----------------------------------------------------------------------------
10 #ifdef GDCM_NO_ANSI_STRING_STREAM
12 # define ostringstream ostrstream
17 //-----------------------------------------------------------------------------
18 // Constructor / Destructor
22 * @param FileName from which to build the dictionary.
24 gdcmDict::gdcmDict(std::string & FileName) {
25 guint16 group, element;
31 std::ifstream from(FileName.c_str());
32 dbg.Error(!from, "gdcmDict::gdcmDict: can't open dictionary",
36 from >> std::hex >> group >> element;
38 from.getline(buff, 256, ' ');
41 from.getline(buff, 256, ' ');
43 from.getline(buff, 256, '\n');
46 gdcmDictEntry * newEntry = new gdcmDictEntry(group, element,
48 AddNewEntry(newEntry);
59 gdcmDict::~gdcmDict() {
60 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag) {
61 gdcmDictEntry* EntryToDelete = tag->second;
66 // Since AddNewEntry adds symetrical in both KeyHt and NameHT we can
67 // assume all the pointed gdcmDictEntries are already cleaned-up when
73 //-----------------------------------------------------------------------------
76 * \brief Print all the dictionary entries contained in this dictionary.
77 * Entries will be sorted by tag i.e. the couple (group, element).
78 * @param os The output stream to be written to.
80 void gdcmDict::Print(std::ostream &os) {
81 os<<"Dict file name : "<<filename<<std::endl;
87 * \brief Print all the dictionary entries contained in this dictionary.
88 * Entries will be sorted by tag i.e. the couple (group, element).
89 * @param os The output stream to be written to.
91 void gdcmDict::PrintByKey(std::ostream &os) {
94 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag){
96 s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ',';
97 s << std::hex << std::setw(4) << tag->second->GetElement() << ") = " << std::dec;
98 s << tag->second->GetVR() << ", ";
99 s << tag->second->GetFourth() << ", ";
100 s << tag->second->GetName() << "." << std::endl;
107 * \brief Print all the dictionary entries contained in this dictionary.
108 * Entries will be sorted by the name of the dictionary entries.
109 * \warning AVOID USING IT : the name IS NOT an identifier
110 * unpredictable result
111 * @param os The output stream to be written to.
113 void gdcmDict::PrintByName(std::ostream& os) {
114 std::ostringstream s;
116 for (TagNameHT::iterator tag = NameHt.begin(); tag != NameHt.end(); ++tag){
118 s << tag->second->GetName() << ",";
119 s << tag->second->GetVR() << ", ";
120 s << tag->second->GetFourth() << ", ";
121 s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ',';
122 s << std::hex << std::setw(4) << tag->second->GetElement() << ") = ";
123 s << std::dec << std::endl;
128 //-----------------------------------------------------------------------------
132 * \brief adds a new Dicom Dictionary Entry
134 * @return false if Dicom Element already existed
136 bool gdcmDict::AddNewEntry(gdcmDictEntry *NewEntry)
139 key = NewEntry->GetKey();
141 if(KeyHt.count(key) == 1)
143 dbg.Verbose(1, "gdcmDict::AddNewEntry already present", key.c_str());
148 KeyHt[NewEntry->GetKey()] = NewEntry;
149 NameHt[NewEntry->GetName()] = NewEntry;
156 * \brief replaces an already existing Dicom Element by a new one
158 * @return false if Dicom Element doesn't exist
160 bool gdcmDict::ReplaceEntry(gdcmDictEntry *NewEntry) {
161 if ( RemoveEntry(NewEntry->gdcmDictEntry::GetKey()) ) {
162 KeyHt[NewEntry->GetKey()] = NewEntry;
163 NameHt[NewEntry->GetName()] = NewEntry;
171 * \brief removes an already existing Dicom Dictionary Entry,
172 * identified by its Tag
173 * @param key (group|element)
174 * @return false if Dicom Dictionary Entry doesn't exist
176 bool gdcmDict::RemoveEntry(TagKey key)
178 if(KeyHt.count(key) == 1)
180 gdcmDictEntry* EntryToDelete = KeyHt.find(key)->second;
184 NameHt.erase(EntryToDelete->GetName());
185 delete EntryToDelete;
193 dbg.Verbose(1, "gdcmDict::RemoveEntry unfound entry", key.c_str());
200 * \brief removes an already existing Dicom Dictionary Entry,
201 * identified by its group,element
203 * @param group Dicom group number of the Dicom Element
204 * @param element Dicom element number of the Dicom Element
205 * @return false if Dicom Dictionary Entry doesn't exist
207 bool gdcmDict::RemoveEntry (guint16 group, guint16 element) {
208 return( RemoveEntry(gdcmDictEntry::TranslateToKey(group, element)) );
213 * \brief Get the dictionnary entry identified by it's name.
214 * @param name element of the ElVal to modify
215 * \warning : NEVER use it !
216 * the 'name' IS NOT an identifier within the Dicom Dicom Dictionary
217 * the name MAY CHANGE between two versions !
218 * @return the corresponding dictionnary entry when existing, NULL otherwise
220 gdcmDictEntry *gdcmDict::GetDictEntryByName(TagName name) {
221 if ( ! NameHt.count(name))
223 return NameHt.find(name)->second;
228 * \brief Get the dictionnary entry identified by a given tag (group,element)
229 * @param group group of the entry to be found
230 * @param element element of the entry to be found
231 * @return the corresponding dictionnary entry when existing, NULL otherwise
233 gdcmDictEntry *gdcmDict::GetDictEntryByNumber(guint16 group, guint16 element) {
234 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
235 if ( ! KeyHt.count(key))
237 return KeyHt.find(key)->second;
242 * \brief Consider all the entries of the public dicom dictionnary.
243 * Build all list of all the tag names of all those entries.
244 * \sa gdcmDictSet::GetPubDictTagNamesByCategory
245 * @return A list of all entries of the public dicom dictionnary.
247 std::list<std::string> *gdcmDict::GetDictEntryNames(void)
249 std::list<std::string> *Result = new std::list<std::string>;
250 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
252 Result->push_back( tag->second->GetName() );
259 * \brief Consider all the entries of the public dicom dictionnary.
260 * Build an hashtable whose keys are the names of the groups
261 * (fourth field in each line of dictionary) and whose corresponding
262 * values are lists of all the dictionnary entries among that
263 * group. Note that apparently the Dicom standard doesn't explicitely
264 * define a name (as a string) for each group.
265 * A typical usage of this method would be to enable a dynamic
266 * configuration of a Dicom file browser: the admin/user can
267 * select in the interface which Dicom tags should be displayed.
268 * \warning Dicom *doesn't* define any name for any 'categorie'
269 * (the dictionnary fourth field was formerly NIH defined
270 * - and no longer he is-
271 * and will be removed when Dicom provides us a text file
272 * with the 'official' Dictionnary, that would be more friendly
273 * than asking us to perform a line by line check of the dictionnary
274 * at the beginning of each year to -try to- guess the changes)
275 * Therefore : please NEVER use that fourth field :-(
277 * @return An hashtable: whose keys are the names of the groups and whose
278 * corresponding values are lists of all the dictionnary entries
281 std::map<std::string, std::list<std::string> > *gdcmDict::GetDictEntryNamesByCategory(void)
283 std::map<std::string, std::list<std::string> > *Result = new std::map<std::string, std::list<std::string> >;
285 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
287 (*Result)[tag->second->GetFourth()].push_back(tag->second->GetName());
292 //-----------------------------------------------------------------------------
295 //-----------------------------------------------------------------------------
298 //-----------------------------------------------------------------------------