2 //-----------------------------------------------------------------------------
11 #ifdef GDCM_NO_ANSI_STRING_STREAM
13 # define ostringstream ostrstream
18 //-----------------------------------------------------------------------------
19 // Constructor / Destructor
23 * @param FileName from which to build the dictionary.
25 gdcmDict::gdcmDict(std::string & FileName) {
26 guint16 group, element;
32 std::ifstream from(FileName.c_str());
33 dbg.Error(!from, "gdcmDict::gdcmDict: can't open dictionary",
37 from >> std::hex >> group >> element;
39 from.getline(buff, 256, ' ');
42 from.getline(buff, 256, ' ');
44 from.getline(buff, 256, '\n');
47 gdcmDictEntry * newEntry = new gdcmDictEntry(group, element,
49 AddNewEntry(newEntry);
60 gdcmDict::~gdcmDict() {
61 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag) {
62 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
133 * @param NewEntry entry to add
134 * @return false if Dicom Element already exists
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
157 * @param NewEntry new entry (overwrites any previous one with same tag)
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 number
202 * @param group Dicom group number of the Dicom Element
203 * @param element Dicom element number of the Dicom Element
204 * @return false if Dicom Dictionary Entry doesn't exist
206 bool gdcmDict::RemoveEntry (guint16 group, guint16 element) {
207 return( RemoveEntry(gdcmDictEntry::TranslateToKey(group, element)) );
212 * \brief Get the dictionnary entry identified by it's name.
213 * @param name element of the ElVal to modify
214 * \warning : NEVER use it !
215 * the 'name' IS NOT an identifier within the Dicom Dicom Dictionary
216 * the name MAY CHANGE between two versions !
217 * @return the corresponding dictionnary entry when existing, NULL otherwise
219 gdcmDictEntry *gdcmDict::GetDictEntryByName(TagName name) {
220 if ( ! NameHt.count(name))
222 return NameHt.find(name)->second;
227 * \brief Get the dictionnary entry identified by a given tag (group,element)
228 * @param group group of the entry to be found
229 * @param element element of the entry to be found
230 * @return the corresponding dictionnary entry when existing, NULL otherwise
232 gdcmDictEntry *gdcmDict::GetDictEntryByNumber(guint16 group, guint16 element) {
233 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
234 if ( ! KeyHt.count(key))
236 return KeyHt.find(key)->second;
241 * \brief Consider all the entries of the public dicom dictionnary.
242 * Build all list of all the tag names of all those entries.
243 * \sa gdcmDictSet::GetPubDictTagNamesByCategory
244 * @return A list of all entries of the public dicom dictionnary.
246 std::list<std::string> *gdcmDict::GetDictEntryNames(void)
248 std::list<std::string> *Result = new std::list<std::string>;
249 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
251 Result->push_back( tag->second->GetName() );
258 * \brief Consider all the entries of the public dicom dictionnary.
259 * Build an hashtable whose keys are the names of the groups
260 * (fourth field in each line of dictionary) and whose corresponding
261 * values are lists of all the dictionnary entries among that
262 * group. Note that apparently the Dicom standard doesn't explicitely
263 * define a name (as a string) for each group.
264 * A typical usage of this method would be to enable a dynamic
265 * configuration of a Dicom file browser: the admin/user can
266 * select in the interface which Dicom tags should be displayed.
267 * \warning Dicom *doesn't* define any name for any 'categorie'
268 * (the dictionnary fourth field was formerly NIH defined
269 * - and no longer he is-
270 * and will be removed when Dicom provides us a text file
271 * with the 'official' Dictionnary, that would be more friendly
272 * than asking us to perform a line by line check of the dictionnary
273 * at the beginning of each year to -try to- guess the changes)
274 * Therefore : please NEVER use that fourth field :-(
276 * @return An hashtable: whose keys are the names of the groups and whose
277 * corresponding values are lists of all the dictionnary entries
280 std::map<std::string, std::list<std::string> > *gdcmDict::GetDictEntryNamesByCategory(void)
282 std::map<std::string, std::list<std::string> > *Result = new std::map<std::string, std::list<std::string> >;
284 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
286 (*Result)[tag->second->GetFourth()].push_back(tag->second->GetName());
291 //-----------------------------------------------------------------------------
294 //-----------------------------------------------------------------------------
297 //-----------------------------------------------------------------------------