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