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