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