]> Creatis software - gdcm.git/blob - src/gdcmSQItem.cxx
ENH : method gdcmValEntry::Write doesn't need the filetype parameter (onlu
[gdcm.git] / src / gdcmSQItem.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSQItem.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/06/22 14:37:04 $
7   Version:   $Revision: 1.13 $
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       for (int i=0;i<SQDepthLevel;i++)
64          s << "   | " ;
65    }
66    std::cout << s.str() << " --- SQItem number " << SQItemNumber  << std::endl;
67    for (ListDocEntry::iterator i = docEntries.begin();  
68         i != docEntries.end();
69         ++i)
70    {
71        os << s.str();
72       //(*i)->SetPrintLevel(printLevel); //self->GetPrintLevel() ?
73       (*i)->SetPrintLevel(2);
74       (*i)->Print(os);   
75    } 
76 }
77
78
79 /*
80  * \ingroup gdcmSQItem
81  * \brief   canonical Writer
82  */
83  void gdcmSQItem::Write(FILE *fp,FileType filetype) {
84    gdcmDocEntry *Entry;
85    for (ListDocEntry::iterator i = docEntries.begin();  
86         i != docEntries.end();
87         ++i)
88    {
89       Entry=*i;
90       (Entry)->WriteCommonPart(fp, filetype);
91
92       if (gdcmBinEntry* BinEntry = dynamic_cast< gdcmBinEntry* >(Entry) ) {
93          BinEntry->Write(fp,filetype);
94          return;
95       }
96       if (gdcmValEntry* ValEntry = dynamic_cast< gdcmValEntry* >(Entry) ) {
97          ValEntry->Write(fp);
98          return;
99       }
100       if (gdcmSeqEntry* SeqEntry = dynamic_cast< gdcmSeqEntry* >(Entry) ) {
101          SeqEntry->Write(fp,filetype);
102          return;
103       }
104    } 
105 }
106
107
108 //-----------------------------------------------------------------------------
109 // Public
110 /**
111  * \brief   adds any Entry (Dicom Element) to the Sequence Item
112  */
113 bool gdcmSQItem::AddEntry(gdcmDocEntry *entry)
114 {
115    docEntries.push_back(entry);
116    //TODO : check if it worked
117    return true;
118 }   
119
120 /**
121  * \brief   Sets Entry (Dicom Element) value of an element,
122  *          specified by it's tag (Group, Number) 
123  *          and the length, too ...  inside a SQ Item
124  *          If the Element is not found, it's just created !
125  * \warning we suppose, right now, the element belongs to a Public Group
126  *          (NOT a shadow one)       
127  * @param   val string value to set
128  * @param   group Group number of the searched tag.
129  * @param   element Element number of the searched tag.
130  * @return  true if element was found or created successfully
131  */
132
133 bool gdcmSQItem::SetEntryByNumber(std::string val,guint16 group, 
134                                   guint16 element)
135 {
136    for(ListDocEntry::iterator i=docEntries.begin();i!=docEntries.end();++i)
137    { 
138       if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 ) 
139          continue;
140
141       if (  ( group   < (*i)->GetGroup() )
142           ||( group == (*i)->GetGroup() && element < (*i)->GetElement()) )
143       {
144          // instead of ReplaceOrCreateByNumber 
145          // that is a method of gdcmDocument :-( 
146          gdcmValEntry* Entry = (gdcmValEntry*)0;
147          TagKey key = gdcmDictEntry::TranslateToKey(group, element);
148
149          if ( ! ptagHT->count(key))
150          {
151             // we assume a Public Dictionnary *is* loaded
152             gdcmDict *PubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
153             // if the invoked (group,elem) doesn't exist inside the Dictionary
154             // we create a VirtualDictEntry
155             gdcmDictEntry *DictEntry = PubDict->GetDictEntryByNumber(group,
156                                                                      element);
157             if (DictEntry == NULL)
158             {
159               DictEntry=gdcmGlobal::GetDicts()->NewVirtualDictEntry(group,
160                                                                     element,
161                                                                     "UN",
162                                                                     "??","??");
163             } 
164             // we assume the constructor didn't fail
165             Entry = new gdcmValEntry(DictEntry);
166             /// \todo
167             /// ----
168             /// better we don't assume too much !
169             /// gdcmSQItem is now used to describe any DICOMDIR related object
170          } else {
171             gdcmDocEntry* FoundEntry = ptagHT->find(key)->second;
172             Entry = dynamic_cast<gdcmValEntry*>(FoundEntry);
173             if (!Entry) 
174                dbg.Verbose(0, "gdcmSQItem::SetEntryByNumber: docEntries"
175                               " contains non gdcmValEntry occurences");
176          }
177          if (Entry)
178             Entry->SetValue(val); 
179          Entry->SetLength(val.length());
180          docEntries.insert(i,Entry); 
181          return true;
182       }   
183       if (group == (*i)->GetGroup() && element == (*i)->GetElement() )
184       {
185          if ( gdcmValEntry* Entry = dynamic_cast<gdcmValEntry*>(*i) )
186             Entry->SetValue(val);
187          (*i)->SetLength(val.length()); 
188          return true;    
189       }
190    }
191 }
192 //-----------------------------------------------------------------------------
193 // Protected
194
195
196 /**
197  * \brief   Gets a Dicom Element inside a SQ Item Entry, by number
198  * @return
199  */
200 gdcmDocEntry *gdcmSQItem::GetDocEntryByNumber(guint16 group, guint16 element) {
201    for(ListDocEntry::iterator i=docEntries.begin();i!=docEntries.end();++i) {
202       if ( (*i)->GetGroup()==group && (*i)->GetElement()==element)
203          return (*i);
204    }   
205    return NULL;
206 }
207
208 /**
209  * \brief   Get the value of a Dicom Element inside a SQ Item Entry, by number
210  * @return
211  */ 
212
213 std::string gdcmSQItem::GetEntryByNumber(guint16 group, guint16 element) { 
214    for(ListDocEntry::iterator i=docEntries.begin();i!=docEntries.end();++i) {
215       if ( (*i)->GetGroup()==group && (*i)->GetElement()==element) {
216          return ((gdcmValEntry *)(*i))->GetValue();
217       }
218    }   
219    return GDCM_UNFOUND;
220 }
221 //-----------------------------------------------------------------------------
222 // Private
223
224
225 //-----------------------------------------------------------------------------