]> Creatis software - gdcm.git/blob - src/gdcmSQItem.cxx
BUG: i was shadowing another var
[gdcm.git] / src / gdcmSQItem.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSQItem.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/11/04 15:08:50 $
7   Version:   $Revision: 1.35 $
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.html 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 #include "gdcmDebug.h"
26 #include <fstream>
27
28 namespace gdcm 
29 {
30 //-----------------------------------------------------------------------------
31 // Constructor / Destructor
32 /**
33  * \ingroup SQItem
34  * \brief   Constructor from a given SQItem
35  */
36 SQItem::SQItem(int depthLevel ) 
37           : DocEntrySet( )
38 {
39    SQDepthLevel = depthLevel;
40 }
41
42 /**
43  * \brief   Canonical destructor.
44  */
45 SQItem::~SQItem() 
46 {
47    for(ListDocEntry::iterator cc = DocEntries.begin();
48                              cc != DocEntries.end();
49                              ++cc)
50    {
51       delete *cc;
52    }
53    DocEntries.clear();
54 }
55
56 //-----------------------------------------------------------------------------
57 // Print
58 /*
59  * \brief   canonical Printer
60  */
61  void SQItem::Print(std::ostream& os)
62  {
63    std::ostringstream s;
64
65    if (SQDepthLevel > 0)
66    {
67       for (int i = 0; i < SQDepthLevel; ++i)
68       {
69          s << "   | " ;
70       }
71    }
72    //std::cout << s.str() << " --- SQItem number " << SQItemNumber  << std::endl;
73    for (ListDocEntry::iterator i  = DocEntries.begin();
74                                i != DocEntries.end();
75                              ++i)
76    {
77       DocEntry* Entry = *i;
78       bool PrintEndLine = true;
79
80       os << s.str();
81       Entry->SetPrintLevel(2);
82       Entry->Print(os); 
83       if ( SeqEntry* seqEntry = dynamic_cast<SeqEntry*>(Entry) )
84       {
85          (void)seqEntry;  //not used
86          PrintEndLine = false;
87       }
88       if (PrintEndLine)
89       {
90          os << std::endl;
91       }
92    } 
93 }
94
95 /*
96  * \ingroup SQItem
97  * \brief   canonical Writer
98  */
99 void SQItem::Write(std::ofstream* fp, FileType filetype)
100 {
101    int j;
102    uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
103    uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
104
105     //we force the writting of an 'Item' Start Element
106     // because we want to write the Item as a 'no Length' item
107    for(j=0;j<4;++j)
108    {
109       binary_write( *fp, item[j]);  // fffe e000 ffff ffff 
110    }
111      
112    for (ListDocEntry::iterator i = DocEntries.begin();  
113                               i != DocEntries.end();
114                              ++i)
115    {   
116       // we skip delimitors (start and end one) because 
117       // we force them as 'no length'
118       if ( (*i)->GetGroup() == 0xfffe )
119       {
120          continue;
121       }
122
123       // Fix in order to make some MR PHILIPS images e-film readable
124       // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
125       // we just *always* ignore spurious fffe|0000 tag ! 
126       if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0x0000 )
127       {
128          break; // FIXME : continue; ?!?
129       }
130
131       (*i)->Write(fp, filetype);
132    }
133       
134     //we force the writting of an 'Item Delimitation' item
135     // because we wrote the Item as a 'no Length' item
136    for(j=0;j<4;++j)
137    {
138       binary_write( *fp, itemt[j]);  // fffe e000 ffff ffff 
139    }
140  
141 }
142
143 //-----------------------------------------------------------------------------
144 // Public
145 /**
146  * \brief   adds any Entry (Dicom Element) to the Sequence Item
147  */
148 bool SQItem::AddEntry(DocEntry* entry)
149 {
150    DocEntries.push_back(entry);
151    //TODO : check if it worked
152    return true;
153 }   
154
155 /**
156  * \brief   Sets Entry (Dicom Element) value of an element,
157  *          specified by it's tag (Group, Number) 
158  *          and the length, too ...  inside a SQ Item
159  *          If the Element is not found, it's just created !
160  * \warning we suppose, right now, the element belongs to a Public Group
161  *          (NOT a shadow one)       
162  * @param   val string value to set
163  * @param   group Group number of the searched tag.
164  * @param   element Element number of the searched tag.
165  * @return  true if element was found or created successfully
166  */
167
168 bool SQItem::SetEntryByNumber(std::string const & val, uint16_t group, 
169                               uint16_t element)
170 {
171    for(ListDocEntry::iterator i = DocEntries.begin(); 
172                               i != DocEntries.end(); 
173                             ++i)
174    { 
175       if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 ) 
176       {
177          continue;
178       }
179
180       if (  ( group   < (*i)->GetGroup() )
181           ||( group == (*i)->GetGroup() && element < (*i)->GetElement()) )
182       {
183          // instead of ReplaceOrCreateByNumber 
184          // that is a method of Document :-( 
185          ValEntry* entry = 0;
186          TagKey key = DictEntry::TranslateToKey(group, element);
187
188          if ( ! PtagHT->count(key))
189          {
190             // we assume a Public Dictionnary *is* loaded
191             Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
192             // if the invoked (group,elem) doesn't exist inside the Dictionary
193             // we create a VirtualDictEntry
194             DictEntry *dictEntry = pubDict->GetDictEntryByNumber(group, element);
195             if (dictEntry == NULL)
196             {
197                dictEntry = 
198                   Global::GetDicts()->NewVirtualDictEntry(group, element,
199                                                           "UN", "??", "??");
200             } 
201             // we assume the constructor didn't fail
202             entry = new ValEntry(dictEntry);
203             /// \todo
204             /// ----
205             /// better we don't assume too much !
206             /// SQItem is now used to describe any DICOMDIR related object
207          }
208          else
209          {
210             DocEntry* foundEntry = PtagHT->find(key)->second;
211             entry = dynamic_cast<ValEntry*>(foundEntry);
212             if (!entry)
213             {
214                dbg.Verbose(0, "SQItem::SetEntryByNumber: docEntries"
215                               " contains non ValEntry occurences");
216             }
217          }
218          if (entry)
219          {
220             entry->SetValue(val); 
221          }
222          entry->SetLength(val.length());
223          DocEntries.insert(i,entry);
224
225          return true;
226       }   
227       if (group == (*i)->GetGroup() && element == (*i)->GetElement() )
228       {
229          if ( ValEntry* entry = dynamic_cast<ValEntry*>(*i) )
230          {
231             entry->SetValue(val);
232          }
233          (*i)->SetLength(val.length()); 
234          return true;    
235       }
236    }
237    return false;
238 }
239 //-----------------------------------------------------------------------------
240 // Protected
241
242
243 /**
244  * \brief   Gets a Dicom Element inside a SQ Item Entry, by number
245  * @return
246  */
247 DocEntry* SQItem::GetDocEntryByNumber(uint16_t group, uint16_t element)
248 {
249    for(ListDocEntry::iterator i = DocEntries.begin();
250                               i != DocEntries.end(); ++i)
251    {
252       if ( (*i)->GetGroup() == group && (*i)->GetElement() == element )
253       {
254          return *i;
255       }
256    }
257    return 0;
258 }
259
260 /**
261  * \brief   Get the value of a Dicom Element inside a SQ Item Entry, by number
262  * @return
263  */ 
264
265 std::string SQItem::GetEntryByNumber(uint16_t group, uint16_t element)
266 {
267    for(ListDocEntry::iterator i = DocEntries.begin();
268                               i != DocEntries.end(); ++i)
269    {
270       if ( (*i)->GetGroup() == group && (*i)->GetElement() == element)
271       {
272          return ((ValEntry *)(*i))->GetValue();   //FIXME
273       }
274    }
275    return GDCM_UNFOUND;
276 }
277 //-----------------------------------------------------------------------------
278 // Private
279
280
281 //-----------------------------------------------------------------------------
282
283 } // end namespace gdcm