1 /*=========================================================================
4 Module: $RCSfile: gdcmSQItem.cxx,v $
6 Date: $Date: 2004/06/23 13:02:36 $
7 Version: $Revision: 1.16 $
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) {
64 for (int i=0;i<SQDepthLevel;i++)
67 std::cout << s.str() << " --- SQItem number " << SQItemNumber << std::endl;
68 for (ListDocEntry::iterator i = docEntries.begin();
69 i != docEntries.end();
72 gdcmDocEntry* Entry = *i;
73 bool PrintEndLine = true;
76 Entry->SetPrintLevel(2);
78 if ( gdcmSeqEntry* SeqEntry = dynamic_cast<gdcmSeqEntry*>(Entry) )
87 * \brief canonical Writer
89 void gdcmSQItem::Write(FILE *fp,FileType filetype)
91 for (ListDocEntry::iterator i = docEntries.begin();
92 i != docEntries.end();
95 (*i)->Write(fp, filetype);
99 //-----------------------------------------------------------------------------
102 * \brief adds any Entry (Dicom Element) to the Sequence Item
104 bool gdcmSQItem::AddEntry(gdcmDocEntry *entry)
106 docEntries.push_back(entry);
107 //TODO : check if it worked
112 * \brief Sets Entry (Dicom Element) value of an element,
113 * specified by it's tag (Group, Number)
114 * and the length, too ... inside a SQ Item
115 * If the Element is not found, it's just created !
116 * \warning we suppose, right now, the element belongs to a Public Group
118 * @param val string value to set
119 * @param group Group number of the searched tag.
120 * @param element Element number of the searched tag.
121 * @return true if element was found or created successfully
124 bool gdcmSQItem::SetEntryByNumber(std::string val,guint16 group,
127 for(ListDocEntry::iterator i=docEntries.begin();i!=docEntries.end();++i)
129 if ( (*i)->GetGroup() == 0xfffe && (*i)->GetElement() == 0xe000 )
132 if ( ( group < (*i)->GetGroup() )
133 ||( group == (*i)->GetGroup() && element < (*i)->GetElement()) )
135 // instead of ReplaceOrCreateByNumber
136 // that is a method of gdcmDocument :-(
137 gdcmValEntry* Entry = (gdcmValEntry*)0;
138 TagKey key = gdcmDictEntry::TranslateToKey(group, element);
140 if ( ! ptagHT->count(key))
142 // we assume a Public Dictionnary *is* loaded
143 gdcmDict *PubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
144 // if the invoked (group,elem) doesn't exist inside the Dictionary
145 // we create a VirtualDictEntry
146 gdcmDictEntry *DictEntry = PubDict->GetDictEntryByNumber(group,
148 if (DictEntry == NULL)
150 DictEntry=gdcmGlobal::GetDicts()->NewVirtualDictEntry(group,
155 // we assume the constructor didn't fail
156 Entry = new gdcmValEntry(DictEntry);
159 /// better we don't assume too much !
160 /// gdcmSQItem is now used to describe any DICOMDIR related object
162 gdcmDocEntry* FoundEntry = ptagHT->find(key)->second;
163 Entry = dynamic_cast<gdcmValEntry*>(FoundEntry);
165 dbg.Verbose(0, "gdcmSQItem::SetEntryByNumber: docEntries"
166 " contains non gdcmValEntry occurences");
169 Entry->SetValue(val);
170 Entry->SetLength(val.length());
171 docEntries.insert(i,Entry);
174 if (group == (*i)->GetGroup() && element == (*i)->GetElement() )
176 if ( gdcmValEntry* Entry = dynamic_cast<gdcmValEntry*>(*i) )
177 Entry->SetValue(val);
178 (*i)->SetLength(val.length());
184 //-----------------------------------------------------------------------------
189 * \brief Gets a Dicom Element inside a SQ Item Entry, by number
192 gdcmDocEntry *gdcmSQItem::GetDocEntryByNumber(guint16 group, guint16 element) {
193 for(ListDocEntry::iterator i=docEntries.begin();i!=docEntries.end();++i) {
194 if ( (*i)->GetGroup()==group && (*i)->GetElement()==element)
201 * \brief Get the value of a Dicom Element inside a SQ Item Entry, by number
205 std::string gdcmSQItem::GetEntryByNumber(guint16 group, guint16 element) {
206 for(ListDocEntry::iterator i=docEntries.begin();i!=docEntries.end();++i) {
207 if ( (*i)->GetGroup()==group && (*i)->GetElement()==element) {
208 return ((gdcmValEntry *)(*i))->GetValue();
213 //-----------------------------------------------------------------------------
217 //-----------------------------------------------------------------------------