]> Creatis software - gdcm.git/blob - src/gdcmDictSet.cxx
remove H Table NameHT
[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  * \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 std::list<std::string> * gdcmDictSet::GetPubDictTagNames(void) {
22    std::list<std::string> * Result = new std::list<std::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  
43  * \warning Dicom *doesn't* define any name for any 'categorie'
44  *          (the dictionnary fourth field was formerly NIH defined
45  *           - and no longer he is-
46  *           and will be removed when Dicom provides us a text file
47  *           with the 'official' Dictionnary, that would be more friendly
48  *           than asking us to perform a line by line check of the dictionnary
49  *           at the beginning of each year to -try to- guess the changes)
50  *           Therefore : please NEVER use that fourth field :-(
51  * *
52  * @return  An hashtable: whose keys are the names of the groups and whose
53  *          corresponding values are lists of all the dictionnary entries
54  *          among that group.
55  */
56 std::map<std::string, std::list<std::string> > * gdcmDictSet::GetPubDictTagNamesByCategory(void) {
57    std::map<std::string, std::list<std::string> > * Result = new std::map<std::string, std::list<std::string> >;
58    TagKeyHT entries = GetDefaultPubDict()->GetEntries();
59
60    for (TagKeyHT::iterator tag = entries.begin(); tag != entries.end(); ++tag){
61       (*Result)[tag->second->GetFourth()].push_back(tag->second->GetName());
62    }
63    return Result;
64 }
65
66 /**
67  * \ingroup gdcmDictSet
68  * \brief   Obtain from the GDCM_DICT_PATH environnement variable the
69  *          path to directory containing the dictionnaries. When
70  *          the environnement variable is absent the path is defaulted
71  *          to "../Dicts/".
72  * @return path to directory containing the dictionnaries
73  */
74 std::string gdcmDictSet::BuildDictPath(void) {
75    std::string ResultPath;
76    const char* EnvPath = (char*)0;
77    EnvPath = getenv("GDCM_DICT_PATH");
78    if (EnvPath && (strlen(EnvPath) != 0)) {
79       ResultPath = EnvPath;
80       if (ResultPath[ResultPath.length() -1] != '/' )
81          ResultPath += '/';
82       dbg.Verbose(1, "gdcmDictSet::BuildDictPath:",
83                      "Dictionary path set from environnement");
84    } else
85       ResultPath = PUB_DICT_PATH;
86    return ResultPath;
87 }
88
89 /** 
90  * \ingroup gdcmDictSet
91  * \brief   The Dictionnary Set obtained with this constructor simply
92  *          contains the Default Public dictionnary.
93  */
94 gdcmDictSet::gdcmDictSet(void) {
95    DictPath = BuildDictPath();
96    std::string PubDictFile = DictPath + PUB_DICT_FILENAME;
97    Dicts[PUB_DICT_NAME] = new gdcmDict(PubDictFile);
98 }
99
100
101 /**
102  * \ingroup gdcmDictSet
103  * \brief  Destructor 
104  */
105 gdcmDictSet::~gdcmDictSet() {
106    for (DictSetHT::iterator tag = Dicts.begin(); tag != Dicts.end(); ++tag) {
107       gdcmDict* EntryToDelete = tag->second;
108       if ( EntryToDelete )
109          delete EntryToDelete;
110    }
111    Dicts.clear();
112 }
113
114 /**
115  * \ingroup gdcmDictSet
116  * \brief   Loads a dictionary from a specified file, and add it
117  *          to already the existing ones contained in this gdcmDictSet.
118  * @param   FileName Absolute or relative filename containing the
119  *          dictionary to load.
120  * @param   Name Symbolic name that be used as identifier of the newly 
121  *          created dictionary.
122  */
123 void gdcmDictSet::LoadDictFromFile(std::string FileName, DictKey Name) {
124    gdcmDict *NewDict = new gdcmDict(FileName);
125    Dicts[Name] = NewDict;
126 }
127
128 /**
129  * \ingroup gdcmDictSet
130  * \brief   Print, in an informal fashion, the list of all the dictionaries
131  *          contained is this gdcmDictSet, along with their respective content.
132  * @param   os Output stream used for printing.
133  */
134 void gdcmDictSet::Print(std::ostream& os) {
135    for (DictSetHT::iterator dict = Dicts.begin(); dict != Dicts.end(); ++dict){
136       os << "Printing dictionary " << dict->first << std::endl;
137       dict->second->Print(os);
138    }
139 }
140
141 /**
142  * \ingroup gdcmDictSet
143  * \brief   Retrieve the specified dictionary (when existing) from this
144  *          gdcmDictSet.
145  * @param   DictName The symbolic name of the searched dictionary.
146  * \result  The retrieved dictionary.
147  */
148 gdcmDict * gdcmDictSet::GetDict(DictKey DictName) {
149    DictSetHT::iterator dict = Dicts.find(DictName);
150    return dict->second;
151 }
152
153 /**
154  * \ingroup gdcmDictSet
155  * \brief   Retrieve the default reference DICOM V3 public dictionary.
156  * \result  The retrieved default dictionary.
157  */
158 gdcmDict * gdcmDictSet::GetDefaultPubDict() {
159    return GetDict(PUB_DICT_NAME);
160 }