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