update dev300-m57
[ooovba.git] / sc / source / ui / dbgui / asciiopt.cxx
blob6b26dee76affa6360c06607fbff1508ad12ffecf
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: asciiopt.cxx,v $
10 * $Revision: 1.25 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
36 #include "global.hxx"
37 #include "scresid.hxx"
38 #include "impex.hxx"
39 #include "asciiopt.hxx"
40 #include "asciiopt.hrc"
41 #include <tools/debug.hxx>
42 #include <rtl/tencinfo.h>
43 #include <unotools/transliterationwrapper.hxx>
44 // ause
45 #include "editutil.hxx"
47 // ============================================================================
49 static const sal_Char __FAR_DATA pStrFix[] = "FIX";
50 static const sal_Char __FAR_DATA pStrMrg[] = "MRG";
53 // ============================================================================
55 ScAsciiOptions::ScAsciiOptions() :
56 bFixedLen ( FALSE ),
57 aFieldSeps ( ';' ),
58 bMergeFieldSeps ( FALSE ),
59 bQuotedFieldAsText(false),
60 bDetectSpecialNumber(false),
61 cTextSep ( cDefaultTextSep ),
62 eCharSet ( gsl_getSystemTextEncoding() ),
63 eLang ( LANGUAGE_SYSTEM ),
64 bCharSetSystem ( FALSE ),
65 nStartRow ( 1 ),
66 nInfoCount ( 0 ),
67 pColStart ( NULL ),
68 pColFormat ( NULL )
73 ScAsciiOptions::ScAsciiOptions(const ScAsciiOptions& rOpt) :
74 bFixedLen ( rOpt.bFixedLen ),
75 aFieldSeps ( rOpt.aFieldSeps ),
76 bMergeFieldSeps ( rOpt.bMergeFieldSeps ),
77 bQuotedFieldAsText(rOpt.bQuotedFieldAsText),
78 bDetectSpecialNumber(rOpt.bDetectSpecialNumber),
79 cTextSep ( rOpt.cTextSep ),
80 eCharSet ( rOpt.eCharSet ),
81 eLang ( rOpt.eLang ),
82 bCharSetSystem ( rOpt.bCharSetSystem ),
83 nStartRow ( rOpt.nStartRow ),
84 nInfoCount ( rOpt.nInfoCount )
86 if (nInfoCount)
88 pColStart = new xub_StrLen[nInfoCount];
89 pColFormat = new BYTE[nInfoCount];
90 for (USHORT i=0; i<nInfoCount; i++)
92 pColStart[i] = rOpt.pColStart[i];
93 pColFormat[i] = rOpt.pColFormat[i];
96 else
98 pColStart = NULL;
99 pColFormat = NULL;
104 ScAsciiOptions::~ScAsciiOptions()
106 delete[] pColStart;
107 delete[] pColFormat;
111 void ScAsciiOptions::SetColInfo( USHORT nCount, const xub_StrLen* pStart, const BYTE* pFormat )
113 delete[] pColStart;
114 delete[] pColFormat;
116 nInfoCount = nCount;
118 if (nInfoCount)
120 pColStart = new xub_StrLen[nInfoCount];
121 pColFormat = new BYTE[nInfoCount];
122 for (USHORT i=0; i<nInfoCount; i++)
124 pColStart[i] = pStart[i];
125 pColFormat[i] = pFormat[i];
128 else
130 pColStart = NULL;
131 pColFormat = NULL;
136 void ScAsciiOptions::SetColumnInfo( const ScCsvExpDataVec& rDataVec )
138 delete[] pColStart;
139 pColStart = NULL;
140 delete[] pColFormat;
141 pColFormat = NULL;
143 nInfoCount = static_cast< sal_uInt16 >( rDataVec.size() );
144 if( nInfoCount )
146 pColStart = new xub_StrLen[ nInfoCount ];
147 pColFormat = new sal_uInt8[ nInfoCount ];
148 for( sal_uInt16 nIx = 0; nIx < nInfoCount; ++nIx )
150 pColStart[ nIx ] = rDataVec[ nIx ].mnIndex;
151 pColFormat[ nIx ] = rDataVec[ nIx ].mnType;
157 ScAsciiOptions& ScAsciiOptions::operator=( const ScAsciiOptions& rCpy )
159 SetColInfo( rCpy.nInfoCount, rCpy.pColStart, rCpy.pColFormat );
161 bFixedLen = rCpy.bFixedLen;
162 aFieldSeps = rCpy.aFieldSeps;
163 bMergeFieldSeps = rCpy.bMergeFieldSeps;
164 bQuotedFieldAsText = rCpy.bQuotedFieldAsText;
165 cTextSep = rCpy.cTextSep;
166 eCharSet = rCpy.eCharSet;
167 bCharSetSystem = rCpy.bCharSetSystem;
168 nStartRow = rCpy.nStartRow;
170 return *this;
174 BOOL ScAsciiOptions::operator==( const ScAsciiOptions& rCmp ) const
176 if ( bFixedLen == rCmp.bFixedLen &&
177 aFieldSeps == rCmp.aFieldSeps &&
178 bMergeFieldSeps == rCmp.bMergeFieldSeps &&
179 bQuotedFieldAsText == rCmp.bQuotedFieldAsText &&
180 cTextSep == rCmp.cTextSep &&
181 eCharSet == rCmp.eCharSet &&
182 bCharSetSystem == rCmp.bCharSetSystem &&
183 nStartRow == rCmp.nStartRow &&
184 nInfoCount == rCmp.nInfoCount )
186 DBG_ASSERT( !nInfoCount || (pColStart && pColFormat && rCmp.pColStart && rCmp.pColFormat),
187 "0-Zeiger in ScAsciiOptions" );
188 for (USHORT i=0; i<nInfoCount; i++)
189 if ( pColStart[i] != rCmp.pColStart[i] ||
190 pColFormat[i] != rCmp.pColFormat[i] )
191 return FALSE;
193 return TRUE;
195 return FALSE;
199 // Der Options-String darf kein Semikolon mehr enthalten (wegen Pickliste)
200 // darum ab Version 336 Komma stattdessen
204 void ScAsciiOptions::ReadFromString( const String& rString )
206 xub_StrLen nCount = rString.GetTokenCount(',');
207 String aToken;
208 xub_StrLen nSub;
209 xub_StrLen i;
212 // Feld-Trenner
215 if ( nCount >= 1 )
217 bFixedLen = bMergeFieldSeps = FALSE;
218 aFieldSeps.Erase();
220 aToken = rString.GetToken(0,',');
221 if ( aToken.EqualsAscii(pStrFix) )
222 bFixedLen = TRUE;
223 nSub = aToken.GetTokenCount('/');
224 for ( i=0; i<nSub; i++ )
226 String aCode = aToken.GetToken( i, '/' );
227 if ( aCode.EqualsAscii(pStrMrg) )
228 bMergeFieldSeps = TRUE;
229 else
231 sal_Int32 nVal = aCode.ToInt32();
232 if ( nVal )
233 aFieldSeps += (sal_Unicode) nVal;
239 // Text-Trenner
242 if ( nCount >= 2 )
244 aToken = rString.GetToken(1,',');
245 sal_Int32 nVal = aToken.ToInt32();
246 cTextSep = (sal_Unicode) nVal;
250 // Zeichensatz
253 if ( nCount >= 3 )
255 aToken = rString.GetToken(2,',');
256 eCharSet = ScGlobal::GetCharsetValue( aToken );
259 // Language
260 if (nCount >= 4)
262 aToken = rString.GetToken(3, ',');
263 eLang = static_cast<LanguageType>(aToken.ToInt32());
267 // Startzeile
270 if ( nCount >= 5 )
272 aToken = rString.GetToken(4,',');
273 nStartRow = aToken.ToInt32();
277 // Spalten-Infos
280 if ( nCount >= 6 )
282 delete[] pColStart;
283 delete[] pColFormat;
285 aToken = rString.GetToken(5,',');
286 nSub = aToken.GetTokenCount('/');
287 nInfoCount = nSub / 2;
288 if (nInfoCount)
290 pColStart = new xub_StrLen[nInfoCount];
291 pColFormat = new BYTE[nInfoCount];
292 for (USHORT nInfo=0; nInfo<nInfoCount; nInfo++)
294 pColStart[nInfo] = (xub_StrLen) aToken.GetToken( 2*nInfo, '/' ).ToInt32();
295 pColFormat[nInfo] = (BYTE) aToken.GetToken( 2*nInfo+1, '/' ).ToInt32();
298 else
300 pColStart = NULL;
301 pColFormat = NULL;
305 // Import quoted field as text.
306 if (nCount >= 7)
308 aToken = rString.GetToken(6, ',');
309 bQuotedFieldAsText = aToken.EqualsAscii("true") ? true : false;
312 // Detect special nubmers.
313 if (nCount >= 8)
315 aToken = rString.GetToken(7, ',');
316 bDetectSpecialNumber = aToken.EqualsAscii("true") ? true : false;
321 String ScAsciiOptions::WriteToString() const
323 String aOutStr;
326 // Feld-Trenner
329 if ( bFixedLen )
330 aOutStr.AppendAscii(pStrFix);
331 else if ( !aFieldSeps.Len() )
332 aOutStr += '0';
333 else
335 xub_StrLen nLen = aFieldSeps.Len();
336 for (xub_StrLen i=0; i<nLen; i++)
338 if (i)
339 aOutStr += '/';
340 aOutStr += String::CreateFromInt32(aFieldSeps.GetChar(i));
342 if ( bMergeFieldSeps )
344 aOutStr += '/';
345 aOutStr.AppendAscii(pStrMrg);
349 aOutStr += ','; // Token-Ende
352 // Text-Trenner
355 aOutStr += String::CreateFromInt32(cTextSep);
356 aOutStr += ','; // Token-Ende
359 // Zeichensatz
362 if ( bCharSetSystem ) // force "SYSTEM"
363 aOutStr += ScGlobal::GetCharsetString( RTL_TEXTENCODING_DONTKNOW );
364 else
365 aOutStr += ScGlobal::GetCharsetString( eCharSet );
366 aOutStr += ','; // Token-Ende
368 // Language
369 aOutStr += String::CreateFromInt32(eLang);
370 aOutStr += ',';
373 // Startzeile
376 aOutStr += String::CreateFromInt32(nStartRow);
377 aOutStr += ','; // Token-Ende
380 // Spalten-Infos
383 DBG_ASSERT( !nInfoCount || (pColStart && pColFormat), "0-Zeiger in ScAsciiOptions" );
384 for (USHORT nInfo=0; nInfo<nInfoCount; nInfo++)
386 if (nInfo)
387 aOutStr += '/';
388 aOutStr += String::CreateFromInt32(pColStart[nInfo]);
389 aOutStr += '/';
390 aOutStr += String::CreateFromInt32(pColFormat[nInfo]);
393 aOutStr += ',';
395 // Import quoted field as text.
396 aOutStr += String::CreateFromAscii(bQuotedFieldAsText ? "true" : "false");
397 aOutStr += ',';
399 // Detect special nubmers.
400 aOutStr += String::CreateFromAscii(bDetectSpecialNumber ? "true" : "false");
402 return aOutStr;
405 #if 0
406 // Code, um die Spalten-Liste aus einem Excel-kompatiblen String zu erzeugen:
407 // (im Moment nicht benutzt)
409 void ScAsciiOptions::InterpretColumnList( const String& rString )
411 // Eingabe ist 1-basiert, pColStart fuer FixedLen ist 0-basiert
413 // Kommas durch Semikolon ersetzen
415 String aSemiStr = rString;
416 USHORT nPos = 0;
418 nPos = aSemiStr.SearchAndReplace( ',', ';', nPos );
419 while ( nPos != STRING_NOTFOUND );
421 // Eintraege sortieren
423 USHORT nCount = aSemiStr.GetTokenCount();
424 USHORT* pTemp = new USHORT[nCount+1];
425 pTemp[0] = 1; // erste Spalte faengt immer bei 1 an
426 USHORT nFound = 1;
427 USHORT i,j;
428 for (i=0; i<nCount; i++)
430 USHORT nVal = (USHORT) aSemiStr.GetToken(i);
431 if (nVal)
433 BOOL bThere = FALSE;
434 nPos = 0;
435 for (j=0; j<nFound; j++)
437 if ( pTemp[j] == nVal )
438 bThere = TRUE;
439 else if ( pTemp[j] < nVal )
440 nPos = j+1;
442 if ( !bThere )
444 if ( nPos < nFound )
445 memmove( &pTemp[nPos+1], &pTemp[nPos], (nFound-nPos)*sizeof(USHORT) );
446 pTemp[nPos] = nVal;
447 ++nFound;
452 // Eintraege uebernehmen
454 delete[] pColStart;
455 delete[] pColFormat;
456 nInfoCount = nFound;
457 if (nInfoCount)
459 pColStart = new USHORT[nInfoCount];
460 pColFormat = new BYTE[nInfoCount];
461 for (i=0; i<nInfoCount; i++)
463 pColStart[i] = pTemp[i] - 1;
464 pColFormat[i] = SC_COL_STANDARD;
467 else
469 pColStart = NULL;
470 pColFormat = NULL;
473 bFixedLen = TRUE; // sonst macht's keinen Sinn
475 // aufraeumen
477 delete[] pTemp;
479 #endif