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
|
/*
*
* Copyright (C) 1994-2024, 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 DcmUniqueIdentifier
*
*/
#include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
#include "dcmtk/ofstd/ofstream.h"
#include "dcmtk/ofstd/ofstring.h"
#include "dcmtk/ofstd/ofstd.h"
#include "dcmtk/dcmdata/dcvrui.h"
#include "dcmtk/dcmdata/dcuid.h"
#define MAX_UI_LENGTH 64
// ********************************
DcmUniqueIdentifier::DcmUniqueIdentifier(const DcmTag &tag,
const Uint32 len)
: DcmByteString(tag, len)
{
/* padding character is NULL not a space! */
setPaddingChar('\0');
setMaxLength(MAX_UI_LENGTH);
setNonSignificantChars("\\");
}
DcmUniqueIdentifier::DcmUniqueIdentifier(const DcmUniqueIdentifier &old)
: DcmByteString(old)
{
}
DcmUniqueIdentifier::~DcmUniqueIdentifier()
{
}
DcmUniqueIdentifier &DcmUniqueIdentifier::operator=(const DcmUniqueIdentifier &obj)
{
DcmByteString::operator=(obj);
return *this;
}
OFCondition DcmUniqueIdentifier::copyFrom(const DcmObject& rhs)
{
if (this != &rhs)
{
if (rhs.ident() != ident()) return EC_IllegalCall;
*this = OFstatic_cast(const DcmUniqueIdentifier &, rhs);
}
return EC_Normal;
}
// ********************************
DcmEVR DcmUniqueIdentifier::ident() const
{
return EVR_UI;
}
OFCondition DcmUniqueIdentifier::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 = DcmUniqueIdentifier::checkStringValue(strVal, vm);
return l_error;
}
// ********************************
void DcmUniqueIdentifier::print(STD_NAMESPACE ostream &out,
const size_t flags,
const int level,
const char * /*pixelFileName*/,
size_t * /*pixelCounter*/)
{
if (valueLoaded())
{
/* get string data (possibly multi-valued) */
char *stringVal = NULL;
Uint32 stringLen = 0;
getString(stringVal, stringLen);
if ((stringVal != NULL) && (stringLen > 0))
{
const char *symbol = NULL;
if (!(flags & DCMTypes::PF_doNotMapUIDsToNames))
{
/* check whether UID number can be mapped to a UID name */
symbol = dcmFindNameOfUID(stringVal);
}
if ((symbol != NULL) && (strlen(symbol) > 0))
{
const size_t bufSize = strlen(symbol) + 1 /* for "=" */ + 1;
char *buffer = new char[bufSize];
if (buffer != NULL)
{
/* concatenate "=" and the UID name */
OFStandard::strlcpy(buffer, "=", bufSize);
OFStandard::strlcat(buffer, symbol, bufSize);
printInfoLine(out, flags, level, buffer, NULL /*tag*/, OFFalse /*isInfo*/);
/* delete temporary character buffer */
delete[] buffer;
} else /* could not allocate buffer */
DcmByteString::print(out, flags, level);
} else /* no symbol (UID name) found or mapping switched off */
DcmByteString::print(out, flags, level);
} else
printInfoLine(out, flags, level, "(no value available)");
} else
printInfoLine(out, flags, level, "(not loaded)");
}
// ********************************
OFCondition DcmUniqueIdentifier::getOFString(OFString &stringVal,
const unsigned long pos,
OFBool normalize)
{
OFCondition l_error = DcmByteString::getOFString(stringVal, pos, normalize);
if (l_error.good() && normalize)
normalizeString(stringVal, !MULTIPART, !DELETE_LEADING, DELETE_TRAILING, getPaddingChar() /* NULL-byte */);
return l_error;
}
// ********************************
OFCondition DcmUniqueIdentifier::putString(const char *stringVal)
{
/* determine length of the string value */
const size_t stringLen = (stringVal != NULL) ? strlen(stringVal) : 0;
/* call the real function */
return putString(stringVal, OFstatic_cast(Uint32, stringLen));
}
OFCondition DcmUniqueIdentifier::putString(const char *stringVal,
const Uint32 stringLen)
{
const char *uid = stringVal;
Uint32 uidLen = stringLen;
/* check whether parameter contains a UID name instead of a UID number */
if ((stringVal != NULL) && (stringVal[0] == '='))
{
uid = dcmFindUIDFromName(stringVal + 1);
/* check whether UID name could be mapped to a UID number */
if (uid == NULL)
{
DCMDATA_DEBUG("DcmUniqueIdentifier::putString() cannot map UID name '"
<< OFSTRING_GUARD(stringVal + 1) << "' to UID value");
/* return with an error */
return EC_UnknownUIDName;
} else
uidLen = OFstatic_cast(Uint32, strlen(uid));
}
/* call inherited method to set the UID string */
return DcmByteString::putString(uid, uidLen);
}
// ********************************
OFCondition DcmUniqueIdentifier::makeMachineByteString(const Uint32 length)
{
/* get string data */
char *value = OFstatic_cast(char *, getValue());
/* determine initial string length */
const size_t len = (length == 0) ? getLengthField() : length;
if ((value != NULL) && (len > 0))
{
/* check whether string representation is not the internal one */
if (getStringMode() != DCM_MachineString)
{
/* check whether automatic input data correction is enabled */
if (dcmEnableAutomaticInputDataCorrection.get())
{
/*
** Remove any leading, embedded, or trailing white space.
** This manipulation attempts to correct problems with
** incorrectly encoded UIDs which have been observed in
** some images.
*/
size_t curPos = 0;
for (size_t i = 0; i < len; i++)
{
if (!OFStandard::isspace(value[i]))
value[curPos++] = value[i];
}
/* there was at least one space character in the string */
if (curPos < len)
{
DCMDATA_WARN("DcmUniqueIdentifier: Element " << getTagName() << " " << getTag()
<< " contains one or more space characters, which were removed");
/* remember new length */
const Uint32 newLen = OFstatic_cast(Uint32, curPos);
/* blank out all trailing characters */
while (curPos < len)
value[curPos++] = '\0';
/* call inherited method: re-computes the string length, etc. */
return DcmByteString::makeMachineByteString(newLen);
}
}
}
}
/* call inherited method: re-computes the string length, etc. */
return DcmByteString::makeMachineByteString(OFstatic_cast(Uint32, len));
}
// ********************************
OFCondition DcmUniqueIdentifier::checkStringValue(const OFString &value,
const OFString &vm)
{
return DcmByteString::checkStringValue(value, vm, "ui", 9, MAX_UI_LENGTH);
}
|