1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
12 #include <sal/config.h>
14 #include <string_view>
16 #include "qahelper.hxx"
18 #include <patattr.hxx>
19 #include <document.hxx>
20 #include <cellform.hxx>
21 #include <cellvalue.hxx>
23 #include <orcus/csv_parser.hpp>
25 #define DEBUG_CSV_HANDLER 0
27 inline OUString
getConditionalFormatString(ScDocument
* pDoc
, SCCOL nCol
, SCROW nRow
, SCTAB nTab
)
31 ScRefCellValue
aCell(*pDoc
, ScAddress(nCol
, nRow
, nTab
));
35 const SfxItemSet
* pCondSet
= pDoc
->GetCondResult( nCol
, nRow
, nTab
);
36 const ScPatternAttr
* pPattern
= pDoc
->GetPattern(nCol
, nRow
, nTab
);
37 ScInterpreterContext
& rContext
= pDoc
->GetNonThreadedContext();
38 sal_uInt32 nFormat
= pPattern
->GetNumberFormat( rContext
, pCondSet
);
39 aString
= ScCellFormat::GetString(*pDoc
, ScAddress(nCol
, nRow
, nTab
), nFormat
, &pColor
, &rContext
);
43 inline OString
createErrorMessage(SCCOL nCol
, SCROW nRow
, SCTAB nTab
)
45 return "Error in Table: " +
46 OString::number(static_cast<sal_Int32
>(nTab
)) +
48 OString::number(static_cast<sal_Int32
>(nCol
)) +
50 OString::number(nRow
);
53 inline OString
createErrorMessage(SCCOL nCol
, SCROW nRow
, SCTAB nTab
, std::u16string_view rExpectedString
, std::u16string_view rString
)
55 return createErrorMessage(nCol
, nRow
, nTab
) + "; Expected: '"
56 + OUStringToOString(rExpectedString
, RTL_TEXTENCODING_UTF8
) + "' Found: '"
57 + OUStringToOString(rString
, RTL_TEXTENCODING_UTF8
) + "'";
60 inline OString
createErrorMessage(SCCOL nCol
, SCROW nRow
, SCTAB nTab
, double aExpected
, double aValue
)
62 return createErrorMessage(nCol
, nRow
, nTab
) + "; Expected: '" + OString::number(aExpected
)
63 + "' Found: '" + OString::number(aValue
) + "'";
67 class csv_handler
: public orcus::csv_handler
70 csv_handler(ScDocument
* pDoc
, SCTAB nTab
, StringType eType
):
75 meStringType(eType
) {}
83 void cell(std::string_view value
, bool /*transient*/)
85 const char* p
= value
.data();
86 std::size_t n
= value
.size();
89 std::cout
<< "Col: " << mnCol
<< " Row: " << mnRow
<< std::endl
;
90 #endif //DEBUG_CSV_HANDLER
94 if (!mpDoc
->GetString(mnCol
, mnRow
, mnTab
).isEmpty())
96 // cell in the document is not empty.
97 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol
, mnRow
, mnTab
).getStr(), false);
100 else if (meStringType
== StringType::PureString
)
102 OUString
aCSVString(p
, n
, RTL_TEXTENCODING_UTF8
);
103 OUString aString
= mpDoc
->GetString(mnCol
, mnRow
, mnTab
);
105 #if DEBUG_CSV_HANDLER
106 std::cout
<< "String: " << OUStringToOString(aString
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
107 std::cout
<< "CSVString: " << OUStringToOString(aCSVString
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
108 std::cout
<< "result: " << (int)(aCSVString
== aString
) << std::endl
;
109 #endif //DEBUG_CSV_HANDLER
111 CPPUNIT_ASSERT_EQUAL_MESSAGE(createErrorMessage(mnCol
, mnRow
, mnTab
, aCSVString
, aString
).getStr(), aCSVString
, aString
);
115 char* pRemainingChars
= nullptr;
116 std::string
aStr(p
, n
);
117 double nValue
= strtod(&aStr
[0], &pRemainingChars
);
118 if (*pRemainingChars
)
121 switch (meStringType
)
123 case StringType::StringValue
:
124 aString
= mpDoc
->GetString(mnCol
, mnRow
, mnTab
);
129 OUString
aCSVString(p
, n
, RTL_TEXTENCODING_UTF8
);
130 #if DEBUG_CSV_HANDLER
131 std::cout
<< "String: " << OUStringToOString(aString
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
132 std::cout
<< "CSVString: " << OUStringToOString(aCSVString
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
133 std::cout
<< "result: " << (int)(aCSVString
== aString
) << std::endl
;
134 #endif //DEBUG_CSV_HANDLER
136 CPPUNIT_ASSERT_EQUAL_MESSAGE(createErrorMessage(mnCol
, mnRow
, mnTab
, aCSVString
, aString
).getStr(), aCSVString
, aString
);
140 double aValue
= mpDoc
->GetValue(mnCol
, mnRow
, mnTab
);
141 #if DEBUG_CSV_HANDLER
142 std::cout
<< "Value: " << aValue
<< std::endl
;
143 std::cout
<< "CSVValue: " << nValue
<< std::endl
;
144 std::cout
<< "result: " << (int)(aValue
== nValue
) << std::endl
;
145 #endif //DEBUG_CSV_HANDLER
146 CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE(createErrorMessage(mnCol
, mnRow
, mnTab
, nValue
, aValue
).getStr(), nValue
, aValue
, 1e-10);
157 StringType meStringType
;
160 class conditional_format_handler
: public orcus::csv_handler
163 conditional_format_handler(ScDocument
* pDoc
, SCTAB nTab
):
175 void cell(std::string_view value
, bool /*transient*/)
177 #if DEBUG_CSV_HANDLER
178 std::cout
<< "Col: " << mnCol
<< " Row: " << mnRow
<< std::endl
;
179 #endif //DEBUG_CSV_HANDLER
180 OUString aString
= getConditionalFormatString(mpDoc
, mnCol
, mnRow
, mnTab
);
181 OUString
aCSVString(value
.data(), value
.size(), RTL_TEXTENCODING_UTF8
);
182 #if DEBUG_CSV_HANDLER
183 std::cout
<< "String: " << OUStringToOString(aString
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
184 std::cout
<< "CSVString: " << OUStringToOString(aCSVString
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
185 std::cout
<< "result: " << (int)(aCSVString
== aString
) << std::endl
;
186 #endif //DEBUG_CSV_HANDLER
187 CPPUNIT_ASSERT_EQUAL_MESSAGE(createErrorMessage(mnCol
, mnRow
, mnTab
, aCSVString
, aString
).getStr(), aCSVString
, aString
);
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */