]> Creatis software - gdcm.git/blob - src/gdcmSQItem.cxx
ENH: Dummy cleanup patch
[gdcm.git] / src / gdcmSQItem.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSQItem.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/07/17 22:47:01 $
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 "gdcmSQItem.h"
20 #include "gdcmSeqEntry.h"
21 #include "gdcmValEntry.h"
22 #include "gdcmBinEntry.h"
23 #include "gdcmGlobal.h"
24 #include "gdcmUtil.h"
25
26 #include "gdcmDebug.h"
27
28
29 //-----------------------------------------------------------------------------
30 // Constructor / Destructor
31 /**
32  * \ingroup gdcmSQItem
33  * \brief   Constructor from a given gdcmSQItem
34  */
35 gdcmSQItem::gdcmSQItem(int depthLevel ) 
36           : gdcmDocEntrySet(depthLevel) {
37    SQDepthLevel = depthLevel +1;
38 }
39
40 /**
41  * \brief   Canonical destructor.
42  */
43 gdcmSQItem::~gdcmSQItem() 
44 {
45    for(ListDocEntry::iterator cc = docEntries.begin();
46        cc != docEntries.end();
47        ++cc)
48    {
49       delete (*cc);
50    }
51    docEntries.clear();
52 }
53
54 //-----------------------------------------------------------------------------
55 // Print
56 /*
57  * \brief   canonical Printer
58  */
59  void gdcmSQItem::Print(std::ostream & os) {
60    std::ostringstream s;
61
62    if (SQDepthLevel>0)
63    {
64       for (int i=0;i<SQDepthLevel;i++)
65          s << "   | " ;
66    }
67    std::cout << s.str() << " --- SQItem number " << SQItemNumber  << std::endl;
68    for (ListDocEntry::iterator i  = docEntries.begin();  
69                                i != docEntries.end();
70                              ++i)
71    {
72       gdcmDocEntry* Entry = *i;
73       bool PrintEndLine = true;
74
75       os << s.str();
76       Entry->SetPrintLevel(2);
77       Entry->Print(os);   
78       if ( gdcmSeqEntry* SeqEntry = dynamic_cast<gdcmSeqEntry*>(Entry) )
79       {
80          (void)SeqEntry;  //not used
81          PrintEndLine = false;
82       }
83       if (PrintEndLine)
84          os << std::endl;
85    } 
86 }
87
88 /*
89  * \ingroup gdcmSQItem
90  * \brief   canonical Writer
91  */
92 void gdcmSQItem::Write(FILE *fp,FileType filetype)
93 {
94    for (ListDocEntry::iterator i = docEntries.begin();  
95         i != docEntries.end();
96         ++i)
97    {
98       // Item Delimitor Item IS the last one of a 'no length' SQItem
99       // (when it exists) we don't write it right now
100       // It will be written outside, because ALL the SQItems are written
101       // as 'no length'
102       if ( (*i)->isItemDelimitor() )
103          break;
104
105       // Fix in order to make some MR PHILIPS images e-film readable
106       // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
107       // we just *always* ignore spurious fffe|0000 tag ! 
108       if ( ((*i)->GetGroup() == 0xfffe) && ((*i)->GetElement() == 0x0000) )  
109          break; 
110
111       // It's up to the gdcmDocEntry Writter to write the SQItem begin element
112       // (fffe|e000) as a 'no length' one
113       (*i)->Write(fp, filetype);
114    } 
115 }
116
117 //-----------------------------------------------------------------------------
118 // Public
119 /**
120  * \brief   adds any Entry (Dicom Element) to the Sequence Item
121  */
122 bool gdcmSQItem::AddEntry(gdcmDocEntry *entry)
123 {
124    docEntries.push_back(entry);
125    //TODO : check if it worked
126    return true;
127 }   
128
129 /**
130  * \brief   Sets Entry (Dicom Element) value of an element,
131  *          specified by it's tag (Group, Number) 
132  *          and the length, too ...  inside a SQ Item
133  *          If the Element is not found, it's just created !
134  * \warning we suppose, right now, the element belongs to a Public Group
135  *          (NOT a shadow one)       
136  * @param   val string value to set
137  * @param   group Group number of the searched tag.
138  * @param   element Element number of the searched tag.
139  * @return  true if element was found or created successfully
140  */
141
142 bool gdcmSQItem::SetEntryByNumber(std::string val,uint16_t group, 
143                                   uint16_t element)
144 {
145    for(ListDocEntry::iterator i=docEntries.begin();i!=docEntries.end();++i)
146    { 
147       if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 ) 
148          continue;
149
150       if (  ( group   < (*i)->GetGroup() )
151           ||( group == (*i)->GetGroup() && element < (*i)->GetElement()) )
152       {
153          // instead of ReplaceOrCreateByNumber 
154          // that is a method of gdcmDocument :-( 
155          gdcmValEntry* Entry = (gdcmValEntry*)0;
156          gdcmTagKey key = gdcmDictEntry::TranslateToKey(group, element);
157
158          if ( ! ptagHT->count(key))
159          {
160             // we assume a Public Dictionnary *is* loaded
161             gdcmDict *PubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
162             // if the invoked (group,elem) doesn't exist inside the Dictionary
163             // we create a VirtualDictEntry
164             gdcmDictEntry *DictEntry = PubDict->GetDictEntryByNumber(group,
165                                                                      element);
166             if (DictEntry == NULL)
167             {
168               DictEntry=gdcmGlobal::GetDicts()->NewVirtualDictEntry(group,
169                                                                     element,
170                                                                     "UN",
171                                                                     "??","??");
172             } 
173             // we assume the constructor didn't fail
174             Entry = new gdcmValEntry(DictEntry);
175             /// \todo
176             /// ----
177             /// better we don't assume too much !
178             /// gdcmSQItem is now used to describe any DICOMDIR related object
179          } else {
180             gdcmDocEntry* FoundEntry = ptagHT->find(key)->second;
181             Entry = dynamic_cast<gdcmValEntry*>(FoundEntry);
182             if (!Entry) 
183                dbg.Verbose(0, "gdcmSQItem::SetEntryByNumber: docEntries"
184                               " contains non gdcmValEntry occurences");
185          }
186          if (Entry)
187             Entry->SetValue(val); 
188          Entry->SetLength(val.length());
189          docEntries.insert(i,Entry); 
190          return true;
191       }   
192       if (group == (*i)->GetGroup() && element == (*i)->GetElement() )
193       {
194          if ( gdcmValEntry* Entry = dynamic_cast<gdcmValEntry*>(*i) )
195             Entry->SetValue(val);
196          (*i)->SetLength(val.length()); 
197          return true;    
198       }
199    }
200    return false;
201 }
202 //-----------------------------------------------------------------------------
203 // Protected
204
205
206 /**
207  * \brief   Gets a Dicom Element inside a SQ Item Entry, by number
208  * @return
209  */
210 gdcmDocEntry *gdcmSQItem::GetDocEntryByNumber(uint16_t group, uint16_t element)
211 {
212    for(ListDocEntry::iterator i=docEntries.begin();i!=docEntries.end();++i)
213    {
214       if ( (*i)->GetGroup()==group && (*i)->GetElement()==element)
215       {
216          return (*i);
217       }
218    }   
219    return NULL;
220 }
221
222 /**
223  * \brief   Get the value of a Dicom Element inside a SQ Item Entry, by number
224  * @return
225  */ 
226
227 std::string gdcmSQItem::GetEntryByNumber(uint16_t group, uint16_t element)
228
229    for(ListDocEntry::iterator i=docEntries.begin();i!=docEntries.end();++i)
230    {
231       if ( (*i)->GetGroup()==group && (*i)->GetElement()==element)
232       {
233          return ((gdcmValEntry *)(*i))->GetValue();
234       }
235    }   
236    return GDCM_UNFOUND;
237 }
238 //-----------------------------------------------------------------------------
239 // Private
240
241
242 //-----------------------------------------------------------------------------