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