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