Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

gdcmDict.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002                                                                                 
00003   Program:   gdcm
00004   Module:    $RCSfile: gdcmDict.cxx,v $
00005   Language:  C++
00006   Date:      $Date: 2005/11/05 13:25:26 $
00007   Version:   $Revision: 1.83 $
00008                                                                                 
00009   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
00010   l'Image). All rights reserved. See Doc/License.txt or
00011   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
00012                                                                                 
00013      This software is distributed WITHOUT ANY WARRANTY; without even
00014      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00015      PURPOSE.  See the above copyright notices for more information.
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 
00028 {
00029 //-----------------------------------------------------------------------------
00032 void FillDefaultDataDict(Dict *d);
00033 
00034 //-----------------------------------------------------------------------------
00035 // Constructor / Destructor
00039 Dict::Dict( )
00040 {
00041    Filename="";
00042 }
00043 
00048 Dict::Dict(std::string const &filename)
00049 {
00050 
00051    std::ifstream from( filename.c_str() );
00052    if ( !from )
00053    {
00054       gdcmWarningMacro( "Can't open dictionary" << filename.c_str());
00055       // Using default embeded one:
00056       FillDefaultDataDict( this );
00057    }
00058    else
00059    {
00060       DoTheLoadingJob(from);
00061       Filename = filename;
00062    }
00063 }
00064 
00068 Dict::~Dict()
00069 {
00070    ClearEntry();
00071 }
00072 
00073 //-----------------------------------------------------------------------------
00074 // Public
00075 
00081 bool Dict::AddDict(std::string const &filename)
00082 {
00083 
00084    std::ifstream from( filename.c_str() );
00085    if ( !from )
00086    {
00087       gdcmWarningMacro( "Can't open dictionary" << filename.c_str());
00088       return false;
00089    }
00090    else
00091    {
00092       DoTheLoadingJob(from);
00093       return true;
00094    }
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 (!from.eof() && from)
00120       {
00121          from >> std::hex;
00122          from >> group;
00123          from >> elem;
00124          from >> vr;
00125          from >> vm;
00126         // from >> std::ws;  //remove white space
00127          std::getline(from, name);
00128  
00129          RemoveEntry(group,elem);
00130       }
00131       from.close();
00132       return true;
00133    }
00134 }
00135 
00141 bool Dict::AddEntry(DictEntry *newEntry) 
00142 {
00143    const TagKey &key = newEntry->GetKey();
00144 
00145    if ( KeyHt.count(key) == 1 )
00146    {
00147       gdcmErrorMacro( "Already present:" << key );
00148       return false;
00149    } 
00150    else 
00151    {
00152       newEntry->Register();
00153       KeyHt.insert( TagKeyHT::value_type(key, newEntry));
00154       return true;
00155    }
00156 }
00157 
00163 bool Dict::ReplaceEntry(DictEntry *newEntry)
00164 {
00165    const TagKey &key = newEntry->GetKey();
00166    if ( RemoveEntry(key) )
00167    {
00168       newEntry->Register();
00169       KeyHt.insert( TagKeyHT::value_type(key, newEntry));
00170       return true;
00171    } 
00172    return false;
00173 }
00174 
00181 bool Dict::RemoveEntry(TagKey const &key) 
00182 {
00183    TagKeyHT::const_iterator it = KeyHt.find(key);
00184    if ( it != KeyHt.end() ) 
00185    {
00186       it->second->Unregister();
00187       KeyHt.erase(key);
00188 
00189       return true;
00190    } 
00191    else 
00192    {
00193       gdcmWarningMacro( "Unfound entry" << key );
00194       return false;
00195   }
00196 }
00197 
00205 bool Dict::RemoveEntry(uint16_t group, uint16_t elem)
00206 {
00207    return RemoveEntry(DictEntry::TranslateToKey(group, elem));
00208 }
00209 
00213 void Dict::ClearEntry()
00214 {
00215    // we assume all the pointed DictEntries are already cleaned-up
00216    // when we clean KeyHt.
00217    TagKeyHT::const_iterator it;
00218    for(it = KeyHt.begin();it!=KeyHt.end();++it)
00219       it->second->Unregister();
00220    KeyHt.clear();
00221 }
00222 
00228 DictEntry *Dict::GetEntry(TagKey const &key)
00229 {
00230    TagKeyHT::iterator it = KeyHt.find(key);
00231    if ( it == KeyHt.end() )
00232    {
00233       return 0;
00234    }
00235    return it->second;
00236 }
00243 DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem)
00244 {
00245    TagKey key = DictEntry::TranslateToKey(group, elem);
00246    TagKeyHT::iterator it = KeyHt.find(key);
00247    if ( it == KeyHt.end() )
00248    {
00249       return 0;
00250    }
00251    return it->second;
00252 }
00253 
00258 DictEntry *Dict::GetFirstEntry()
00259 {
00260    ItKeyHt = KeyHt.begin();
00261    if ( ItKeyHt != KeyHt.end() )
00262       return ItKeyHt->second;
00263    return NULL;
00264 }
00265 
00271 DictEntry *Dict::GetNextEntry()
00272 {
00273    gdcmAssertMacro (ItKeyHt != KeyHt.end());
00274 
00275    ++ItKeyHt;
00276    if (ItKeyHt != KeyHt.end())
00277       return ItKeyHt->second;
00278    return NULL;
00279 }
00280 
00281 //-----------------------------------------------------------------------------
00282 // Protected
00283 
00284 //-----------------------------------------------------------------------------
00285 // Private
00290 void Dict::DoTheLoadingJob(std::ifstream &from)
00291 {
00292    uint16_t group;
00293    uint16_t elem;
00294    VRKey vr;
00295    TagName vm;
00296    TagName name;
00297 
00298    DictEntry *newEntry;
00299    while (!from.eof() && from)
00300    {
00301       from >> std::hex;
00302       from >> group;
00303       from >> elem;
00304       from >> vr;
00305       from >> vm;
00306       from >> std::ws;  //remove white space
00307       std::getline(from, name);
00308 
00309       newEntry = DictEntry::New(group, elem, vr, vm, name);
00310       AddEntry(newEntry);
00311       newEntry->Delete();
00312    }
00313    from.close();
00314 }
00315 //-----------------------------------------------------------------------------
00316 // Print
00323 void Dict::Print(std::ostream &os, std::string const & )
00324 {
00325    os << "Dict file name : " << Filename << std::endl;
00326    std::ostringstream s;
00327 
00328    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
00329    {
00330       s << "Entry : ";
00331       s << "(" << tag->second->GetKey() << ") = "
00332         << std::dec;
00333       s << tag->second->GetVR() << ", ";
00334       s << tag->second->GetVM() << ", ";
00335       s << tag->second->GetName() << "."  << std::endl;
00336    }
00337    os << s.str();
00338 }
00339 
00340 //-----------------------------------------------------------------------------
00341 } // end namespace gdcm

Generated on Fri Jan 20 10:14:24 2006 for gdcm by  doxygen 1.4.4