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 .
21 #include "scresid.hxx"
23 #include "asciiopt.hxx"
24 #include "asciiopt.hrc"
25 #include <comphelper/string.hxx>
26 #include <rtl/tencinfo.h>
27 #include <unotools/transliterationwrapper.hxx>
29 #include "editutil.hxx"
31 // ============================================================================
33 static const sal_Char pStrFix
[] = "FIX";
34 static const sal_Char pStrMrg
[] = "MRG";
37 // ============================================================================
39 ScAsciiOptions::ScAsciiOptions() :
41 aFieldSeps ( OUString(';') ),
42 bMergeFieldSeps ( false ),
43 bQuotedFieldAsText(false),
44 bDetectSpecialNumber(false),
45 cTextSep ( cDefaultTextSep
),
46 eCharSet ( osl_getThreadTextEncoding() ),
47 eLang ( LANGUAGE_SYSTEM
),
48 bCharSetSystem ( false ),
57 ScAsciiOptions::ScAsciiOptions(const ScAsciiOptions
& rOpt
) :
58 bFixedLen ( rOpt
.bFixedLen
),
59 aFieldSeps ( rOpt
.aFieldSeps
),
60 bMergeFieldSeps ( rOpt
.bMergeFieldSeps
),
61 bQuotedFieldAsText(rOpt
.bQuotedFieldAsText
),
62 bDetectSpecialNumber(rOpt
.bDetectSpecialNumber
),
63 cTextSep ( rOpt
.cTextSep
),
64 eCharSet ( rOpt
.eCharSet
),
66 bCharSetSystem ( rOpt
.bCharSetSystem
),
67 nStartRow ( rOpt
.nStartRow
),
68 nInfoCount ( rOpt
.nInfoCount
)
72 pColStart
= new sal_Int32
[nInfoCount
];
73 pColFormat
= new sal_uInt8
[nInfoCount
];
74 for (sal_uInt16 i
=0; i
<nInfoCount
; i
++)
76 pColStart
[i
] = rOpt
.pColStart
[i
];
77 pColFormat
[i
] = rOpt
.pColFormat
[i
];
88 ScAsciiOptions::~ScAsciiOptions()
95 void ScAsciiOptions::SetColInfo( sal_uInt16 nCount
, const sal_Int32
* pStart
, const sal_uInt8
* pFormat
)
104 pColStart
= new sal_Int32
[nInfoCount
];
105 pColFormat
= new sal_uInt8
[nInfoCount
];
106 for (sal_uInt16 i
=0; i
<nInfoCount
; i
++)
108 pColStart
[i
] = pStart
[i
];
109 pColFormat
[i
] = pFormat
[i
];
120 void ScAsciiOptions::SetColumnInfo( const ScCsvExpDataVec
& rDataVec
)
127 nInfoCount
= static_cast< sal_uInt16
>( rDataVec
.size() );
130 pColStart
= new sal_Int32
[ nInfoCount
];
131 pColFormat
= new sal_uInt8
[ nInfoCount
];
132 for( sal_uInt16 nIx
= 0; nIx
< nInfoCount
; ++nIx
)
134 pColStart
[ nIx
] = rDataVec
[ nIx
].mnIndex
;
135 pColFormat
[ nIx
] = rDataVec
[ nIx
].mnType
;
141 ScAsciiOptions
& ScAsciiOptions::operator=( const ScAsciiOptions
& rCpy
)
143 SetColInfo( rCpy
.nInfoCount
, rCpy
.pColStart
, rCpy
.pColFormat
);
145 bFixedLen
= rCpy
.bFixedLen
;
146 aFieldSeps
= rCpy
.aFieldSeps
;
147 bMergeFieldSeps
= rCpy
.bMergeFieldSeps
;
148 bQuotedFieldAsText
= rCpy
.bQuotedFieldAsText
;
149 cTextSep
= rCpy
.cTextSep
;
150 eCharSet
= rCpy
.eCharSet
;
151 bCharSetSystem
= rCpy
.bCharSetSystem
;
152 nStartRow
= rCpy
.nStartRow
;
158 sal_Bool
ScAsciiOptions::operator==( const ScAsciiOptions
& rCmp
) const
160 if ( bFixedLen
== rCmp
.bFixedLen
&&
161 aFieldSeps
== rCmp
.aFieldSeps
&&
162 bMergeFieldSeps
== rCmp
.bMergeFieldSeps
&&
163 bQuotedFieldAsText
== rCmp
.bQuotedFieldAsText
&&
164 cTextSep
== rCmp
.cTextSep
&&
165 eCharSet
== rCmp
.eCharSet
&&
166 bCharSetSystem
== rCmp
.bCharSetSystem
&&
167 nStartRow
== rCmp
.nStartRow
&&
168 nInfoCount
== rCmp
.nInfoCount
)
170 OSL_ENSURE( !nInfoCount
|| (pColStart
&& pColFormat
&& rCmp
.pColStart
&& rCmp
.pColFormat
),
171 "0-Zeiger in ScAsciiOptions" );
172 for (sal_uInt16 i
=0; i
<nInfoCount
; i
++)
173 if ( pColStart
[i
] != rCmp
.pColStart
[i
] ||
174 pColFormat
[i
] != rCmp
.pColFormat
[i
] )
183 // Der Options-String darf kein Semikolon mehr enthalten (wegen Pickliste)
184 // darum ab Version 336 Komma stattdessen
188 void ScAsciiOptions::ReadFromString( const String
& rString
)
190 xub_StrLen nCount
= comphelper::string::getTokenCount(rString
, ',');
201 bFixedLen
= bMergeFieldSeps
= false;
204 aToken
= rString
.GetToken(0,',');
205 if ( aToken
.EqualsAscii(pStrFix
) )
206 bFixedLen
= sal_True
;
207 nSub
= comphelper::string::getTokenCount(aToken
, '/');
208 for ( i
=0; i
<nSub
; i
++ )
210 String aCode
= aToken
.GetToken( i
, '/' );
211 if ( aCode
.EqualsAscii(pStrMrg
) )
212 bMergeFieldSeps
= sal_True
;
215 sal_Int32 nVal
= aCode
.ToInt32();
217 aFieldSeps
+= (sal_Unicode
) nVal
;
228 aToken
= rString
.GetToken(1,',');
229 sal_Int32 nVal
= aToken
.ToInt32();
230 cTextSep
= (sal_Unicode
) nVal
;
239 aToken
= rString
.GetToken(2,',');
240 eCharSet
= ScGlobal::GetCharsetValue( aToken
);
249 aToken
= rString
.GetToken(3,',');
250 nStartRow
= aToken
.ToInt32();
262 aToken
= rString
.GetToken(4,',');
263 nSub
= comphelper::string::getTokenCount(aToken
, '/');
264 nInfoCount
= nSub
/ 2;
267 pColStart
= new sal_Int32
[nInfoCount
];
268 pColFormat
= new sal_uInt8
[nInfoCount
];
269 for (sal_uInt16 nInfo
=0; nInfo
<nInfoCount
; nInfo
++)
271 pColStart
[nInfo
] = (sal_Int32
) aToken
.GetToken( 2*nInfo
, '/' ).ToInt32();
272 pColFormat
[nInfo
] = (sal_uInt8
) aToken
.GetToken( 2*nInfo
+1, '/' ).ToInt32();
285 aToken
= rString
.GetToken(5, ',');
286 eLang
= static_cast<LanguageType
>(aToken
.ToInt32());
289 // Import quoted field as text.
292 aToken
= rString
.GetToken(6, ',');
293 bQuotedFieldAsText
= aToken
.EqualsAscii("true") ? true : false;
296 // Detect special nubmers.
299 aToken
= rString
.GetToken(7, ',');
300 bDetectSpecialNumber
= aToken
.EqualsAscii("true") ? true : false;
303 bDetectSpecialNumber
= sal_True
; // default of versions that didn't add the parameter
305 // 9th token is used for "Save as shown" in export options
306 // 10th token is used for "Save cell formulas" in export options
310 String
ScAsciiOptions::WriteToString() const
320 else if ( !aFieldSeps
.Len() )
324 xub_StrLen nLen
= aFieldSeps
.Len();
325 for (xub_StrLen i
=0; i
<nLen
; i
++)
329 aOutStr
+= OUString::number(aFieldSeps
.GetChar(i
));
331 if ( bMergeFieldSeps
)
340 OUString::number(cTextSep
) + ",";
346 if ( bCharSetSystem
) // force "SYSTEM"
347 aOutStr
+= ScGlobal::GetCharsetString( RTL_TEXTENCODING_DONTKNOW
);
349 aOutStr
+= ScGlobal::GetCharsetString( eCharSet
);
352 OUString::number(nStartRow
) + ",";
358 OSL_ENSURE( !nInfoCount
|| (pColStart
&& pColFormat
), "0-Zeiger in ScAsciiOptions" );
359 for (sal_uInt16 nInfo
=0; nInfo
<nInfoCount
; nInfo
++)
363 aOutStr
+= OUString::number(pColStart
[nInfo
]) +
365 OUString::number(pColFormat
[nInfo
]);
368 // #i112025# the options string is used in macros and linked sheets,
369 // so new options must be added at the end, to remain compatible
373 OUString::number(eLang
) + "," +
374 // Import quoted field as text.
375 OUString::boolean( bQuotedFieldAsText
) + "," +
376 // Detect special numbers.
377 OUString::boolean( bDetectSpecialNumber
);
379 // 9th token is used for "Save as shown" in export options
380 // 10th token is used for "Save cell formulas" in export options
385 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */