]> Creatis software - gdcm.git/blob - src/gdcmSQItem.cxx
Messages
[gdcm.git] / src / gdcmSQItem.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSQItem.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/27 09:52:33 $
7   Version:   $Revision: 1.78 $
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 "gdcmGlobal.h"
22 #include "gdcmDictSet.h"
23 #include "gdcmUtil.h"
24 #include "gdcmDebug.h"
25
26 #include <fstream>
27
28 namespace gdcm 
29 {
30 //-----------------------------------------------------------------------------
31 // Constructor / Destructor
32 /**
33  * \brief   Constructor from a given SQItem
34  */
35 SQItem::SQItem(int depthLevel ) 
36           : DocEntrySet( )
37 {
38    SQDepthLevel = depthLevel;
39    SQItemNumber = 0;
40 }
41
42 /**
43  * \brief   Canonical destructor.
44  */
45 SQItem::~SQItem() 
46 {
47    ClearEntry();
48 }
49
50 //-----------------------------------------------------------------------------
51 // Public
52 /*
53  * \brief   canonical Writer
54  * @param fp     file pointer to an already open file. 
55  * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
56  */
57 void SQItem::WriteContent(std::ofstream *fp, FileType filetype)
58 {
59    int j;
60    uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
61    uint16_t itemt[4]= { 0xfffe, 0xe00d, 0xffff, 0xffff };
62
63     //we force the writting of an 'Item' Start Element
64     // because we want to write the Item as a 'no Length' item
65    for(j=0;j<4;++j)
66    {
67       binary_write( *fp, item[j]);  // fffe e000 ffff ffff 
68    }
69      
70    for (ListDocEntry::iterator it = DocEntries.begin();  
71                                it != DocEntries.end();
72                              ++it)
73    {   
74       // we skip delimitors (start and end one) because 
75       // we force them as 'no length'
76       if ( (*it)->GetGroup() == 0xfffe )
77       {
78          continue;
79       }
80
81       // Fix in order to make some MR PHILIPS images e-film readable
82       // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
83       // we just *always* ignore spurious fffe|0000 tag ! 
84       if ( (*it)->GetGroup() == 0xfffe && (*it)->GetElement() == 0x0000 )
85       {
86           break; // FIXME : break or continue; ?!?  
87                  // --> makes no difference since the only bugged file we have
88                  // contains 'impossible tag' fffe|0000 in last position !                            
89       }
90
91       (*it)->WriteContent(fp, filetype);
92    }
93       
94     //we force the writting of an 'Item Delimitation' item
95     // because we wrote the Item as a 'no Length' item
96    for(j=0;j<4;++j)
97    {
98       binary_write( *fp, itemt[j]);  // fffe e000 ffff ffff 
99    } 
100 }
101
102 /**
103  * \brief   Inserts *in the right place* any Entry (Dicom Element)
104  *          into the Sequence Item
105  * @param entry Entry to add
106  * @return always true 
107  */
108 bool SQItem::AddEntry(DocEntry *entry)
109 {   
110    if (DocEntries.empty() )
111    {
112       DocEntries.push_back(entry);
113       entry->Register();
114       return true;
115    }
116  
117    ListDocEntry::iterator insertSpot;
118    ListDocEntry::iterator it = DocEntries.end();
119    do
120    {
121       it--;
122
123       if ( (*it)->IsItemDelimitor() )
124       {
125          continue;
126       }
127       if ( (*it)->GetGroup() < entry->GetGroup() )
128          break;
129       else
130          if ( (*it)->GetGroup() == entry->GetGroup() &&
131               (*it)->GetElement() < entry->GetElement() )
132             break;
133    } while (it != DocEntries.begin() );
134   
135    ++it;
136    insertSpot = it;
137    //++insertSpot; // ?!?
138    DocEntries.insert(insertSpot, entry); 
139    entry->Register();
140    return true;
141 }   
142
143 /**
144  * \brief   Clear the std::list from given entry AND delete the entry.
145  * @param   entryToRemove Entry to remove AND delete.
146  * @return true if the entry was found and removed; false otherwise
147  */
148 bool SQItem::RemoveEntry( DocEntry *entryToRemove )
149 {
150    for(ListDocEntry::iterator it = DocEntries.begin();
151                               it != DocEntries.end();
152                             ++it)
153    {
154       if ( *it == entryToRemove )
155       {
156          DocEntries.erase(it);
157          gdcmDebugMacro( "One element erased: " << entryToRemove->GetKey() );
158          entryToRemove->Unregister();
159          return true;
160       }
161    }
162    gdcmWarningMacro( "Entry not found: " << entryToRemove->GetKey() );
163    return false ;
164 }
165
166 /**
167  * \brief  Remove all entry in the Sequence Item 
168  */
169 void SQItem::ClearEntry()
170 {
171    for(ListDocEntry::iterator cc = DocEntries.begin();
172                               cc != DocEntries.end();
173                             ++cc)
174    {
175       (*cc)->Unregister();
176    }
177    DocEntries.clear();
178 }
179
180 /**
181  * \brief  Move all the entries from a given Sequence Item 
182  */
183 void SQItem::MoveObject(SQItem *source)
184 {
185    DocEntries = source->DocEntries;
186    source->DocEntries.clear();
187 }
188
189 /**
190  * \brief   Get the first Dicom entry while visiting the SQItem
191  * \return  The first DocEntry if found, otherwhise 0
192  */
193 DocEntry *SQItem::GetFirstEntry()
194 {
195    ItDocEntries = DocEntries.begin();
196    if ( ItDocEntries != DocEntries.end() )
197       return *ItDocEntries;
198    return 0;   
199 }
200                                                                                 
201 /**
202  * \brief   Get the next Dicom entry while visiting the SQItem
203  * \return  The next DocEntry if found, otherwhise NULL
204  */
205 DocEntry *SQItem::GetNextEntry()
206 {
207    ++ItDocEntries;
208    if ( ItDocEntries != DocEntries.end() )
209       return  *ItDocEntries;
210    return NULL;
211 }
212
213 /**
214  * \brief   Gets a Dicom Element inside a SQ Item Entry
215  * @param   group   Group number of the Entry
216  * @param   elem  Element number of the Entry
217  * @return Entry whose (group,elem) was passed. 0 if not found
218  */
219 DocEntry *SQItem::GetDocEntry(uint16_t group, uint16_t elem)
220 {
221    for(ListDocEntry::iterator i =  DocEntries.begin();
222                               i != DocEntries.end(); 
223                             ++i)
224    {
225       if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
226          return *i;
227    }
228    return NULL;
229 }
230
231 //-----------------------------------------------------------------------------
232 // Protected
233
234 //-----------------------------------------------------------------------------
235 // Private
236
237 //-----------------------------------------------------------------------------
238 // Print
239 /*
240  * \brief   canonical Printer
241  * @param os     Stream to print to. 
242  * @param indent Indentation string to be prepended during printing.
243  */
244 void SQItem::Print(std::ostream &os, std::string const &)
245 {
246    std::ostringstream s;
247
248    if (SQDepthLevel > 0)
249    {
250       for (int i = 0; i < SQDepthLevel; ++i)
251       {
252          s << "   | " ;
253       }
254    }
255    os << s.str() << " --- SQItem number " << SQItemNumber  << std::endl;
256    for (ListDocEntry::iterator i  = DocEntries.begin();
257                                i != DocEntries.end();
258                              ++i)
259    {
260       DocEntry *Entry = *i;
261       bool PrintEndLine = true;
262
263       os << s.str();
264       Entry->SetPrintLevel(PrintLevel);
265       Entry->Print(os); 
266       if ( dynamic_cast<SeqEntry*>(Entry) )
267       {
268          PrintEndLine = false;
269       }
270       if (PrintEndLine)
271       {
272          os << std::endl;
273       }
274    } 
275 }
276
277 //-----------------------------------------------------------------------------
278 } // end namespace gdcm