# HG changeset patch # User Sebastien Jodogne # Date 1461307663 -7200 # Node ID 9e0f408db796b5899f2eb7ff42dca8a9be973d0f # Parent 0c9c4e36c2b9a18be6a32fc7930d3db6b886f00d refactoring diff -r 0c9c4e36c2b9 -r 9e0f408db796 OrthancServer/ParsedDicomFile.cpp --- a/OrthancServer/ParsedDicomFile.cpp Fri Apr 15 21:42:10 2016 +0200 +++ b/OrthancServer/ParsedDicomFile.cpp Fri Apr 22 08:47:43 2016 +0200 @@ -583,31 +583,33 @@ } - static void ReplaceInternal(DcmDataset& dicom, - std::auto_ptr& element, - DicomReplaceMode mode) + static bool IsReplaceAllowed(DcmDataset& dicom, + const DcmTagKey& tag, + DicomReplaceMode mode) { - const DcmTagKey& tag = element->getTag(); - - if (!dicom.findAndDeleteElement(tag).good()) + if (dicom.findAndDeleteElement(tag).good()) { - // This field does not exist, act wrt. the specified "mode" + // This tag was existing, it has been deleted + return true; + } + else + { + // This tag was absent, act wrt. the specified "mode" switch (mode) { case DicomReplaceMode_InsertIfAbsent: - break; + return true; case DicomReplaceMode_ThrowIfAbsent: throw OrthancException(ErrorCode_InexistentItem); case DicomReplaceMode_IgnoreIfAbsent: - return; + return false; + + default: + throw OrthancException(ErrorCode_ParameterOutOfRange); } } - - // Either the tag was not existing, or the replace mode was set to - // "InsertIfAbsent" - InsertInternal(dicom, element.release()); } @@ -670,8 +672,15 @@ std::auto_ptr element(FromDcmtkBridge::CreateElementForTag(tag)); FromDcmtkBridge::FillElementWithString(*element, tag, utf8Value, false, GetEncoding()); - ReplaceInternal(*pimpl_->file_->getDataset(), element, mode); - UpdateStorageUid(tag, utf8Value, false); + + DcmDataset& dicom = *pimpl_->file_->getDataset(); + if (IsReplaceAllowed(dicom, element->getTag(), mode)) + { + // Either the tag was previously existing, or the replace mode + // was set to "InsertIfAbsent" + InsertInternal(dicom, element.release()); + UpdateStorageUid(tag, utf8Value, false); + } } @@ -683,17 +692,24 @@ InvalidateCache(); std::auto_ptr element(FromDcmtkBridge::FromJson(tag, value, decodeDataUriScheme, GetEncoding())); - ReplaceInternal(*pimpl_->file_->getDataset(), element, mode); - if (tag == DICOM_TAG_SOP_CLASS_UID || - tag == DICOM_TAG_SOP_INSTANCE_UID) + DcmDataset& dicom = *pimpl_->file_->getDataset(); + if (IsReplaceAllowed(dicom, element->getTag(), mode)) { - if (value.type() != Json::stringValue) + // Either the tag was previously existing, or the replace mode + // was set to "InsertIfAbsent" + InsertInternal(dicom, element.release()); + + if (tag == DICOM_TAG_SOP_CLASS_UID || + tag == DICOM_TAG_SOP_INSTANCE_UID) { - throw OrthancException(ErrorCode_BadParameterType); + if (value.type() != Json::stringValue) + { + throw OrthancException(ErrorCode_BadParameterType); + } + + UpdateStorageUid(tag, value.asString(), decodeDataUriScheme); } - - UpdateStorageUid(tag, value.asString(), decodeDataUriScheme); } }