1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * Version: MPL 1.1 / GPLv3+ / LGPLv3+
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License or as specified alternatively below. You may obtain a copy of
8 * the License at http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * Major Contributor(s):
16 * Copyright (C) 2011 Markus Mohrhard <markus.mohrhard@googlemail.com> (initial developer)
18 * All Rights Reserved.
20 * For minor contributions see the git repository.
22 * Alternatively, the contents of this file may be used under the terms of
23 * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
24 * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
25 * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
26 * instead of those above.
33 #include "patattr.hxx"
34 #include "scitems.hxx"
35 #include "document.hxx"
36 #include "cellform.hxx"
38 #define DEBUG_CSV_HANDLER 0
42 rtl::OUString
getConditionalFormatString(ScDocument
* pDoc
, SCCOL nCol
, SCROW nRow
, SCTAB nTab
)
44 rtl::OUString aString
;
46 ScBaseCell
* pCell
= pDoc
->GetCell(ScAddress(nCol
, nRow
, nTab
));
50 const SfxItemSet
* pCondSet
= pDoc
->GetCondResult( nCol
, nRow
, nTab
);
51 const ScPatternAttr
* pPattern
= pDoc
->GetPattern(nCol
, nRow
, nTab
);
52 SvNumberFormatter
* pFormatter
= pDoc
->GetFormatTable();
53 sal_uInt32 nFormat
= pPattern
->GetNumberFormat( pFormatter
, pCondSet
);
54 ScCellFormat::GetString( pCell
, nFormat
, aString
, &pColor
, *pFormatter
);
58 rtl::OString
createErrorMessage(SCCOL nCol
, SCROW nRow
, SCTAB nTab
)
60 rtl::OStringBuffer
aString("Error in Table: ");
61 aString
.append(static_cast<sal_Int32
>(nTab
));
62 aString
.append(" Column: ");
63 aString
.append(static_cast<sal_Int32
>(nCol
));
64 aString
.append(" Row: ");
66 return aString
.makeStringAndClear();
69 rtl::OString
createErrorMessage(SCCOL nCol
, SCROW nRow
, SCTAB nTab
, const rtl::OUString
& rExpectedString
, const rtl::OUString
& rString
)
71 rtl::OStringBuffer
aString(createErrorMessage(nCol
, nRow
, nTab
));
72 aString
.append("; Expected: '");
73 aString
.append(rtl::OUStringToOString(rExpectedString
, RTL_TEXTENCODING_UTF8
));
74 aString
.append("' Found: '");
75 aString
.append(rtl::OUStringToOString(rString
, RTL_TEXTENCODING_UTF8
));
77 return aString
.makeStringAndClear();
80 rtl::OString
createErrorMessage(SCCOL nCol
, SCROW nRow
, SCTAB nTab
, double aExpected
, double aValue
)
82 rtl::OStringBuffer
aString(createErrorMessage(nCol
, nRow
, nTab
));
83 aString
.append("; Expected: '");
84 aString
.append(aExpected
);
85 aString
.append("' Found: '");
86 aString
.append(aValue
);
88 return aString
.makeStringAndClear();
94 enum StringType
{ PureString
, FormulaValue
, StringValue
};
99 csv_handler(ScDocument
* pDoc
, SCTAB nTab
, StringType eType
= StringValue
):
104 meStringType(eType
) {}
106 void begin_parse() {}
118 void cell(const char* p
, size_t n
)
120 #if DEBUG_CSV_HANDLER
121 std::cout
<< "Col: " << mnCol
<< " Row: " << mnRow
<< std::endl
;
122 #endif //DEBUG_CSV_HANDLER
126 if (!mpDoc
->GetString(mnCol
, mnRow
, mnTab
).isEmpty())
128 // cell in the document is not empty.
129 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol
, mnRow
, mnTab
).getStr(), false);
132 else if (meStringType
== PureString
)
134 rtl::OUString
aCSVString(p
, n
, RTL_TEXTENCODING_UTF8
);
135 rtl::OUString aString
;
136 mpDoc
->GetString(mnCol
, mnRow
, mnTab
, aString
);
138 #if DEBUG_CSV_HANDLER
139 std::cout
<< "String: " << rtl::OUStringToOString(aString
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
140 std::cout
<< "CSVString: " << rtl::OUStringToOString(aCSVString
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
141 std::cout
<< "result: " << (int)(aCSVString
== aString
) << std::endl
;
142 #endif //DEBUG_CSV_HANDLER
144 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol
, mnRow
, mnTab
, aCSVString
, aString
).getStr(), aString
== aCSVString
);
148 char* pRemainingChars
= NULL
;
149 std::string
aStr(p
, n
);
150 double nValue
= strtod(&aStr
[0], &pRemainingChars
);
151 if (*pRemainingChars
)
153 rtl::OUString aString
;
154 switch (meStringType
)
157 mpDoc
->GetString(mnCol
, mnRow
, mnTab
, aString
);
160 mpDoc
->GetFormula(mnCol
, mnRow
, mnTab
, aString
);
165 rtl::OUString
aCSVString(p
, n
, RTL_TEXTENCODING_UTF8
);
166 #if DEBUG_CSV_HANDLER
167 std::cout
<< "String: " << rtl::OUStringToOString(aString
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
168 std::cout
<< "CSVString: " << rtl::OUStringToOString(aCSVString
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
169 std::cout
<< "result: " << (int)(aCSVString
== aString
) << std::endl
;
170 #endif //DEBUG_CSV_HANDLER
172 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol
, mnRow
, mnTab
, aCSVString
, aString
).getStr(), aString
== aCSVString
);
177 mpDoc
->GetValue(mnCol
, mnRow
, mnTab
, aValue
);
178 #if DEBUG_CSV_HANDLER
179 std::cout
<< "Value: " << aValue
<< std::endl
;
180 std::cout
<< "CSVValue: " << nValue
<< std::endl
;
181 std::cout
<< "result: " << (int)(aValue
== nValue
) << std::endl
;
182 #endif //DEBUG_CSV_HANDLER
183 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol
, mnRow
, mnTab
, nValue
, aValue
).getStr(), aValue
== nValue
);
194 StringType meStringType
;
198 class conditional_format_handler
201 conditional_format_handler(ScDocument
* pDoc
, SCTAB nTab
):
207 void begin_parse() {}
219 void cell(const char* p
, size_t n
)
221 #if DEBUG_CSV_HANDLER
222 std::cout
<< "Col: " << mnCol
<< " Row: " << mnRow
<< std::endl
;
223 #endif //DEBUG_CSV_HANDLER
224 rtl::OUString aString
= getConditionalFormatString(mpDoc
, mnCol
, mnRow
, mnTab
);
225 rtl::OUString
aCSVString(p
, n
, RTL_TEXTENCODING_UTF8
);
226 #if DEBUG_CSV_HANDLER
227 std::cout
<< "String: " << rtl::OUStringToOString(aString
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
228 std::cout
<< "CSVString: " << rtl::OUStringToOString(aCSVString
, RTL_TEXTENCODING_UTF8
).getStr() << std::endl
;
229 std::cout
<< "result: " << (int)(aCSVString
== aString
) << std::endl
;
230 #endif //DEBUG_CSV_HANDLER
231 CPPUNIT_ASSERT_MESSAGE(createErrorMessage(mnCol
, mnRow
, mnTab
, aCSVString
, aString
).getStr(), aString
== aCSVString
);
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */