]> Creatis software - gdcm.git/blob - src/gdcmDictSet.cxx
* Add a RefCounter object that is deleted only when it's reference count is
[gdcm.git] / src / gdcmDictSet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDictSet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/20 15:24:08 $
7   Version:   $Revision: 1.71 $
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 <fstream>
22 #include <stdlib.h>  // For getenv
23 #include <stdio.h>   // For sprintf
24
25 namespace gdcm 
26 {
27
28 //-----------------------------------------------------------------------------
29 // Constructor / Destructor
30 /** 
31  * \brief   The Dictionary Set obtained with this constructor simply
32  *          contains the Default Public dictionary.
33  */
34 DictSet::DictSet() 
35 {
36    DictPath = BuildDictPath();
37    std::string pubDictFile(DictPath);
38    pubDictFile += PUB_DICT_FILENAME;
39    Dicts[PUB_DICT_NAME] = new Dict(pubDictFile);
40 }
41
42 /**
43  * \brief  Destructor 
44  */
45 DictSet::~DictSet() 
46 {
47    // Remove dictionaries
48    for (DictSetHT::iterator tag = Dicts.begin(); tag != Dicts.end(); ++tag) 
49    {
50       Dict *entryToDelete = tag->second;
51       if ( entryToDelete )
52       {
53          delete entryToDelete;
54       }
55       tag->second = NULL;
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    Dict *newDict = new Dict(filename);
74    AppendDict(newDict, name);
75
76    return newDict;
77 }
78
79 /**
80  * \brief   Retrieve the specified dictionary (when existing) from this
81  *          DictSet.
82  * @param   dictName The symbolic name of the searched dictionary.
83  * \result  The retrieved dictionary.
84  */
85 Dict *DictSet::GetDict(DictKey const &dictName) 
86 {
87    DictSetHT::iterator dict = Dicts.find(dictName);
88    if ( dict != Dicts.end() )
89    {
90       return dict->second;
91    }
92    return NULL;
93 }
94
95 /**
96  * \brief   Get the first dictionary while visiting the DictSet
97  * \return  The first Dict if found, otherwhise NULL
98  */
99 Dict *DictSet::GetFirstDict()
100 {
101    ItDictHt = Dicts.begin();
102    if ( ItDictHt != Dicts.end() )
103       return ItDictHt->second;
104    return NULL;
105 }
106
107 /**
108  * \brief   Get the next dictionary while visiting the Hash table (DictSetHT)
109  * \note : meaningfull only if GetFirstEntry already called
110  * \return  The next Dict if found, otherwhise NULL
111  */
112 Dict *DictSet::GetNextDict()
113 {
114    gdcmAssertMacro (ItDictHt != Dicts.end());
115   
116    ++ItDictHt;
117    if ( ItDictHt != Dicts.end() )
118       return ItDictHt->second;
119    return NULL;
120 }
121
122 /**
123  * \brief   Obtain from the GDCM_DICT_PATH environnement variable the
124  *          path to directory containing the dictionaries. When
125  *          the environnement variable is absent the path is defaulted
126  *          to "../Dicts/".
127  * @return  path to directory containing the dictionaries
128  */
129 std::string DictSet::BuildDictPath() 
130 {
131    std::string resultPath;
132    const char *envPath;
133    envPath = getenv("GDCM_DICT_PATH");
134
135    if (envPath && (strlen(envPath) != 0)) 
136    {
137       resultPath = envPath;
138       gdcmWarningMacro( "Dictionary path set from environnement");
139    } 
140    else
141    {
142       resultPath = PUB_DICT_PATH;
143    }
144    if ( resultPath[resultPath.length()-1] != '/' )
145    {
146       resultPath += '/';
147    }
148
149    return resultPath;
150 }
151
152 //-----------------------------------------------------------------------------
153 // Protected
154 /**
155  * \brief   Adds a Dictionary to a DictSet
156  * \return  always true
157  */
158 bool DictSet::AppendDict(Dict *newDict, DictKey const &name)
159 {
160    Dicts[name] = newDict;
161
162    return true;
163 }
164
165 //-----------------------------------------------------------------------------
166 // Private
167
168 //-----------------------------------------------------------------------------
169 // Print
170 /**
171  * \brief   Print, in an informal fashion, the list of all the dictionaries
172  *          contained is this DictSet, along with their respective content.
173  * @param   os Output stream used for printing.
174  * @param indent Indentation string to be prepended during printing
175  */
176 void DictSet::Print(std::ostream &os, std::string const & )
177 {
178    for (DictSetHT::iterator dict = Dicts.begin(); dict != Dicts.end(); ++dict)
179    {
180       os << "Printing dictionary " << dict->first << std::endl;
181       dict->second->Print(os);
182    }
183 }
184
185 //-----------------------------------------------------------------------------
186 } // end namespace gdcm