]> Creatis software - gdcm.git/blob - src/gdcmSQItem.cxx
* In order to fix memory leaks:
[gdcm.git] / src / gdcmSQItem.cxx
1 /*=========================================================================
2   
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSQItem.cxx,v $
5   Language:  C++
6   Date:      $Date: 2004/06/19 23:51:04 $
7   Version:   $Revision: 1.10 $
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 #include "gdcmDebug.h"
26
27 //-----------------------------------------------------------------------------
28 // Constructor / Destructor
29 /**
30  * \ingroup gdcmSQItem
31  * \brief   Constructor from a given gdcmSQItem
32  */
33 gdcmSQItem::gdcmSQItem(int depthLevel ) 
34           : gdcmDocEntrySet(depthLevel) {
35    SQDepthLevel = depthLevel +1;
36 }
37
38 /**
39  * \brief   Canonical destructor.
40  */
41 gdcmSQItem::~gdcmSQItem() 
42 {
43    for(ListDocEntry::iterator cc = docEntries.begin();
44        cc != docEntries.end();
45        ++cc)
46    {
47       delete (*cc);
48    }
49    docEntries.clear();
50 }
51
52 //-----------------------------------------------------------------------------
53 // Print
54 /*
55  * \brief   canonical Printer
56  */
57  void gdcmSQItem::Print(std::ostream & os) {
58    std::ostringstream s;
59
60    if (SQDepthLevel>0) {
61       for (int i=0;i<SQDepthLevel;i++)
62          s << "   | " ;
63    }
64         std::cout << s.str() << "SQItemNumber " << SQItemNumber  << std::endl;
65    for (ListDocEntry::iterator i = docEntries.begin();  
66         i != docEntries.end();
67         ++i)
68    {
69        os << s.str();
70       //(*i)->SetPrintLevel(printLevel); //self->GetPrintLevel() ?
71       (*i)->SetPrintLevel(2);
72       (*i)->Print(os);   
73    } 
74 }
75
76 //-----------------------------------------------------------------------------
77 // Public
78 /**
79  * \brief   adds any Entry (Dicom Element) to the Sequence Item
80  */
81 bool gdcmSQItem::AddEntry(gdcmDocEntry *entry)
82 {
83    docEntries.push_back(entry);
84    //TODO : check if it worked
85    return true;
86 }   
87
88 /**
89  * \brief   Sets Entry (Dicom Element) value of an element,
90  *          specified by it's tag (Group, Number) 
91  *          and the length, too ...  inside a SQ Item
92  *          If the Element is not found, it's just created !
93  * \warning we suppose, right now, the element belongs to a Public Group
94  *          (NOT a shadow one)       
95  * @param   val string value to set
96  * @param   group Group of the searched tag.
97  * @param   element Element of the searched tag.
98  * @return  true if element was found or created successfully
99  */
100 bool gdcmSQItem::SetEntryByNumber(std::string val,guint16 group, 
101                                   guint16 element)
102 {
103    for(ListDocEntry::iterator i=docEntries.begin();i!=docEntries.end();++i)
104    { 
105       if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 ) 
106          continue;
107       if (  ( group   < (*i)->GetGroup() )
108           ||( group == (*i)->GetGroup() && element < (*i)->GetElement()) )
109       {
110          // instead of ReplaceOrCreateByNumber 
111          // that is a method of gdcmDocument :-( 
112          gdcmValEntry* Entry = (gdcmValEntry*)0;
113          TagKey key = gdcmDictEntry::TranslateToKey(group, element);
114          if ( ! ptagHT->count(key))
115          {
116             // we assume a Public Dictionnary *is* loaded
117             gdcmDict *PubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
118             // if the invoked (group,elem) doesn't exist inside the Dictionary
119             // we create a VirtualDictEntry
120             gdcmDictEntry *DictEntry = PubDict->GetDictEntryByNumber(group,
121                                                                      element);
122             if (DictEntry == NULL)
123             {
124               DictEntry=gdcmGlobal::GetDicts()->NewVirtualDictEntry(group,
125                                                                     element,
126                                                                     "UN",
127                                                                     "??","??");
128             } 
129             // we assume the constructor didn't fail
130             Entry = new gdcmValEntry(DictEntry);
131             /// \todo
132             /// ----
133             /// better we don't assume too much !
134             /// gdcmSQItem is now used to describe any DICOMDIR related object
135          } else {
136             gdcmDocEntry* FoundEntry = ptagHT->find(key)->second;
137             Entry = dynamic_cast<gdcmValEntry*>(FoundEntry);
138             if (!Entry) 
139                dbg.Verbose(0, "gdcmSQItem::SetEntryByNumber: docEntries"
140                               " contains non gdcmValEntry occurences");
141          }
142          if (Entry)
143             Entry->SetValue(val); 
144          Entry->SetLength(val.length());
145          docEntries.insert(i,Entry); 
146          return true;
147       }
148       if (group == (*i)->GetGroup() && element == (*i)->GetElement() )
149       {
150          if ( gdcmValEntry* Entry = dynamic_cast<gdcmValEntry*>(*i) )
151             Entry->SetValue(val);
152          (*i)->SetLength(val.length()); 
153          return true;    
154       }
155    }
156 }
157 //-----------------------------------------------------------------------------
158 // Protected
159
160 //-----------------------------------------------------------------------------
161 // Private
162
163 // end-user intended : the guy *wants* to create his own SeQuence ?!?
164
165 /// \brief to be written if really usefull
166 gdcmDocEntry *gdcmSQItem::NewDocEntryByNumber(guint16 group,
167                                               guint16 element) {
168 /// \todo TODO                            
169    gdcmDocEntry *a;
170    std::cout << " gdcmSQItem::NewDocEntryByNumber : TODO" <<std::endl; 
171    return a;                              
172 }
173
174 /// \brief to be written if really usefull
175 gdcmDocEntry *gdcmSQItem::NewDocEntryByName  (std::string Name) {
176 /// \todo TODO                            
177    gdcmDocEntry *a;
178    std::cout << " gdcmSQItem::NewDocEntryByName : TODO" <<std::endl; 
179    return a;                                      
180 }
181
182 /**
183  * \brief   Gets a Dicom Element inside a SQ Item Entry, by name
184  * @return
185  */
186  gdcmDocEntry *gdcmSQItem::GetDocEntryByName(std::string name) {
187    gdcmDict *PubDict=gdcmGlobal::GetDicts()->GetDefaultPubDict();
188    gdcmDictEntry *dictEntry = (*PubDict).GetDictEntryByName(name);
189    if( dictEntry == NULL)
190       return NULL;
191    return GetDocEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement());      
192 }
193
194 /**
195  * \ingroup gdcmSQItem
196  * \brief   Gets a Dicom Element inside a SQ Item Entry, by number
197  * @return
198  */
199 gdcmDocEntry *gdcmSQItem::GetDocEntryByNumber(guint16 group, guint16 element) {                              
200    for(ListDocEntry::iterator i=docEntries.begin();i!=docEntries.end();++i) {
201       if ( (*i)->GetGroup()==group && (*i)->GetElement()==element)
202          return (*i);
203    }   
204    return NULL;                                           
205 }
206
207 /**
208  * \ingroup gdcmSQItem
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 /**
223  * \ingroup gdcmSQItem
224  * \brief   Get the value of a Dicom Element inside a SQ Item Entry, by name
225  * @param   name : name of the searched element.
226  * @return
227  */ 
228
229 std::string gdcmSQItem::GetEntryByName(TagName name)  {
230    gdcmDict *PubDict=gdcmGlobal::GetDicts()->GetDefaultPubDict();
231    gdcmDictEntry *dictEntry = (*PubDict).GetDictEntryByName(name); 
232
233    if( dictEntry == NULL)
234       return GDCM_UNFOUND;
235    return GetEntryByNumber(dictEntry->GetGroup(),dictEntry->GetElement()); 
236 }
237
238 //-----------------------------------------------------------------------------