]> Creatis software - gdcm.git/blob - src/gdcmSQItem.cxx
* Remove memory leaks on the DicomDir
[gdcm.git] / src / gdcmSQItem.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSQItem.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/12/03 17:13:18 $
7   Version:   $Revision: 1.39 $
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    os << "S ";
64    std::ostringstream s;
65
66    if (SQDepthLevel > 0)
67    {
68       for (int i = 0; i < SQDepthLevel; ++i)
69       {
70          s << "   | " ;
71       }
72    }
73    //std::cout << s.str() << " --- SQItem number " << SQItemNumber  << std::endl;
74    for (ListDocEntry::iterator i  = DocEntries.begin();
75                                i != DocEntries.end();
76                              ++i)
77    {
78       DocEntry* Entry = *i;
79       bool PrintEndLine = true;
80
81       os << s.str();
82       Entry->SetPrintLevel(2);
83       Entry->Print(os); 
84       if ( SeqEntry* seqEntry = dynamic_cast<SeqEntry*>(Entry) )
85       {
86          (void)seqEntry;  //not used
87          PrintEndLine = false;
88       }
89       if (PrintEndLine)
90       {
91          os << std::endl;
92       }
93    } 
94 }
95
96 /*
97  * \ingroup SQItem
98  * \brief   canonical Writer
99  */
100 void SQItem::WriteContent(std::ofstream* fp, FileType filetype)
101 {
102    int j;
103    uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
104    uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
105
106     //we force the writting of an 'Item' Start Element
107     // because we want to write the Item as a 'no Length' item
108    for(j=0;j<4;++j)
109    {
110       binary_write( *fp, item[j]);  // fffe e000 ffff ffff 
111    }
112      
113    for (ListDocEntry::iterator it = DocEntries.begin();  
114                               it != DocEntries.end();
115                              ++it)
116    {   
117       // we skip delimitors (start and end one) because 
118       // we force them as 'no length'
119       if ( (*it)->GetGroup() == 0xfffe )
120       {
121          continue;
122       }
123
124       // Fix in order to make some MR PHILIPS images e-film readable
125       // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
126       // we just *always* ignore spurious fffe|0000 tag ! 
127       if ( (*it)->GetGroup() == 0xfffe && (*it)->GetElement() == 0x0000 )
128       {
129          break; // FIXME : continue; ?!?
130       }
131
132       (*it)->WriteContent(fp, filetype);
133    }
134       
135     //we force the writting of an 'Item Delimitation' item
136     // because we wrote the Item as a 'no Length' item
137    for(j=0;j<4;++j)
138    {
139       binary_write( *fp, itemt[j]);  // fffe e000 ffff ffff 
140    }
141  
142 }
143
144 //-----------------------------------------------------------------------------
145 // Public
146 /**
147  * \brief   adds any Entry (Dicom Element) to the Sequence Item
148  */
149 bool SQItem::AddEntry(DocEntry* entry)
150 {
151    DocEntries.push_back(entry);
152    //TODO : check if it worked
153    return true;
154 }   
155
156 /**
157  * \brief   Sets Entry (Dicom Element) value of an element,
158  *          specified by it's tag (Group, Number) 
159  *          and the length, too ...  inside a SQ Item
160  *          If the Element is not found, it's just created !
161  * \warning we suppose, right now, the element belongs to a Public Group
162  *          (NOT a shadow one)       
163  * @param   val string value to set
164  * @param   group Group number of the searched tag.
165  * @param   element Element number of the searched tag.
166  * @return  true if element was found or created successfully
167  */
168
169 bool SQItem::SetEntryByNumber(std::string const & val, uint16_t group, 
170                               uint16_t element)
171 {
172    for(ListDocEntry::iterator i = DocEntries.begin(); 
173                               i != DocEntries.end(); 
174                             ++i)
175    { 
176       if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 ) 
177       {
178          continue;
179       }
180
181       if (  ( group   < (*i)->GetGroup() )
182           ||( group == (*i)->GetGroup() && element < (*i)->GetElement()) )
183       {
184          // instead of ReplaceOrCreateByNumber 
185          // that is a method of Document :-( 
186          ValEntry* entry = 0;
187          TagKey key = DictEntry::TranslateToKey(group, element);
188
189          if ( ! PtagHT->count(key))
190          {
191             // we assume a Public Dictionnary *is* loaded
192             Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
193             // if the invoked (group,elem) doesn't exist inside the Dictionary
194             // we create a VirtualDictEntry
195             DictEntry *dictEntry = pubDict->GetDictEntryByNumber(group, element);
196             if (dictEntry == NULL)
197             {
198                dictEntry = 
199                   Global::GetDicts()->NewVirtualDictEntry(group, element,
200                                                           "UN", "??", "??");
201             } 
202             // we assume the constructor didn't fail
203             entry = new ValEntry(dictEntry);
204             /// \todo
205             /// ----
206             /// better we don't assume too much !
207             /// SQItem is now used to describe any DICOMDIR related object
208          }
209          else
210          {
211             DocEntry* foundEntry = PtagHT->find(key)->second;
212             entry = dynamic_cast<ValEntry*>(foundEntry);
213             if (!entry)
214             {
215                dbg.Verbose(0, "SQItem::SetEntryByNumber: docEntries"
216                               " contains non ValEntry occurences");
217             }
218          }
219          if (entry)
220          {
221             entry->SetValue(val); 
222          }
223          entry->SetLength(val.length());
224          DocEntries.insert(i,entry);
225
226          return true;
227       }   
228       if (group == (*i)->GetGroup() && element == (*i)->GetElement() )
229       {
230          if ( ValEntry* entry = dynamic_cast<ValEntry*>(*i) )
231          {
232             entry->SetValue(val);
233          }
234          (*i)->SetLength(val.length()); 
235          return true;    
236       }
237    }
238    return false;
239 }
240
241 /**
242  * \brief   Clear the hash table from given entry AND delete the entry.
243  * @param   entryToRemove Entry to remove AND delete.
244  * \warning Some problems when using under Windows... prefer the use of
245  *          Initialize / GetNext methods
246  */
247 bool SQItem::RemoveEntry( DocEntry* entryToRemove)
248 {
249    for(ListDocEntry::iterator it = DocEntries.begin();
250        it != DocEntries.end();
251        ++it)
252    {
253       if( *it == entryToRemove)
254       {
255          DocEntries.erase(it);
256          dbg.Verbose(0, "SQItem::RemoveEntry: one element erased.");
257          delete entryToRemove;
258          return true;
259       }
260    }
261                                                                                 
262    dbg.Verbose(0, "SQItem::RemoveEntry: value not present ");
263    return false ;
264 }
265                                                                                 
266 /**
267  * \brief   Clear the hash table from given entry BUT keep the entry.
268  * @param   entryToRemove Entry to remove.
269  */
270 bool SQItem::RemoveEntryNoDestroy(DocEntry* entryToRemove)
271 {
272    for(ListDocEntry::iterator it = DocEntries.begin();
273        it != DocEntries.end();
274        ++it)
275    {
276       if( *it == entryToRemove)
277       {
278          DocEntries.erase(it);
279          dbg.Verbose(0, "SQItem::RemoveEntry: one element erased.");
280          return true;
281       }
282    }
283                                                                                 
284    dbg.Verbose(0, "SQItem::RemoveEntry: value not present ");
285    return false ;
286 }
287                                                                                 
288 /**
289  * \brief   Initialise the visit of the chained list
290  */
291 void SQItem::Initialize()
292 {
293    ItDocEntries = DocEntries.begin();
294 }
295                                                                                 
296 /**
297  * \brief   Get the next entry whil visiting the chained list
298  * \return  The next DocEntry if found, otherwhise NULL
299  */
300 DocEntry *SQItem::GetNextEntry()
301 {
302    if (ItDocEntries != DocEntries.end())
303    {
304       DocEntry *tmp = *ItDocEntries;
305       ++ItDocEntries;
306                                                                                 
307       return(tmp);
308    }
309    else
310    {
311       return(NULL);
312    }
313 }
314
315 //-----------------------------------------------------------------------------
316 // Protected
317 /**
318  * \brief   Gets a Dicom Element inside a SQ Item Entry, by number
319  * @return
320  */
321 DocEntry* SQItem::GetDocEntryByNumber(uint16_t group, uint16_t element)
322 {
323    for(ListDocEntry::iterator i = DocEntries.begin();
324                               i != DocEntries.end(); ++i)
325    {
326       if ( (*i)->GetGroup() == group && (*i)->GetElement() == element )
327       {
328          return *i;
329       }
330    }
331    return 0;
332 }
333
334 /**
335  * \brief   Get the value of a Dicom Element inside a SQ Item Entry, by number
336  * @return
337  */ 
338
339 std::string SQItem::GetEntryByNumber(uint16_t group, uint16_t element)
340 {
341    for(ListDocEntry::iterator i = DocEntries.begin();
342                               i != DocEntries.end(); ++i)
343    {
344       if ( (*i)->GetGroup() == group && (*i)->GetElement() == element)
345       {
346          return ((ValEntry *)(*i))->GetValue();   //FIXME
347       }
348    }
349    return GDCM_UNFOUND;
350 }
351 //-----------------------------------------------------------------------------
352 // Private
353
354
355 //-----------------------------------------------------------------------------
356
357 } // end namespace gdcm