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