]> Creatis software - gdcm.git/blob - src/gdcmDictSet.cxx
Avoid troubles while parsing source dictionaries
[gdcm.git] / src / gdcmDictSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDictSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/03/22 13:19:25 $
7   Version:   $Revision: 1.74 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmDictSet.h"
20 #include "gdcmDebug.h"
21 #include "gdcmGlobal.h"
22 #include <fstream>
23 #include <stdlib.h>  // For getenv
24 #include <stdio.h>   // For sprintf
25
26 namespace gdcm 
27 {
28
29 //-----------------------------------------------------------------------------
30 // Constructor / Destructor
31 /** 
32  * \brief   The Dictionary Set obtained with this constructor simply
33  *          contains the Default Public dictionary.
34  */
35 DictSet::DictSet() 
36 {
37    DictPath = BuildDictPath();
38    std::string pubDictFile(DictPath);
39    pubDictFile += PUB_DICT_FILENAME;
40    Dicts[PUB_DICT_NAME] = Dict::New(pubDictFile);
41    // Stored redundantly to avoid at access HTable DictSet every time.
42    Global::DefaultPubDict = Dicts[PUB_DICT_NAME];
43 }
44
45 /**
46  * \brief  Destructor 
47  */
48 DictSet::~DictSet() 
49 {
50    // Remove dictionaries
51    for (DictSetHT::iterator tag = Dicts.begin(); tag != Dicts.end(); ++tag) 
52    {
53       if ( tag->second )
54          tag->second->Delete();
55    }
56    Dicts.clear();
57 }
58
59 //-----------------------------------------------------------------------------
60 // Public
61 /**
62  * \brief   Loads a dictionary from a specified file, and add it
63  *          to already the existing ones contained in this DictSet.
64  * @param   filename Absolute or relative filename containing the
65  *          dictionary to load.
66  * @param   name Symbolic name that be used as identifier of the newly 
67  *          created dictionary.
68  */
69 Dict *DictSet::LoadDictFromFile(std::string const &filename, 
70                                 DictKey const &name) 
71 {
72    Dict *newDict = Dict::New(filename);
73    Dicts[name] = newDict;
74
75    return newDict;
76 }
77
78 /**
79  * \brief   Retrieve the specified dictionary (when existing) from this
80  *          DictSet.
81  * @param   dictName The symbolic name of the searched dictionary.
82  * \result  The retrieved dictionary.
83  */
84 Dict *DictSet::GetDict(DictKey const &dictName) 
85 {
86    DictSetHT::iterator dict = Dicts.find(dictName);
87    if ( dict != Dicts.end() )
88    {
89       return dict->second;
90    }
91    return NULL;
92 }
93
94 /**
95  * \brief   Get the first dictionary while visiting the DictSet
96  * \return  The first Dict if found, otherwhise NULL
97  */
98 Dict *DictSet::GetFirstDict()
99 {
100    ItDictHt = Dicts.begin();
101    if ( ItDictHt != Dicts.end() )
102       return ItDictHt->second;
103    return NULL;
104 }
105
106 /**
107  * \brief   Get the next dictionary while visiting the Hash table (DictSetHT)
108  * \note : meaningfull only if GetFirstEntry already called
109  * \return  The next Dict if found, otherwhise NULL
110  */
111 Dict *DictSet::GetNextDict()
112 {
113    gdcmAssertMacro (ItDictHt != Dicts.end());
114   
115    ++ItDictHt;
116    if ( ItDictHt != Dicts.end() )
117       return ItDictHt->second;
118    return NULL;
119 }
120
121 /**
122  * \brief   Obtain from the GDCM_DICT_PATH environnement variable the
123  *          path to directory containing the dictionaries. When
124  *          the environnement variable is absent the path is defaulted
125  *          to "../Dicts/".
126  * @return  path to directory containing the dictionaries
127  */
128 std::string DictSet::BuildDictPath() 
129 {
130    std::string resultPath;
131    const char *envPath;
132    envPath = getenv("GDCM_DICT_PATH");
133
134    if (envPath && (strlen(envPath) != 0)) 
135    {
136       resultPath = envPath;
137       gdcmStaticWarningMacro( "Dictionary path set from environnement");
138    } 
139    else
140    {
141       resultPath = PUB_DICT_PATH;
142    }
143    if ( resultPath[resultPath.length()-1] != '/' )
144    {
145       resultPath += '/';
146    }
147
148    return resultPath;
149 }
150
151 //-----------------------------------------------------------------------------
152 // Protected
153
154 //-----------------------------------------------------------------------------
155 // Private
156
157 //-----------------------------------------------------------------------------
158 // Print
159 /**
160  * \brief   Print, in an informal fashion, the list of all the dictionaries
161  *          contained is this DictSet, along with their respective content.
162  * @param   os Output stream used for printing.
163  * @param indent Indentation string to be prepended during printing
164  */
165 void DictSet::Print(std::ostream &os, std::string const & )
166 {
167    for (DictSetHT::iterator dict = Dicts.begin(); dict != Dicts.end(); ++dict)
168    {
169       os << "Printing dictionary " << dict->first << std::endl;
170       dict->second->Print(os);
171    }
172 }
173
174 //-----------------------------------------------------------------------------
175 } // end namespace gdcm