]> Creatis software - gdcm.git/blob - src/gdcmDictSet.cxx
* gdcmPython/__init__.py doesn't crash anymore when running in
[gdcm.git] / src / gdcmDictSet.cxx
1 // gdcmDictEntry
2
3 #include <fstream>
4 #include <stdlib.h>  // For getenv
5 #include "gdcmUtil.h"
6 #include "gdcmDictSet.h"
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 string gdcmDictSet::DictPath = gdcmDictSet::BuildDictPath();
15 gdcmDict* gdcmDictSet::DefaultPubDict = gdcmDictSet::LoadDefaultPubDict();
16
17 /** 
18  * \ingroup gdcmDictSet
19  * \brief   Consider all the entries of the public dicom dictionnary. 
20  *          Build all list of all the tag names of all those entries.
21  * \sa      gdcmDictSet::GetPubDictTagNamesByCategory
22  * @return  A list of all entries of the public dicom dictionnary.
23  */
24 list<string> * gdcmDictSet::GetPubDictTagNames(void) {
25    list<string> * Result = new list<string>;
26    TagKeyHT entries = gdcmDictSet::DefaultPubDict->GetEntries();
27    
28    for (TagKeyHT::iterator tag = entries.begin(); tag != entries.end(); ++tag){
29       Result->push_back( tag->second->GetName() );
30    }
31    return Result;
32 }
33
34 /** 
35  * \ingroup gdcmDictSet
36  * \brief   Consider all the entries of the public dicom dictionnary.
37  *          Build an hashtable whose keys are the names of the groups
38  *          (fourth field in each line of dictionary) and whose corresponding
39  *          values are lists of all the dictionnary entries among that
40  *          group. Note that apparently the Dicom standard doesn't explicitely
41  *          define a name (as a string) for each group.
42  *          A typical usage of this method would be to enable a dynamic
43  *          configuration of a Dicom file browser: the admin/user can
44  *          select in the interface which Dicom tags should be displayed.
45  * @return  An hashtable: whose keys are the names of the groups and whose
46  *          corresponding values are lists of all the dictionnary entries
47  *          among that group.
48  */
49 map<string, list<string> > * gdcmDictSet::GetPubDictTagNamesByCategory(void) {
50    map<string, list<string> > * Result = new map<string, list<string> >;
51    TagKeyHT entries = gdcmDictSet::DefaultPubDict->GetEntries();
52
53    for (TagKeyHT::iterator tag = entries.begin(); tag != entries.end(); ++tag){
54       (*Result)[tag->second->GetFourth()].push_back(tag->second->GetName());
55    }
56    return Result;
57 }
58
59 /**
60  * \ingroup gdcmDictSet
61  * \brief   Obtain from the GDCM_DICT_PATH environnement variable the
62  *          path to directory containing the dictionnaries. When
63  *          the environnement variable is absent the path is defaulted
64  *          to "../Dicts/".
65  */
66 string gdcmDictSet::BuildDictPath(void) {
67    string ResultPath;
68    const char* EnvPath = (char*)0;
69    EnvPath = getenv("GDCM_DICT_PATH");
70    if (EnvPath && (strlen(EnvPath) != 0)) {
71       ResultPath = EnvPath;
72       if (ResultPath[ResultPath.length() -1] != '/' )
73          ResultPath += '/';
74       dbg.Verbose(1, "gdcmDictSet::BuildDictPath:",
75                      "Dictionary path set from environnement");
76    } else
77       ResultPath = PUB_DICT_PATH;
78    return ResultPath;
79 }
80
81 /**
82  * \ingroup gdcmDictSet
83  * \brief   Loads the default public DICOM V3 dictionary as a gdcmDict.
84  * \return  The newly build reference public dictionary.
85  */
86 gdcmDict* gdcmDictSet::LoadDefaultPubDict(void) {
87    string PubDictFile = gdcmDictSet::DictPath + PUB_DICT_FILENAME;
88    return new gdcmDict(PubDictFile.c_str());
89 }
90
91 /** 
92  * \ingroup gdcmDictSet
93  * \brief   The Dictionnary Set obtained with this constructor simply
94  *          contains the Default Public dictionnary.
95  */
96 gdcmDictSet::gdcmDictSet(void) {
97    dicts[PUB_DICT_NAME] = DefaultPubDict;
98 }
99
100 /**
101  * \ingroup gdcmDictSet
102  * \brief   Loads a dictionary from a specified file, and add it
103  *          to allready the existing ones contained in this gdcmDictSet.
104  * @param   FileName Absolute or relative filename containing the
105  *          dictionary to load.
106  * @param   Name Symbolic name that be used as identifier of the newly 
107  *          created dictionary.
108  */
109 void gdcmDictSet::LoadDictFromFile(string FileName, DictKey Name) {
110    gdcmDict *NewDict = new gdcmDict(FileName.c_str());
111    dicts[Name] = NewDict;
112 }
113
114 /**
115  * \ingroup gdcmDictSet
116  * \brief   Print, in an informal fashion, the list of all the dictionaries
117  *          contained is this gdcmDictSet, along with their respective content.
118  * @param   os Output stream used for printing.
119  */
120 void gdcmDictSet::Print(ostream& os) {
121    for (DictSetHT::iterator dict = dicts.begin(); dict != dicts.end(); ++dict){
122       os << "Printing dictionary " << dict->first << " \n";
123       dict->second->Print(os);
124    }
125 }
126
127 /**
128  * \ingroup gdcmDictSet
129  * \brief   Retrieve the specified dictionary (when existing) from this
130  *          gdcmDictSet.
131  * @param   DictName The synbolic name of the searched dictionary.
132  * \result  The retrieved dictionary.
133  */
134 gdcmDict * gdcmDictSet::GetDict(DictKey DictName) {
135    DictSetHT::iterator dict = dicts.find(DictName);
136    return dict->second;
137 }
138
139 /**
140  * \ingroup gdcmDictSet
141  * \brief   Retrieve the default reference DICOM V3 public dictionary.
142  * \result  The retrieved default dictionary.
143  */
144 gdcmDict * gdcmDictSet::GetDefaultPubDict() {
145    return GetDict(PUB_DICT_NAME);
146 }