Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | 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/02/05 01:37:08 $
00007   Version:   $Revision: 1.73 $
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 //-----------------------------------------------------------------------------
00030 void FillDefaultDataDict(Dict *d);
00031 
00032 //-----------------------------------------------------------------------------
00033 // Constructor / Destructor
00037 Dict::Dict( )
00038 {
00039    Filename="";
00040 }
00041 
00046 Dict::Dict(std::string const &filename)
00047 {
00048    uint16_t group;
00049    uint16_t element;
00050    TagName vr;
00051    TagName vm;
00052    TagName name;
00053 
00054    std::ifstream from( filename.c_str() );
00055    if( !from )
00056    {
00057       gdcmWarningMacro( "Can't open dictionary" << filename.c_str());
00058       // Using default embeded one:
00059       FillDefaultDataDict( this );
00060    }
00061    else
00062    {
00063       while (!from.eof())
00064       {
00065          from >> std::hex;
00066          from >> group;
00067          from >> element;
00068          from >> vr;
00069          from >> vm;
00070          from >> std::ws;  //remove white space
00071          std::getline(from, name);
00072    
00073          const DictEntry newEntry(group, element, vr, vm, name);
00074          AddEntry(newEntry);
00075       }
00076 
00077       Filename = filename;
00078       from.close();
00079    }
00080 }
00081 
00085 Dict::~Dict()
00086 {
00087    ClearEntry();
00088 }
00089 
00090 //-----------------------------------------------------------------------------
00091 // Public
00097 bool Dict::AddEntry(DictEntry const &newEntry) 
00098 {
00099    const TagKey &key = newEntry.GetKey();
00100 
00101    if(KeyHt.count(key) == 1)
00102    {
00103       gdcmWarningMacro( "Already present" << key.c_str());
00104       return false;
00105    } 
00106    else 
00107    {
00108       KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
00109       return true;
00110    }
00111 }
00112 
00118 bool Dict::ReplaceEntry(DictEntry const &newEntry)
00119 {
00120    if ( RemoveEntry(newEntry.GetKey()) )
00121    {
00122        KeyHt.insert( TagKeyHT::value_type(newEntry.GetKey(), newEntry));
00123        return true;
00124    } 
00125    return false;
00126 }
00127 
00134 bool Dict::RemoveEntry(TagKey const &key) 
00135 {
00136    TagKeyHT::const_iterator it = KeyHt.find(key);
00137    if(it != KeyHt.end()) 
00138    {
00139       KeyHt.erase(key);
00140 
00141       return true;
00142    } 
00143    else 
00144    {
00145       gdcmWarningMacro( "Unfound entry" << key.c_str());
00146       return false;
00147   }
00148 }
00149 
00157 bool Dict::RemoveEntry(uint16_t group, uint16_t elem)
00158 {
00159    return RemoveEntry(DictEntry::TranslateToKey(group, elem));
00160 }
00161 
00165 void Dict::ClearEntry()
00166 {
00167    // we assume all the pointed DictEntries are already cleaned-up
00168    // when we clean KeyHt.
00169    KeyHt.clear();
00170 }
00171 
00178 DictEntry *Dict::GetEntry(uint16_t group, uint16_t elem)
00179 {
00180    TagKey key = DictEntry::TranslateToKey(group, elem);
00181    TagKeyHT::iterator it = KeyHt.find(key);
00182    if ( it == KeyHt.end() )
00183    {
00184       return 0;
00185    }
00186    return &(it->second);
00187 }
00188 
00193 DictEntry *Dict::GetFirstEntry()
00194 {
00195    ItKeyHt = KeyHt.begin();
00196    if( ItKeyHt != KeyHt.end() )
00197       return &(ItKeyHt->second);
00198    return NULL;
00199 }
00200 
00206 DictEntry *Dict::GetNextEntry()
00207 {
00208    gdcmAssertMacro (ItKeyHt != KeyHt.end());
00209 
00210    ++ItKeyHt;
00211    if (ItKeyHt != KeyHt.end())
00212       return &(ItKeyHt->second);
00213    return NULL;
00214 }
00215 
00216 //-----------------------------------------------------------------------------
00217 // Protected
00218 
00219 //-----------------------------------------------------------------------------
00220 // Private
00221 
00222 //-----------------------------------------------------------------------------
00223 // Print
00230 void Dict::Print(std::ostream &os, std::string const & )
00231 {
00232    os << "Dict file name : " << Filename << std::endl;
00233    std::ostringstream s;
00234 
00235    for (TagKeyHT::iterator tag = KeyHt.begin(); tag != KeyHt.end(); ++tag)
00236    {
00237       s << "Entry : ";
00238       s << "(" << std::hex << std::setw(4) << tag->second.GetGroup() << ',';
00239       s << std::hex << std::setw(4) << tag->second.GetElement() << ") = "
00240         << std::dec;
00241       s << tag->second.GetVR() << ", ";
00242       s << tag->second.GetVM() << ", ";
00243       s << tag->second.GetName() << "."  << std::endl;
00244    }
00245    os << s.str();
00246 }
00247 
00248 //-----------------------------------------------------------------------------
00249 } // end namespace gdcm

Generated on Thu Feb 10 22:17:57 2005 for gdcm by doxygen 1.3.6