File: modbase.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 (285 lines) | stat: -rw-r--r-- 7,931 bytes parent folder | download | duplicates (2)
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
/*
 *
 *  Copyright (C) 2015-2024, Open Connections GmbH
 *  All rights reserved.  See COPYRIGHT file for details.
 *
 *  This software and supporting documentation are maintained by
 *
 *    OFFIS e.V.
 *    R&D Division Health
 *    Escherweg 2
 *    D-26121 Oldenburg, Germany
 *
 *
 *  Module: dcmiod
 *
 *  Author: Michael Onken
 *
 *  Purpose: Abstract base class for IOD Modules or other attribute collections
 *
 */

#include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
#include "dcmtk/dcmiod/modbase.h"
#include "dcmtk/dcmdata/dcitem.h"
#include "dcmtk/dcmiod/iodrules.h"
#include "dcmtk/dcmiod/iodutil.h"

IODComponent::IODComponent(const IODComponent& rhs)
    : m_Item(OFstatic_cast(DcmItem*, rhs.m_Item->clone()))
    , m_Rules(rhs.m_Rules->clone())
    , m_Parent(OFnullptr)
    , m_CheckValueOnWrite(rhs.m_CheckValueOnWrite)
{
}

IODComponent::IODComponent(OFshared_ptr<DcmItem> item, OFshared_ptr<IODRules> rules, IODComponent* parent)
    : m_Item(item)
    , m_Rules(rules)
    , m_Parent(parent)
    , m_CheckValueOnWrite(OFTrue)
{
    if (!m_Item)
        m_Item.reset(new DcmItem());

    if (!m_Rules)
        m_Rules.reset(new IODRules());
}

IODComponent::IODComponent(IODComponent* parent)
    : m_Item()
    , m_Rules()
    , m_Parent(parent)
    , m_CheckValueOnWrite()
{
    m_Item.reset(new DcmItem());
    m_Rules.reset(new IODRules());
    if (parent)
    {
        m_CheckValueOnWrite = parent->getValueCheckOnWrite();
    }
}

IODComponent::~IODComponent()
{
    // Nothing to do for shared pointers
}

IODComponent& IODComponent::operator=(const IODComponent& rhs)
{
    if (&rhs != this)
    {
        m_Item.reset(OFstatic_cast(DcmItem*, rhs.m_Item->clone()));
        m_Rules.reset(rhs.m_Rules->clone());
        m_Parent = OFnullptr;
        m_CheckValueOnWrite = rhs.m_CheckValueOnWrite;
    }
    return *this;
}

void IODComponent::inventMissing()
{
    // Try to fill in default values
    OFVector<IODRule*> writeRules     = m_Rules->getByModule(getName());
    OFVector<IODRule*>::iterator rule = writeRules.begin();
    while (rule != writeRules.end())
    {
        OFString val = (*rule)->getDefaultValue();
        // We have a default value
        if (val.length() != 0)
        {
            // Check if element is not set
            DcmElement* elem = NULL;
            if (getData().findAndGetElement((*rule)->getTagKey(), elem).bad())
            {
                elem = DcmItem::newDicomElement((*rule)->getTagKey());
                if (elem == NULL)
                {
                    DCMIOD_ERROR("Could not allocate element " << (*rule)->getTagKey());
                }
                else
                {
                    elem->putString(val.c_str());
                    m_Item->insert(elem);
                }
            }
            else if (elem->getLength() == 0)
            {
                elem->putString(val.c_str());
            }
        }
        rule++;
    }
}

void IODComponent::clearData()
{
    OFVector<IODRule*> modRules       = m_Rules->getByModule(getName());
    OFVector<IODRule*>::iterator rule = modRules.begin();
    while (rule != modRules.end())
    {
        m_Item->findAndDeleteElement((*rule)->getTagKey());
        rule++;
    }
}

void IODComponent::makeOptional()
{
    OFVector<IODRule*> modRules       = m_Rules->getByModule(getName());
    OFVector<IODRule*>::iterator rule = modRules.begin();
    while (rule != modRules.end())
    {
        (*rule)->setType("3");
        rule++;
    }
}

OFCondition IODComponent::read(DcmItem& source, const OFBool clearOldData)
{
    // Debug
    DCMIOD_DEBUG("Reading component: " << getName());

    // Clear old data if desired
    if (clearOldData)
    {
        clearData();
    }

    // Do actual reading
    read(source, *m_Rules, *m_Item, getName());
    // We do not report errors here (only logger output)
    return EC_Normal;
}

OFCondition IODComponent::write(DcmItem& destination)
{
    // Debug
    DCMIOD_DEBUG("Writing component: " << getName());

    // Invent missing values
    inventMissing();

    // Start writing
    OFCondition result = EC_Normal;
    result             = write(*m_Item, *m_Rules, destination, getName(), m_CheckValueOnWrite);
    return result;
}

int IODComponent::compare(const IODComponent& rhs) const
{
    return this->m_Item.get()->compare(*(rhs.m_Item.get()));
}

OFCondition IODComponent::check(const OFBool quiet)
{
    OFCondition result;
    IODRules::iterator it = m_Rules->begin();
    while (it != m_Rules->end())
    {
        result = (*it).second->check(*m_Item, quiet);
        if (result.bad())
            break;
        else
            it++;
    }
    return result;
}

// static helper
OFCondition IODComponent::read(DcmItem& source, IODRules& rules, DcmItem& destination, const OFString& componentName)
{
    OFVector<IODRule*> modRules       = rules.getByModule(componentName);
    OFVector<IODRule*>::iterator rule = modRules.begin();
    while (rule != modRules.end())
    {
        // We do not read/copy sequences but only check them since they are not
        // handled within this component
        OFBool isSequence = (DcmTag((*rule)->getTagKey()).getEVR() == EVR_SQ);
        if (isSequence)
        {
            DcmElement* elem = NULL;
            OFCondition cond = source.findAndGetElement((*rule)->getTagKey(), elem);
            DcmIODUtil::checkElementValue(elem,
                                          (*rule)->getTagKey(),
                                          (*rule)->getVM(),
                                          (*rule)->getType(),
                                          cond,
                                          (*rule)->getModule().c_str(),
                                          dcmtk::log4cplus::WARN_LOG_LEVEL);
        }
        else // Normal attributes are checked and copied over into this IOD component
        {
            DcmElement* elem = NULL;
            DcmIODUtil::getAndCheckElementFromDataset(source, elem, *rule);
            if (elem != NULL)
            {
                OFCondition result = destination.insert(elem, OFTrue);
                if (result.bad())
                {
                    DCMIOD_ERROR("Cannot insert element with tag: " << (*rule)->getTagKey());
                    delete elem;
                }
            }
        }
        rule++;
    }
    return EC_Normal;
}

// static helper
OFCondition IODComponent::write(DcmItem& source, IODRules& rules, DcmItem& destination, const OFString& componentName, const OFBool checkValue)
{
    OFCondition result = EC_Normal;

    OFVector<IODRule*> writeRules     = rules.getByModule(componentName);
    OFVector<IODRule*>::iterator rule = writeRules.begin();
    while (rule != writeRules.end())
    {
        DcmElement* elem = NULL;
        source.findAndGetElement((*rule)->getTagKey(), elem, OFFalse /* only this level */, OFTrue /* create copy*/);
        DcmIODUtil::addElementToDataset(result, destination, elem, *rule, checkValue);
        rule++;
    }

    return result;
}

void IODComponent::setValueCheckOnWrite(const OFBool checkValue)
{
    m_CheckValueOnWrite = checkValue;
}

OFBool IODComponent::getValueCheckOnWrite() const
{
    return m_CheckValueOnWrite;
}

// -------- IODModule --------------

IODModule::IODModule()
    : IODComponent()
{
    // nothing to do, IODComponent does the work
}

IODModule::IODModule(const IODModule& rhs)
    : IODComponent(rhs.m_Item, rhs.m_Rules, rhs.m_Parent)
{
}

IODModule::IODModule(OFshared_ptr<DcmItem> item, OFshared_ptr<IODRules> rules)
    : IODComponent(item, rules, NULL /* No parent for modules */)
{
    // nothing to do, IODComponent does the work
}

IODModule& IODModule::operator=(const IODModule& rhs)
{
    if (this != &rhs)
    {
        m_Item   = rhs.m_Item;
        m_Rules  = rhs.m_Rules;
        m_Parent = rhs.m_Parent;
    }
    return *this;
}