1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
28 const char pStrFix
[] = "FIX";
30 // The option string can no longer contain a semicolon (because of pick list),
31 // therefore, starting with version 336 comma instead
33 ScImportOptions::ScImportOptions( std::u16string_view rStr
)
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.
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;
49 bEvaluateFormulas
= true; // true if not present at all, for compatibility
51 sal_Int32 nTokenCount
= comphelper::string::getTokenCount(rStr
, ',');
52 if ( nTokenCount
< 3 )
56 // first 3 tokens: common
57 OUString
aToken( o3tl::getToken(rStr
, 0, ',', nIdx
) );
58 if( aToken
.equalsIgnoreAsciiCase( pStrFix
) )
61 nFieldSepCode
= ScAsciiOptions::GetWeightedFieldSep( aToken
, true);
62 nTextSepCode
= static_cast<sal_Unicode
>(o3tl::toInt32(o3tl::getToken(rStr
, 0, ',', nIdx
)));
63 aStrFont
= o3tl::getToken(rStr
, 0, ',', nIdx
);
64 eCharSet
= ScGlobal::GetCharsetValue(aStrFont
);
66 if ( nTokenCount
== 4 )
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
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 )
87 const OUString
aTok(o3tl::getToken(rStr
,0, ',', nIdx
));
89 nSheetToExport
= -1; // all
90 else if (aTok
.isEmpty() || CharClass::isAsciiNumeric(aTok
))
91 nSheetToExport
= aTok
.toInt32();
93 nSheetToExport
= -23; // invalid, force error
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";
103 OUString
ScImportOptions::BuildString() const
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
116 OUString::boolean( bSaveNumberAsSuch
) + // "save number as such": not in ScAsciiOptions
118 OUString::boolean( bSaveAsShown
) + // "save as shown": not in ScAsciiOptions
120 OUString::boolean( bSaveFormulas
) + // "save formulas": not in ScAsciiOptions
122 OUString::boolean( bRemoveSpace
) + // same as "Remove space" in ScAsciiOptions
124 OUString::number(nSheetToExport
) + // Only available for command line --convert-to
126 OUString::boolean( bEvaluateFormulas
) + // same as "Evaluate formulas" in ScAsciiOptions
128 OUString::boolean(bIncludeBOM
) ; // same as "Include BOM" in ScAsciiOptions
133 void ScImportOptions::SetTextEncoding( rtl_TextEncoding nEnc
)
135 eCharSet
= (nEnc
== RTL_TEXTENCODING_DONTKNOW
?
136 osl_getThreadTextEncoding() : nEnc
);
137 aStrFont
= ScGlobal::GetCharsetString( nEnc
);
140 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */