Branch data 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 : 87 : void loadFile(const rtl::OUString& aFileName, std::string& aContent)
48 : : {
49 [ + - ]: 87 : 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 [ + - ]: 87 : std::ifstream aFile(aOFileName.getStr());
64 : :
65 : 87 : rtl::OStringBuffer aErrorMsg("Could not open csv file: ");
66 [ + - ]: 87 : aErrorMsg.append(aOFileName);
67 [ + - ][ + - ]: 87 : CPPUNIT_ASSERT_MESSAGE(aErrorMsg.getStr(), aFile);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
68 [ + - ]: 87 : std::ostringstream aOStream;
69 [ + - ][ + - ]: 87 : aOStream << aFile.rdbuf();
70 [ + - ]: 87 : aFile.close();
71 [ + - ][ + - ]: 87 : aContent = aOStream.str();
[ + - ][ + - ]
72 : 87 : }
73 : :
74 : 84 : void testFile(rtl::OUString& aFileName, ScDocument* pDoc, SCTAB nTab, StringType aStringFormat = StringValue)
75 : : {
76 : 84 : csv_handler aHandler(pDoc, nTab, aStringFormat);
77 [ + - ]: 84 : orcus::csv_parser_config aConfig;
78 [ + - ]: 84 : aConfig.delimiters.push_back(',');
79 [ + - ]: 84 : aConfig.delimiters.push_back(';');
80 : 84 : aConfig.text_qualifier = '"';
81 : 84 : aConfig.trim_cell_value = false;
82 : :
83 : :
84 [ + - ]: 84 : std::string aContent;
85 [ + - ]: 84 : loadFile(aFileName, aContent);
86 [ + - ][ + - ]: 84 : orcus::csv_parser<csv_handler> parser ( &aContent[0], aContent.size() , aHandler, aConfig);
87 : : try
88 : : {
89 [ + - ]: 84 : 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 : 84 : }
98 : 84 : }
99 : :
100 : : //need own handler because conditional formatting strings must be generated
101 : 3 : void testCondFile(rtl::OUString& aFileName, ScDocument* pDoc, SCTAB nTab)
102 : : {
103 : 3 : conditional_format_handler aHandler(pDoc, nTab);
104 [ + - ]: 3 : orcus::csv_parser_config aConfig;
105 [ + - ]: 3 : aConfig.delimiters.push_back(',');
106 [ + - ]: 3 : aConfig.delimiters.push_back(';');
107 : 3 : aConfig.text_qualifier = '"';
108 [ + - ]: 3 : std::string aContent;
109 [ + - ]: 3 : loadFile(aFileName, aContent);
110 [ + - ][ + - ]: 3 : orcus::csv_parser<conditional_format_handler> parser ( &aContent[0], aContent.size() , aHandler, aConfig);
111 : : try
112 : : {
113 [ + - ]: 3 : 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 : 3 : }
122 : :
123 : 3 : }
124 : :
125 : : #endif
126 : :
127 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|