LibreOffice Module sc (master) 1
asciiopt.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <global.hxx>
21#include <asciiopt.hxx>
22#include <comphelper/string.hxx>
23#include <osl/thread.h>
24#include <o3tl/string_view.hxx>
25
26constexpr std::u16string_view pStrFix = u"FIX";
27constexpr std::u16string_view pStrMrg = u"MRG";
28
30 bFixedLen ( false ),
31 aFieldSeps ( OUString(';') ),
32 bMergeFieldSeps ( false ),
33 bRemoveSpace ( false ),
34 bQuotedFieldAsText(false),
35 bDetectSpecialNumber(false),
36 bEvaluateFormulas(true),
37 bSkipEmptyCells(false),
38 bSaveAsShown(true),
39 bSaveFormulas(false),
40 cTextSep ( cDefaultTextSep ),
41 eCharSet ( osl_getThreadTextEncoding() ),
42 eLang ( LANGUAGE_SYSTEM ),
43 bCharSetSystem ( false ),
44 nStartRow ( 1 )
45{
46}
47
49{
50 sal_uInt16 nInfoCount = static_cast< sal_uInt16 >( rDataVec.size() );
51 mvColStart.resize(nInfoCount);
52 mvColFormat.resize(nInfoCount);
53 for( sal_uInt16 nIx = 0; nIx < nInfoCount; ++nIx )
54 {
55 mvColStart[ nIx ] = rDataVec[ nIx ].mnIndex;
56 mvColFormat[ nIx ] = rDataVec[ nIx ].mnType;
57 }
58}
59
60static OUString lcl_decodeSepString( std::u16string_view rSepNums, bool & o_bMergeFieldSeps )
61{
62 if ( rSepNums.empty() )
63 return OUString();
64
65 OUStringBuffer aFieldSeps;
66 sal_Int32 nPos = 0;
67 do
68 {
69 const std::u16string_view aCode = o3tl::getToken(rSepNums, 0, '/', nPos );
70 if ( aCode == pStrMrg )
71 o_bMergeFieldSeps = true;
72 else
73 {
74 sal_Int32 nVal = o3tl::toInt32(aCode);
75 if ( nVal )
76 aFieldSeps.append(sal_Unicode(nVal));
77 }
78 }
79 while ( nPos >= 0 );
80
81 return aFieldSeps.makeStringAndClear();
82}
83
84// The options string must not contain semicolons (because of the pick list),
85// use comma as separator.
86
87void ScAsciiOptions::ReadFromString( std::u16string_view rString )
88{
89 sal_Int32 nPos = rString.empty() ? -1 : 0;
90
91 // Token 0: Field separator.
92 if ( nPos >= 0 )
93 {
94 bFixedLen = bMergeFieldSeps = false;
95
96 const std::u16string_view aToken = o3tl::getToken(rString, 0, ',', nPos);
97 if ( aToken == pStrFix )
98 bFixedLen = true;
100 }
101
102 // Token 1: Text separator.
103 if ( nPos >= 0 )
104 {
105 const sal_Int32 nVal = o3tl::toInt32(o3tl::getToken(rString, 0, ',', nPos));
106 cTextSep = static_cast<sal_Unicode>(nVal);
107 }
108
109 // Token 2: Text encoding.
110 if ( nPos >= 0 )
111 {
113 }
114
115 // Token 3: Number of start row.
116 if ( nPos >= 0 )
117 {
118 nStartRow = o3tl::toInt32(o3tl::getToken(rString, 0, ',', nPos));
119 }
120
121 // Token 4: Column info.
122 if ( nPos >= 0 )
123 {
124 const std::u16string_view aToken = o3tl::getToken(rString, 0, ',', nPos);
125 const sal_Int32 nInfoCount = comphelper::string::getTokenCount(aToken, '/')/2;
126 mvColStart.resize(nInfoCount);
127 mvColFormat.resize(nInfoCount);
128 sal_Int32 nP = 0;
129 for (sal_Int32 nInfo=0; nInfo<nInfoCount; ++nInfo)
130 {
131 mvColStart[nInfo] = o3tl::toInt32(o3tl::getToken(aToken, 0, '/', nP));
132 mvColFormat[nInfo] = static_cast<sal_uInt8>(o3tl::toInt32(o3tl::getToken(aToken, 0, '/', nP)));
133 }
134 }
135
136 // Token 5: Language.
137 if (nPos >= 0)
138 {
139 eLang = static_cast<LanguageType>(o3tl::toInt32(o3tl::getToken(rString, 0, ',', nPos)));
140 }
141
142 // Token 6: Import quoted field as text.
143 if (nPos >= 0)
144 {
145 bQuotedFieldAsText = o3tl::getToken(rString, 0, ',', nPos) == u"true";
146 }
147
148 // Token 7: Detect special numbers.
149 if (nPos >= 0)
150 {
151 bDetectSpecialNumber = o3tl::getToken(rString, 0, ',', nPos) == u"true";
152 }
153 else
154 bDetectSpecialNumber = true; // default of versions that didn't add the parameter
155
156 // Token 8: used for "Save as shown" in export options
157 if ( nPos >= 0 )
158 {
159 bSaveAsShown = o3tl::getToken(rString, 0, ',', nPos) == u"true";
160 }
161 else
162 bSaveAsShown = true; // default value
163
164 // Token 9: used for "Save cell formulas" in export options
165 if ( nPos >= 0 )
166 {
167 bSaveFormulas = o3tl::getToken(rString, 0, ',', nPos) == u"true";
168 }
169 else
170 bSaveFormulas = false;
171
172 // Token 10: Boolean for Trim spaces.
173 if (nPos >= 0)
174 {
175 bRemoveSpace = o3tl::getToken(rString, 0, ',', nPos) == u"true";
176 }
177 else
178 bRemoveSpace = false;
179
180 // Token 11: sheet to export for --convert-to csv
181 // Does not need to be evaluated here but may be present.
182 if (nPos >= 0)
183 {
184 o3tl::getToken(rString, 0, ',', nPos);
185 }
186
187 // Token 12: evaluate formulas.
188 if (nPos >= 0)
189 {
190 // If present, defaults to "false".
191 bEvaluateFormulas = o3tl::getToken(rString, 0, ',', nPos) == u"true";
192 }
193 else
194 bEvaluateFormulas = true; // default of versions that didn't add the parameter
195}
196
198{
199 OUStringBuffer aOutStr;
200
201 // Token 0: Field separator.
202 if ( bFixedLen )
203 aOutStr.append(pStrFix);
204 else if ( aFieldSeps.isEmpty() )
205 aOutStr.append("0");
206 else
207 {
208 sal_Int32 nLen = aFieldSeps.getLength();
209 for (sal_Int32 i=0; i<nLen; i++)
210 {
211 if (i)
212 aOutStr.append("/");
213 aOutStr.append(OUString::number(aFieldSeps[i]));
214 }
215 if ( bMergeFieldSeps )
216 {
217 aOutStr.append("/");
218 aOutStr.append(pStrMrg);
219 }
220 }
221
222 // Token 1: Text Quote character.
223 aOutStr.append("," + OUString::number(cTextSep) + ",");
224
225 //Token 2: Text encoding.
226 if ( bCharSetSystem ) // force "SYSTEM"
227 aOutStr.append(ScGlobal::GetCharsetString( RTL_TEXTENCODING_DONTKNOW ));
228 else
229 aOutStr.append(ScGlobal::GetCharsetString( eCharSet ));
230
231 //Token 3: Number of start row.
232 aOutStr.append("," + OUString::number(nStartRow) + ",");
233
234 //Token 4: Column info.
235 for (size_t nInfo=0; nInfo<mvColStart.size(); nInfo++)
236 {
237 if (nInfo)
238 aOutStr.append("/");
239 aOutStr.append(OUString::number(mvColStart[nInfo]) +
240 "/" +
241 OUString::number(mvColFormat[nInfo]));
242 }
243
244 // #i112025# the options string is used in macros and linked sheets,
245 // so new options must be added at the end, to remain compatible
246 // Always keep in sync with ScImportOptions.
247
248 aOutStr.append("," +
249 // Token 5: Language
250 OUString::number(static_cast<sal_uInt16>(eLang)) + "," +
251 // Token 6: Import quoted field as text.
252 OUString::boolean( bQuotedFieldAsText ) + "," +
253 // Token 7: Detect special numbers.
254 OUString::boolean( bDetectSpecialNumber ) + "," +
255 // Token 8: used for "Save as shown" in export options
256 OUString::boolean( bSaveAsShown ) +"," +
257 // Token 9: used for "Save cell formulas" in export options
258 OUString::boolean( bSaveFormulas ) + "," +
259 // Token 10: Trim Space
260 OUString::boolean( bRemoveSpace ) +
261 // Token 11: sheet to export, always 0 for current sheet
262 ",0," +
263 // Token 12: evaluate formulas in import
264 OUString::boolean( bEvaluateFormulas )
265 );
266 return aOutStr.makeStringAndClear();
267}
268
269// static
270sal_Unicode ScAsciiOptions::GetWeightedFieldSep( const OUString & rFieldSeps, bool bDecodeNumbers )
271{
272 bool bMergeFieldSeps = false;
273 OUString aFieldSeps( bDecodeNumbers ? lcl_decodeSepString( rFieldSeps, bMergeFieldSeps) : rFieldSeps);
274 if (aFieldSeps.isEmpty())
275 {
276 return 0;
277 }
278 else if (aFieldSeps.getLength() == 1)
279 return aFieldSeps[0];
280 else
281 {
282 // There can be only one separator for output. See also fdo#53449
283 if (aFieldSeps.indexOf(',') != -1)
284 return ',';
285 else if (aFieldSeps.indexOf('\t') != -1)
286 return '\t';
287 else if (aFieldSeps.indexOf(';') != -1)
288 return ';';
289 else if (aFieldSeps.indexOf(' ') != -1)
290 return ' ';
291 else
292 return aFieldSeps[0];
293 }
294}
295
296/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr std::u16string_view pStrFix
Definition: asciiopt.cxx:26
static OUString lcl_decodeSepString(std::u16string_view rSepNums, bool &o_bMergeFieldSeps)
Definition: asciiopt.cxx:60
constexpr std::u16string_view pStrMrg
Definition: asciiopt.cxx:27
bool bEvaluateFormulas
Definition: asciiopt.hxx:36
bool bSaveFormulas
Definition: asciiopt.hxx:39
OUString aFieldSeps
Definition: asciiopt.hxx:31
bool bSaveAsShown
Definition: asciiopt.hxx:38
void SetColumnInfo(const ScCsvExpDataVec &rDataVec)
Definition: asciiopt.cxx:48
static sal_Unicode GetWeightedFieldSep(const OUString &rFieldSeps, bool bDecodeNumbers)
From the import field separators obtain the one most likely to be used for export,...
Definition: asciiopt.cxx:270
bool bDetectSpecialNumber
Definition: asciiopt.hxx:35
rtl_TextEncoding eCharSet
Definition: asciiopt.hxx:41
LanguageType eLang
Definition: asciiopt.hxx:42
bool bQuotedFieldAsText
Definition: asciiopt.hxx:34
bool bCharSetSystem
Definition: asciiopt.hxx:43
void ReadFromString(std::u16string_view rString)
Definition: asciiopt.cxx:87
bool bMergeFieldSeps
Definition: asciiopt.hxx:32
std::vector< sal_uInt8 > mvColFormat
Definition: asciiopt.hxx:46
sal_Int32 nStartRow
Definition: asciiopt.hxx:44
sal_Unicode cTextSep
Definition: asciiopt.hxx:40
bool bRemoveSpace
Definition: asciiopt.hxx:33
OUString WriteToString() const
Definition: asciiopt.cxx:197
std::vector< sal_Int32 > mvColStart
Definition: asciiopt.hxx:45
static rtl_TextEncoding GetCharsetValue(std::u16string_view rCharSet)
Definition: global.cxx:564
static OUString GetCharsetString(rtl_TextEncoding eVal)
Definition: global.cxx:592
::std::vector< ScCsvExpData > ScCsvExpDataVec
Definition: csvcontrol.hxx:73
float u
#define LANGUAGE_SYSTEM
sal_uInt16 nPos
sal_Int32 getTokenCount(std::string_view rIn, char cTok)
int i
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
unsigned char sal_uInt8
sal_uInt16 sal_Unicode