1 /*=========================================================================
4 Module: $RCSfile: gdcmDictSet.cxx,v $
6 Date: $Date: 2005/07/11 20:44:52 $
7 Version: $Revision: 1.68 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
19 #include "gdcmDictSet.h"
20 #include "gdcmDebug.h"
22 #include <stdlib.h> // For getenv
23 #include <stdio.h> // For sprintf
28 //-----------------------------------------------------------------------------
29 // Constructor / Destructor
31 * \brief The Dictionary Set obtained with this constructor simply
32 * contains the Default Public dictionary.
36 DictPath = BuildDictPath();
37 std::string pubDictFile(DictPath);
38 pubDictFile += PUB_DICT_FILENAME;
39 Dicts[PUB_DICT_NAME] = new Dict(pubDictFile);
47 // Remove dictionaries
48 for (DictSetHT::iterator tag = Dicts.begin(); tag != Dicts.end(); ++tag)
50 Dict *entryToDelete = tag->second;
59 // Remove virtual dictionary entries
60 VirtualEntries.clear();
63 //-----------------------------------------------------------------------------
66 * \brief Loads a dictionary from a specified file, and add it
67 * to already the existing ones contained in this DictSet.
68 * @param filename Absolute or relative filename containing the
70 * @param name Symbolic name that be used as identifier of the newly
73 Dict *DictSet::LoadDictFromFile(std::string const &filename,
76 Dict *newDict = new Dict(filename);
77 AppendDict(newDict, name);
83 * \brief Retrieve the specified dictionary (when existing) from this
85 * @param dictName The symbolic name of the searched dictionary.
86 * \result The retrieved dictionary.
88 Dict *DictSet::GetDict(DictKey const &dictName)
90 DictSetHT::iterator dict = Dicts.find(dictName);
91 if ( dict != Dicts.end() )
99 * \brief Create a DictEntry which will be referenced in no dictionary
100 * @param group Group number of the Entry
101 * @param elem Element number of the Entry
102 * @param vr Value Representation of the Entry
103 * @param vm Value Multiplicity of the Entry
104 * @param name English name of the Entry
105 * @return virtual entry
107 DictEntry *DictSet::NewVirtualDictEntry( uint16_t group,
115 // Let's follow 'Purify' advice
117 // const std::string tag = DictEntry::TranslateToKey(group,elem)
118 // + "#" + vr + "#" + vm + "#" + name;
126 sprintf(res,"%04x|%04x", group, elem);
127 ExtendedTagKey tag = res;
128 tag += "#" + vr + "#" + vm + "#" + name;
131 ExtendedTagKeyHT::iterator it;
133 it = VirtualEntries.find(tag);
134 if ( it != VirtualEntries.end() )
136 entry = &(it->second);
140 DictEntry ent(group, elem, vr, vm, name);
141 VirtualEntries.insert(
142 ExtendedTagKeyHT::value_type(tag, ent) );
143 entry = &(VirtualEntries.find(tag)->second);
150 * \brief Get the first entry while visiting the DictSet
151 * \return The first Dict if found, otherwhise NULL
153 Dict *DictSet::GetFirstEntry()
155 ItDictHt = Dicts.begin();
156 if ( ItDictHt != Dicts.end() )
157 return ItDictHt->second;
162 * \brief Get the next entry while visiting the Hash table (DictSetHT)
163 * \note : meaningfull only if GetFirstEntry already called
164 * \return The next Dict if found, otherwhise NULL
166 Dict *DictSet::GetNextEntry()
168 gdcmAssertMacro (ItDictHt != Dicts.end());
171 if ( ItDictHt != Dicts.end() )
172 return ItDictHt->second;
177 * \brief Obtain from the GDCM_DICT_PATH environnement variable the
178 * path to directory containing the dictionaries. When
179 * the environnement variable is absent the path is defaulted
181 * @return path to directory containing the dictionaries
183 std::string DictSet::BuildDictPath()
185 std::string resultPath;
187 envPath = getenv("GDCM_DICT_PATH");
189 if (envPath && (strlen(envPath) != 0))
191 resultPath = envPath;
192 gdcmWarningMacro( "Dictionary path set from environnement");
196 resultPath = PUB_DICT_PATH;
198 if ( resultPath[resultPath.length()-1] != '/' )
206 //-----------------------------------------------------------------------------
209 * \brief Adds a Dictionary to a DictSet
210 * \return always true
212 bool DictSet::AppendDict(Dict *newDict, DictKey const &name)
214 Dicts[name] = newDict;
219 //-----------------------------------------------------------------------------
222 //-----------------------------------------------------------------------------
225 * \brief Print, in an informal fashion, the list of all the dictionaries
226 * contained is this DictSet, along with their respective content.
227 * @param os Output stream used for printing.
228 * @param indent Indentation string to be prepended during printing
230 void DictSet::Print(std::ostream &os, std::string const & )
232 for (DictSetHT::iterator dict = Dicts.begin(); dict != Dicts.end(); ++dict)
234 os << "Printing dictionary " << dict->first << std::endl;
235 dict->second->Print(os);
239 //-----------------------------------------------------------------------------
240 } // end namespace gdcm