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