]> Creatis software - gdcm.git/blob - src/gdcmSQItem.cxx
* Minor coding-style clean up
[gdcm.git] / src / gdcmSQItem.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSQItem.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/18 08:35:50 $
7   Version:   $Revision: 1.76 $
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       return true;
114    }
115  
116    ListDocEntry::iterator insertSpot;
117    ListDocEntry::iterator it = DocEntries.end();
118    do
119    {
120       it--;
121
122       if ( (*it)->IsItemDelimitor() )
123       {
124          continue;
125       }
126       if ( (*it)->GetGroup() < entry->GetGroup() )
127          break;
128       else
129          if ( (*it)->GetGroup() == entry->GetGroup() &&
130               (*it)->GetElement() < entry->GetElement() )
131             break;
132    } while (it != DocEntries.begin() );
133   
134    insertSpot = it++;
135    insertSpot++; // ?!?
136    DocEntries.insert(insertSpot, entry); 
137    return true;
138 }   
139
140 /**
141  * \brief   Clear the std::list from given entry AND delete the entry.
142  * @param   entryToRemove Entry to remove AND delete.
143  * @return true if the entry was found and removed; false otherwise
144  */
145 bool SQItem::RemoveEntry( DocEntry *entryToRemove )
146 {
147    for(ListDocEntry::iterator it = DocEntries.begin();
148                               it != DocEntries.end();
149                             ++it)
150    {
151       if ( *it == entryToRemove )
152       {
153          DocEntries.erase(it);
154          gdcmWarningMacro( "One element erased: " << entryToRemove->GetKey() );
155          delete entryToRemove;
156          return true;
157       }
158    }
159    gdcmWarningMacro( "Entry not found: " << entryToRemove->GetKey() );
160    return false ;
161 }
162
163 /**
164  * \brief   Clear the std::list from given entry BUT keep the entry.
165  * @param   entryToRemove Entry to remove.
166  * @return true if the entry was found and removed; false otherwise
167  */
168 bool SQItem::RemoveEntryNoDestroy(DocEntry *entryToRemove)
169 {
170    for(ListDocEntry::iterator it =  DocEntries.begin();
171                               it != DocEntries.end();
172                             ++it)
173    {
174       if ( *it == entryToRemove )
175       {
176          DocEntries.erase(it);
177          gdcmWarningMacro( "One element removed, no destroyed: "
178                             << entryToRemove->GetKey() );
179          return true;
180       }
181    }
182                                                                                 
183    gdcmWarningMacro( "Entry not found:" << entryToRemove->GetKey() );
184    return false ;
185 }
186                                                                                 
187 /**
188  * \brief  Remove all entry in the Sequence Item 
189  */
190 void SQItem::ClearEntry()
191 {
192    for(ListDocEntry::iterator cc = DocEntries.begin();
193                               cc != DocEntries.end();
194                             ++cc)
195    {
196       delete *cc;
197    }
198    DocEntries.clear();
199 }
200
201 /**
202  * \brief  Clear the std::list from given Sequence Item  BUT keep the entries
203  */
204 void SQItem::ClearEntryNoDestroy()
205 {
206    DocEntries.clear();
207 }
208
209
210 /**
211  * \brief  Move all the entries from a given Sequence Item 
212  */
213 void SQItem::MoveObject(SQItem *source)
214 {
215    DocEntries = source->DocEntries;
216    source->ClearEntryNoDestroy();
217 }
218
219 /**
220  * \brief   Get the first Dicom entry while visiting the SQItem
221  * \return  The first DocEntry if found, otherwhise 0
222  */
223 DocEntry *SQItem::GetFirstEntry()
224 {
225    ItDocEntries = DocEntries.begin();
226    if ( ItDocEntries != DocEntries.end() )
227       return *ItDocEntries;
228    return 0;   
229 }
230                                                                                 
231 /**
232  * \brief   Get the next Dicom entry while visiting the SQItem
233  * \return  The next DocEntry if found, otherwhise NULL
234  */
235 DocEntry *SQItem::GetNextEntry()
236 {
237    ++ItDocEntries;
238    if ( ItDocEntries != DocEntries.end() )
239       return  *ItDocEntries;
240    return NULL;
241 }
242
243 /**
244  * \brief   Gets a Dicom Element inside a SQ Item Entry
245  * @param   group   Group number of the Entry
246  * @param   elem  Element number of the Entry
247  * @return Entry whose (group,elem) was passed. 0 if not found
248  */
249 DocEntry *SQItem::GetDocEntry(uint16_t group, uint16_t elem)
250 {
251    for(ListDocEntry::iterator i =  DocEntries.begin();
252                               i != DocEntries.end(); 
253                             ++i)
254    {
255       if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
256          return *i;
257    }
258    return NULL;
259 }
260
261 //-----------------------------------------------------------------------------
262 // Protected
263
264 //-----------------------------------------------------------------------------
265 // Private
266
267 //-----------------------------------------------------------------------------
268 // Print
269 /*
270  * \brief   canonical Printer
271  * @param os     Stream to print to. 
272  * @param indent Indentation string to be prepended during printing.
273  */
274 void SQItem::Print(std::ostream &os, std::string const &)
275 {
276    std::ostringstream s;
277
278    if (SQDepthLevel > 0)
279    {
280       for (int i = 0; i < SQDepthLevel; ++i)
281       {
282          s << "   | " ;
283       }
284    }
285    os << s.str() << " --- SQItem number " << SQItemNumber  << std::endl;
286    for (ListDocEntry::iterator i  = DocEntries.begin();
287                                i != DocEntries.end();
288                              ++i)
289    {
290       DocEntry *Entry = *i;
291       bool PrintEndLine = true;
292
293       os << s.str();
294       Entry->SetPrintLevel(PrintLevel);
295       Entry->Print(os); 
296       if ( dynamic_cast<SeqEntry*>(Entry) )
297       {
298          PrintEndLine = false;
299       }
300       if (PrintEndLine)
301       {
302          os << std::endl;
303       }
304    } 
305 }
306
307 //-----------------------------------------------------------------------------
308 } // end namespace gdcm