1 /*=========================================================================
4 Module: $RCSfile: gdcmSQItem.cxx,v $
6 Date: $Date: 2004/06/22 13:47:33 $
7 Version: $Revision: 1.11 $
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.
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.
17 =========================================================================*/
19 #include "gdcmSQItem.h"
20 #include "gdcmSeqEntry.h"
21 #include "gdcmValEntry.h"
22 #include "gdcmBinEntry.h"
23 #include "gdcmGlobal.h"
26 #include "gdcmDebug.h"
29 //-----------------------------------------------------------------------------
30 // Constructor / Destructor
33 * \brief Constructor from a given gdcmSQItem
35 gdcmSQItem::gdcmSQItem(int depthLevel )
36 : gdcmDocEntrySet(depthLevel) {
37 SQDepthLevel = depthLevel +1;
41 * \brief Canonical destructor.
43 gdcmSQItem::~gdcmSQItem()
45 for(ListDocEntry::iterator cc = docEntries.begin();
46 cc != docEntries.end();
54 //-----------------------------------------------------------------------------
57 * \brief canonical Printer
59 void gdcmSQItem::Print(std::ostream & os) {
63 for (int i=0;i<SQDepthLevel;i++)
66 std::cout << s.str() << " --- SQItem number " << SQItemNumber << std::endl;
67 for (ListDocEntry::iterator i = docEntries.begin();
68 i != docEntries.end();
72 //(*i)->SetPrintLevel(printLevel); //self->GetPrintLevel() ?
73 (*i)->SetPrintLevel(2);
81 * \brief canonical Writer
83 void gdcmSQItem::Write(FILE *fp,FileType filetype) {
85 for (ListDocEntry::iterator i = docEntries.begin();
86 i != docEntries.end();
90 (Entry)->WriteCommonPart(fp, filetype);
92 if (gdcmBinEntry* BinEntry = dynamic_cast< gdcmBinEntry* >(Entry) ) {
93 BinEntry->Write(fp,filetype);
96 if (gdcmValEntry* ValEntry = dynamic_cast< gdcmValEntry* >(Entry) ) {
97 ValEntry->Write(fp,filetype);
100 if (gdcmSeqEntry* SeqEntry = dynamic_cast< gdcmSeqEntry* >(Entry) ) {
101 SeqEntry->Write(fp,filetype);
108 //-----------------------------------------------------------------------------
111 * \brief adds any Entry (Dicom Element) to the Sequence Item
113 bool gdcmSQItem::AddEntry(gdcmDocEntry *entry)
115 docEntries.push_back(entry);
116 //TODO : check if it worked
121 * \brief Sets Entry (Dicom Element) value of an element,
122 * specified by it's tag (Group, Number)
123 * and the length, too ... inside a SQ Item
124 * If the Element is not found, it's just created !
125 * \warning we suppose, right now, the element belongs to a Public Group
127 * @param val string value to set
128 * @param group Group number of the searched tag.
129 * @param element Element number of the searched tag.
130 * @return true if element was found or created successfully
133 bool gdcmSQItem::SetEntryByNumber(std::string val,guint16 group,
136 for(ListDocEntry::iterator i=docEntries.begin();i!=docEntries.end();++i)
138 if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 )
141 if ( ( group < (*i)->GetGroup() )
142 ||( group == (*i)->GetGroup() && element < (*i)->GetElement()) )
144 // instead of ReplaceOrCreateByNumber
145 // that is a method of gdcmDocument :-(
146 gdcmValEntry* Entry = (gdcmValEntry*)0;
147 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
149 if ( ! ptagHT->count(key))
151 // we assume a Public Dictionnary *is* loaded
152 gdcmDict *PubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
153 // if the invoked (group,elem) doesn't exist inside the Dictionary
154 // we create a VirtualDictEntry
155 gdcmDictEntry *DictEntry = PubDict->GetDictEntryByNumber(group,
157 if (DictEntry == NULL)
159 DictEntry=gdcmGlobal::GetDicts()->NewVirtualDictEntry(group,
164 // we assume the constructor didn't fail
165 Entry = new gdcmValEntry(DictEntry);
168 /// better we don't assume too much !
169 /// gdcmSQItem is now used to describe any DICOMDIR related object
171 gdcmDocEntry* FoundEntry = ptagHT->find(key)->second;
172 Entry = dynamic_cast<gdcmValEntry*>(FoundEntry);
174 dbg.Verbose(0, "gdcmSQItem::SetEntryByNumber: docEntries"
175 " contains non gdcmValEntry occurences");
178 Entry->SetValue(val);
179 Entry->SetLength(val.length());
180 docEntries.insert(i,Entry);
183 if (group == (*i)->GetGroup() && element == (*i)->GetElement() )
185 if ( gdcmValEntry* Entry = dynamic_cast<gdcmValEntry*>(*i) )
186 Entry->SetValue(val);
187 (*i)->SetLength(val.length());
192 //-----------------------------------------------------------------------------
197 * \brief Gets a Dicom Element inside a SQ Item Entry, by number
200 gdcmDocEntry *gdcmSQItem::GetDocEntryByNumber(guint16 group, guint16 element) {
201 for(ListDocEntry::iterator i=docEntries.begin();i!=docEntries.end();++i) {
202 if ( (*i)->GetGroup()==group && (*i)->GetElement()==element)
209 * \brief Get the value of a Dicom Element inside a SQ Item Entry, by number
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();
221 //-----------------------------------------------------------------------------
225 //-----------------------------------------------------------------------------