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/.
14 #include "patattr.hxx"
15 #include "scitems.hxx"
16 #include "document.hxx"
17 #include "cellform.hxx"
18 #include "cellvalue.hxx"
20 #define DEBUG_CSV_HANDLER 0
24 OUString
getConditionalFormatString(ScDocument
* pDoc
, SCCOL nCol
, SCROW nRow
, SCTAB nTab
)
29 aCell
.assign(*pDoc
, ScAddress(nCol
, nRow
, nTab
));
33 const SfxItemSet
* pCondSet
= pDoc
->GetCondResult( nCol
, nRow
, nTab
);
34 const ScPatternAttr
* pPattern
= pDoc
->GetPattern(nCol
, nRow
, nTab
);
35 SvNumberFormatter
* pFormatter
= pDoc
->GetFormatTable();
36 sal_uInt32 nFormat
= pPattern
->GetNumberFormat( pFormatter
, pCondSet
);
37 aString
= ScCellFormat::GetString(*pDoc
, ScAddress(nCol
, nRow
, nTab
), nFormat
, &pColor
, *pFormatter
);
41 OString
createErrorMessage(SCCOL nCol
, SCROW nRow
, SCTAB nTab
)
43 OStringBuffer
aString("Error in Table: ");
44 aString
.append(static_cast<sal_Int32
>(nTab
));
45 aString
.append(" Column: ");
46 aString
.append(static_cast<sal_Int32
>(nCol
));
47 aString
.append(" Row: ");
49 return aString
.makeStringAndClear();
52 OString
createErrorMessage(SCCOL nCol
, SCROW nRow
, SCTAB nTab
, const OUString
& rExpectedString
, const OUString
& rString
)
54 OStringBuffer
aString(createErrorMessage(nCol
, nRow
, nTab
));
55 aString
.append("; Expected: '");
56 aString
.append(OUStringToOString(rExpectedString
, RTL_TEXTENCODING_UTF8
));
57 aString
.append("' Found: '");
58 aString
.append(OUStringToOString(rString
, RTL_TEXTENCODING_UTF8
));
60 return aString
.makeStringAndClear();
63 OString
createErrorMessage(SCCOL nCol
, SCROW nRow
, SCTAB nTab
, double aExpected
, double aValue
)
65 OStringBuffer
aString(createErrorMessage(nCol
, nRow
, nTab
));
66 aString
.append("; Expected: '");
67 aString
.append(aExpected
);
68 aString
.append("' Found: '");
69 aString
.append(aValue
);
71 return aString
.makeStringAndClear();
77 enum StringType
{ PureString
, FormulaValue
, StringValue
};
82 csv_handler(ScDocument
* pDoc
, SCTAB nTab
, StringType eType
= StringValue
):
87 meStringType(eType
) {}
101 void cell(const char* p
, size_t n
)
103 #if DEBUG_CSV_HANDLER
104 std::cout
<< "Col: " << mnCol
<< " Row: " << mnRow
<< std::endl
;
105 #endif //DEBUG_CSV_HANDLER
109 if (!mpDoc
->GetString(mnCol
, mnRow
, mnTab
).isEmpty())
111 // cell in the document is not empty.
112 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol
, mnRow
, mnTab
).getStr(), false);
115 else if (meStringType
== PureString
)
117 OUString
aCSVString(p
, n
, RTL_TEXTENCODING_UTF8
);
118 OUString aString
= mpDoc
->GetString(mnCol
, mnRow
, mnTab
);
120 #if DEBUG_CSV_HANDLER
121 std::cout
<< "String: " << OUStringToOString(aString
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
122 std::cout
<< "CSVString: " << OUStringToOString(aCSVString
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
123 std::cout
<< "result: " << (int)(aCSVString
== aString
) << std::endl
;
124 #endif //DEBUG_CSV_HANDLER
126 CPPUNIT_ASSERT_EQUAL_MESSAGE(createErrorMessage(mnCol
, mnRow
, mnTab
, aCSVString
, aString
).getStr(), aString
, aCSVString
);
130 char* pRemainingChars
= NULL
;
131 std::string
aStr(p
, n
);
132 double nValue
= strtod(&aStr
[0], &pRemainingChars
);
133 if (*pRemainingChars
)
136 switch (meStringType
)
139 aString
= mpDoc
->GetString(mnCol
, mnRow
, mnTab
);
142 mpDoc
->GetFormula(mnCol
, mnRow
, mnTab
, aString
);
147 OUString
aCSVString(p
, n
, RTL_TEXTENCODING_UTF8
);
148 #if DEBUG_CSV_HANDLER
149 std::cout
<< "String: " << OUStringToOString(aString
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
150 std::cout
<< "CSVString: " << OUStringToOString(aCSVString
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
151 std::cout
<< "result: " << (int)(aCSVString
== aString
) << std::endl
;
152 #endif //DEBUG_CSV_HANDLER
154 CPPUNIT_ASSERT_EQUAL_MESSAGE(createErrorMessage(mnCol
, mnRow
, mnTab
, aCSVString
, aString
).getStr(), aString
, aCSVString
);
159 mpDoc
->GetValue(mnCol
, mnRow
, mnTab
, aValue
);
160 #if DEBUG_CSV_HANDLER
161 std::cout
<< "Value: " << aValue
<< std::endl
;
162 std::cout
<< "CSVValue: " << nValue
<< std::endl
;
163 std::cout
<< "result: " << (int)(aValue
== nValue
) << std::endl
;
164 #endif //DEBUG_CSV_HANDLER
165 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol
, mnRow
, mnTab
, nValue
, aValue
).getStr(), aValue
== nValue
);
176 StringType meStringType
;
180 class conditional_format_handler
183 conditional_format_handler(ScDocument
* pDoc
, SCTAB nTab
):
189 void begin_parse() {}
201 void cell(const char* p
, size_t n
)
203 #if DEBUG_CSV_HANDLER
204 std::cout
<< "Col: " << mnCol
<< " Row: " << mnRow
<< std::endl
;
205 #endif //DEBUG_CSV_HANDLER
206 OUString aString
= getConditionalFormatString(mpDoc
, mnCol
, mnRow
, mnTab
);
207 OUString
aCSVString(p
, n
, RTL_TEXTENCODING_UTF8
);
208 #if DEBUG_CSV_HANDLER
209 std::cout
<< "String: " << OUStringToOString(aString
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
210 std::cout
<< "CSVString: " << OUStringToOString(aCSVString
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
211 std::cout
<< "result: " << (int)(aCSVString
== aString
) << std::endl
;
212 #endif //DEBUG_CSV_HANDLER
213 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol
, mnRow
, mnTab
, aCSVString
, aString
).getStr(), aString
== aCSVString
);
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */