/////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2002-2025, Open Design Alliance (the "Alliance"). // All rights reserved. // // This software and its documentation and related materials are owned by // the Alliance. The software may only be incorporated into application // programs owned by members of the Alliance, subject to a signed // Membership Agreement and Supplemental Software License Agreement with the // Alliance. The structure and organization of this software are the valuable // trade secrets of the Alliance and its suppliers. The software is also // protected by copyright law and international treaty provisions. Application // programs incorporating this software must include the following statement // with their copyright notices: // // This application incorporates Open Design Alliance software pursuant to a license // agreement with Open Design Alliance. // Open Design Alliance Copyright (C) 2002-2025 by Open Design Alliance. // All rights reserved. // // By use of this software, its documentation or related materials, you // acknowledge and accept the above terms. /////////////////////////////////////////////////////////////////////////////// #ifndef _WRITE_UTILS_H_ #define _WRITE_UTILS_H_ #include "OdaCommon.h" #include "DebugStuff.h" #include "OdStreamBuf.h" #include "OdPlatformSettings.h" #include "daiBuildOptions.h" #include "daiSettings.h" #include "StringArray.h" #define STL_USING_VECTOR #define STL_USING_STRING #include "OdaSTL.h" #include "OdDToStr.h" #include "TD_PackPush.h" class OdDbHandle; //DOM-IGNORE-BEGIN /** \details Implements the Data Access Interface (DAI) that provides functionality for manipulating data that is defined within the EXPRESS SCHEMA format. */ namespace OdDAI { /** \details Contains functionality that helps with writing data within the Data Access Interface (DAI). */ namespace WrUtils { /** Write error code.*/ const OdUInt32 writeErorCode = static_cast(-1); using ValueBuffer = OdUInt8*; /** \details Writes a value into a specified buffer. \param valueToWrite [in] A value to write. \param returnValue [out] A raw pointer to a written value that is created in the function and is returned to a calling subroutine. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 writeValue(const OdDbHandle& valueToWrite, ValueBuffer& returnValue); /** \details Writes a value into a specified buffer. \param valueToWrite [in] A value to write. \param returnValue [out] A raw pointer to a written value that is created in the function and is returned to a calling subroutine. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 writeValue(const int valueToWrite, ValueBuffer& returnValue); /** \details Writes a value into a specified buffer. \param valueToWrite [in] A value to write. \param returnValue [out] A raw pointer to a written value that is created in the function and is returned to a calling subroutine. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 writeValue(const long int& valueToWrite, ValueBuffer& returnValue); /** \details Writes a value into a specified buffer. \param valueToWrite [in] A value to write. \param returnValue [out] A raw pointer to a written value that is created in the function and is returned to a calling subroutine. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 writeValue(const OdUInt64& valueToWrite, ValueBuffer& returnValue); /** \details Writes a value into a specified buffer. \param valueToWrite [in] A value to write. \param returnValue [out] A raw pointer to a written value that is created in the function and is returned to a calling subroutine. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 writeValue(const double& valueToWrite, ValueBuffer& returnValue); /** \details Writes a value into a specified buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 writeValue(const OdDbHandle& valueToWrite, std::vector& writeBuffer); /** \details Writes a value into a specified buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 writeValue(const int valueToWrite, std::vector& writeBuffer); /** \details Writes a value into a specified buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 writeValue(const long int valueToWrite, std::vector& writeBuffer); /** \details Writes a value into a specified buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 writeValue(const OdUInt64& valueToWrite, std::vector& writeBuffer); /** \details Writes a value into a specified buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 writeValue(const double& valueToWrite, std::vector& writeBuffer); /** \details Writes a value into a specified buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 writeValue(const std::string& valueToWrite, std::vector& writeBuffer); /** \details Writes a value into a specified buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 writeValue(const OdAnsiString& valueToWrite, std::vector& writeBuffer); /** \details Writes a value into a specified buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 writeValue(const char* valueToWrite, std::vector& writeBuffer); /** \details Writes a value into a specified buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 writeValue(const char& valueToWrite, std::vector& writeBuffer); /** \details Appends a new value into a specified existing buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 appendValue(const OdDbHandle& valueToWrite, std::vector& writeBuffer); /** \details Appends a new value into a specified existing buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 appendValue(int valueToWrite, std::vector& writeBuffer); /** \details Appends a new value into a specified existing buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 appendValue(const OdUInt64& valueToWrite, std::vector& writeBuffer); /** \details Appends a new value into a specified existing buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 appendValue(const double& valueToWrite, std::vector& writeBuffer); /** \details Appends a new value into a specified existing buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 appendValue(const std::string& valueToWrite, std::vector& writeBuffer); /** \details Appends a new value into a specified existing buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 appendValue(const OdAnsiString& valueToWrite, std::vector& writeBuffer); /** \details Appends a new value into a specified existing buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 appendValue(const char* valueToWrite, std::vector& writeBuffer); /** \details Appends a new value into a specified existing buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ OdUInt32 appendValue(const char& valueToWrite, std::vector& writeBuffer); /** \details Appends a new value into a specified existing buffer. \param bufferToWrite [in] A value to write. \param sizeBuffer [in] A size of the buffer. \param writeBuffer [out] A buffer where the value is written. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ void appendValue(const char* bufferToWrite, OdUInt32 sizeBuffer, std::vector& writeBuffer); /** \details A template function that writes a value into a specified buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \param writeBufferSize [in] A size of the buffer. \param printfTemplate [in] A null-terminated string that contains a template for output with printf-like functions. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ template inline OdUInt32 writeValue(const ValueType& valueToWrite, BufferType& writeBuffer, size_t writeBufferSize, const char* printfTemplate) { #if defined(_MSC_VER) && _MSC_VER < 1900 return _snprintf(reinterpret_cast(&writeBuffer[0]), writeBufferSize, printfTemplate, valueToWrite); #else return snprintf(reinterpret_cast(&writeBuffer[0]), writeBufferSize, printfTemplate, valueToWrite); #endif } /** \details A template function that writes a value into a specified buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \param writeBufferSize [in] A size of the buffer. \param printfTemplate [in] A null-terminated string that contains a template for output with printf-like functions. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ template inline OdUInt32 writeValue(const double& valueToWrite, BufferType& writeBuffer, size_t writeBufferSize, const char* printfTemplate) { char *buf = reinterpret_cast(&writeBuffer[0]); #define DOUBLE_OUTPUT_VAR_OLD 1 #define DOUBLE_OUTPUT_VAR_USE_COMMA_REPLACE 2 #define DOUBLE_OUTPUT_VAR_USE_ODA_CONVERTER 4 #define DOUBLE_OUTPUT_CURRENT DOUBLE_OUTPUT_VAR_USE_ODA_CONVERTER #if DOUBLE_OUTPUT_CURRENT == DOUBLE_OUTPUT_VAR_USE_ODA_CONVERTER auto prec = settings().get_digitsNumber(); char format = {}; if (prec == 0) { prec = 17; format = 'G'; } else { format = 'f'; } odDToStr(buf, valueToWrite, format, prec); char* posDot = std::strchr(buf, '.'); OdUInt32 writtenSize = static_cast(std::strlen(buf)); #elif DOUBLE_OUTPUT_CURRENT == DOUBLE_OUTPUT_VAR_USE_COMMA_REPLACE // #if defined(_MSC_VER) && _MSC_VER < 1900 OdUInt32 writtenSize = _snprintf(buf, writeBufferSize, printfTemplate, valueToWrite); #else OdUInt32 writtenSize = snprintf(buf, writeBufferSize, printfTemplate, valueToWrite); #endif char* posComma = strchr(buf, ','); char* posDot = nullptr; if (posComma != nullptr) { *posComma = '.'; posDot = posComma; } else { posDot = strchr(buf, '.'); } #elif DOUBLE_OUTPUT_CURRENT == DOUBLE_OUTPUT_VAR_OLD #if defined(_MSC_VER) && _MSC_VER < 1900 OdUInt32 writtenSize = _snprintf(buf, writeBufferSize, printfTemplate, valueToWrite); #else OdUInt32 writtenSize = snprintf(buf, writeBufferSize, printfTemplate, valueToWrite); #endif char* posDot = strchr(buf, '.'); #else static_assert(0 && "double output is not defined") #endif // Terminating null character automatically appended by snprintf if (posDot == nullptr) { char *posE = strchr(buf, 'E'); if (posE == nullptr) { buf[writtenSize++] = '.'; buf[writtenSize] = 0; } else { Od_memmove_s(posE + 1, posE, strlen(posE)); buf[++writtenSize] = 0; *posE = '.'; } } else if (OdDAI::settings().get_digitsNumber() > 0) { while (buf[writtenSize-1] == '0') { --writtenSize; } } return writtenSize; } /** \details A template function that appends a value into a specified existing buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \param printfTemplate [in] A null-terminated string that contains a template for output with printf-like functions. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ template inline OdUInt32 appendValue(const ValueType& valueToWrite, std::vector& writeBuffer, const char* printfTemplate) { OdUInt8 appendBuffer[writeBufferSize + 1] = { 0 }; OdUInt32 countWrite = writeValue(valueToWrite, appendBuffer, writeBufferSize + 1, printfTemplate); if (countWrite == writeErorCode) { return writeErorCode; } size_t insertionIndex = writeBuffer.size(); writeBuffer.resize(insertionIndex + countWrite); Od_memcpy_s(&writeBuffer[insertionIndex], writeBuffer.size(), &appendBuffer[0], countWrite); return countWrite; } /** \details A template function that writes a value into a specified buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \param printfTemplate [in] A null-terminated string that contains a template for output with printf-like functions. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ template inline OdUInt32 writeValue(const ValueType& valueToWrite, std::vector& writeBuffer, const char* printfTemplate) { return appendValue(valueToWrite, writeBuffer, printfTemplate); } /** \details A template function that writes a value into a specified buffer. \param valueToWrite [in] A value to write. \param writeBuffer [out] A buffer where the value is written. \param printfTemplate [in] A null-terminated string that contains a template for output with printf-like functions. \returns An unsigned 32-bit integer that contains the number of written bytes, or -1 if the write operation fails. */ template inline OdUInt32 writeValue(const ValueType& valueToWrite, OdUInt8* writeBuffer, const char* printfTemplate) { return writeValue(valueToWrite, writeBuffer, writeBufferSize, printfTemplate); } } } #include "TD_PackPop.h" //DOM-IGNORE-END #endif