]> Creatis software - gdcm.git/blob - src/gdcmDictSet.cxx
* src/*.[h|cxx] : coding style
[gdcm.git] / src / gdcmDictSet.cxx
1 // gdcmDictEntry
2 //-----------------------------------------------------------------------------
3 #include "gdcmDictSet.h"
4 #include "gdcmUtil.h"
5 #include <fstream>
6 #include <stdlib.h>  // For getenv
7
8 #define PUB_DICT_NAME     "DicomV3Dict"
9 #ifndef PUB_DICT_PATH
10 #  define PUB_DICT_PATH     "../Dicts/"
11 #endif
12 #define PUB_DICT_FILENAME "dicomV3.dic"
13
14 //-----------------------------------------------------------------------------
15 // Constructor / Destructor
16 /** 
17  * \ingroup gdcmDictSet
18  * \brief   The Dictionnary Set obtained with this constructor simply
19  *          contains the Default Public dictionnary.
20  */
21 gdcmDictSet::gdcmDictSet(void) {
22    DictPath = BuildDictPath();
23    std::string PubDictFile = DictPath + PUB_DICT_FILENAME;
24    Dicts[PUB_DICT_NAME] = new gdcmDict(PubDictFile);
25 }
26
27 /**
28  * \ingroup gdcmDictSet
29  * \brief  Destructor 
30  */
31 gdcmDictSet::~gdcmDictSet() {
32    for (DictSetHT::iterator tag = Dicts.begin(); tag != Dicts.end(); ++tag) {
33       gdcmDict* EntryToDelete = tag->second;
34       if ( EntryToDelete )
35          delete EntryToDelete;
36    }
37    Dicts.clear();
38 }
39
40 //-----------------------------------------------------------------------------
41 // Print
42 /**
43  * \ingroup gdcmDictSet
44  * \brief   Print, in an informal fashion, the list of all the dictionaries
45  *          contained is this gdcmDictSet, along with their respective content.
46  * @param   os Output stream used for printing.
47  */
48 void gdcmDictSet::Print(std::ostream& os) {
49    for (DictSetHT::iterator dict = Dicts.begin(); dict != Dicts.end(); ++dict){
50       os << "Printing dictionary " << dict->first << std::endl;
51       dict->second->Print(os);
52    }
53 }
54
55 //-----------------------------------------------------------------------------
56 // Public
57 /** 
58  * \ingroup gdcmDictSet
59  * \brief   Consider all the entries of the public dicom dictionnary. 
60  *          Build all list of all the tag names of all those entries.
61  * \sa      gdcmDictSet::GetPubDictTagNamesByCategory
62  * @return  A list of all entries of the public dicom dictionnary.
63  */
64 std::list<std::string> * gdcmDictSet::GetPubDictTagNames(void) {
65    std::list<std::string> * Result = new std::list<std::string>;
66    TagKeyHT entries = GetDefaultPubDict()->GetEntries();
67    
68    for (TagKeyHT::iterator tag = entries.begin(); tag != entries.end(); ++tag){
69       Result->push_back( tag->second->GetName() );
70    }
71    return Result;
72 }
73
74 /** 
75  * \ingroup gdcmDictSet
76  * \brief   Consider all the entries of the public dicom dictionnary.
77  *          Build an hashtable whose keys are the names of the groups
78  *          (fourth field in each line of dictionary) and whose corresponding
79  *          values are lists of all the dictionnary entries among that
80  *          group. Note that apparently the Dicom standard doesn't explicitely
81  *          define a name (as a string) for each group.
82  *          A typical usage of this method would be to enable a dynamic
83  *          configuration of a Dicom file browser: the admin/user can
84  *          select in the interface which Dicom tags should be displayed.
85  * \warning Dicom *doesn't* define any name for any 'categorie'
86  *          (the dictionnary fourth field was formerly NIH defined
87  *           - and no longer he is-
88  *           and will be removed when Dicom provides us a text file
89  *           with the 'official' Dictionnary, that would be more friendly
90  *           than asking us to perform a line by line check of the dictionnary
91  *           at the beginning of each year to -try to- guess the changes)
92  *           Therefore : please NEVER use that fourth field :-(
93  * *
94  * @return  An hashtable: whose keys are the names of the groups and whose
95  *          corresponding values are lists of all the dictionnary entries
96  *          among that group.
97  */
98 std::map<std::string, std::list<std::string> > * gdcmDictSet::GetPubDictTagNamesByCategory(void) {
99    std::map<std::string, std::list<std::string> > * Result = new std::map<std::string, std::list<std::string> >;
100    TagKeyHT entries = GetDefaultPubDict()->GetEntries();
101
102    for (TagKeyHT::iterator tag = entries.begin(); tag != entries.end(); ++tag){
103       (*Result)[tag->second->GetFourth()].push_back(tag->second->GetName());
104    }
105    return Result;
106 }
107
108 /**
109  * \ingroup gdcmDictSet
110  * \brief   Loads a dictionary from a specified file, and add it
111  *          to already the existing ones contained in this gdcmDictSet.
112  * @param   FileName Absolute or relative filename containing the
113  *          dictionary to load.
114  * @param   Name Symbolic name that be used as identifier of the newly 
115  *          created dictionary.
116  */
117 void gdcmDictSet::LoadDictFromFile(std::string FileName, DictKey Name) {
118    gdcmDict *NewDict = new gdcmDict(FileName);
119    AppendDict(NewDict,Name);
120 }
121
122 /**
123  * \ingroup gdcmDictSet
124  * \brief   Retrieve the specified dictionary (when existing) from this
125  *          gdcmDictSet.
126  * @param   DictName The symbolic name of the searched dictionary.
127  * \result  The retrieved dictionary.
128  */
129 gdcmDict * gdcmDictSet::GetDict(DictKey DictName) {
130    DictSetHT::iterator dict = Dicts.find(DictName);
131    return dict->second;
132 }
133
134 /**
135  * \ingroup gdcmDictSet
136  * \brief   Retrieve the default reference DICOM V3 public dictionary.
137  * \result  The retrieved default dictionary.
138  */
139 gdcmDict * gdcmDictSet::GetDefaultPubDict() {
140    return GetDict(PUB_DICT_NAME);
141 }
142
143 /**
144  * \ingroup gdcmDictSet
145  * \brief   Obtain from the GDCM_DICT_PATH environnement variable the
146  *          path to directory containing the dictionnaries. When
147  *          the environnement variable is absent the path is defaulted
148  *          to "../Dicts/".
149  * @return path to directory containing the dictionnaries
150  */
151 std::string gdcmDictSet::BuildDictPath(void) {
152    std::string ResultPath;
153    const char* EnvPath = (char*)0;
154    EnvPath = getenv("GDCM_DICT_PATH");
155    if (EnvPath && (strlen(EnvPath) != 0)) {
156       ResultPath = EnvPath;
157       if (ResultPath[ResultPath.length() -1] != '/' )
158          ResultPath += '/';
159       dbg.Verbose(1, "gdcmDictSet::BuildDictPath:",
160                      "Dictionary path set from environnement");
161    } else
162       ResultPath = PUB_DICT_PATH;
163    return ResultPath;
164 }
165
166 //-----------------------------------------------------------------------------
167 // Protected
168 bool gdcmDictSet::AppendDict(gdcmDict* NewDict,DictKey Name)
169 {
170    Dicts[Name] = NewDict;
171    return(true);
172 }
173
174 //-----------------------------------------------------------------------------
175 // Private
176
177 //-----------------------------------------------------------------------------
178
179