2 //-----------------------------------------------------------------------------
6 #ifdef GDCM_NO_ANSI_STRING_STREAM
8 # define ostringstream ostrstream
13 //-----------------------------------------------------------------------------
14 // Constructor / Destructor
18 * @param FileName from which to build the dictionary.
20 gdcmDict::gdcmDict(std::string & FileName) {
21 std::ifstream from(FileName.c_str());
22 dbg.Error(!from, "gdcmDict::gdcmDict: can't open dictionary",
24 guint16 group, element;
25 // CLEANME : use defines for all those constants
33 from >> std::hex >> group >> element;
35 from.getline(buff, 256, ' ');
38 from.getline(buff, 256, ' ');
40 from.getline(buff, 256, '\n');
42 gdcmDictEntry * newEntry = new gdcmDictEntry(group, element,
44 // FIXME: use AddNewEntry
45 NameHt[name] = newEntry;
46 KeyHt[gdcmDictEntry::TranslateToKey(group, element)] = newEntry;
55 gdcmDict::~gdcmDict() {
56 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag) {
57 gdcmDictEntry* EntryToDelete = tag->second;
62 // Since AddNewEntry adds symetrical in both KeyHt and NameHT we can
63 // assume all the pointed gdcmDictEntries are already cleaned-up when
69 //-----------------------------------------------------------------------------
72 * \brief Print all the dictionary entries contained in this dictionary.
73 * Entries will be sorted by tag i.e. the couple (group, element).
74 * @param os The output stream to be written to.
76 void gdcmDict::Print(std::ostream &os) {
82 * \brief Print all the dictionary entries contained in this dictionary.
83 * Entries will be sorted by tag i.e. the couple (group, element).
84 * @param os The output stream to be written to.
86 void gdcmDict::PrintByKey(std::ostream &os) {
89 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag){
91 s << "(" << std::hex << tag->second->GetGroup() << ',';
92 s << std::hex << tag->second->GetElement() << ") = " << std::dec;
93 s << tag->second->GetVR() << ", ";
94 s << tag->second->GetFourth() << ", ";
95 s << tag->second->GetName() << "." << std::endl;
102 * \brief Print all the dictionary entries contained in this dictionary.
103 * Entries will be sorted by the name of the dictionary entries.
104 * \warning AVOID USING IT : the name IS NOT an identifier
105 * unpredictable result
106 * @param os The output stream to be written to.
108 void gdcmDict::PrintByName(std::ostream& os) {
109 std::ostringstream s;
111 for (TagNameHT::iterator tag = NameHt.begin(); tag != NameHt.end(); ++tag){
113 s << tag->second->GetName() << ",";
114 s << tag->second->GetVR() << ", ";
115 s << tag->second->GetFourth() << ", ";
116 s << "(" << std::hex << tag->second->GetGroup() << ',';
117 s << std::hex << tag->second->GetElement() << ") = " << std::dec << std::endl;
122 //-----------------------------------------------------------------------------
126 * \brief adds a new Dicom Dictionary Entry
128 * @return false if Dicom Element already existed
130 bool gdcmDict::AddNewEntry(gdcmDictEntry *NewEntry)
133 key = NewEntry->GetKey();
135 if(KeyHt.count(key) == 1)
137 dbg.Verbose(1, "gdcmDict::AddNewEntry already present", key.c_str());
142 KeyHt[NewEntry->GetKey()] = NewEntry;
143 NameHt[NewEntry->GetName()] = NewEntry;
150 * \brief replaces an already existing Dicom Element by a new one
152 * @return false if Dicom Element doesn't exist
154 bool gdcmDict::ReplaceEntry(gdcmDictEntry *NewEntry) {
155 if ( RemoveEntry(NewEntry->gdcmDictEntry::GetKey()) ) {
156 KeyHt[NewEntry->GetKey()] = NewEntry;
157 NameHt[NewEntry->GetName()] = NewEntry;
165 * \brief removes an already existing Dicom Dictionary Entry,
166 * identified by its Tag
167 * @param key (group|element)
168 * @return false if Dicom Dictionary Entry doesn't exist
170 bool gdcmDict::RemoveEntry(TagKey key)
172 if(KeyHt.count(key) == 1)
174 gdcmDictEntry* EntryToDelete = KeyHt.find(key)->second;
178 NameHt.erase(EntryToDelete->GetName());
179 delete EntryToDelete;
187 dbg.Verbose(1, "gdcmDict::RemoveEntry unfound entry", key.c_str());
194 * \brief removes an already existing Dicom Dictionary Entry,
195 * identified by its group,element
197 * @param group Dicom group number of the Dicom Element
198 * @param element Dicom element number of the Dicom Element
199 * @return false if Dicom Dictionary Entry doesn't exist
201 bool gdcmDict::RemoveEntry (guint16 group, guint16 element) {
202 return( RemoveEntry(gdcmDictEntry::TranslateToKey(group, element)) );
207 * \brief Get the dictionnary entry identified by it's name.
208 * @param name element of the ElVal to modify
209 * \warning : NEVER use it !
210 * the 'name' IS NOT an identifier within the Dicom Dicom Dictionary
211 * the name MAY CHANGE between two versions !
212 * @return the corresponding dictionnary entry when existing, NULL otherwise
214 gdcmDictEntry *gdcmDict::GetTagByName(TagName name) {
215 if ( ! NameHt.count(name))
217 return NameHt.find(name)->second;
222 * \brief Get the dictionnary entry identified by a given tag (group,element)
223 * @param group group of the entry to be found
224 * @param element element of the entry to be found
225 * @return the corresponding dictionnary entry when existing, NULL otherwise
227 gdcmDictEntry *gdcmDict::GetTagByNumber(guint16 group, guint16 element) {
228 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
229 if ( ! KeyHt.count(key))
231 return KeyHt.find(key)->second;
236 * \brief Consider all the entries of the public dicom dictionnary.
237 * Build all list of all the tag names of all those entries.
238 * \sa gdcmDictSet::GetPubDictTagNamesByCategory
239 * @return A list of all entries of the public dicom dictionnary.
241 std::list<std::string> *gdcmDict::GetTagNames(void)
243 std::list<std::string> *Result = new std::list<std::string>;
244 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
246 Result->push_back( tag->second->GetName() );
253 * \brief Consider all the entries of the public dicom dictionnary.
254 * Build an hashtable whose keys are the names of the groups
255 * (fourth field in each line of dictionary) and whose corresponding
256 * values are lists of all the dictionnary entries among that
257 * group. Note that apparently the Dicom standard doesn't explicitely
258 * define a name (as a string) for each group.
259 * A typical usage of this method would be to enable a dynamic
260 * configuration of a Dicom file browser: the admin/user can
261 * select in the interface which Dicom tags should be displayed.
262 * \warning Dicom *doesn't* define any name for any 'categorie'
263 * (the dictionnary fourth field was formerly NIH defined
264 * - and no longer he is-
265 * and will be removed when Dicom provides us a text file
266 * with the 'official' Dictionnary, that would be more friendly
267 * than asking us to perform a line by line check of the dictionnary
268 * at the beginning of each year to -try to- guess the changes)
269 * Therefore : please NEVER use that fourth field :-(
271 * @return An hashtable: whose keys are the names of the groups and whose
272 * corresponding values are lists of all the dictionnary entries
275 std::map<std::string, std::list<std::string> > *gdcmDict::GetTagNamesByCategory(void)
277 std::map<std::string, std::list<std::string> > *Result = new std::map<std::string, std::list<std::string> >;
279 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
281 (*Result)[tag->second->GetFourth()].push_back(tag->second->GetName());
286 //-----------------------------------------------------------------------------
289 //-----------------------------------------------------------------------------
292 //-----------------------------------------------------------------------------