2 //-----------------------------------------------------------------------------
11 #ifdef GDCM_NO_ANSI_STRING_STREAM
13 # define ostringstream ostrstream
18 //-----------------------------------------------------------------------------
19 // 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);
58 gdcmDict::~gdcmDict() {
59 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag) {
60 gdcmDictEntry* EntryToDelete = tag->second;
64 // Since AddNewEntry adds symetrical in both KeyHt and NameHT we can
65 // assume all the pointed gdcmDictEntries are already cleaned-up when
71 //-----------------------------------------------------------------------------
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::Print(std::ostream &os) {
79 os<<"Dict file name : "<<filename<<std::endl;
84 * \brief Print all the dictionary entries contained in this dictionary.
85 * Entries will be sorted by tag i.e. the couple (group, element).
86 * @param os The output stream to be written to.
88 void gdcmDict::PrintByKey(std::ostream &os) {
91 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag){
93 s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ',';
94 s << std::hex << std::setw(4) << tag->second->GetElement() << ") = " << std::dec;
95 s << tag->second->GetVR() << ", ";
96 s << tag->second->GetFourth() << ", ";
97 s << tag->second->GetName() << "." << std::endl;
103 * \brief Print all the dictionary entries contained in this dictionary.
104 * Entries will be sorted by the name of the dictionary entries.
105 * \warning AVOID USING IT : the name IS NOT an identifier;
106 * unpredictable result
107 * @param os The output stream to be written to.
109 void gdcmDict::PrintByName(std::ostream& os) {
110 std::ostringstream s;
112 for (TagNameHT::iterator tag = NameHt.begin(); tag != NameHt.end(); ++tag){
114 s << tag->second->GetName() << ",";
115 s << tag->second->GetVR() << ", ";
116 s << tag->second->GetFourth() << ", ";
117 s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ',';
118 s << std::hex << std::setw(4) << tag->second->GetElement() << ") = ";
119 s << std::dec << std::endl;
124 //-----------------------------------------------------------------------------
128 * \brief adds a new Dicom Dictionary Entry
129 * @param NewEntry entry to add
130 * @return false if Dicom Element already exists
132 bool gdcmDict::AddNewEntry(gdcmDictEntry *NewEntry)
135 key = NewEntry->GetKey();
137 if(KeyHt.count(key) == 1)
139 dbg.Verbose(1, "gdcmDict::AddNewEntry already present", key.c_str());
144 KeyHt[NewEntry->GetKey()] = NewEntry;
145 NameHt[NewEntry->GetName()] = NewEntry;
152 * \brief replaces an already existing Dicom Element by a new one
153 * @param NewEntry new entry (overwrites any previous one with same tag)
154 * @return false if Dicom Element doesn't exist
156 bool gdcmDict::ReplaceEntry(gdcmDictEntry *NewEntry) {
157 if ( RemoveEntry(NewEntry->gdcmDictEntry::GetKey()) ) {
158 KeyHt[NewEntry->GetKey()] = NewEntry;
159 NameHt[NewEntry->GetName()] = NewEntry;
167 * \brief removes an already existing Dicom Dictionary Entry,
168 * identified by its Tag
169 * @param key (group|element)
170 * @return false if Dicom Dictionary Entry doesn't exist
172 bool gdcmDict::RemoveEntry(TagKey key)
174 if(KeyHt.count(key) == 1)
176 gdcmDictEntry* EntryToDelete = KeyHt.find(key)->second;
180 NameHt.erase(EntryToDelete->GetName());
181 delete EntryToDelete;
189 dbg.Verbose(1, "gdcmDict::RemoveEntry unfound entry", key.c_str());
196 * \brief removes an already existing Dicom Dictionary Entry,
197 * identified by its group,element number
198 * @param group Dicom group number of the Dicom Element
199 * @param element Dicom element number of the Dicom Element
200 * @return false if Dicom Dictionary Entry doesn't exist
202 bool gdcmDict::RemoveEntry (guint16 group, guint16 element) {
203 return( RemoveEntry(gdcmDictEntry::TranslateToKey(group, element)) );
208 * \brief Get the dictionnary entry identified by it's name.
209 * @param name element of the ElVal to modify
210 * \warning : NEVER use it !
211 * the 'name' IS NOT an identifier within the Dicom Dicom Dictionary
212 * the name MAY CHANGE between two versions !
213 * @return the corresponding dictionnary entry when existing, NULL otherwise
215 gdcmDictEntry *gdcmDict::GetDictEntryByName(TagName name) {
216 if ( ! NameHt.count(name))
218 return NameHt.find(name)->second;
223 * \brief Get the dictionnary entry identified by a given tag (group,element)
224 * @param group group of the entry to be found
225 * @param element element of the entry to be found
226 * @return the corresponding dictionnary entry when existing, NULL otherwise
228 gdcmDictEntry *gdcmDict::GetDictEntryByNumber(guint16 group, guint16 element) {
229 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
230 if ( ! KeyHt.count(key))
232 return KeyHt.find(key)->second;
237 * \brief Consider all the entries of the public dicom dictionnary.
238 * Build all list of all the tag names of all those entries.
239 * \sa gdcmDictSet::GetPubDictTagNamesByCategory
240 * @return A list of all entries of the public dicom dictionnary.
242 std::list<std::string> *gdcmDict::GetDictEntryNames(void)
244 std::list<std::string> *Result = new std::list<std::string>;
245 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
247 Result->push_back( tag->second->GetName() );
254 * \brief Consider all the entries of the public dicom dictionnary.
255 * Build an hashtable whose keys are the names of the groups
256 * (fourth field in each line of dictionary) and whose corresponding
257 * values are lists of all the dictionnary entries among that
258 * group. Note that apparently the Dicom standard doesn't explicitely
259 * define a name (as a string) for each group.
260 * A typical usage of this method would be to enable a dynamic
261 * configuration of a Dicom file browser: the admin/user can
262 * select in the interface which Dicom tags should be displayed.
263 * \warning Dicom *doesn't* define any name for any 'categorie'
264 * (the dictionnary fourth field was formerly NIH defined
265 * - and no longer he is-
266 * and will be removed when Dicom provides us a text file
267 * with the 'official' Dictionnary, that would be more friendly
268 * than asking us to perform a line by line check of the dictionnary
269 * at the beginning of each year to -try to- guess the changes)
270 * Therefore : please NEVER use that fourth field :-(
272 * @return An hashtable: whose keys are the names of the groups and whose
273 * corresponding values are lists of all the dictionnary entries
276 std::map<std::string, std::list<std::string> > *gdcmDict::GetDictEntryNamesByCategory(void)
278 std::map<std::string, std::list<std::string> > *Result = new std::map<std::string, std::list<std::string> >;
280 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
282 (*Result)[tag->second->GetFourth()].push_back(tag->second->GetName());
287 //-----------------------------------------------------------------------------
290 //-----------------------------------------------------------------------------
293 //-----------------------------------------------------------------------------