]> Creatis software - gdcm.git/blob - src/gdcmDictSet.cxx
Avoid tiny memory leak
[gdcm.git] / src / gdcmDictSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDictSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2006/04/14 08:22:13 $
7   Version:   $Revision: 1.75 $
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    Global::DefaultPubDict = 0; // just a pointer!
51    // Remove dictionaries
52    for (DictSetHT::iterator tag = Dicts.begin(); tag != Dicts.end(); ++tag) 
53    {
54       if ( tag->second )
55          tag->second->Delete();
56    }
57    Dicts.clear();
58 }
59
60 //-----------------------------------------------------------------------------
61 // Public
62 /**
63  * \brief   Loads a dictionary from a specified file, and add it
64  *          to already the existing ones contained in this DictSet.
65  * @param   filename Absolute or relative filename containing the
66  *          dictionary to load.
67  * @param   name Symbolic name that be used as identifier of the newly 
68  *          created dictionary.
69  */
70 Dict *DictSet::LoadDictFromFile(std::string const &filename, 
71                                 DictKey const &name) 
72 {
73    assert(Dicts.find(name)==Dicts.end());
74    ///\todo RemoveDict(name); when Dict already exist
75    Dict *newDict = Dict::New(filename);
76    Dicts[name] = newDict;
77
78    return newDict;
79 }
80
81 /**
82  * \brief   Retrieve the specified dictionary (when existing) from this
83  *          DictSet.
84  * @param   dictName The symbolic name of the searched dictionary.
85  * \result  The retrieved dictionary.
86  */
87 Dict *DictSet::GetDict(DictKey const &dictName) 
88 {
89    DictSetHT::iterator dict = Dicts.find(dictName);
90    if ( dict != Dicts.end() )
91    {
92       return dict->second;
93    }
94    return NULL;
95 }
96
97 /**
98  * \brief   Get the first dictionary while visiting the DictSet
99  * \return  The first Dict if found, otherwhise NULL
100  */
101 Dict *DictSet::GetFirstDict()
102 {
103    ItDictHt = Dicts.begin();
104    if ( ItDictHt != Dicts.end() )
105       return ItDictHt->second;
106    return NULL;
107 }
108
109 /**
110  * \brief   Get the next dictionary while visiting the Hash table (DictSetHT)
111  * \note : meaningfull only if GetFirstEntry already called
112  * \return  The next Dict if found, otherwhise NULL
113  */
114 Dict *DictSet::GetNextDict()
115 {
116    gdcmAssertMacro (ItDictHt != Dicts.end());
117   
118    ++ItDictHt;
119    if ( ItDictHt != Dicts.end() )
120       return ItDictHt->second;
121    return NULL;
122 }
123
124 /**
125  * \brief   Obtain from the GDCM_DICT_PATH environnement variable the
126  *          path to directory containing the dictionaries. When
127  *          the environnement variable is absent the path is defaulted
128  *          to "../Dicts/".
129  * @return  path to directory containing the dictionaries
130  */
131 std::string DictSet::BuildDictPath() 
132 {
133    std::string resultPath;
134    /*const*/ char *envPath;
135    envPath = getenv("GDCM_DICT_PATH");
136
137    if (envPath && (strlen(envPath) != 0)) 
138    {
139       resultPath = envPath;
140       gdcmStaticWarningMacro( "Dictionary path set from environnement");
141    } 
142    else
143    {
144       resultPath = PUB_DICT_PATH;
145    }
146    if ( resultPath[resultPath.length()-1] != '/' )
147    {
148       resultPath += '/';
149    }
150    free (envPath);
151    return resultPath;
152 }
153
154 //-----------------------------------------------------------------------------
155 // Protected
156
157 //-----------------------------------------------------------------------------
158 // Private
159
160 //-----------------------------------------------------------------------------
161 // Print
162 /**
163  * \brief   Print, in an informal fashion, the list of all the dictionaries
164  *          contained is this DictSet, along with their respective content.
165  * @param   os Output stream used for printing.
166  * @param indent Indentation string to be prepended during printing
167  */
168 void DictSet::Print(std::ostream &os, std::string const & )
169 {
170    for (DictSetHT::iterator dict = Dicts.begin(); dict != Dicts.end(); ++dict)
171    {
172       os << "Printing dictionary " << dict->first << std::endl;
173       dict->second->Print(os);
174    }
175 }
176
177 //-----------------------------------------------------------------------------
178 } // end namespace gdcm