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