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