LibreOffice Module sc (master) 1
imoptdlg.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 <imoptdlg.hxx>
21#include <asciiopt.hxx>
22#include <comphelper/string.hxx>
24#include <osl/thread.h>
25#include <o3tl/string_view.hxx>
26#include <global.hxx>
27
28const char pStrFix[] = "FIX";
29
30// The option string can no longer contain a semicolon (because of pick list),
31// therefore, starting with version 336 comma instead
32
33ScImportOptions::ScImportOptions( std::u16string_view rStr )
34{
35 // Use the same string format as ScAsciiOptions,
36 // because the import options string is passed here when a CSV file is loaded and saved again.
37 // The old format is still supported because it might be used in macros.
38
39 bFixedWidth = false;
40 nFieldSepCode = 0;
41 nTextSepCode = 0;
42 eCharSet = RTL_TEXTENCODING_DONTKNOW;
43 bSaveAsShown = true; // "true" if not in string (after CSV import)
44 bQuoteAllText = false;
45 bSaveNumberAsSuch = true;
46 bSaveFormulas = false;
47 bRemoveSpace = false;
49 bEvaluateFormulas = true; // true if not present at all, for compatibility
50 bIncludeBOM = false;
51 sal_Int32 nTokenCount = comphelper::string::getTokenCount(rStr, ',');
52 if ( nTokenCount < 3 )
53 return;
54
55 sal_Int32 nIdx{ 0 };
56 // first 3 tokens: common
57 OUString aToken( o3tl::getToken(rStr, 0, ',', nIdx ) );
58 if( aToken.equalsIgnoreAsciiCase( pStrFix ) )
59 bFixedWidth = true;
60 else
62 nTextSepCode = static_cast<sal_Unicode>(o3tl::toInt32(o3tl::getToken(rStr, 0, ',', nIdx)));
63 aStrFont = o3tl::getToken(rStr, 0, ',', nIdx);
65
66 if ( nTokenCount == 4 )
67 {
68 // compatibility with old options string: "Save as shown" as 4th token, numeric
69 bSaveAsShown = o3tl::toInt32(o3tl::getToken(rStr, 0, ',', nIdx)) != 0;
70 bQuoteAllText = true; // use old default then
71 }
72 else
73 {
74 // look at the same positions as in ScAsciiOptions
75 if ( nTokenCount >= 7 )
76 bQuoteAllText = o3tl::getToken(rStr, 3, ',', nIdx) == u"true"; // 7th token
77 if ( nTokenCount >= 8 )
78 bSaveNumberAsSuch = o3tl::getToken(rStr, 0, ',', nIdx) == u"true";
79 if ( nTokenCount >= 9 )
80 bSaveAsShown = o3tl::getToken(rStr, 0, ',', nIdx) == u"true";
81 if ( nTokenCount >= 10 )
82 bSaveFormulas = o3tl::getToken(rStr, 0, ',', nIdx) == u"true";
83 if ( nTokenCount >= 11 )
84 bRemoveSpace = o3tl::getToken(rStr, 0, ',', nIdx) == u"true";
85 if ( nTokenCount >= 12 )
86 {
87 const OUString aTok(o3tl::getToken(rStr,0, ',', nIdx));
88 if (aTok == "-1")
89 nSheetToExport = -1; // all
90 else if (aTok.isEmpty() || CharClass::isAsciiNumeric(aTok))
91 nSheetToExport = aTok.toInt32();
92 else
93 nSheetToExport = -23; // invalid, force error
94 }
95 if ( nTokenCount >= 13 )
96 // If present, defaults to "false".
97 bEvaluateFormulas = o3tl::getToken(rStr, 0, ',', nIdx) == u"true";
98 if (nTokenCount >= 14)
99 bIncludeBOM = o3tl::getToken(rStr, 0, ',', nIdx) == u"true";
100 }
101}
102
104{
105 OUString aResult;
106
107 if( bFixedWidth )
108 aResult += pStrFix;
109 else
110 aResult += OUString::number(nFieldSepCode);
111 aResult += "," + OUString::number(nTextSepCode) + "," + aStrFont +
112 // use the same string format as ScAsciiOptions:
113 ",1,,0," + // first row, no column info, default language
114 OUString::boolean( bQuoteAllText ) + // same as "quoted field as text" in ScAsciiOptions
115 "," +
116 OUString::boolean( bSaveNumberAsSuch ) + // "save number as such": not in ScAsciiOptions
117 "," +
118 OUString::boolean( bSaveAsShown ) + // "save as shown": not in ScAsciiOptions
119 "," +
120 OUString::boolean( bSaveFormulas ) + // "save formulas": not in ScAsciiOptions
121 "," +
122 OUString::boolean( bRemoveSpace ) + // same as "Remove space" in ScAsciiOptions
123 "," +
124 OUString::number(nSheetToExport) + // Only available for command line --convert-to
125 "," +
126 OUString::boolean( bEvaluateFormulas ) + // same as "Evaluate formulas" in ScAsciiOptions
127 "," +
128 OUString::boolean(bIncludeBOM) ; // same as "Include BOM" in ScAsciiOptions
129
130 return aResult;
131}
132
133void ScImportOptions::SetTextEncoding( rtl_TextEncoding nEnc )
134{
135 eCharSet = (nEnc == RTL_TEXTENCODING_DONTKNOW ?
136 osl_getThreadTextEncoding() : nEnc);
138}
139
140/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool isAsciiNumeric(std::u16string_view rStr)
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:287
static rtl_TextEncoding GetCharsetValue(std::u16string_view rCharSet)
Definition: global.cxx:570
static OUString GetCharsetString(rtl_TextEncoding eVal)
Definition: global.cxx:598
bool bSaveFormulas
Definition: imoptdlg.hxx:52
OUString BuildString() const
Definition: imoptdlg.cxx:103
void SetTextEncoding(rtl_TextEncoding nEnc)
Definition: imoptdlg.cxx:133
sal_Unicode nTextSepCode
Definition: imoptdlg.hxx:45
sal_Int32 nSheetToExport
Definition: imoptdlg.hxx:58
bool bEvaluateFormulas
Definition: imoptdlg.hxx:54
ScImportOptions(std::u16string_view rStr)
Definition: imoptdlg.cxx:33
bool bSaveNumberAsSuch
Definition: imoptdlg.hxx:51
bool bQuoteAllText
Definition: imoptdlg.hxx:50
OUString aStrFont
Definition: imoptdlg.hxx:46
sal_Unicode nFieldSepCode
Definition: imoptdlg.hxx:44
rtl_TextEncoding eCharSet
Definition: imoptdlg.hxx:47
float u
const char pStrFix[]
Definition: imoptdlg.cxx:28
sal_Int32 getTokenCount(std::string_view rIn, char cTok)
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)
sal_uInt16 sal_Unicode