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