Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
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/
9 : *
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
13 : * License.
14 : *
15 : * Major Contributor(s):
16 : * Copyright (C) 2012 Markus Mohrhard <markus.mohrhard@googlemail.com> (initial developer)
17 : *
18 : * All Rights Reserved.
19 : *
20 : * For minor contributions see the git repository.
21 : *
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.
27 : */
28 :
29 : #ifndef SC_QA_HELPER_HXX
30 : #define SC_QA_HELPER_HXX
31 :
32 : #include "helper/csv_handler.hxx"
33 : #include "helper/debughelper.hxx"
34 : #include "orcus/csv_parser.hpp"
35 : #include <fstream>
36 : #include <string>
37 : #include <sstream>
38 :
39 : #include <osl/detail/android-bootstrap.h>
40 :
41 : // Why is this here and not in osl, and using the already existing file
42 : // handling APIs? Do we really want to add arbitrary new file handling
43 : // wrappers here and there (and then having to handle the Android (and
44 : // eventually perhaps iOS) special cases here, too)? Please move this to osl,
45 : // it sure looks gemerally useful. Or am I missing something?
46 :
47 32 : void loadFile(const rtl::OUString& aFileName, std::string& aContent)
48 : {
49 32 : rtl::OString aOFileName = rtl::OUStringToOString(aFileName, RTL_TEXTENCODING_UTF8);
50 :
51 : #ifdef ANDROID
52 : const char *contents;
53 : size_t size;
54 : if (strncmp(aOFileName.getStr(), "/assets/", sizeof("/assets/")-1) == 0) {
55 : contents = (const char *) lo_apkentry(aOFileName.getStr(), &size);
56 : if (contents != 0) {
57 : aContent = std::string(contents, size);
58 : return;
59 : }
60 : }
61 : #endif
62 :
63 32 : std::ifstream aFile(aOFileName.getStr());
64 :
65 32 : rtl::OStringBuffer aErrorMsg("Could not open csv file: ");
66 32 : aErrorMsg.append(aOFileName);
67 32 : CPPUNIT_ASSERT_MESSAGE(aErrorMsg.getStr(), aFile);
68 32 : std::ostringstream aOStream;
69 32 : aOStream << aFile.rdbuf();
70 32 : aFile.close();
71 32 : aContent = aOStream.str();
72 32 : }
73 :
74 28 : void testFile(rtl::OUString& aFileName, ScDocument* pDoc, SCTAB nTab, StringType aStringFormat = StringValue)
75 : {
76 28 : csv_handler aHandler(pDoc, nTab, aStringFormat);
77 28 : orcus::csv_parser_config aConfig;
78 28 : aConfig.delimiters.push_back(',');
79 28 : aConfig.delimiters.push_back(';');
80 28 : aConfig.text_qualifier = '"';
81 28 : aConfig.trim_cell_value = false;
82 :
83 :
84 28 : std::string aContent;
85 28 : loadFile(aFileName, aContent);
86 28 : orcus::csv_parser<csv_handler> parser ( &aContent[0], aContent.size() , aHandler, aConfig);
87 : try
88 : {
89 28 : parser.parse();
90 : }
91 0 : catch (const orcus::csv_parse_error& e)
92 : {
93 0 : std::cout << "reading csv content file failed: " << e.what() << std::endl;
94 0 : rtl::OStringBuffer aErrorMsg("csv parser error: ");
95 0 : aErrorMsg.append(e.what());
96 0 : CPPUNIT_ASSERT_MESSAGE(aErrorMsg.getStr(), false);
97 28 : }
98 28 : }
99 :
100 : //need own handler because conditional formatting strings must be generated
101 4 : void testCondFile(rtl::OUString& aFileName, ScDocument* pDoc, SCTAB nTab)
102 : {
103 4 : conditional_format_handler aHandler(pDoc, nTab);
104 4 : orcus::csv_parser_config aConfig;
105 4 : aConfig.delimiters.push_back(',');
106 4 : aConfig.delimiters.push_back(';');
107 4 : aConfig.text_qualifier = '"';
108 4 : std::string aContent;
109 4 : loadFile(aFileName, aContent);
110 4 : orcus::csv_parser<conditional_format_handler> parser ( &aContent[0], aContent.size() , aHandler, aConfig);
111 : try
112 : {
113 4 : parser.parse();
114 : }
115 0 : catch (const orcus::csv_parse_error& e)
116 : {
117 0 : std::cout << "reading csv content file failed: " << e.what() << std::endl;
118 0 : rtl::OStringBuffer aErrorMsg("csv parser error: ");
119 0 : aErrorMsg.append(e.what());
120 0 : CPPUNIT_ASSERT_MESSAGE(aErrorMsg.getStr(), false);
121 4 : }
122 :
123 4 : }
124 :
125 : #define ASSERT_DOUBLES_EQUAL( expected, result ) \
126 : CPPUNIT_ASSERT_DOUBLES_EQUAL( (expected), (result), 1e-14 )
127 :
128 : #define ASSERT_DOUBLES_EQUAL_MESSAGE( message, expected, result ) \
129 : CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE( (message), (expected), (result), 1e-14 )
130 :
131 : #endif
132 :
133 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|