File: dsrrefin.cc

package info (click to toggle)
dcmtk 3.6.9-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,648 kB
  • sloc: ansic: 426,874; cpp: 318,177; makefile: 6,401; sh: 4,341; yacc: 1,026; xml: 482; lex: 321; perl: 277
file content (458 lines) | stat: -rw-r--r-- 15,008 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
 *
 *  Copyright (C) 2011-2019, OFFIS e.V.
 *  All rights reserved.  See COPYRIGHT file for details.
 *
 *  This software and supporting documentation were developed by
 *
 *    OFFIS e.V.
 *    R&D Division Health
 *    Escherweg 2
 *    D-26121 Oldenburg, Germany
 *
 *
 *  Module: dcmsr
 *
 *  Author: Joerg Riesmeier
 *
 *  Purpose:
 *    classes: DSRReferencedInstanceList
 *
 */


#include "dcmtk/config/osconfig.h"    /* make sure OS specific configuration is included first */

#include "dcmtk/dcmsr/dsrrefin.h"
#include "dcmtk/dcmsr/dsrxmld.h"

#include "dcmtk/dcmdata/dcdeftag.h"
#include "dcmtk/dcmdata/dcuid.h"
#include "dcmtk/dcmdata/dcvrui.h"


DSRReferencedInstanceList::DSRReferencedInstanceList()
  : ItemList(),
    Iterator()
{
    /* initialize list cursor */
    Iterator = ItemList.end();
}


DSRReferencedInstanceList::~DSRReferencedInstanceList()
{
    /* clear list and delete allocated memory */
    clear();
}


void DSRReferencedInstanceList::clear()
{
    Iterator = ItemList.begin();
    const OFListIterator(ItemStruct *) last = ItemList.end();
    /* delete all items and free memory */
    while (Iterator != last)
    {
        delete (*Iterator);
        Iterator = ItemList.erase(Iterator);
    }
    /* make sure that the list is empty */
    ItemList.clear();
    Iterator = ItemList.end();
}


OFBool DSRReferencedInstanceList::isEmpty() const
{
    return ItemList.empty();
}


size_t DSRReferencedInstanceList::getNumberOfItems() const
{
    return ItemList.size();
}


OFCondition DSRReferencedInstanceList::read(DcmItem &dataset,
                                            const size_t flags)
{
    /* first, check whether sequence is present and non-empty */
    DcmSequenceOfItems sequence(DCM_ReferencedInstanceSequence);
    OFCondition result = getElementFromDataset(dataset, sequence);
    checkElementValue(sequence, "1-n", "1C", result, "SRDocumentGeneralModule");
    if (result.good())
    {
        ItemStruct *item = NULL;
        OFString sopClassUID, instanceUID;
        /* iterate over all sequence items */
        DcmObject *dobj = NULL;
        while ((dobj = sequence.nextInContainer(dobj)) != NULL)
        {
            DcmItem *ditem = OFstatic_cast(DcmItem *, dobj);
            /* get the SOP class and instance UID */
            if (getAndCheckStringValueFromDataset(*ditem, DCM_ReferencedSOPClassUID, sopClassUID, "1", "1", "ReferencedInstanceSequence").good() &&
                getAndCheckStringValueFromDataset(*ditem, DCM_ReferencedSOPInstanceUID, instanceUID, "1", "1", "ReferencedInstanceSequence").good())
            {
                /* add new item to the list */
                if (addItem(sopClassUID, instanceUID, item).good())
                {
                    /* read additional information */
                    item->PurposeOfReference.readSequence(*ditem, DCM_PurposeOfReferenceCodeSequence, "1", flags);
                }
            }
        }
    }
    return result;
}


OFCondition DSRReferencedInstanceList::write(DcmItem &dataset) const
{
    OFCondition result = EC_Normal;
    /* iterate over all list items */
    OFListConstIterator(ItemStruct *) iter = ItemList.begin();
    const OFListConstIterator(ItemStruct *) last = ItemList.end();
    while ((iter != last) && result.good())
    {
        ItemStruct *item = *iter;
        /* check whether list item really exists and is valid */
        if ((item != NULL) && !item->SOPClassUID.empty() && !item->InstanceUID.empty())
        {
            DcmItem *ditem = NULL;
            /* create a new item (and a sequence if required) */
            result = dataset.findOrCreateSequenceItem(DCM_ReferencedInstanceSequence, ditem, -2 /*append new*/);
            /* write item data */
            if (result.good())
            {
                /* open question: should we check the return values? */
                putStringValueToDataset(*ditem, DCM_ReferencedSOPClassUID, item->SOPClassUID);
                putStringValueToDataset(*ditem, DCM_ReferencedSOPInstanceUID, item->InstanceUID);
                item->PurposeOfReference.writeSequence(*ditem, DCM_PurposeOfReferenceCodeSequence);
            }
        }
        ++iter;
    }
    return result;
}


