00001
00002
00003 #include "gdcmDict.h"
00004 #include "gdcmUtil.h"
00005
00006 #include <fstream>
00007 #include <iostream>
00008 #include <iomanip>
00009
00010 #ifdef GDCM_NO_ANSI_STRING_STREAM
00011 # include <strstream>
00012 # define ostringstream ostrstream
00013 # else
00014 # include <sstream>
00015 #endif
00016
00017
00018
00024 gdcmDict::gdcmDict(std::string & FileName) {
00025 guint16 group, element;
00026 char buff[1024];
00027 TagName vr;
00028 TagName fourth;
00029 TagName name;
00030
00031 std::ifstream from(FileName.c_str());
00032 dbg.Error(!from, "gdcmDict::gdcmDict: can't open dictionary",
00033 FileName.c_str());
00034
00035 while (!from.eof()) {
00036 from >> std::hex >> group >> element;
00037 eatwhite(from);
00038 from.getline(buff, 256, ' ');
00039 vr = buff;
00040 eatwhite(from);
00041 from.getline(buff, 256, ' ');
00042 fourth = buff;
00043 from.getline(buff, 256, '\n');
00044 name = buff;
00045
00046 gdcmDictEntry * newEntry = new gdcmDictEntry(group, element,
00047 vr, fourth, name);
00048 AddNewEntry(newEntry);
00049 }
00050 from.close();
00051
00052 filename=FileName;
00053 }
00054
00059 gdcmDict::~gdcmDict() {
00060 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag) {
00061 gdcmDictEntry* EntryToDelete = tag->second;
00062 if ( EntryToDelete )
00063 delete EntryToDelete;
00064 }
00065
00066
00067
00068
00069 KeyHt.clear();
00070 NameHt.clear();
00071 }
00072
00073
00074
00080 void gdcmDict::Print(std::ostream &os) {
00081 os<<"Dict file name : "<<filename<<std::endl;
00082 PrintByKey(os);
00083 }
00084
00091 void gdcmDict::PrintByKey(std::ostream &os) {
00092 std::ostringstream s;
00093
00094 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag){
00095 s << "Entry : ";
00096 s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ',';
00097 s << std::hex << std::setw(4) << tag->second->GetElement() << ") = " << std::dec;
00098 s << tag->second->GetVR() << ", ";
00099 s << tag->second->GetFourth() << ", ";
00100 s << tag->second->GetName() << "." << std::endl;
00101 }
00102 os << s.str();
00103 }
00104
00113 void gdcmDict::PrintByName(std::ostream& os) {
00114 std::ostringstream s;
00115
00116 for (TagNameHT::iterator tag = NameHt.begin(); tag != NameHt.end(); ++tag){
00117 s << "Entry : ";
00118 s << tag->second->GetName() << ",";
00119 s << tag->second->GetVR() << ", ";
00120 s << tag->second->GetFourth() << ", ";
00121 s << "(" << std::hex << std::setw(4) << tag->second->GetGroup() << ',';
00122 s << std::hex << std::setw(4) << tag->second->GetElement() << ") = ";
00123 s << std::dec << std::endl;
00124 }
00125 os << s.str();
00126 }
00127
00128
00129
00136 bool gdcmDict::AddNewEntry(gdcmDictEntry *NewEntry)
00137 {
00138 TagKey key;
00139 key = NewEntry->GetKey();
00140
00141 if(KeyHt.count(key) == 1)
00142 {
00143 dbg.Verbose(1, "gdcmDict::AddNewEntry already present", key.c_str());
00144 return(false);
00145 }
00146 else
00147 {
00148 KeyHt[NewEntry->GetKey()] = NewEntry;
00149 NameHt[NewEntry->GetName()] = NewEntry;
00150 return(true);
00151 }
00152 }
00153
00160 bool gdcmDict::ReplaceEntry(gdcmDictEntry *NewEntry) {
00161 if ( RemoveEntry(NewEntry->gdcmDictEntry::GetKey()) ) {
00162 KeyHt[NewEntry->GetKey()] = NewEntry;
00163 NameHt[NewEntry->GetName()] = NewEntry;
00164 return (true);
00165 }
00166 return (false);
00167 }
00168
00176 bool gdcmDict::RemoveEntry(TagKey key)
00177 {
00178 if(KeyHt.count(key) == 1)
00179 {
00180 gdcmDictEntry* EntryToDelete = KeyHt.find(key)->second;
00181
00182 if ( EntryToDelete )
00183 {
00184 NameHt.erase(EntryToDelete->GetName());
00185 delete EntryToDelete;
00186 }
00187
00188 KeyHt.erase(key);
00189 return (true);
00190 }
00191 else
00192 {
00193 dbg.Verbose(1, "gdcmDict::RemoveEntry unfound entry", key.c_str());
00194 return (false);
00195 }
00196 }
00197
00206 bool gdcmDict::RemoveEntry (guint16 group, guint16 element) {
00207 return( RemoveEntry(gdcmDictEntry::TranslateToKey(group, element)) );
00208 }
00209
00219 gdcmDictEntry *gdcmDict::GetDictEntryByName(TagName name) {
00220 if ( ! NameHt.count(name))
00221 return NULL;
00222 return NameHt.find(name)->second;
00223 }
00224
00232 gdcmDictEntry *gdcmDict::GetDictEntryByNumber(guint16 group, guint16 element) {
00233 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
00234 if ( ! KeyHt.count(key))
00235 return NULL;
00236 return KeyHt.find(key)->second;
00237 }
00238
00246 std::list<std::string> *gdcmDict::GetDictEntryNames(void)
00247 {
00248 std::list<std::string> *Result = new std::list<std::string>;
00249 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
00250 {
00251 Result->push_back( tag->second->GetName() );
00252 }
00253 return Result;
00254 }
00255
00280 std::map<std::string, std::list<std::string> > *gdcmDict::GetDictEntryNamesByCategory(void)
00281 {
00282 std::map<std::string, std::list<std::string> > *Result = new std::map<std::string, std::list<std::string> >;
00283
00284 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
00285 {
00286 (*Result)[tag->second->GetFourth()].push_back(tag->second->GetName());
00287 }
00288 return Result;
00289 }
00290
00291
00292
00293
00294
00295
00296
00297