]> Creatis software - gdcm.git/blob - src/gdcmDocEntrySet.cxx
ENH: Who would have bet ! Some cosmetic cleanups
[gdcm.git] / src / gdcmDocEntrySet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntrySet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/08/01 03:20:23 $
7   Version:   $Revision: 1.15 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.htm for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmDebug.h"
20 #include "gdcmCommon.h"
21 #include "gdcmGlobal.h"
22 #include "gdcmDocEntrySet.h"
23 #include "gdcmException.h"
24 #include "gdcmDocEntry.h"
25 #include "gdcmSeqEntry.h"
26 #include "gdcmValEntry.h"
27 #include "gdcmBinEntry.h"
28
29 //-----------------------------------------------------------------------------
30 // Constructor / Destructor
31 /**
32  * \ingroup gdcmDocEntrySet
33  * \brief   Constructor from a given gdcmDocEntrySet
34  */
35 gdcmDocEntrySet::gdcmDocEntrySet(int depthLevel)
36 {
37    SQDepthLevel = depthLevel + 1;   //magic +1 !
38 }
39 /**
40  * \brief   Canonical destructor.
41  */
42 gdcmDocEntrySet::~gdcmDocEntrySet()
43 {
44 }
45 //-----------------------------------------------------------------------------
46 // Print
47 /*
48  * \ingroup gdcmDocEntrySet
49  * \brief   canonical Printer
50  */
51
52
53 //-----------------------------------------------------------------------------
54 // Public
55
56
57
58 /**
59  * \brief   Build a new Element Value from all the low level arguments. 
60  *          Check for existence of dictionary entry, and build
61  *          a default one when absent.
62  * @param   Group group   number of the underlying DictEntry
63  * @param   Elem  element number of the underlying DictEntry
64  */
65 gdcmValEntry *gdcmDocEntrySet::NewValEntryByNumber(uint16_t group,
66                                                    uint16_t elem) 
67 {
68    // Find out if the tag we encountered is in the dictionaries:
69    gdcmDictEntry *dictEntry = GetDictEntryByNumber(group, elem);
70    if (!dictEntry)
71    {
72       dictEntry = NewVirtualDictEntry(group, elem);
73    }
74
75    gdcmValEntry *newEntry = new gdcmValEntry(dictEntry);
76    if (!newEntry) 
77    {
78       dbg.Verbose(1, "gdcmDocument::NewValEntryByNumber",
79                   "failed to allocate gdcmValEntry");
80       return 0;
81    }
82    return newEntry;
83 }
84
85
86 /**
87  * \brief   Build a new Element Value from all the low level arguments. 
88  *          Check for existence of dictionary entry, and build
89  *          a default one when absent.
90  * @param   Group group   number of the underlying DictEntry
91  * @param   Elem  element number of the underlying DictEntry
92  */
93 gdcmBinEntry *gdcmDocEntrySet::NewBinEntryByNumber(uint16_t group,
94                                                    uint16_t elem) 
95 {
96    // Find out if the tag we encountered is in the dictionaries:
97    gdcmDictEntry *dictEntry = GetDictEntryByNumber(group, elem);
98    if (!dictEntry)
99    {
100       dictEntry = NewVirtualDictEntry(group, elem);
101    }
102
103    gdcmBinEntry *newEntry = new gdcmBinEntry(dictEntry);
104    if (!newEntry) 
105    {
106       dbg.Verbose(1, "gdcmDocument::NewBinEntryByNumber",
107                   "failed to allocate gdcmBinEntry");
108       return 0;
109    }
110    return newEntry;
111 }
112 //-----------------------------------------------------------------------------
113 // Protected
114
115 /**
116  * \brief   Gets a Dicom Element inside a SQ Item Entry, by name
117  * @return
118  */
119  gdcmDocEntry *gdcmDocEntrySet::GetDocEntryByName(std::string const & name)
120  {
121    gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
122    gdcmDictEntry *dictEntry = pubDict->GetDictEntryByName(name);
123    if( !dictEntry )
124    {
125       return 0;
126    }
127
128    return GetDocEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement());      
129 }
130
131
132 /**
133  * \brief   Get the value of a Dicom Element inside a SQ Item Entry, by name
134  * @param   name : name of the searched element.
135  * @return
136  */ 
137
138 std::string gdcmDocEntrySet::GetEntryByName(TagName const & name)
139 {
140    gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
141    gdcmDictEntry *dictEntry = pubDict->GetDictEntryByName(name); 
142
143    if( !dictEntry )
144    {
145       return GDCM_UNFOUND;
146    }
147
148    return GetEntryByNumber(dictEntry->GetGroup(), dictEntry->GetElement()); 
149 }
150
151
152 /**
153  * \brief   Request a new virtual dict entry to the dict set
154  * @param   group     group  number of the underlying DictEntry
155  * @param   element  element number of the underlying DictEntry
156  * @param   vr     VR of the underlying DictEntry
157  * @param   fourth owner group
158  * @param   name   english name
159  */
160 gdcmDictEntry* gdcmDocEntrySet::NewVirtualDictEntry(uint16_t group,
161                                                     uint16_t element,
162                                                     std::string const & vr,
163                                                     std::string const & fourth,
164                                                     std::string const & name)
165 {
166    return gdcmGlobal::GetDicts()->NewVirtualDictEntry(group,element,vr,fourth,name);
167 }
168
169 /** \brief 
170  * Probabely move, as is, to gdcmDocEntrySet, as a non virtual method
171  * an remove gdcmDocument::NewDocEntryByNumber
172  */
173 gdcmDocEntry* gdcmDocEntrySet::NewDocEntryByNumber(uint16_t group,
174                                                    uint16_t elem)
175 {
176    // Find out if the tag we encountered is in the dictionaries:
177    gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
178    gdcmDictEntry *dictEntry = pubDict->GetDictEntryByNumber(group, elem);
179    if (!dictEntry)
180    {
181       dictEntry = NewVirtualDictEntry(group, elem);
182    }
183
184    gdcmDocEntry *newEntry = new gdcmDocEntry(dictEntry);
185    if (!newEntry) 
186    {
187       dbg.Verbose(1, "gdcmSQItem::NewDocEntryByNumber",
188                   "failed to allocate gdcmDocEntry");
189       return 0;
190    }
191    return newEntry;
192 }
193
194 /// \brief 
195 gdcmDocEntry *gdcmDocEntrySet::NewDocEntryByName  (std::string const & name)
196 {
197   gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
198   gdcmDictEntry *newTag = pubDict->GetDictEntryByName(name);
199    if (!newTag)
200    {
201       newTag = NewVirtualDictEntry(0xffff, 0xffff, "LO", "unkn", name);
202    }
203
204    gdcmDocEntry* newEntry = new gdcmDocEntry(newTag);
205    if (!newEntry) 
206    {
207       dbg.Verbose(1, "gdcmSQItem::ObtainDocEntryByName",
208                   "failed to allocate gdcmDocEntry");
209       return 0;
210    }
211
212    return newEntry;
213 }
214
215
216 /**
217  * \brief   Searches both the public and the shadow dictionary (when they
218  *          exist) for the presence of the DictEntry with given name.
219  *          The public dictionary has precedence on the shadow one.
220  * @param   Name name of the searched DictEntry
221  * @return  Corresponding DictEntry when it exists, NULL otherwise.
222  */
223 gdcmDictEntry *gdcmDocEntrySet::GetDictEntryByName(std::string const & name) 
224 {
225    gdcmDictEntry *found = 0;
226    gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
227    if (!pubDict) 
228    {
229       dbg.Verbose(0, "gdcmDocument::GetDictEntry",
230                      "we SHOULD have a default dictionary");
231    }
232    else
233    {
234       found = pubDict->GetDictEntryByName(name);  
235    }
236    return found;
237 }
238
239 /**
240  * \brief   Searches both the public and the shadow dictionary (when they
241  *          exist) for the presence of the DictEntry with given
242  *          group and element. The public dictionary has precedence on the
243  *          shadow one.
244  * @param   group   group number of the searched DictEntry
245  * @param   element element number of the searched DictEntry
246  * @return  Corresponding DictEntry when it exists, NULL otherwise.
247  */
248 gdcmDictEntry *gdcmDocEntrySet::GetDictEntryByNumber(uint16_t group,
249                                                      uint16_t element) 
250 {
251    gdcmDictEntry *found = 0;
252    gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
253    if (!pubDict) 
254    {
255       dbg.Verbose(0, "gdcmDocument::GetDictEntry",
256                      "we SHOULD have a default dictionary");
257    }
258    else
259    {
260       found = pubDict->GetDictEntryByNumber(group, element);  
261    }
262    return found;
263 }
264
265
266 //-----------------------------------------------------------------------------
267 // Private
268
269
270 //-----------------------------------------------------------------------------