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>
23 #include <unotools/charclass.hxx>
24 #include <osl/thread.h>
25 #include <o3tl/string_view.hxx>
26 #include <global.hxx>
27 
28 const 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 
33 ScImportOptions::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;
48  nSheetToExport = 0;
49  bEvaluateFormulas = true; // true if not present at all, for compatibility
50  sal_Int32 nTokenCount = comphelper::string::getTokenCount(rStr, ',');
51  if ( nTokenCount < 3 )
52  return;
53 
54  sal_Int32 nIdx{ 0 };
55  // first 3 tokens: common
56  OUString aToken( o3tl::getToken(rStr, 0, ',', nIdx ) );
57  if( aToken.equalsIgnoreAsciiCase( pStrFix ) )
58  bFixedWidth = true;
59  else
61  nTextSepCode = static_cast<sal_Unicode>(o3tl::toInt32(o3tl::getToken(rStr, 0, ',', nIdx)));
62  aStrFont = o3tl::getToken(rStr, 0, ',', nIdx);
64 
65  if ( nTokenCount == 4 )
66  {
67  // compatibility with old options string: "Save as shown" as 4th token, numeric
68  bSaveAsShown = o3tl::toInt32(o3tl::getToken(rStr, 0, ',', nIdx)) != 0;
69  bQuoteAllText = true; // use old default then
70  }
71  else
72  {
73  // look at the same positions as in ScAsciiOptions
74  if ( nTokenCount >= 7 )
75  bQuoteAllText = o3tl::getToken(rStr, 3, ',', nIdx) == u"true"; // 7th token
76  if ( nTokenCount >= 8 )
77  bSaveNumberAsSuch = o3tl::getToken(rStr, 0, ',', nIdx) == u"true";
78  if ( nTokenCount >= 9 )
79  bSaveAsShown = o3tl::getToken(rStr, 0, ',', nIdx) == u"true";
80  if ( nTokenCount >= 10 )
81  bSaveFormulas = o3tl::getToken(rStr, 0, ',', nIdx) == u"true";
82  if ( nTokenCount >= 11 )
83  bRemoveSpace = o3tl::getToken(rStr, 0, ',', nIdx) == u"true";
84  if ( nTokenCount >= 12 )
85  {
86  const OUString aTok(o3tl::getToken(rStr,0, ',', nIdx));
87  if (aTok == "-1")
88  nSheetToExport = -1; // all
89  else if (aTok.isEmpty() || CharClass::isAsciiNumeric(aTok))
90  nSheetToExport = aTok.toInt32();
91  else
92  nSheetToExport = -23; // invalid, force error
93  }
94  if ( nTokenCount >= 13 )
95  // If present, defaults to "false".
96  bEvaluateFormulas = o3tl::getToken(rStr, 0, ',', nIdx) == u"true";
97  }
98 }
99 
101 {
102  OUString aResult;
103 
104  if( bFixedWidth )
105  aResult += pStrFix;
106  else
107  aResult += OUString::number(nFieldSepCode);
108  aResult += "," + OUString::number(nTextSepCode) + "," + aStrFont +
109  // use the same string format as ScAsciiOptions:
110  ",1,,0," + // first row, no column info, default language
111  OUString::boolean( bQuoteAllText ) + // same as "quoted field as text" in ScAsciiOptions
112  "," +
113  OUString::boolean( bSaveNumberAsSuch ) + // "save number as such": not in ScAsciiOptions
114  "," +
115  OUString::boolean( bSaveAsShown ) + // "save as shown": not in ScAsciiOptions
116  "," +
117  OUString::boolean( bSaveFormulas ) + // "save formulas": not in ScAsciiOptions
118  "," +
119  OUString::boolean( bRemoveSpace ) + // same as "Remove space" in ScAsciiOptions
120  "," +
121  OUString::number(nSheetToExport) + // Only available for command line --convert-to
122  "," +
123  OUString::boolean( bEvaluateFormulas ) ; // same as "Evaluate formulas" in ScAsciiOptions
124 
125  return aResult;
126 }
127 
128 void ScImportOptions::SetTextEncoding( rtl_TextEncoding nEnc )
129 {
130  eCharSet = (nEnc == RTL_TEXTENCODING_DONTKNOW ?
131  osl_getThreadTextEncoding() : nEnc);
133 }
134 
135 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Unicode nFieldSepCode
Definition: imoptdlg.hxx:44
bool bSaveFormulas
Definition: imoptdlg.hxx:52
std::basic_string_view< charT, traits > getToken(std::basic_string_view< charT, traits > sv, charT delimiter, std::size_t &position)
static rtl_TextEncoding GetCharsetValue(std::u16string_view rCharSet)
Definition: global.cxx:564
static OUString GetCharsetString(rtl_TextEncoding eVal)
Definition: global.cxx:592
bool bSaveNumberAsSuch
Definition: imoptdlg.hxx:51
sal_uInt16 sal_Unicode
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, if multiple separators weighted comma, tab, semicolon, space and other.
Definition: asciiopt.cxx:270
OUString aStrFont
Definition: imoptdlg.hxx:46
sal_Int32 nSheetToExport
Definition: imoptdlg.hxx:57
sal_Int32 getTokenCount(std::string_view rIn, char cTok)
sal_Unicode nTextSepCode
Definition: imoptdlg.hxx:45
ScImportOptions(std::u16string_view rStr)
Definition: imoptdlg.cxx:33
float u
sal_Int32 toInt32(std::u16string_view str, sal_Int16 radix=10)
const char pStrFix[]
Definition: imoptdlg.cxx:28
bool bQuoteAllText
Definition: imoptdlg.hxx:50
void SetTextEncoding(rtl_TextEncoding nEnc)
Definition: imoptdlg.cxx:128
OUString BuildString() const
Definition: imoptdlg.cxx:100
rtl_TextEncoding eCharSet
Definition: imoptdlg.hxx:47
bool bEvaluateFormulas
Definition: imoptdlg.hxx:54