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