/src/libreoffice/sc/source/ui/dbgui/imoptdlg.cxx
Line | Count | Source |
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 | 0 | { |
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 | 0 | bFixedWidth = false; |
40 | 0 | nFieldSepCode = 0; |
41 | 0 | nTextSepCode = 0; |
42 | 0 | eEncoding = RTL_TEXTENCODING_DONTKNOW; |
43 | 0 | bSaveAsShown = true; // "true" if not in string (after CSV import) |
44 | 0 | bQuoteAllText = false; |
45 | 0 | bSaveNumberAsSuch = true; |
46 | 0 | bSaveFormulas = false; |
47 | 0 | bRemoveSpace = false; |
48 | 0 | nSheetToExport = 0; |
49 | 0 | bEvaluateFormulas = true; // true if not present at all, for compatibility |
50 | 0 | bIncludeBOM = false; |
51 | 0 | nEndianness = SvStreamEndian::LITTLE; |
52 | 0 | sal_Int32 nTokenCount = comphelper::string::getTokenCount(rStr, ','); |
53 | 0 | if ( nTokenCount < 3 ) |
54 | 0 | return; |
55 | | |
56 | 0 | sal_Int32 nIdx{ 0 }; |
57 | | // first 3 tokens: common |
58 | 0 | OUString aToken( o3tl::getToken(rStr, 0, ',', nIdx ) ); |
59 | 0 | if( aToken.equalsIgnoreAsciiCase( pStrFix ) ) |
60 | 0 | bFixedWidth = true; |
61 | 0 | else |
62 | 0 | nFieldSepCode = ScAsciiOptions::GetWeightedFieldSep( aToken, true); |
63 | 0 | nTextSepCode = static_cast<sal_Unicode>(o3tl::toInt32(o3tl::getToken(rStr, 0, ',', nIdx))); |
64 | 0 | aStrFont = o3tl::getToken(rStr, 0, ',', nIdx); |
65 | 0 | eEncoding = ScGlobal::GetEncodingValue(aStrFont); |
66 | |
|
67 | 0 | if ( nTokenCount == 4 ) |
68 | 0 | { |
69 | | // compatibility with old options string: "Save as shown" as 4th token, numeric |
70 | 0 | bSaveAsShown = o3tl::toInt32(o3tl::getToken(rStr, 0, ',', nIdx)) != 0; |
71 | 0 | bQuoteAllText = true; // use old default then |
72 | 0 | } |
73 | 0 | else |
74 | 0 | { |
75 | | // look at the same positions as in ScAsciiOptions |
76 | 0 | if ( nTokenCount >= 7 ) |
77 | 0 | bQuoteAllText = o3tl::getToken(rStr, 3, ',', nIdx) == u"true"; // 7th token |
78 | 0 | if ( nTokenCount >= 8 ) |
79 | 0 | bSaveNumberAsSuch = o3tl::getToken(rStr, 0, ',', nIdx) == u"true"; |
80 | 0 | if ( nTokenCount >= 9 ) |
81 | 0 | bSaveAsShown = o3tl::getToken(rStr, 0, ',', nIdx) == u"true"; |
82 | 0 | if ( nTokenCount >= 10 ) |
83 | 0 | bSaveFormulas = o3tl::getToken(rStr, 0, ',', nIdx) == u"true"; |
84 | 0 | if ( nTokenCount >= 11 ) |
85 | 0 | bRemoveSpace = o3tl::getToken(rStr, 0, ',', nIdx) == u"true"; |
86 | 0 | if ( nTokenCount >= 12 ) |
87 | 0 | { |
88 | 0 | const OUString aTok(o3tl::getToken(rStr,0, ',', nIdx)); |
89 | 0 | if (aTok == "-1") |
90 | 0 | nSheetToExport = -1; // all |
91 | 0 | else if (aTok.isEmpty() || CharClass::isAsciiNumeric(aTok)) |
92 | 0 | nSheetToExport = aTok.toInt32(); |
93 | 0 | else |
94 | 0 | nSheetToExport = -23; // invalid, force error |
95 | 0 | } |
96 | 0 | if ( nTokenCount >= 13 ) |
97 | | // If present, defaults to "false". |
98 | 0 | bEvaluateFormulas = o3tl::getToken(rStr, 0, ',', nIdx) == u"true"; |
99 | 0 | if (nTokenCount >= 14) |
100 | 0 | bIncludeBOM = o3tl::getToken(rStr, 0, ',', nIdx) == u"true"; |
101 | 0 | if (nTokenCount >= 15) |
102 | 0 | nEndianness = static_cast<SvStreamEndian>(o3tl::toInt32(o3tl::getToken(rStr, 0, ',', nIdx))); |
103 | 0 | } |
104 | 0 | } |
105 | | |
106 | | OUString ScImportOptions::BuildString() const |
107 | 0 | { |
108 | 0 | OUString aResult; |
109 | |
|
110 | 0 | if( bFixedWidth ) |
111 | 0 | aResult += pStrFix; |
112 | 0 | else |
113 | 0 | aResult += OUString::number(nFieldSepCode); |
114 | 0 | aResult += "," + OUString::number(nTextSepCode) + "," + aStrFont + |
115 | | // use the same string format as ScAsciiOptions: |
116 | 0 | ",1,,0," + // first row, no column info, default language |
117 | 0 | OUString::boolean( bQuoteAllText ) + // same as "quoted field as text" in ScAsciiOptions |
118 | 0 | "," + |
119 | 0 | OUString::boolean( bSaveNumberAsSuch ) + // "save number as such": not in ScAsciiOptions |
120 | 0 | "," + |
121 | 0 | OUString::boolean( bSaveAsShown ) + // "save as shown": not in ScAsciiOptions |
122 | 0 | "," + |
123 | 0 | OUString::boolean( bSaveFormulas ) + // "save formulas": not in ScAsciiOptions |
124 | 0 | "," + |
125 | 0 | OUString::boolean( bRemoveSpace ) + // same as "Remove space" in ScAsciiOptions |
126 | 0 | "," + |
127 | 0 | OUString::number(nSheetToExport) + // Only available for command line --convert-to |
128 | 0 | "," + |
129 | 0 | OUString::boolean( bEvaluateFormulas ) + // same as "Evaluate formulas" in ScAsciiOptions |
130 | 0 | "," + |
131 | 0 | OUString::boolean(bIncludeBOM) + // same as "Include BOM" in ScAsciiOptions |
132 | 0 | "," + |
133 | 0 | OUString::number( static_cast<sal_uInt16>(nEndianness) ); // same as "Endianness" in ScAsciiOptions |
134 | |
|
135 | 0 | return aResult; |
136 | 0 | } |
137 | | |
138 | | void ScImportOptions::SetTextEncoding( rtl_TextEncoding nEnc ) |
139 | 0 | { |
140 | 0 | eEncoding = (nEnc == RTL_TEXTENCODING_DONTKNOW ? |
141 | 0 | osl_getThreadTextEncoding() : nEnc); |
142 | 0 | aStrFont = ScGlobal::GetEncodingString( nEnc ); |
143 | 0 | } |
144 | | |
145 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |