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
|
/*
*
* Copyright (C) 1994-2022, 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: dcmdata
*
* Author: Gerd Ehlers, Andreas Barth
*
* Purpose: Implementation of class DcmIntegerString
*
*/
#include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
#include "dcmtk/dcmdata/dcvris.h"
#include "dcmtk/dcmdata/dcjson.h"
#include "dcmtk/ofstd/ofstring.h"
#define MAX_IS_LENGTH 12
// ********************************
DcmIntegerString::DcmIntegerString(const DcmTag &tag,
const Uint32 len)
: DcmByteString(tag, len)
{
setMaxLength(MAX_IS_LENGTH);
setNonSignificantChars(" \\");
}
DcmIntegerString::DcmIntegerString(const DcmIntegerString &old)
: DcmByteString(old)
{
}
DcmIntegerString::~DcmIntegerString()
{
}
DcmIntegerString &DcmIntegerString::operator=(const DcmIntegerString &obj)
{
DcmByteString::operator=(obj);
return *this;
}
OFCondition DcmIntegerString::copyFrom(const DcmObject& rhs)
{
if (this != &rhs)
{
if (rhs.ident() != ident()) return EC_IllegalCall;
*this = OFstatic_cast(const DcmIntegerString &, rhs);
}
return EC_Normal;
}
// ********************************
DcmEVR DcmIntegerString::ident() const
{
return EVR_IS;
}
OFCondition DcmIntegerString::checkValue(const OFString &vm,
const OFBool /*oldFormat*/)
{
OFString strVal;
/* get "raw value" without any modifications (if possible) */
OFCondition l_error = getStringValue(strVal);
if (l_error.good())
l_error = DcmIntegerString::checkStringValue(strVal, vm);
return l_error;
}
// ********************************
OFCondition DcmIntegerString::getSint32(Sint32 &sintVal,
const unsigned long pos)
{
/* get integer string value */
OFString str;
OFCondition l_error = getOFString(str, pos, OFTrue);
if (l_error.good())
{
/* convert string to integer value */
#ifdef SCNd32
if (sscanf(str.c_str(), "%" SCNd32, &sintVal) != 1)
#elif SIZEOF_LONG == 8
if (sscanf(str.c_str(), "%d", &sintVal) != 1)
#else
if (sscanf(str.c_str(), "%ld", &sintVal) != 1)
#endif
l_error = EC_CorruptedData;
}
return l_error;
}
// ********************************
OFCondition DcmIntegerString::getOFString(OFString &stringVal,
const unsigned long pos,
OFBool normalize)
{
/* call inherited method */
OFCondition l_error = DcmByteString::getOFString(stringVal, pos, normalize);
/* normalize string if required */
if (l_error.good() && normalize)
normalizeString(stringVal, !MULTIPART, DELETE_LEADING, DELETE_TRAILING);
return l_error;
}
// ********************************
OFCondition DcmIntegerString::checkStringValue(const OFString &value,
const OFString &vm)
{
return DcmByteString::checkStringValue(value, vm, "is", 8, MAX_IS_LENGTH);
}
// ********************************
OFCondition DcmIntegerString::writeJson(STD_NAMESPACE ostream &out,
DcmJsonFormat &format)
{
/* always write JSON Opener */
writeJsonOpener(out, format);
OFBool isValid;
if (!isEmpty())
{
/* write element value */
OFString bulkDataValue;
if (format.asBulkDataURI(getTag(), bulkDataValue))
{
format.printBulkDataURIPrefix(out);
DcmJsonFormat::printString(out, bulkDataValue);
}
else
{
const unsigned long vm = getVM();
if (vm > 0)
{
OFString value;
OFString vmstring = "1";
OFCondition status = getOFString(value, 0L);
if (status.bad())
return status;
format.printValuePrefix(out);
isValid = checkStringValue(value, vmstring).good();
switch (format.getJsonNumStringPolicy())
{
case DcmJsonFormat::NSP_auto:
if (isValid) DcmJsonFormat::printNumberInteger(out, value);
else DcmJsonFormat::printValueString(out, value);
break;
case DcmJsonFormat::NSP_always_number:
if (isValid) DcmJsonFormat::printNumberInteger(out, value);
else return EC_CannotWriteStringAsJsonNumber;
break;
case DcmJsonFormat::NSP_always_string:
DcmJsonFormat::printValueString(out, value);
break;
}
for (unsigned long valNo = 1; valNo < vm; ++valNo)
{
status = getOFString(value, valNo);
if (status.bad())
return status;
format.printNextArrayElementPrefix(out);
isValid = checkStringValue(value, vmstring).good();
switch (format.getJsonNumStringPolicy())
{
case DcmJsonFormat::NSP_auto:
if (isValid) DcmJsonFormat::printNumberInteger(out, value);
else DcmJsonFormat::printValueString(out, value);
break;
case DcmJsonFormat::NSP_always_number:
if (isValid) DcmJsonFormat::printNumberInteger(out, value);
else return EC_CannotWriteStringAsJsonNumber;
break;
case DcmJsonFormat::NSP_always_string:
DcmJsonFormat::printValueString(out, value);
break;
}
}
format.printValueSuffix(out);
}
}
}
/* write JSON Closer */
writeJsonCloser(out, format);
/* always report success */
return EC_Normal;
}
|