11#ifndef OPENXLSX_XLUTILITIES_HPP
12#define OPENXLSX_XLUTILITIES_HPP
38 unsigned char uc =
static_cast<unsigned char>(c);
39 if (uc < 0x20 && uc != 0x09 && uc != 0x0A && uc != 0x0D) {
54 result.reserve(sv.size());
56 unsigned char uc =
static_cast<unsigned char>(c);
57 if (uc >= 0x20 || uc == 0x09 || uc == 0x0A || uc == 0x0D) {
105 if (cellRef ==
nullptr or *cellRef ==
'\0')
return 0;
108 const char* p = cellRef;
111 while (*p >=
'A' and *p <=
'Z') {
112 colNo = colNo * 26 +
static_cast<uint32_t
>(*p -
'A' + 1);
117 return (colNo > 0 and colNo <=
MAX_COLS) ?
static_cast<uint16_t
>(colNo) : 0;
134 temp[idx++] =
static_cast<char>(
'A' + (colNo % 26));
140 while (idx > 0) { buffer[j++] = temp[--idx]; }
154 #if defined(__GNUC__) || defined(__clang__)
155__attribute__((always_inline))
inline char*
makeCellAddress(uint32_t row, uint16_t col,
char* buffer)
noexcept
156#elif defined(_MSC_VER)
157__forceinline
char*
makeCellAddress(uint32_t row, uint16_t col,
char* buffer)
noexcept
168 for (
const char* c = colLetters; *c; ++c) { *p++ = *c; }
175 rowStr[idx++] =
static_cast<char>(
'0' + (r % 10));
181 while (idx > 0) { *p++ = rowStr[--idx]; }
202 using namespace std::literals::string_literals;
229 using namespace std::literals::string_literals;
234 return "node_document"s;
236 return "node_element"s;
238 return "node_pcdata"s;
240 return "node_cdata"s;
242 return "node_comment"s;
246 return "node_declaration"s;
248 return "node_doctype"s;
265 return "Relationships";
267 return "WorkbookMacroEnabled";
273 return "ExternalLink";
279 return "SharedStrings";
287 return "ChartColorStyle";
289 return "ControlProperties";
291 return "CalculationChain";
297 return "SlicerCache";
301 return "PivotCacheDefinition";
303 return "PivotCacheRecords";
306 return "CoreProperties";
308 return "ExtendedProperties";
310 return "CustomProperties";
318 return "ThreadedComments";
339 return "CoreProperties";
341 return "ExtendedProperties";
343 return "CustomProperties";
351 return "Dialogsheet";
355 return "CalculationChain";
357 return "ExternalLink";
359 return "ExternalLinkPath";
369 return "ChartColorStyle";
377 return "SharedStrings";
379 return "PrinterSettings";
385 return "SlicerCache";
389 return "PivotCacheDefinition";
391 return "PivotCacheRecords";
394 return "ControlProperties";
400 return "ThreadedComments";
417 using namespace std::literals::string_literals;
418 throw XLCellAddressError(
"rowNumber "s + std::to_string(rowNumber) +
" is outside valid range [1;"s +
423 if (hintRowNumber && hintRowNode && *hintRowNumber == rowNumber && !hintRowNode->empty()) {
430 if (hintRowNumber && hintRowNode && !hintRowNode->empty() && rowNumber > *hintRowNumber) {
431 result = *hintRowNode;
441 else if (hintRowNumber && hintRowNode && !hintRowNode->empty() && rowNumber < *hintRowNumber) {
442 result = *hintRowNode;
482 if (hintRowNumber && hintRowNode) {
483 *hintRowNumber = rowNumber;
484 *hintRowNode = result;
506 if (not cols.
empty()) {
508 while (not col.
empty()) {
513 for (
int i = minCol; i <= maxCol && i <= count; ++i) {
514 if (i > 0) styles[i - 1] = style;
525 if (not cols.
empty()) {
527 while (not col.
empty()) {
547 const std::string& cellRef,
550 std::vector<XLStyleIndex>
const& colStyles = {})
555 if (rowStyle.
empty()) {
556 if (colStyles.size() > 0)
577 std::vector<XLStyleIndex>
const& colStyles = {})
582 if (rowStyle.
empty()) {
583 if (colStyles.size() > 0)
605 uint16_t columnNumber,
606 uint32_t rowNumber = 0,
607 std::vector<XLStyleIndex>
const& colStyles = {},
608 uint16_t* hintColNumber =
nullptr,
609 XMLNode* hintCellNode =
nullptr)
612 using namespace std::literals::string_literals;
613 throw XLException(
"XLWorksheet::column: columnNumber "s + std::to_string(columnNumber) +
" is outside allowed range [1;"s +
621 if (hintColNumber && hintCellNode && *hintColNumber == columnNumber && !hintCellNode->
empty() && hintCellNode->
parent() == rowNode) {
622 return *hintCellNode;
628 if (hintColNumber && hintCellNode && !hintCellNode->
empty() && hintCellNode->
parent() == rowNode) {
629 cellNode = *hintCellNode;
630 if (columnNumber > *hintColNumber) {
631 uint16_t currentCol = *hintColNumber;
632 while (!cellNode.
empty() && currentCol < columnNumber) {
636 if (currentCol > columnNumber || cellNode.
empty()) {
639 char cellAddrBuf[16];
645 else if (columnNumber < *hintColNumber) {
646 uint16_t currentCol = *hintColNumber;
647 while (!cellNode.
empty() && currentCol > columnNumber) {
651 if (currentCol < columnNumber || cellNode.
empty()) {
654 char cellAddrBuf[16];
667 if (cellNode.
empty() or (lastCellCol < columnNumber)) {
669 char cellAddrBuf[16];
673 else if (lastCellCol - columnNumber < columnNumber) {
674 uint16_t currentCol = lastCellCol;
675 while (not cellNode.
empty() and (currentCol > columnNumber)) {
679 if (cellNode.
empty() or (currentCol < columnNumber)) {
682 char cellAddrBuf[16];
690 while (currentCol < columnNumber) {
694 if (currentCol > columnNumber) {
696 char cellAddrBuf[16];
704 if (hintColNumber && hintCellNode) {
705 *hintColNumber = columnNumber;
706 *hintCellNode = cellNode;
720 if (data.size() < 8)
return {0, 0};
723 if (
reinterpret_cast<const uint8_t*
>(data.data())[0] == 0x89 and data.substr(1, 3) ==
"PNG") {
724 if (data.size() < 24)
return {0, 0};
726 const uint8_t* p =
reinterpret_cast<const uint8_t*
>(data.data());
727 uint32_t w = (
static_cast<uint32_t
>(p[16]) << 24) | (
static_cast<uint32_t
>(p[17]) << 16) | (
static_cast<uint32_t
>(p[18]) << 8) |
728 static_cast<uint32_t
>(p[19]);
729 uint32_t h = (
static_cast<uint32_t
>(p[20]) << 24) | (
static_cast<uint32_t
>(p[21]) << 16) | (
static_cast<uint32_t
>(p[22]) << 8) |
730 static_cast<uint32_t
>(p[23]);
735 if (
reinterpret_cast<const uint8_t*
>(data.data())[0] == 0xFF and
reinterpret_cast<const uint8_t*
>(data.data())[1] == 0xD8) {
737 while (offset + 4 < data.size()) {
738 const uint8_t* p =
reinterpret_cast<const uint8_t*
>(data.data()) + offset;
739 if (p[0] != 0xFF)
break;
740 uint8_t marker = p[1];
741 uint32_t length = (
static_cast<uint32_t
>(p[2]) << 8) |
static_cast<uint32_t
>(p[3]);
744 if ((marker >= 0xC0 and marker <= 0xC3) or (marker >= 0xC5 and marker <= 0xC7) or (marker >= 0xC9 and marker <= 0xCB) ||
745 (marker >= 0xCD and marker <= 0xCF))
747 if (offset + 9 > data.size())
return {0, 0};
749 uint32_t h = (
static_cast<uint32_t
>(p[5]) << 8) |
static_cast<uint32_t
>(p[6]);
750 uint32_t w = (
static_cast<uint32_t
>(p[7]) << 8) |
static_cast<uint32_t
>(p[8]);
753 offset += length + 2;
Layer-2 OOXML DOM idioms built on the frozen XLXmlParser substrate.
XMLNode previous_sibling_of_type(T type_=node_element) const
Definition XLXmlParser.hpp:220
XMLAttribute append_attribute(const char *name_)
Definition XLXmlParser.cpp:114
XMLNode prepend_child(T type_=node_element)
Definition XLXmlParser.hpp:164
XMLNode child(const char *name_) const
Definition XLXmlParser.cpp:150
XMLNode last_child_of_type(T type_=node_element) const
Definition XLXmlParser.hpp:208
XMLAttribute attribute(const char *name_) const
Definition XLXmlParser.cpp:96
XMLNode insert_child_before(T type_, const XMLNode &node)
Definition XLXmlParser.hpp:176
XMLNode first_child_of_type(T type_=node_element) const
Definition XLXmlParser.hpp:204
bool empty() const
Definition XLXmlParser.cpp:58
XMLNode next_sibling_of_type(T type_=node_element) const
Definition XLXmlParser.hpp:216
XMLNode insert_child_after(T type_, const XMLNode &node)
Definition XLXmlParser.hpp:172
XMLNode append_child(T type_=node_element)
Definition XLXmlParser.hpp:160
XMLNode parent() const
Definition XLXmlParser.cpp:144
Definition XLException.hpp:60
Definition XLException.hpp:33
Definition XLException.hpp:78
Definition XLXmlParser.hpp:283
bool set_value(const char *rhs)
Definition XLXmlParser.cpp:533
unsigned long long as_ullong(unsigned long long def=0) const
Definition XLXmlParser.cpp:623
unsigned int as_uint(unsigned int def=0) const
Definition XLXmlParser.cpp:599
const char * value() const
Definition XLXmlParser.cpp:521
bool empty() const
Definition XLXmlParser.cpp:513
int as_int(int def=0) const
Definition XLXmlParser.cpp:593
Definition IXLCellProvider.hpp:24
std::string XLValueTypeString(XLValueType t)
Get a string representation of pugi::xml_node_type.
Definition XLUtilities.hpp:200
constexpr uint32_t MAX_ROWS
Definition XLConstants.hpp:19
constexpr XMLNodeType node_comment
Definition XLXmlParser.hpp:74
constexpr XMLNodeType node_document
Definition XLXmlParser.hpp:70
void ignore(const T &)
Get rid of compiler warnings about unused variables (-Wunused-variable) or unused parameters (-Wunusu...
Definition XLUtilities.hpp:192
std::string XLContentTypeString(OpenXLSX::XLContentType const &t)
Get a string representation of OpenXLSX::XLContentType.
Definition XLUtilities.hpp:258
constexpr XMLNodeType node_cdata
Definition XLXmlParser.hpp:73
int XMLNodeType
Definition XLXmlParser.hpp:68
constexpr const XLStyleIndex XLDefaultCellFormat
Definition XLStyles_Common.hpp:54
XLContentType
Definition XLContentTypes.hpp:38
@ Workbook
Definition XLContentTypes.hpp:39
@ Slicer
Definition XLContentTypes.hpp:64
@ Chart
Definition XLContentTypes.hpp:49
@ ControlProperties
Definition XLContentTypes.hpp:52
@ CoreProperties
Definition XLContentTypes.hpp:55
@ SharedStrings
Definition XLContentTypes.hpp:47
@ ChartColorStyle
Definition XLContentTypes.hpp:51
@ Styles
Definition XLContentTypes.hpp:46
@ Table
Definition XLContentTypes.hpp:59
@ Worksheet
Definition XLContentTypes.hpp:42
@ ExternalLink
Definition XLContentTypes.hpp:44
@ VBAProject
Definition XLContentTypes.hpp:54
@ PivotCacheDefinition
Definition XLContentTypes.hpp:66
@ Drawing
Definition XLContentTypes.hpp:48
@ PivotCacheRecords
Definition XLContentTypes.hpp:67
@ PivotTable
Definition XLContentTypes.hpp:63
@ CalculationChain
Definition XLContentTypes.hpp:53
@ Hyperlink
Definition XLContentTypes.hpp:61
@ Comments
Definition XLContentTypes.hpp:58
@ Unknown
Definition XLContentTypes.hpp:62
@ SlicerCache
Definition XLContentTypes.hpp:65
@ ExtendedProperties
Definition XLContentTypes.hpp:56
@ Persons
Definition XLContentTypes.hpp:69
@ WorkbookMacroEnabled
Definition XLContentTypes.hpp:41
@ ChartStyle
Definition XLContentTypes.hpp:50
@ Theme
Definition XLContentTypes.hpp:45
@ CustomProperties
Definition XLContentTypes.hpp:57
@ Relationships
Definition XLContentTypes.hpp:40
@ Chartsheet
Definition XLContentTypes.hpp:43
@ VMLDrawing
Definition XLContentTypes.hpp:60
@ ThreadedComments
Definition XLContentTypes.hpp:68
XLRelationshipType
An enum of the possible relationship (or XML document) types used in relationship (....
Definition XLRelationships.hpp:68
@ Workbook
Definition XLRelationships.hpp:72
@ Slicer
Definition XLRelationships.hpp:97
@ Chart
Definition XLRelationships.hpp:82
@ ControlProperties
Definition XLRelationships.hpp:91
@ CoreProperties
Definition XLRelationships.hpp:69
@ SharedStrings
Definition XLRelationships.hpp:88
@ ChartColorStyle
Definition XLRelationships.hpp:84
@ Macrosheet
Definition XLRelationships.hpp:76
@ Person
Definition XLRelationships.hpp:102
@ Styles
Definition XLRelationships.hpp:81
@ Table
Definition XLRelationships.hpp:93
@ Worksheet
Definition XLRelationships.hpp:73
@ ExternalLink
Definition XLRelationships.hpp:78
@ VBAProject
Definition XLRelationships.hpp:90
@ PivotCacheDefinition
Definition XLRelationships.hpp:99
@ Drawing
Definition XLRelationships.hpp:86
@ PrinterSettings
Definition XLRelationships.hpp:89
@ PivotCacheRecords
Definition XLRelationships.hpp:100
@ PivotTable
Definition XLRelationships.hpp:96
@ CalculationChain
Definition XLRelationships.hpp:77
@ Hyperlink
Definition XLRelationships.hpp:94
@ Comments
Definition XLRelationships.hpp:92
@ Unknown
Definition XLRelationships.hpp:95
@ SlicerCache
Definition XLRelationships.hpp:98
@ ExtendedProperties
Definition XLRelationships.hpp:70
@ ExternalLinkPath
Definition XLRelationships.hpp:79
@ Image
Definition XLRelationships.hpp:85
@ Dialogsheet
Definition XLRelationships.hpp:75
@ ChartStyle
Definition XLRelationships.hpp:83
@ Theme
Definition XLRelationships.hpp:80
@ CustomProperties
Definition XLRelationships.hpp:71
@ Chartsheet
Definition XLRelationships.hpp:74
@ VMLDrawing
Definition XLRelationships.hpp:87
@ ThreadedComments
Definition XLRelationships.hpp:101
char * makeCellAddress(uint32_t row, uint16_t col, char *buffer) noexcept
Generate cell address string directly without creating XLCellReference object. Performance optimizati...
Definition XLUtilities.hpp:159
constexpr XMLNodeType node_element
Definition XLXmlParser.hpp:71
void setDefaultCellAttributes(XMLNode cellNode, const std::string &cellRef, XMLNode rowNode, uint16_t colNo, std::vector< XLStyleIndex > const &colStyles={})
set the cell reference, and a default cell style attribute if and only if row or column style is !...
Definition XLUtilities.hpp:546
std::string xml_node_type_string(XMLNodeType t)
Get a string representation of XMLNodeType.
Definition XLUtilities.hpp:227
constexpr uint16_t MAX_COLS
Definition XLConstants.hpp:18
constexpr XMLNodeType node_doctype
Definition XLXmlParser.hpp:76
XLStyleIndex getColumnStyle(XMLNode rowNode, uint16_t colNo)
Definition XLUtilities.hpp:522
XLValueType
Enum defining the valid value types for a an Excel spreadsheet cell.
Definition XLCellValue.hpp:47
@ Float
Definition XLCellValue.hpp:47
@ RichText
Definition XLCellValue.hpp:47
@ String
Definition XLCellValue.hpp:47
@ Boolean
Definition XLCellValue.hpp:47
@ Error
Definition XLCellValue.hpp:47
@ Integer
Definition XLCellValue.hpp:47
@ Empty
Definition XLCellValue.hpp:47
std::string XLRelationshipTypeString(OpenXLSX::XLRelationshipType const &t)
Get a string representation of OpenXLSX::XLRelationshipType.
Definition XLUtilities.hpp:334
constexpr XMLNodeType node_pi
Definition XLXmlParser.hpp:77
OpenXLSX_xml_node XMLNode
Definition XLXmlParser.hpp:64
std::vector< XLStyleIndex > getColumnStyles(XMLNode rowNode, uint16_t count)
get the style attribute s for the indicated column, if any is set
Definition XLUtilities.hpp:502
std::pair< uint32_t, uint32_t > getImageDimensions(const std::string &data)
Extract image dimensions (width and height) from raw image data (PNG/JPEG).
Definition XLUtilities.hpp:718
bool isCleanXmlString(std::string_view sv) noexcept
Checks if a string contains only valid XML 1.0 characters.
Definition XLUtilities.hpp:35
constexpr XMLNodeType node_null
Definition XLXmlParser.hpp:69
XMLNode getCellNode(XMLNode rowNode, uint16_t columnNumber, uint32_t rowNumber=0, std::vector< XLStyleIndex > const &colStyles={}, uint16_t *hintColNumber=nullptr, XMLNode *hintCellNode=nullptr)
Retrieve the xml node representing the cell at the given row and column. If the node doesn't exist,...
Definition XLUtilities.hpp:604
size_t XLStyleIndex
Definition XLStyles_Common.hpp:36
void appendEscaped(std::string &out, std::string_view sv)
Fast XML escape: appends escaped characters to out to avoid intermediate allocations.
Definition XLUtilities.hpp:69
XMLNode getRowNode(XMLNode sheetDataNode, uint32_t rowNumber, uint32_t *hintRowNumber=nullptr, XMLNode *hintRowNode=nullptr)
Definition XLUtilities.hpp:414
constexpr XMLNodeType node_pcdata
Definition XLXmlParser.hpp:72
constexpr XMLNodeType node_declaration
Definition XLXmlParser.hpp:75
char * columnToLetters(uint16_t colNo, char *buffer) noexcept
Convert column number to column letter string (A, B, ..., Z, AA, AB, ..., XFD) Performance optimizati...
Definition XLUtilities.hpp:127
std::string sanitizeXmlString(std::string_view sv)
Removes invalid XML 1.0 control characters from a string to prevent file corruption.
Definition XLUtilities.hpp:51
uint16_t extractColumnFromCellRef(const char *cellRef) noexcept
Lightweight function to extract column number from a cell reference string. This is a performance-opt...
Definition XLUtilities.hpp:103