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