OFCondition DSRReferencedInstanceList::readXML(const DSRXMLDocument &doc,
                                               DSRXMLCursor cursor,
                                               const size_t flags)
{
    OFCondition result = SR_EC_InvalidDocument;
    ItemStruct *item = NULL;
    OFString sopClassUID, instanceUID;
    /* iterate over all nodes */
    while (cursor.valid())
    {
        /* check for known element tags */
        if (doc.checkNode(cursor, "value").good())
        {
            /* retrieve SOP class and instance UID */
            if (!doc.getStringFromAttribute(doc.getNamedChildNode(cursor, "sopclass"), sopClassUID, "uid").empty() &&
                !doc.getStringFromAttribute(doc.getNamedChildNode(cursor, "instance"), instanceUID, "uid").empty())
            {
                result = addItem(sopClassUID, instanceUID, item);
                if (result.good())
                {
                    DSRXMLCursor childCursor = cursor.getChild();
                    /* clear any previously stored information */
                    item->clear();
                    while (childCursor.valid())
                    {
                        /* check for known element tags */
                        if (doc.matchNode(childCursor, "purpose"))
                            item->PurposeOfReference.readXML(doc, childCursor, flags);
                        /* proceed with next node */
                        childCursor.gotoNext();
                    }
                }
            }
        }
        /* proceed with next node */
        cursor.gotoNext();
    }
    return result;
}


OFCondition DSRReferencedInstanceList::writeXML(STD_NAMESPACE ostream &stream,
                                                const size_t flags) const
{
    OFString tmpString;
    /* iterate over all list items */
    OFListConstIterator(ItemStruct *) iter = ItemList.begin();
    const OFListConstIterator(ItemStruct *) last = ItemList.end();
    while (iter != last)
    {
        ItemStruct *item = *iter;
        /* check whether list item really exists */
        if (item != NULL)
        {
            /* write instance level */
            stream << "<value>" << OFendl;
            stream << "<sopclass uid=\"" << item->SOPClassUID << "\">";
            /* retrieve name of SOP class (if known) */
            stream << dcmFindNameOfUID(item->SOPClassUID.c_str(), "" /* empty value as default */);
            stream << "</sopclass>" << OFendl;
            stream << "<instance uid=\"" << item->InstanceUID << "\"/>" << OFendl;
            if (flags & DSRTypes::XF_codeComponentsAsAttribute)
                stream << "<purpose";     // bracket ">" is closed in next writeXML() call
            else
                stream << "<purpose>" << OFendl;
            item->PurposeOfReference.writeXML(stream, flags);
            stream << "</purpose>" << OFendl;
            stream << "</value>" << OFendl;
        }
        ++iter;
    }
    return EC_Normal;
}


OFCondition DSRReferencedInstanceList::addItem(const OFString &sopClassUID,
                                               const OFString &instanceUID,
                                               ItemStruct *&item)
{
    OFCondition result = EC_IllegalParameter;
    /* check parameters first */
    if (!sopClassUID.empty() && !instanceUID.empty())
    {
        result = EC_Normal;
        OFListIterator(ItemStruct *) oldCursor = Iterator;
        /* check whether item already exists */
        if (gotoItem(sopClassUID, instanceUID).bad())
        {
            /* if not create new item and add it to the list */
            item = new ItemStruct(sopClassUID, instanceUID);
            if (item != NULL)
            {
                ItemList.push_back(item);
                /* set cursor to new position */
                Iterator = --ItemList.end();
            } else {
                /* restore old cursor */
                Iterator = oldCursor;
                result = EC_MemoryExhausted;
            }
        } else {
            DCMSR_WARN("SOP instance " << instanceUID
                << " already exists in ReferencedInstanceSequence ... overwriting");
            /* gotoItem() was successful, set item pointer */
            item = *Iterator;
        }
    } else
        item = NULL;
    return result;
}


OFCondition DSRReferencedInstanceList::addItem(const OFString &sopClassUID,
                                               const OFString &instanceUID,
                                               const OFBool check)
{
    OFCondition result = EC_Normal;
    /* check whether the passed values are valid */
    if (check)
        result = checkSOPInstance(sopClassUID, instanceUID);
    /* call the "real" function (empty parameters are rejected) */
    if (result.good())
    {
        ItemStruct *item = NULL;
        result = addItem(sopClassUID, instanceUID, item);
    }
    return result;
}


OFCondition DSRReferencedInstanceList::removeItem()
{
    OFCondition result = EC_IllegalCall;
    /* check whether list is empty or iterator is invalid */
    if (!ItemList.empty() && (Iterator != ItemList.end()))
    {
        /* free memory */
        delete (*Iterator);
        /* remove item from list */
        Iterator = ItemList.erase(Iterator);
        result = EC_Normal;
    }
    return result;
}


OFCondition DSRReferencedInstanceList::removeItem(const OFString &sopClassUID,
                                                  const OFString &instanceUID)
{
    /* goto specified item ... */
    OFCondition result = gotoItem(sopClassUID, instanceUID);
    /* ... and remove it */
    if (result.good())
        result = removeItem();
    return result;
}


