]> Creatis software - gdcm.git/blob - src/gdcmDocEntrySet.cxx
Now, TestCopyDicom deals with private Entries
[gdcm.git] / src / gdcmDocEntrySet.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocEntrySet.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/09/14 16:47:08 $
7   Version:   $Revision: 1.21 $
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 Val Entry 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 Bin Entry 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 /**
114  * \brief   Build a new Seq Entry from all the low level arguments. 
115  *          Check for existence of dictionary entry, and build
116  *          a default one when absent.
117  * @param   Group group   number of the underlying DictEntry
118  * @param   Elem  element number of the underlying DictEntry
119  */
120 gdcmSeqEntry *gdcmDocEntrySet::NewSeqEntryByNumber(uint16_t Group,
121                                                    uint16_t Elem) 
122 {
123    // Find out if the tag we encountered is in the dictionaries:
124    gdcmDictEntry *DictEntry = GetDictEntryByNumber(Group, Elem);
125    if (!DictEntry)
126       DictEntry = NewVirtualDictEntry(Group, Elem);
127
128    gdcmSeqEntry *NewEntry = new gdcmSeqEntry(DictEntry, 1); // FIXME : 1
129    if (!NewEntry) 
130    {
131       dbg.Verbose(1, "gdcmDocument::NewSeqEntryByNumber",
132                   "failed to allocate gdcmSeqEntry");
133       return NULL;
134    }
135    return NewEntry;
136 }
137 //-----------------------------------------------------------------------------
138 // Protected
139
140 /**
141  * \brief   Gets a Dicom Element inside a SQ Item Entry, by name
142  * @return
143  */
144  gdcmDocEntry *gdcmDocEntrySet::GetDocEntryByName(std::string const & name)
145  {
146    gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
147    gdcmDictEntry *dictEntry = pubDict->GetDictEntryByName(name);
148    if( !dictEntry )
149    {
150       return 0;
151    }
152
153    return GetDocEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement());      
154 }
155
156
157 /**
158  * \brief   Get the value of a Dicom Element inside a SQ Item Entry, by name
159  * @param   name : name of the searched element.
160  * @return
161  */ 
162
163 std::string gdcmDocEntrySet::GetEntryByName(TagName const & name)
164 {
165    gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
166    gdcmDictEntry *dictEntry = pubDict->GetDictEntryByName(name); 
167
168    if( !dictEntry )
169    {
170       return GDCM_UNFOUND;
171    }
172
173    return GetEntryByNumber(dictEntry->GetGroup(), dictEntry->GetElement()); 
174 }
175
176
177 /**
178  * \brief   Request a new virtual dict entry to the dict set
179  * @param   group     group  number of the underlying DictEntry
180  * @param   element  element number of the underlying DictEntry
181  * @param   vr     VR of the underlying DictEntry
182  * @param   fourth owner group
183  * @param   name   english name
184  */
185 gdcmDictEntry* gdcmDocEntrySet::NewVirtualDictEntry(uint16_t group,
186                                                     uint16_t element,
187                                                     std::string const & vr,
188                                                     std::string const & fourth,
189                                                     std::string const & name)
190 {
191    return gdcmGlobal::GetDicts()->NewVirtualDictEntry(group,element,vr,fourth,name);
192 }
193
194 /** \brief 
195  * Creates a new DocEntry (without any 'value' ...)
196  * @param   group     group  number of the underlying DictEntry
197  * @param   elem  elem number of the underlying DictEntry 
198  */
199 gdcmDocEntry* gdcmDocEntrySet::NewDocEntryByNumber(uint16_t group,
200                                                    uint16_t elem)
201 {
202    // Find out if the tag we encountered is in the dictionaries:
203    gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
204    gdcmDictEntry *dictEntry = pubDict->GetDictEntryByNumber(group, elem);
205    if (!dictEntry)
206    {
207       dictEntry = NewVirtualDictEntry(group, elem);
208    }
209
210    gdcmDocEntry *newEntry = new gdcmDocEntry(dictEntry);
211    if (!newEntry) 
212    {
213       dbg.Verbose(1, "gdcmSQItem::NewDocEntryByNumber",
214                   "failed to allocate gdcmDocEntry");
215       return 0;
216    }
217    return newEntry;
218 }
219
220
221 /** \brief 
222  * Creates a new DocEntry (without any 'value' ...)
223  * @param   group     group  number of the underlying DictEntry
224  * @param   elem  elem number of the underlying DictEntry 
225  * @param   VR   V(alue) R(epresentation) of the Entry -if private Entry- 
226
227  */
228 gdcmDocEntry* gdcmDocEntrySet::NewDocEntryByNumber(uint16_t group,
229                                                    uint16_t elem,
230                                                    std::string const &VR)
231 {
232    // Find out if the tag we encountered is in the dictionaries:
233    gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
234    gdcmDictEntry *dictEntry = pubDict->GetDictEntryByNumber(group, elem);
235    if (!dictEntry)
236    {
237       dictEntry = NewVirtualDictEntry(group, elem, VR);
238    }
239
240    gdcmDocEntry *newEntry = new gdcmDocEntry(dictEntry);
241    if (!newEntry) 
242    {
243       dbg.Verbose(1, "gdcmSQItem::NewDocEntryByNumber",
244                   "failed to allocate gdcmDocEntry");
245       return 0;
246    }
247    return newEntry;
248 }
249 /* \brief
250  * Probabely move, as is, to gdcmDocEntrySet, as a non virtual method
251  * an remove gdcmDocument::NewDocEntryByName
252  */
253 gdcmDocEntry *gdcmDocEntrySet::NewDocEntryByName  (std::string const & name)
254 {
255   gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
256   gdcmDictEntry *newTag = pubDict->GetDictEntryByName(name);
257    if (!newTag)
258    {
259       newTag = NewVirtualDictEntry(0xffff, 0xffff, "LO", "unkn", name);
260    }
261
262    gdcmDocEntry* newEntry = new gdcmDocEntry(newTag);
263    if (!newEntry) 
264    {
265       dbg.Verbose(1, "gdcmSQItem::ObtainDocEntryByName",
266                   "failed to allocate gdcmDocEntry");
267       return 0;
268    }
269
270    return newEntry;
271 }
272
273
274 /**
275  * \brief   Searches both the public and the shadow dictionary (when they
276  *          exist) for the presence of the DictEntry with given name.
277  *          The public dictionary has precedence on the shadow one.
278  * @param   name Name of the searched DictEntry
279  * @return  Corresponding DictEntry when it exists, NULL otherwise.
280  */
281 gdcmDictEntry *gdcmDocEntrySet::GetDictEntryByName(std::string const & name) 
282 {
283    gdcmDictEntry *found = 0;
284    gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
285    if (!pubDict) 
286    {
287       dbg.Verbose(0, "gdcmDocument::GetDictEntry",
288                      "we SHOULD have a default dictionary");
289    }
290    else
291    {
292       found = pubDict->GetDictEntryByName(name);  
293    }
294    return found;
295 }
296
297 /**
298  * \brief   Searches both the public and the shadow dictionary (when they
299  *          exist) for the presence of the DictEntry with given
300  *          group and element. The public dictionary has precedence on the
301  *          shadow one.
302  * @param   group   group number of the searched DictEntry
303  * @param   element element number of the searched DictEntry
304  * @return  Corresponding DictEntry when it exists, NULL otherwise.
305  */
306 gdcmDictEntry *gdcmDocEntrySet::GetDictEntryByNumber(uint16_t group,
307                                                      uint16_t element) 
308 {
309    gdcmDictEntry *found = 0;
310    gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
311    if (!pubDict) 
312    {
313       dbg.Verbose(0, "gdcmDocument::GetDictEntry",
314                      "we SHOULD have a default dictionary");
315    }
316    else
317    {
318       found = pubDict->GetDictEntryByNumber(group, element);  
319    }
320    return found;
321 }
322
323
324 //-----------------------------------------------------------------------------
325 // Private
326
327
328 //-----------------------------------------------------------------------------