00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "gdcmDict.h"
00020 #include "gdcmUtil.h"
00021 #include "gdcmDebug.h"
00022
00023 #include <fstream>
00024 #include <iostream>
00025 #include <iomanip>
00026
00027 namespace GDCM_NAME_SPACE
00028 {
00029
00032 void FillDefaultDataDict(Dict *d);
00033
00034
00035
00039 Dict::Dict( )
00040 {
00041 Filename="";
00042 }
00043
00048 Dict::Dict(std::string const &filename)
00049 {
00050 gdcmDebugMacro( "in Dict::Dict, filename =[" << filename << "]" );
00051 std::ifstream from( filename.c_str() );
00052 if ( !from )
00053 {
00054 gdcmWarningMacro( "Can't open dictionary" << filename.c_str());
00055
00056 FillDefaultDataDict( this );
00057 }
00058 else
00059 {
00060 gdcmDebugMacro( "in Dict::Dict, DoTheLoadingJob filename =["
00061 << filename << "]" );
00062 DoTheLoadingJob(from);
00063 Filename = filename;
00064 }
00065 }
00066
00070 Dict::~Dict()
00071 {
00072 ClearEntry();
00073 }
00074
00075
00076
00077
00083 bool Dict::AddDict(std::string const &filename)
00084 {
00085 std::ifstream from( filename.c_str() );
00086 if ( !from )
00087 {
00088 gdcmWarningMacro( "Can't open dictionary" << filename.c_str());
00089 return false;
00090 }
00091 else
00092 {
00093 DoTheLoadingJob(from);
00094 return true;
00095 }
00096 }
00097
00103 bool Dict::RemoveDict(std::string const &filename)
00104 {
00105 std::ifstream from( filename.c_str() );
00106 if ( !from )
00107 {
00108 gdcmWarningMacro( "Can't open dictionary" << filename.c_str());
00109 return false;
00110 }
00111 else
00112 {
00113 uint16_t group;
00114 uint16_t elem;
00115 TagName vr;
00116 TagName vm;
00117 TagName name;
00118
00119 while ( true )
00120 {
00121 from >> std::hex;
00122 from >> group;
00123
00124 if (from.eof())
00125 break;
00126
00127 from >> elem;
00128 from >> vr;
00129 from >> vm;
00130
00131 std::getline(from, name);
00132
00133 RemoveEntry(group,elem);
00134 }
00135 from.close();
00136 return true;
00137 }
00138 }
00139
00145 bool Dict::AddEntry(DictEntry *newEntry)
00146 {
00147 const TagKey &key = newEntry->GetKey();
00148
00149 if ( KeyHt.count(key) == 1 )
00150 {
00151 gdcmErrorMacro( "Already present:" << key );
00152 return false;
00153 }
00154 else
00155 {
00156 newEntry->Register();
00157 KeyHt.insert( TagKeyHT::value_type(key, newEntry));
00158 return true;
00159 }
00160 }
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00189 bool Dict::RemoveEntry(TagKey const &key)
00190 {
00191 TagKeyHT::const_iterator it = KeyHt.find(key);
00192 if ( it != KeyHt.end() )
00193 {
00194 it->second->Unregister();
00195 KeyHt.erase(key);
00196
00197 return true;
00198 }
00199 else
00200 {
00201 gdcmWarningMacro( "Unfound entry" << key );
00202 return false;
00203 }
00204 }
00205
00213 bool Dict::RemoveEntry(uint16_t group, uint16_t elem)
00214 {
00215 return RemoveEntry(DictEntry::TranslateToKey(group, elem));
00216 }
00217
00221 void Dict::ClearEntry()
00222 {
00223
00224
00225 TagKeyHT::const_iterator it;
00226
00227 for(it = KeyHt.begin();it!=KeyHt.end();++it)
00228 it->second->Unregister();
00229 KeyHt.clear();
00230
00231 }
00232
00238 DictEntry *Dict::GetEntry(TagKey const &key)
00239 {
00240 TagKeyHT::iterator it = KeyHt.find(key);
00241 if ( it == KeyHt.end() )
00242 {
00243 return 0;
00244 }
00245 return it->second;
00246 }
00253 DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem)
00254 {
00255 TagKey key = DictEntry::TranslateToKey(group, elem);
00256 TagKeyHT::iterator it = KeyHt.find(key);
00257 if ( it == KeyHt.end() )
00258 {
00259 return 0;
00260 }
00261 return it->second;
00262 }
00263
00268 DictEntry *Dict::GetFirstEntry()
00269 {
00270 ItKeyHt = KeyHt.begin();
00271 if ( ItKeyHt != KeyHt.end() )
00272 return ItKeyHt->second;
00273 return NULL;
00274 }
00275
00281 DictEntry *Dict::GetNextEntry()
00282 {
00283 gdcmAssertMacro (ItKeyHt != KeyHt.end());
00284
00285 ++ItKeyHt;
00286 if (ItKeyHt != KeyHt.end())
00287 return ItKeyHt->second;
00288 return NULL;
00289 }
00290
00291
00292
00293
00294
00295
00300 void Dict::DoTheLoadingJob(std::ifstream &from)
00301 {
00302 if (!from)
00303 return;
00304
00305 uint16_t group;
00306 uint16_t elem;
00307 VRKey vr;
00308 TagName vm;
00309 TagName name;
00310
00311 DictEntry *newEntry;
00312 while ( true )
00313 {
00314 from >> std::hex;
00315 from >> group;
00316 from >> elem;
00317 from >> vr;
00318 from >> vm;
00319 from >> std::ws;
00320 std::getline(from, name);
00321
00322 if(from.eof()) {
00323 break;
00324 }
00325
00326 newEntry = DictEntry::New(group, elem, vr, vm, name);
00327 AddEntry(newEntry);
00328 newEntry->Delete();
00329 }
00330 from.close();
00331 }
00332
00333
00340 void Dict::Print(std::ostream &os, std::string const & )
00341 {
00342 os << "Dict file name : [" << Filename << "]" << std::endl;
00343 std::ostringstream s;
00344
00345 for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
00346 {
00347 std::cout << tag->second->GetKey() << " " << tag->second->GetName()
00348 << std::endl;
00349 s << "Entry : ";
00350 s << "(" << tag->second->GetKey() << ") = "
00351 << std::dec;
00352 s << tag->second->GetVR() << ", ";
00353 s << tag->second->GetVM() << ", ";
00354 s << tag->second->GetName() << "." << std::endl;
00355
00356 }
00357 os << s.str();
00358
00359 }
00360
00361
00362 }