]> Creatis software - gdcm.git/blob - src/gdcmDictSet.cxx
56482cb300f1d2e505c96d93873bbc5dc6c9f05a
[gdcm.git] / src / gdcmDictSet.cxx
1 // gdcmDictEntry
2
3 #include <fstream>
4 #include <stdlib.h>  // For getenv
5 #include "gdcm.h"
6 #include "gdcmUtil.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 gdcmDict* gdcmDictSet::LoadDefaultPubDict(void) {
82    string PubDictFile = gdcmDictSet::DictPath + PUB_DICT_FILENAME;
83    return new gdcmDict(PubDictFile.c_str());
84 }
85
86 /** 
87  * \ingroup gdcmDictSet
88  * \brief   The Dictionnary Set obtained with this constructor simply
89  *          contains the Default Public dictionnary.
90  */
91 gdcmDictSet::gdcmDictSet(void) {
92    dicts[PUB_DICT_NAME] = DefaultPubDict;
93 }
94
95 void gdcmDictSet::LoadDictFromFile(string FileName, DictKey Name) {
96    gdcmDict *NewDict = new gdcmDict(FileName.c_str());
97    dicts[Name] = NewDict;
98 }
99
100 void gdcmDictSet::Print(ostream& os) {
101    for (DictSetHT::iterator dict = dicts.begin(); dict != dicts.end(); ++dict){
102       os << "Printing dictionary " << dict->first << " \n";
103       dict->second->Print(os);
104    }
105 }
106
107 gdcmDict * gdcmDictSet::GetDict(DictKey DictName) {
108    DictSetHT::iterator dict = dicts.find(DictName);
109    return dict->second;
110 }
111
112 gdcmDict * gdcmDictSet::GetDefaultPubDict() {
113    return GetDict(PUB_DICT_NAME);
114 }