OFCondition DSRReferencedInstanceList::gotoItem(const OFString &sopClassUID,
                                                const OFString &instanceUID)
{
    OFCondition result = EC_IllegalParameter;
    /* check parameters first */
    if (!sopClassUID.empty() && !instanceUID.empty())
    {
        OFBool sopClassMatch = OFFalse;
        result = SR_EC_SOPInstanceNotFound;
        /* start with first item */
        Iterator = ItemList.begin();
        const OFListIterator(ItemStruct *) last = ItemList.end();
        /* search for given item */
        while ((Iterator != last) && result.bad())
        {
            /* if instance found, exit loop */
            if ((*Iterator != NULL) && ((*Iterator)->InstanceUID == instanceUID))
            {
                /* finally, check whether SOP class matches */
                sopClassMatch = ((*Iterator)->SOPClassUID == sopClassUID);
                result = EC_Normal;
            } else
                ++Iterator;
        }
        /* report an error in case of SOP class mismatch */
        if (result.good() && !sopClassMatch)
            result = SR_EC_DifferentSOPClassesForAnInstance;
    }
    return result;
}


OFCondition DSRReferencedInstanceList::gotoFirstItem()
{
    OFCondition result = EC_IllegalCall;
    /* check for empty item list */
    if (!ItemList.empty())
    {
        /* set cursor to first list item */
        Iterator = ItemList.begin();
        result = EC_Normal;
    }
    return result;
}


OFCondition DSRReferencedInstanceList::gotoNextItem()
{
    OFCondition result = EC_IllegalCall;
    /* goto next list item */
    if (++Iterator != ItemList.end())
    {
        /* check whether list item is valid */
        if (*Iterator != NULL)
            result = EC_Normal;
        else
            result = EC_CorruptedData;
    }
    return result;
}


DSRReferencedInstanceList::ItemStruct *DSRReferencedInstanceList::getCurrentItem() const
{
    ItemStruct *item = NULL;
    /* check whether current item is valid */
    OFListConstIterator(ItemStruct *) it = Iterator;
    if (it != ItemList.end())
        item = *Iterator;
    return item;
}


const OFString &DSRReferencedInstanceList::getSOPClassUID(OFString &sopClassUID) const
{
    /* check whether current item is valid */
    ItemStruct *item = getCurrentItem();
    /* get requested value or clear string if invalid */
    if (item != NULL)
        sopClassUID = item->SOPClassUID;
    else
        sopClassUID.clear();
    return sopClassUID;
}


const OFString &DSRReferencedInstanceList::getSOPInstanceUID(OFString &instanceUID) const
{
    /* check whether current item is valid */
    ItemStruct *item = getCurrentItem();
    /* get requested value or clear string if invalid */
    if (item != NULL)
        instanceUID = item->InstanceUID;
    else
        instanceUID.clear();
    return instanceUID;
}


OFCondition DSRReferencedInstanceList::getPurposeOfReference(DSRCodedEntryValue &purposeOfReference) const
{
    OFCondition result = EC_IllegalCall;
    /* check whether current item is valid */
    ItemStruct *item = getCurrentItem();
    /* get requested value or clear code value if invalid */
    if (item != NULL)
    {
        purposeOfReference = item->PurposeOfReference;
        result = EC_Normal;
    } else
        purposeOfReference.clear();
    return result;
}


OFCondition DSRReferencedInstanceList::setPurposeOfReference(const DSRCodedEntryValue &purposeOfReference,
                                                             const OFBool check)
{
    OFCondition result = EC_IllegalCall;
    /* check whether current item is valid */
    ItemStruct *item = getCurrentItem();
    if (item != NULL)
    {
        if (check)
        {
            /* check whether the passed value is valid */
            result = checkPurposeOfReference(purposeOfReference);
        } else {
            /* make sure that the mandatory values are non-empty */
            result = purposeOfReference.isEmpty() ? SR_EC_InvalidValue : EC_Normal;
        }
        if (result.good())
            item->PurposeOfReference = purposeOfReference;
    }
    return result;
}


OFCondition DSRReferencedInstanceList::checkSOPInstance(const OFString &sopClassUID,
                                                        const OFString &instanceUID) const
{
    OFCondition result = EC_Normal;
    /* the two UID values should never be empty */
    if (sopClassUID.empty() || instanceUID.empty())
        result = SR_EC_InvalidValue;
    /* check for conformance with VR and VM */
    if (result.good())
        result = DcmUniqueIdentifier::checkStringValue(sopClassUID, "1");
    if (result.good())
        result = DcmUniqueIdentifier::checkStringValue(instanceUID, "1");
    return result;
}


OFCondition DSRReferencedInstanceList::checkPurposeOfReference(const DSRCodedEntryValue &purposeOfReference) const
{
    /* purpose of reference should never be empty */
    return purposeOfReference.isEmpty() ? SR_EC_InvalidValue
                                        : purposeOfReference.checkCurrentValue();
}