]> Creatis software - gdcm.git/blob - src/gdcmSQItem.cxx
Jean-Pierre Roux
[gdcm.git] / src / gdcmSQItem.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSQItem.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/06/24 18:03:14 $
7   Version:   $Revision: 1.17 $
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.htm 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 "gdcmUtil.h"
25
26 #include "gdcmDebug.h"
27
28
29 //-----------------------------------------------------------------------------
30 // Constructor / Destructor
31 /**
32  * \ingroup gdcmSQItem
33  * \brief   Constructor from a given gdcmSQItem
34  */
35 gdcmSQItem::gdcmSQItem(int depthLevel ) 
36           : gdcmDocEntrySet(depthLevel) {
37    SQDepthLevel = depthLevel +1;
38 }
39
40 /**
41  * \brief   Canonical destructor.
42  */
43 gdcmSQItem::~gdcmSQItem() 
44 {
45    for(ListDocEntry::iterator cc = docEntries.begin();
46        cc != docEntries.end();
47        ++cc)
48    {
49       delete (*cc);
50    }
51    docEntries.clear();
52 }
53
54 //-----------------------------------------------------------------------------
55 // Print
56 /*
57  * \brief   canonical Printer
58  */
59  void gdcmSQItem::Print(std::ostream & os) {
60    std::ostringstream s;
61
62    if (SQDepthLevel>0)
63    {
64       for (int i=0;i<SQDepthLevel;i++)
65          s << "   | " ;
66    }
67    std::cout << s.str() << " --- SQItem number " << SQItemNumber  << std::endl;
68    for (ListDocEntry::iterator i  = docEntries.begin();  
69                                i != docEntries.end();
70                              ++i)
71    {
72       gdcmDocEntry* Entry = *i;
73       bool PrintEndLine = true;
74
75       os << s.str();
76       Entry->SetPrintLevel(2);
77       Entry->Print(os);   
78       if ( gdcmSeqEntry* SeqEntry = dynamic_cast<gdcmSeqEntry*>(Entry) )
79          PrintEndLine = false;
80       if (PrintEndLine)
81          os << std::endl;
82    } 
83 }
84
85 /*
86  * \ingroup gdcmSQItem
87  * \brief   canonical Writer
88  */
89 void gdcmSQItem::Write(FILE *fp,FileType filetype)
90 {
91    for (ListDocEntry::iterator i = docEntries.begin();  
92         i != docEntries.end();
93         ++i)
94    {
95       // Item Delimitor Item IS the last one of a 'no length' SQItem
96       // (when it exists) we don't write it right now
97       // It will be written outside, because ALL the SQItems are written
98       // as 'no length'
99       if ( (*i)->isItemDelimitor() )
100          break;
101       if ( ((*i)->GetGroup() == 0xfffe) && ((*i)->GetElement() == 0x0000) ) 
102         // Fix in order to make some MR PHILIPS images e-film readable
103         // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
104         // we just *always* ignore spurious fffe|0000 tag !   
105          return;                        
106       // It's up to the gdcmDocEntry Writter to write the SQItem begin element
107       // (fffe|e000) as a 'no length' one
108       (*i)->Write(fp, filetype);
109    } 
110 }
111
112 //-----------------------------------------------------------------------------
113 // Public
114 /**
115  * \brief   adds any Entry (Dicom Element) to the Sequence Item
116  */
117 bool gdcmSQItem::AddEntry(gdcmDocEntry *entry)
118 {
119    docEntries.push_back(entry);
120    //TODO : check if it worked
121    return true;
122 }   
123
124 /**
125  * \brief   Sets Entry (Dicom Element) value of an element,
126  *          specified by it's tag (Group, Number) 
127  *          and the length, too ...  inside a SQ Item
128  *          If the Element is not found, it's just created !
129  * \warning we suppose, right now, the element belongs to a Public Group
130  *          (NOT a shadow one)       
131  * @param   val string value to set
132  * @param   group Group number of the searched tag.
133  * @param   element Element number of the searched tag.
134  * @return  true if element was found or created successfully
135  */
136
137 bool gdcmSQItem::SetEntryByNumber(std::string val,guint16 group, 
138                                   guint16 element)
139 {
140    for(ListDocEntry::iterator i=docEntries.begin();i!=docEntries.end();++i)
141    { 
142       if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 ) 
143          continue;
144
145       if (  ( group   < (*i)->GetGroup() )
146           ||( group == (*i)->GetGroup() && element < (*i)->GetElement()) )
147       {
148          // instead of ReplaceOrCreateByNumber 
149          // that is a method of gdcmDocument :-( 
150          gdcmValEntry* Entry = (gdcmValEntry*)0;
151          TagKey key = gdcmDictEntry::TranslateToKey(group, element);
152
153          if ( ! ptagHT->count(key))
154          {
155             // we assume a Public Dictionnary *is* loaded
156             gdcmDict *PubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
157             // if the invoked (group,elem) doesn't exist inside the Dictionary
158             // we create a VirtualDictEntry
159             gdcmDictEntry *DictEntry = PubDict->GetDictEntryByNumber(group,
160                                                                      element);
161             if (DictEntry == NULL)
162             {
163               DictEntry=gdcmGlobal::GetDicts()->NewVirtualDictEntry(group,
164                                                                     element,
165                                                                     "UN",
166                                                                     "??","??");
167             } 
168             // we assume the constructor didn't fail
169             Entry = new gdcmValEntry(DictEntry);
170             /// \todo
171             /// ----
172             /// better we don't assume too much !
173             /// gdcmSQItem is now used to describe any DICOMDIR related object
174          } else {
175             gdcmDocEntry* FoundEntry = ptagHT->find(key)->second;
176             Entry = dynamic_cast<gdcmValEntry*>(FoundEntry);
177             if (!Entry) 
178                dbg.Verbose(0, "gdcmSQItem::SetEntryByNumber: docEntries"
179                               " contains non gdcmValEntry occurences");
180          }
181          if (Entry)
182             Entry->SetValue(val); 
183          Entry->SetLength(val.length());
184          docEntries.insert(i,Entry); 
185          return true;
186       }   
187       if (group == (*i)->GetGroup() && element == (*i)->GetElement() )
188       {
189          if ( gdcmValEntry* Entry = dynamic_cast<gdcmValEntry*>(*i) )
190             Entry->SetValue(val);
191          (*i)->SetLength(val.length()); 
192          return true;    
193       }
194    }
195    return false;
196 }
197 //-----------------------------------------------------------------------------
198 // Protected
199
200
201 /**
202  * \brief   Gets a Dicom Element inside a SQ Item Entry, by number
203  * @return
204  */
205 gdcmDocEntry *gdcmSQItem::GetDocEntryByNumber(guint16 group, guint16 element) {
206    for(ListDocEntry::iterator i=docEntries.begin();i!=docEntries.end();++i) {
207       if ( (*i)->GetGroup()==group && (*i)->GetElement()==element)
208          return (*i);
209    }   
210    return NULL;
211 }
212
213 /**
214  * \brief   Get the value of a Dicom Element inside a SQ Item Entry, by number
215  * @return
216  */ 
217
218 std::string gdcmSQItem::GetEntryByNumber(guint16 group, guint16 element) { 
219    for(ListDocEntry::iterator i=docEntries.begin();i!=docEntries.end();++i) {
220       if ( (*i)->GetGroup()==group && (*i)->GetElement()==element) {
221          return ((gdcmValEntry *)(*i))->GetValue();
222       }
223    }   
224    return GDCM_UNFOUND;
225 }
226 //-----------------------------------------------------------------------------
227 // Private
228
229
230 //-----------------------------------------------------------------------------