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 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
|
/*
*
* 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, Joerg Riesmeier
*
* Purpose: Implementation of class DcmPersonName
*
*/
#include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */
#include "dcmtk/dcmdata/dcjson.h"
#include "dcmtk/dcmdata/dcvrpn.h"
#include "dcmtk/ofstd/ofstd.h"
// ********************************
const char* const DcmPersonName::componentGroupNames[3] = { "Alphabetic", "Ideographic", "Phonetic" };
DcmPersonName::DcmPersonName(const DcmTag &tag,
const Uint32 len)
: DcmCharString(tag, len)
{
setMaxLength(64); // not correct: max length of PN is 3*64+2 = 194 characters (not bytes!)
setNonSignificantChars(" \\^=");
}
DcmPersonName::DcmPersonName(const DcmPersonName& old)
: DcmCharString(old)
{
}
DcmPersonName::~DcmPersonName()
{
}
DcmPersonName &DcmPersonName::operator=(const DcmPersonName &obj)
{
DcmCharString::operator=(obj);
return *this;
}
OFCondition DcmPersonName::copyFrom(const DcmObject& rhs)
{
if (this != &rhs)
{
if (rhs.ident() != ident()) return EC_IllegalCall;
*this = OFstatic_cast(const DcmPersonName &, rhs);
}
return EC_Normal;
}
// ********************************
DcmEVR DcmPersonName::ident() const
{
return EVR_PN;
}
OFCondition DcmPersonName::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())
{
OFString charset;
/* try to determine the value of the SpecificCharacterSet element */
if (getSpecificCharacterSet(charset) == EC_CorruptedData)
charset = "UNKNOWN";
l_error = DcmPersonName::checkStringValue(strVal, vm, charset);
}
return l_error;
}
// ********************************
OFCondition DcmPersonName::getOFString(OFString &stringVal,
const unsigned long pos,
OFBool normalize)
{
OFCondition l_error = DcmCharString::getOFString(stringVal, pos, normalize);
if (l_error.good() && normalize)
normalizeString(stringVal, !MULTIPART, !DELETE_LEADING, DELETE_TRAILING);
return l_error;
}
OFCondition DcmPersonName::writeXML(STD_NAMESPACE ostream &out,
const size_t flags)
{
/* PN requires special handling in the Native DICOM Model format */
if (flags & DCMTypes::XF_useNativeModel)
{
const OFBool convertNonASCII = (flags & DCMTypes::XF_convertNonASCII) > 0;
/* write normal XML start tag */
DcmElement::writeXMLStartTag(out, flags);
/* if the value is empty, we do not need to insert any PersonName attribute at all */
if (!isEmpty())
{
/* condition variable, not meant to be used as a result of this method */
OFCondition result;
/* iterate over multiple Person Names if necessary */
const unsigned long vm = getVM();
/* strings to hold family, first, and middle name as well as prefix and suffix component */
OFString components[5];
/* arrays in order to permit looping while creating the output */
const char* compNames[5] = { "FamilyName", "GivenName", "MiddleName", "NamePrefix", "NameSuffix" };
for (unsigned int it = 0; it < vm; it++)
{
out << "<PersonName number=\"" << (it + 1) << "\">" << OFendl;
OFString allGroups, oneCompGroup;
result = getOFString(allGroups, it);
if (result.good())
{
/* process alphabetic, ideographic and phonetic encoding, as available */
for (unsigned int cg = 0; cg < 3; cg++)
{
/* get one component group (more efficient to check for non-zero length on whole group later) */
result = getComponentGroup(allGroups, cg, oneCompGroup);
if (result.good() && !oneCompGroup.empty())
{
/* get all name components from current group, i.e. last, first, middle name, prefix, suffix.
* uses single group, so the component group parameter is always 0
*/
result = getNameComponentsFromString(oneCompGroup, components[0], components[1], components[2], components[3], components[4], 0);
}
/* output one component group, e.g. <SingleByte> <FamilyName>Onken</FamilyName> </SingleByte> */
if (result.good())
{
out << "<" << componentGroupNames[cg] << ">" << OFendl; // e.g. <SingleByte>
/* go through components (last name, first name, ...) */
for (unsigned short c = 0; c < 5; c++)
{
if (!components[c].empty())
{
/* output name component, e.g. <FamilyName>Onken</FamilyName> */
out << "<" << compNames[c] << ">";
/* check whether conversion to XML markup string is required */
if (OFStandard::checkForMarkupConversion(components[c], convertNonASCII))
OFStandard::convertToMarkupStream(out, components[c], convertNonASCII);
else
out << components[c];
out << "</" << compNames[c] << ">" << OFendl;
}
}
out << "</" << componentGroupNames[cg] << ">" << OFendl; // e.g. </SingleByte>
}
}
}
out << "</PersonName>" << OFendl;
}
}
/* write normal XML end tag */
DcmElement::writeXMLEndTag(out, flags);
/* always report success */
return EC_Normal;
} else {
/* DCMTK-specific format does not require anything special */
return DcmElement::writeXML(out, flags);
}
}
// ********************************
OFCondition DcmPersonName::writeJson(STD_NAMESPACE ostream &out,
DcmJsonFormat &format)
{
// helper struct to operate on the value pointers
struct Lexer
{
// initializes everything with foolproof defaults
Lexer()
: it(OFnullptr)
, end(OFnullptr)
, componentGroupName(componentGroupNames)
, currentComponent(0)
, hasTrailingNull(OFFalse)
{
}
// resets component groups and components for the new value and
// remembers that there was a value separator, so that trailing
// 'null' values can be printed.
void handleValue()
{
currentComponent = 0;
componentGroupName = componentGroupNames;
hasTrailingNull = OFTrue;
}
// check component group validity and set members
// appropriately
// emit warnings for invalid component groups
void handleComponentGroup()
{
if (componentGroupName != componentGroupNames + 2)
{
currentComponent = 0;
++componentGroupName;
}
else
{
DCMDATA_ERROR("DcmPersonName::writeJson(): omitting invalid "
"component group (more than three component groups present)");
// seek to the end of the current component group - 1, since
// the iterator will be incremented in the calling code
while (++it != end && *it != '\\' && *it != '=');
--it;
}
}
// searches for the next significant character
// (no delimiter or padding character).
// keeps track of the component group name and component
// number.
// returns OFFalse if no more significant characters exist.
OFBool nextValue()
{
for (; it != end; ++it) switch (*it)
{
case '\\':
handleValue();
return OFTrue;
case '=':
handleComponentGroup();
break;
case '^':
++currentComponent;
break;
case ' ':
break;
default:
return OFTrue;
}
// this ends the output, unless we have to write
// a trailing 'null'
return hasTrailingNull;
}
// searches for the next significant character
// (no delimiter or padding character)
// within the current value.
// keeps track of the current component group and component
// number.
// returns OFFalse if no more significant characters exist
// within the current value.
OFBool nextComponentGroup()
{
for (; it != end; ++it) switch(*it)
{
case '\\':
// skip over any value separator
++it;
handleValue();
return OFFalse;
case '=':
handleComponentGroup();
break;
case '^':
++currentComponent;
break;
case ' ':
break;
default:
return OFTrue;
}
return OFFalse;
}
// searches for the next significant character
// (no delimiter or padding character)
// within the current component group.
// keeps track of the current component number.
// returns OFFalse if no more significant characters exist
// within the current component group.
OFBool nextComponent()
{
for (; it != end; ++it) switch(*it)
{
case '\\':
case '=':
return OFFalse;
case '^':
++currentComponent;
break;
case ' ':
break;
default:
return OFTrue;
}
return OFFalse;
}
// writes all significant characters of the current component,
// prepending '^' characters as required by the current component
// number.
void writeComponent(STD_NAMESPACE ostream &out)
{
for (; currentComponent; --currentComponent)
out << '^';
const char* const begin = it;
while (++it != end && *it != '\\' && *it != '=' && *it != '^');
const char* componentEnd = it - 1;
while (*componentEnd == ' ')
--componentEnd;
OFString s(begin, 0, componentEnd - begin + 1);
DcmJsonFormat::escapeControlCharacters(out, s);
}
// writes the name of the current component group and all components
// within the current component group that contain at least one significant
// character.
// All others are replaced by a single '^' character each, except for the
// trailing ones (that are omitted).
void writeComponentGroup(STD_NAMESPACE ostream &out, DcmJsonFormat &format)
{
out << format.indent() << '"' << *componentGroupName << "\":"
<< format.space() << '"';
writeComponent(out);
while (nextComponent())
writeComponent(out);
out << '"';
}
// writes all component groups of the current value that contain at least
// a single significant character, separated by ',' characters.
// all other component groups are omitted entirely.
// writes 'null' if all component groups are empty.
void writeCurrentValue(STD_NAMESPACE ostream &out, DcmJsonFormat &format)
{
hasTrailingNull = OFFalse;
if (nextComponentGroup())
{
out << '{' << format.newline();
++format.indent();
writeComponentGroup(out, format);
while (nextComponentGroup())
{
out << ',' << format.newline();
writeComponentGroup(out, format);
}
out << format.newline() << --format.indent() << '}';
}
else out << "null";
}
// points to the character that is currently being processed
const char* it;
// points to the end of the entire attribute's data, including
// all values
const char* const end;
// points to the respective entry of the componentGroupNames
// array that contains the name of the component group that
// is currently being processed
const char* const * componentGroupName;
// denotes the number of empty components within the current
// component group since the last component that contained
// at least one significant character.
unsigned currentComponent;
// will be OFTrue if the last value is empty, so that a
// trailing 'null' will be written
OFBool hasTrailingNull;
} lexer;
// initialize the lexer, return an error if the data
// of the current attribute can't be accessed for whatever
// reason
OFCondition result = DcmCharString::getString(OFconst_cast(char*&, lexer.it));
if (result.bad())
return result;
OFconst_cast(const char*&, lexer.end) = lexer.it + DcmCharString::getLength();
// write attribute name etc., even if the attribute is empty
DcmElement::writeJsonOpener(out, format);
// search for the first significant character within the
// entire data or the first value separator '\'
if (lexer.nextValue())
{
// write the fist value or 'null'
format.printValuePrefix(out);
lexer.writeCurrentValue(out, format);
// search for the first significant character within
// the next value or the next '\'
while (lexer.nextValue())
{
// write array element separator and the next
// value or 'null'
format.printNextArrayElementPrefix(out);
lexer.writeCurrentValue(out, format);
}
// close the array after the last value
format.printValueSuffix(out);
}
// close the current attribute
DcmElement::writeJsonCloser(out, format);
return EC_Normal;
}
// ********************************
OFCondition DcmPersonName::getNameComponents(OFString &lastName,
OFString &firstName,
OFString &middleName,
OFString &namePrefix,
OFString &nameSuffix,
const unsigned long pos,
const unsigned int componentGroup)
{
OFString dicomName;
OFCondition l_error = getOFString(dicomName, pos);
if (l_error.good())
l_error = getNameComponentsFromString(dicomName, lastName, firstName, middleName, namePrefix, nameSuffix, componentGroup);
else
{
lastName.clear();
firstName.clear();
middleName.clear();
namePrefix.clear();
nameSuffix.clear();
}
return l_error;
}
OFCondition DcmPersonName::getNameComponentsFromString(const OFString &dicomName,
OFString &lastName,
OFString &firstName,
OFString &middleName,
OFString &namePrefix,
OFString &nameSuffix,
const unsigned int componentGroup)
{
OFCondition l_error = EC_Normal;
/* initialize all name components */
lastName.clear();
firstName.clear();
middleName.clear();
namePrefix.clear();
nameSuffix.clear();
if (!dicomName.empty())
{
/* Excerpt from DICOM part 5:
"For the purpose of writing names in ideographic characters and in
phonetic characters, up to 3 groups of components may be used."
*/
OFString name;
l_error = getComponentGroup(dicomName, componentGroup, name);
/* check whether component group is valid (= non-empty) */
if (l_error.good() && !name.empty())
{
/* find caret separators */
/* (tbd: add more sophisticated heuristics for comma and space separated names) */
const size_t pos1 = name.find('^');
if (pos1 != OFString_npos)
{
const size_t pos2 = name.find('^', pos1 + 1);
lastName = name.substr(0, pos1);
if (pos2 != OFString_npos)
{
const size_t pos3 = name.find('^', pos2 + 1);
firstName = name.substr(pos1 + 1, pos2 - pos1 - 1);
if (pos3 != OFString_npos)
{
const size_t pos4 = name.find('^', pos3 + 1);
middleName = name.substr(pos2 + 1, pos3 - pos2 - 1);
if (pos4 != OFString_npos)
{
namePrefix = name.substr(pos3 + 1, pos4 - pos3 - 1);
nameSuffix = name.substr(pos4 + 1);
} else
namePrefix = name.substr(pos3 + 1);
} else
middleName = name.substr(pos2 + 1);
} else
firstName = name.substr(pos1 + 1);
} else
lastName = name;
}
}
return l_error;
}
// ********************************
OFCondition DcmPersonName::getFormattedName(OFString &formattedName,
const unsigned long pos,
const unsigned int componentGroup)
{
OFString dicomName;
OFCondition l_error = getOFString(dicomName, pos);
if (l_error.good())
l_error = getFormattedNameFromString(dicomName, formattedName, componentGroup);
else
formattedName.clear();
return l_error;
}
OFCondition DcmPersonName::getComponentGroup(const OFString &allCmpGroups,
const unsigned int groupNo,
OFString &cmpGroup)
{
OFCondition l_error = EC_IllegalParameter;
cmpGroup.clear();
/* Excerpt from DICOM part 5:
"For the purpose of writing names in ideographic characters and in
phonetic characters, up to 3 groups of components may be used."
*/
if (groupNo < 3)
{
// find component group (0..2)
const size_t posA = allCmpGroups.find('=');
if (posA != OFString_npos)
{
if (groupNo > 0)
{
const size_t posB = allCmpGroups.find('=', posA + 1);
if (posB != OFString_npos)
{
if (groupNo == 1)
cmpGroup = allCmpGroups.substr(posA + 1, posB - posA - 1);
else /* groupNo == 2 */
cmpGroup = allCmpGroups.substr(posB + 1);
l_error = EC_Normal;
}
else if (groupNo == 1)
{
cmpGroup = allCmpGroups.substr(posA + 1);
l_error = EC_Normal;
}
} else {
/* groupNo == 0 */
cmpGroup = allCmpGroups.substr(0, posA);
l_error = EC_Normal;
}
}
else if (groupNo == 0)
{
cmpGroup = allCmpGroups;
l_error = EC_Normal;
}
}
return l_error;
}
OFCondition DcmPersonName::getFormattedNameFromString(const OFString &dicomName,
OFString &formattedName,
const unsigned int componentGroup)
{
OFString lastName, firstName, middleName, namePrefix, nameSuffix;
OFCondition l_error = getNameComponentsFromString(dicomName, lastName, firstName, middleName, namePrefix, nameSuffix, componentGroup);
if (l_error.good())
l_error = getFormattedNameFromComponents(lastName, firstName, middleName, namePrefix, nameSuffix, formattedName);
else
formattedName.clear();
return l_error;
}
OFCondition DcmPersonName::getFormattedNameFromComponents(const OFString &lastName,
const OFString &firstName,
const OFString &middleName,
const OFString &namePrefix,
const OFString &nameSuffix,
OFString &formattedName)
{
formattedName.clear();
/* concatenate name components */
if (!namePrefix.empty())
formattedName += namePrefix;
if (!firstName.empty())
{
if (!formattedName.empty())
formattedName += ' ';
formattedName += firstName;
}
if (!middleName.empty())
{
if (!formattedName.empty())
formattedName += ' ';
formattedName += middleName;
}
if (!lastName.empty())
{
if (!formattedName.empty())
formattedName += ' ';
formattedName += lastName;
}
if (!nameSuffix.empty())
{
if (!formattedName.empty())
formattedName += ", ";
formattedName += nameSuffix;
}
return EC_Normal;
}
OFCondition DcmPersonName::getStringFromNameComponents(const OFString &lastName,
const OFString &firstName,
const OFString &middleName,
const OFString &namePrefix,
const OFString &nameSuffix,
OFString &dicomName)
{
const size_t middleLen = middleName.length();
const size_t prefixLen = namePrefix.length();
const size_t suffixLen = nameSuffix.length();
/* concatenate name components */
dicomName = lastName;
if (firstName.length() + middleLen + prefixLen + suffixLen > 0)
dicomName += '^';
dicomName += firstName;
if (middleLen + prefixLen + suffixLen > 0)
dicomName += '^';
dicomName += middleName;
if (prefixLen + suffixLen > 0)
dicomName += '^';
dicomName += namePrefix;
if (suffixLen > 0)
dicomName += '^';
dicomName += nameSuffix;
return EC_Normal;
}
// ********************************
OFCondition DcmPersonName::putNameComponents(const OFString &lastName,
const OFString &firstName,
const OFString &middleName,
const OFString &namePrefix,
const OFString &nameSuffix)
{
OFString dicomName;
/* concatenate name components */
OFCondition l_error = getStringFromNameComponents(lastName, firstName, middleName, namePrefix, nameSuffix, dicomName);
/* put element value */
if (l_error.good())
l_error = putOFStringArray(dicomName);
return l_error;
}
// ********************************
OFCondition DcmPersonName::checkStringValue(const OFString &value,
const OFString &vm,
const OFString &charset)
{
/* currently not checked: maximum length per component group (64 characters) */
OFCondition result = EC_Normal;
const size_t valLen = value.length();
if (valLen > 0)
{
size_t posStart = 0;
unsigned long vmNum = 0;
/* iterate over all value components */
while (posStart != OFString_npos)
{
++vmNum;
/* search for next component separator */
const size_t posEnd = value.find('\\', posStart);
const size_t length = (posEnd == OFString_npos) ? valLen - posStart : posEnd - posStart;
if (dcmEnableVRCheckerForStringValues.get())
{
/* check for non-ASCII characters (if default character set used) */
if (charset.empty() || (charset == "ISO_IR 6"))
{
if (DcmByteString::containsExtendedCharacters(value.c_str() + posStart, length))
{
result = EC_InvalidCharacter;
break;
}
}
/* currently, the VR checker only supports ASCII and Latin-1 */
if (charset.empty() || (charset == "ISO_IR 6") || (charset == "ISO_IR 100"))
{
/* check value representation (VR) */
const int vrID = DcmElement::scanValue(value, "pn", posStart, length);
if (vrID != 11)
{
result = EC_ValueRepresentationViolated;
break;
}
}
}
posStart = (posEnd == OFString_npos) ? posEnd : posEnd + 1;
}
if (result.good() && !vm.empty())
{
/* check value multiplicity (VM) */
result = DcmElement::checkVM(vmNum, vm);
}
}
return result;
}
|