]> Creatis software - gdcm.git/blob - src/gdcmSQItem.cxx
* Example/Volume2Dicom.cxx : comment unused variables
[gdcm.git] / src / gdcmSQItem.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSQItem.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/12/06 11:37:38 $
7   Version:   $Revision: 1.41 $
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 "gdcmDictSet.h"
25 #include "gdcmUtil.h"
26 #include "gdcmDebug.h"
27
28 #include <fstream>
29
30 namespace gdcm 
31 {
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
34 /**
35  * \ingroup SQItem
36  * \brief   Constructor from a given SQItem
37  */
38 SQItem::SQItem(int depthLevel ) 
39           : DocEntrySet( )
40 {
41    SQDepthLevel = depthLevel;
42 }
43
44 /**
45  * \brief   Canonical destructor.
46  */
47 SQItem::~SQItem() 
48 {
49    for(ListDocEntry::iterator cc = DocEntries.begin();
50                              cc != DocEntries.end();
51                              ++cc)
52    {
53       delete *cc;
54    }
55    DocEntries.clear();
56 }
57
58 //-----------------------------------------------------------------------------
59 // Print
60 /*
61  * \brief   canonical Printer
62  */
63 void SQItem::Print(std::ostream& os)
64 {
65    std::ostringstream s;
66
67    if (SQDepthLevel > 0)
68    {
69       for (int i = 0; i < SQDepthLevel; ++i)
70       {
71          s << "   | " ;
72       }
73    }
74    //std::cout << s.str() << " --- SQItem number " << SQItemNumber  << std::endl;
75    for (ListDocEntry::iterator i  = DocEntries.begin();
76                                i != DocEntries.end();
77                              ++i)
78    {
79       DocEntry* Entry = *i;
80       bool PrintEndLine = true;
81
82       os << s.str();
83       Entry->SetPrintLevel(2);
84       Entry->Print(os); 
85       if ( SeqEntry* seqEntry = dynamic_cast<SeqEntry*>(Entry) )
86       {
87          (void)seqEntry;  //not used
88          PrintEndLine = false;
89       }
90       if (PrintEndLine)
91       {
92          os << std::endl;
93       }
94    } 
95 }
96
97 /*
98  * \ingroup SQItem
99  * \brief   canonical Writer
100  */
101 void SQItem::WriteContent(std::ofstream* fp, FileType filetype)
102 {
103    int j;
104    uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
105    uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
106
107     //we force the writting of an 'Item' Start Element
108     // because we want to write the Item as a 'no Length' item
109    for(j=0;j<4;++j)
110    {
111       binary_write( *fp, item[j]);  // fffe e000 ffff ffff 
112    }
113      
114    for (ListDocEntry::iterator it = DocEntries.begin();  
115                               it != DocEntries.end();
116                              ++it)
117    {   
118       // we skip delimitors (start and end one) because 
119       // we force them as 'no length'
120       if ( (*it)->GetGroup() == 0xfffe )
121       {
122          continue;
123       }
124
125       // Fix in order to make some MR PHILIPS images e-film readable
126       // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
127       // we just *always* ignore spurious fffe|0000 tag ! 
128       if ( (*it)->GetGroup() == 0xfffe && (*it)->GetElement() == 0x0000 )
129       {
130          break; // FIXME : continue; ?!?
131       }
132
133       (*it)->WriteContent(fp, filetype);
134    }
135       
136     //we force the writting of an 'Item Delimitation' item
137     // because we wrote the Item as a 'no Length' item
138    for(j=0;j<4;++j)
139    {
140       binary_write( *fp, itemt[j]);  // fffe e000 ffff ffff 
141    }
142  
143 }
144
145 //-----------------------------------------------------------------------------
146 // Public
147 /**
148  * \brief   adds any Entry (Dicom Element) to the Sequence Item
149  */
150 bool SQItem::AddEntry(DocEntry* entry)
151 {
152    DocEntries.push_back(entry);
153    //TODO : check if it worked
154    return true;
155 }   
156
157 /**
158  * \brief   Sets Entry (Dicom Element) value of an element,
159  *          specified by it's tag (Group, Number) 
160  *          and the length, too ...  inside a SQ Item
161  *          If the Element is not found, it's just created !
162  * \warning we suppose, right now, the element belongs to a Public Group
163  *          (NOT a shadow one)       
164  * @param   val string value to set
165  * @param   group Group number of the searched tag.
166  * @param   element Element number of the searched tag.
167  * @return  true if element was found or created successfully
168  */
169
170 bool SQItem::SetEntryByNumber(std::string const & val, uint16_t group, 
171                               uint16_t element)
172 {
173    for(ListDocEntry::iterator i = DocEntries.begin(); 
174                               i != DocEntries.end(); 
175                             ++i)
176    { 
177       if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 ) 
178       {
179          continue;
180       }
181
182       if (  ( group  < (*i)->GetGroup() )
183           ||( group == (*i)->GetGroup() && element < (*i)->GetElement()) )
184       {
185          // instead of ReplaceOrCreateByNumber 
186          // that is a method of Document :-( 
187          ValEntry* entry = 0;
188          TagKey key = DictEntry::TranslateToKey(group, element);
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
204          if (entry)
205          {
206             entry->SetValue(val); 
207          }
208          entry->SetLength(val.length());
209          DocEntries.insert(i,entry);
210
211          return true;
212       }   
213       if (group == (*i)->GetGroup() && element == (*i)->GetElement() )
214       {
215          if ( ValEntry* entry = dynamic_cast<ValEntry*>(*i) )
216          {
217             entry->SetValue(val);
218          }
219          (*i)->SetLength(val.length()); 
220          return true;    
221       }
222    }
223    return false;
224 }
225
226 /**
227  * \brief   Clear the hash table from given entry AND delete the entry.
228  * @param   entryToRemove Entry to remove AND delete.
229  * \warning Some problems when using under Windows... prefer the use of
230  *          Initialize / GetNext methods
231  */
232 bool SQItem::RemoveEntry( DocEntry* entryToRemove)
233 {
234    for(ListDocEntry::iterator it = DocEntries.begin();
235        it != DocEntries.end();
236        ++it)
237    {
238       if( *it == entryToRemove)
239       {
240          DocEntries.erase(it);
241          dbg.Verbose(0, "SQItem::RemoveEntry: one element erased.");
242          delete entryToRemove;
243          return true;
244       }
245    }
246                                                                                 
247    dbg.Verbose(0, "SQItem::RemoveEntry: value not present ");
248    return false ;
249 }
250                                                                                 
251 /**
252  * \brief   Clear the hash table from given entry BUT keep the entry.
253  * @param   entryToRemove Entry to remove.
254  */
255 bool SQItem::RemoveEntryNoDestroy(DocEntry* entryToRemove)
256 {
257    for(ListDocEntry::iterator it = DocEntries.begin();
258        it != DocEntries.end();
259        ++it)
260    {
261       if( *it == entryToRemove)
262       {
263          DocEntries.erase(it);
264          dbg.Verbose(0, "SQItem::RemoveEntry: one element erased.");
265          return true;
266       }
267    }
268                                                                                 
269    dbg.Verbose(0, "SQItem::RemoveEntry: value not present ");
270    return false ;
271 }
272                                                                                 
273 /**
274  * \brief   Initialise the visit of the chained list
275  */
276 void SQItem::Initialize()
277 {
278    ItDocEntries = DocEntries.begin();
279 }
280                                                                                 
281 /**
282  * \brief   Get the next entry whil visiting the chained list
283  * \return  The next DocEntry if found, otherwhise NULL
284  */
285 DocEntry *SQItem::GetNextEntry()
286 {
287    if (ItDocEntries != DocEntries.end())
288    {
289       DocEntry *tmp = *ItDocEntries;
290       ++ItDocEntries;
291                                                                                 
292       return(tmp);
293    }
294    else
295    {
296       return(NULL);
297    }
298 }
299
300 //-----------------------------------------------------------------------------
301 // Protected
302 /**
303  * \brief   Gets a Dicom Element inside a SQ Item Entry, by number
304  * @return
305  */
306 DocEntry* SQItem::GetDocEntryByNumber(uint16_t group, uint16_t element)
307 {
308    for(ListDocEntry::iterator i = DocEntries.begin();
309                               i != DocEntries.end(); ++i)
310    {
311       if ( (*i)->GetGroup() == group && (*i)->GetElement() == element )
312       {
313          return *i;
314       }
315    }
316    return 0;
317 }
318
319 /**
320  * \brief   Get the value of a Dicom Element inside a SQ Item Entry, by number
321  * @return
322  */ 
323
324 std::string SQItem::GetEntryByNumber(uint16_t group, uint16_t element)
325 {
326    for(ListDocEntry::iterator i = DocEntries.begin();
327                               i != DocEntries.end(); ++i)
328    {
329       if ( (*i)->GetGroup() == group && (*i)->GetElement() == element)
330       {
331          return ((ValEntry *)(*i))->GetValue();   //FIXME
332       }
333    }
334    return GDCM_UNFOUND;
335 }
336 //-----------------------------------------------------------------------------
337 // Private
338
339
340 //-----------------------------------------------------------------------------
341
342 } // end namespace gdcm