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