00001 /*========================================================================= 00002 00003 Program: gdcm 00004 Module: $RCSfile: gdcmDictSet.cxx,v $ 00005 Language: C++ 00006 Date: $Date: 2007/05/23 14:18:09 $ 00007 Version: $Revision: 1.78 $ 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 "gdcmDictSet.h" 00020 #include "gdcmDebug.h" 00021 #include "gdcmGlobal.h" 00022 #include <fstream> 00023 #include <stdlib.h> // For getenv 00024 #include <stdio.h> // For sprintf 00025 00026 namespace GDCM_NAME_SPACE 00027 { 00028 00029 //----------------------------------------------------------------------------- 00030 // Constructor / Destructor 00035 DictSet::DictSet() 00036 { 00037 DictPath = BuildDictPath(); 00038 std::string pubDictFile(DictPath); 00039 pubDictFile += PUB_DICT_FILENAME; 00040 Dicts[PUB_DICT_NAME] = Dict::New(pubDictFile); 00041 // Stored redundantly to avoid at access HTable DictSet every time. 00042 Global::DefaultPubDict = Dicts[PUB_DICT_NAME]; 00043 } 00044 00048 DictSet::~DictSet() 00049 { 00050 Global::DefaultPubDict = 0; // just a pointer! 00051 // Remove dictionaries 00052 for (DictSetHT::iterator tag = Dicts.begin(); tag != Dicts.end(); ++tag) 00053 { 00054 if ( tag->second ) 00055 tag->second->Delete(); 00056 } 00057 Dicts.clear(); 00058 } 00059 00060 //----------------------------------------------------------------------------- 00061 // Public 00070 Dict *DictSet::LoadDictFromFile(std::string const &filename, 00071 DictKey const &name) 00072 { 00073 assert(Dicts.find(name)==Dicts.end()); 00075 Dict *newDict = Dict::New(filename); 00076 Dicts[name] = newDict; 00077 00078 return newDict; 00079 } 00080 00087 Dict *DictSet::GetDict(DictKey const &dictName) 00088 { 00089 DictSetHT::iterator dict = Dicts.find(dictName); 00090 if ( dict != Dicts.end() ) 00091 { 00092 return dict->second; 00093 } 00094 return NULL; 00095 } 00096 00101 Dict *DictSet::GetFirstDict() 00102 { 00103 ItDictHt = Dicts.begin(); 00104 if ( ItDictHt != Dicts.end() ) 00105 return ItDictHt->second; 00106 return NULL; 00107 } 00108 00114 Dict *DictSet::GetNextDict() 00115 { 00116 gdcmAssertMacro (ItDictHt != Dicts.end()); 00117 00118 ++ItDictHt; 00119 if ( ItDictHt != Dicts.end() ) 00120 return ItDictHt->second; 00121 return NULL; 00122 } 00123 00131 std::string DictSet::BuildDictPath() 00132 { 00133 std::string resultPath; 00134 const char *envPath = getenv("GDCM_DICT_PATH"); 00135 00136 if (envPath && (strlen(envPath) != 0)) 00137 { 00138 resultPath = envPath; 00139 gdcmStaticWarningMacro( "Dictionary path set from environnement"); 00140 } 00141 else 00142 { 00143 resultPath = PUB_DICT_PATH; 00144 } 00145 if ( resultPath.length() && resultPath[resultPath.length()-1] != '/' ) 00146 { 00147 resultPath += '/'; 00148 } 00149 return resultPath; 00150 } 00151 00152 //----------------------------------------------------------------------------- 00153 // Protected 00154 00155 //----------------------------------------------------------------------------- 00156 // Private 00157 00158 //----------------------------------------------------------------------------- 00159 // Print 00166 void DictSet::Print(std::ostream &os, std::string const & ) 00167 { 00168 for (DictSetHT::iterator dict = Dicts.begin(); dict != Dicts.end(); ++dict) 00169 { 00170 os << "Printing dictionary " << dict->first << std::endl; 00171 dict->second->Print(os); 00172 } 00173 } 00174 00175 //----------------------------------------------------------------------------- 00176 } // end namespace gdcm