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;
65 // Since AddNewEntry adds symetrical in both KeyHt and NameHT we can
66 // assume all the pointed gdcmDictEntries are already cleaned-up when
72 //-----------------------------------------------------------------------------
75 * \brief Print all the dictionary entries contained in this dictionary.
76 * Entries will be sorted by tag i.e. the couple (group, element).
77 * @param os The output stream to be written to.
79 void gdcmDict::Print(std::ostream &os) {
80 os<<"Dict file name : "<<filename<<std::endl;
86 * \brief Print all the dictionary entries contained in this dictionary.
87 * Entries will be sorted by tag i.e. the couple (group, element).
88 * @param os The output stream to be written to.
90 void gdcmDict::PrintByKey(std::ostream &os) {
93 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag){
95 s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ',';
96 s << std::hex << std::setw(4) << tag->second->GetElement() << ") = " << std::dec;
97 s << tag->second->GetVR() << ", ";
98 s << tag->second->GetFourth() << ", ";
99 s << tag->second->GetName() << "." << std::endl;
106 * \brief Print all the dictionary entries contained in this dictionary.
107 * Entries will be sorted by the name of the dictionary entries.
108 * \warning AVOID USING IT : the name IS NOT an identifier;
109 * unpredictable result
110 * @param os The output stream to be written to.
112 void gdcmDict::PrintByName(std::ostream& os) {
113 std::ostringstream s;
115 for (TagNameHT::iterator tag = NameHt.begin(); tag != NameHt.end(); ++tag){
117 s << tag->second->GetName() << ",";
118 s << tag->second->GetVR() << ", ";
119 s << tag->second->GetFourth() << ", ";
120 s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ',';
121 s << std::hex << std::setw(4) << tag->second->GetElement() << ") = ";
122 s << std::dec << std::endl;
127 //-----------------------------------------------------------------------------
131 * \brief adds a new Dicom Dictionary Entry
132 * @param NewEntry entry to add
133 * @return false if Dicom Element already exists
135 bool gdcmDict::AddNewEntry(gdcmDictEntry *NewEntry)
138 key = NewEntry->GetKey();
140 if(KeyHt.count(key) == 1)
142 dbg.Verbose(1, "gdcmDict::AddNewEntry already present", key.c_str());
147 KeyHt[NewEntry->GetKey()] = NewEntry;
148 NameHt[NewEntry->GetName()] = NewEntry;
155 * \brief replaces an already existing Dicom Element by a new one
156 * @param NewEntry new entry (overwrites any previous one with same tag)
157 * @return false if Dicom Element doesn't exist
159 bool gdcmDict::ReplaceEntry(gdcmDictEntry *NewEntry) {
160 if ( RemoveEntry(NewEntry->gdcmDictEntry::GetKey()) ) {
161 KeyHt[NewEntry->GetKey()] = NewEntry;
162 NameHt[NewEntry->GetName()] = NewEntry;
170 * \brief removes an already existing Dicom Dictionary Entry,
171 * identified by its Tag
172 * @param key (group|element)
173 * @return false if Dicom Dictionary Entry doesn't exist
175 bool gdcmDict::RemoveEntry(TagKey key)
177 if(KeyHt.count(key) == 1)
179 gdcmDictEntry* EntryToDelete = KeyHt.find(key)->second;
183 NameHt.erase(EntryToDelete->GetName());
184 delete EntryToDelete;
192 dbg.Verbose(1, "gdcmDict::RemoveEntry unfound entry", key.c_str());
199 * \brief removes an already existing Dicom Dictionary Entry,
200 * identified by its group,element number
201 * @param group Dicom group number of the Dicom Element
202 * @param element Dicom element number of the Dicom Element
203 * @return false if Dicom Dictionary Entry doesn't exist
205 bool gdcmDict::RemoveEntry (guint16 group, guint16 element) {
206 return( RemoveEntry(gdcmDictEntry::TranslateToKey(group, element)) );
211 * \brief Get the dictionnary entry identified by it's name.
212 * @param name element of the ElVal to modify
213 * \warning : NEVER use it !
214 * the 'name' IS NOT an identifier within the Dicom Dicom Dictionary
215 * the name MAY CHANGE between two versions !
216 * @return the corresponding dictionnary entry when existing, NULL otherwise
218 gdcmDictEntry *gdcmDict::GetDictEntryByName(TagName name) {
219 if ( ! NameHt.count(name))
221 return NameHt.find(name)->second;
226 * \brief Get the dictionnary entry identified by a given tag (group,element)
227 * @param group group of the entry to be found
228 * @param element element of the entry to be found
229 * @return the corresponding dictionnary entry when existing, NULL otherwise
231 gdcmDictEntry *gdcmDict::GetDictEntryByNumber(guint16 group, guint16 element) {
232 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
233 if ( ! KeyHt.count(key))
235 return KeyHt.find(key)->second;
240 * \brief Consider all the entries of the public dicom dictionnary.
241 * Build all list of all the tag names of all those entries.
242 * \sa gdcmDictSet::GetPubDictTagNamesByCategory
243 * @return A list of all entries of the public dicom dictionnary.
245 std::list<std::string> *gdcmDict::GetDictEntryNames(void)
247 std::list<std::string> *Result = new std::list<std::string>;
248 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
250 Result->push_back( tag->second->GetName() );
257 * \brief Consider all the entries of the public dicom dictionnary.
258 * Build an hashtable whose keys are the names of the groups
259 * (fourth field in each line of dictionary) and whose corresponding
260 * values are lists of all the dictionnary entries among that
261 * group. Note that apparently the Dicom standard doesn't explicitely
262 * define a name (as a string) for each group.
263 * A typical usage of this method would be to enable a dynamic
264 * configuration of a Dicom file browser: the admin/user can
265 * select in the interface which Dicom tags should be displayed.
266 * \warning Dicom *doesn't* define any name for any 'categorie'
267 * (the dictionnary fourth field was formerly NIH defined
268 * - and no longer he is-
269 * and will be removed when Dicom provides us a text file
270 * with the 'official' Dictionnary, that would be more friendly
271 * than asking us to perform a line by line check of the dictionnary
272 * at the beginning of each year to -try to- guess the changes)
273 * Therefore : please NEVER use that fourth field :-(
275 * @return An hashtable: whose keys are the names of the groups and whose
276 * corresponding values are lists of all the dictionnary entries
279 std::map<std::string, std::list<std::string> > *gdcmDict::GetDictEntryNamesByCategory(void)
281 std::map<std::string, std::list<std::string> > *Result = new std::map<std::string, std::list<std::string> >;
283 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
285 (*Result)[tag->second->GetFourth()].push_back(tag->second->GetName());
290 //-----------------------------------------------------------------------------
293 //-----------------------------------------------------------------------------
296 //-----------------------------------------------------------------------------