1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: asciiopt.cxx,v $
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"
37 #include "scresid.hxx"
39 #include "asciiopt.hxx"
40 #include "asciiopt.hrc"
41 #include <tools/debug.hxx>
42 #include <rtl/tencinfo.h>
43 #include <unotools/transliterationwrapper.hxx>
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() :
58 bMergeFieldSeps ( FALSE
),
59 bQuotedFieldAsText(false),
60 bDetectSpecialNumber(false),
61 cTextSep ( cDefaultTextSep
),
62 eCharSet ( gsl_getSystemTextEncoding() ),
63 eLang ( LANGUAGE_SYSTEM
),
64 bCharSetSystem ( FALSE
),
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
),
82 bCharSetSystem ( rOpt
.bCharSetSystem
),
83 nStartRow ( rOpt
.nStartRow
),
84 nInfoCount ( rOpt
.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
];
104 ScAsciiOptions::~ScAsciiOptions()
111 void ScAsciiOptions::SetColInfo( USHORT nCount
, const xub_StrLen
* pStart
, const BYTE
* pFormat
)
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
];
136 void ScAsciiOptions::SetColumnInfo( const ScCsvExpDataVec
& rDataVec
)
143 nInfoCount
= static_cast< sal_uInt16
>( rDataVec
.size() );
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
;
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
] )
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(',');
217 bFixedLen
= bMergeFieldSeps
= FALSE
;
220 aToken
= rString
.GetToken(0,',');
221 if ( aToken
.EqualsAscii(pStrFix
) )
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
;
231 sal_Int32 nVal
= aCode
.ToInt32();
233 aFieldSeps
+= (sal_Unicode
) nVal
;
244 aToken
= rString
.GetToken(1,',');
245 sal_Int32 nVal
= aToken
.ToInt32();
246 cTextSep
= (sal_Unicode
) nVal
;
255 aToken
= rString
.GetToken(2,',');
256 eCharSet
= ScGlobal::GetCharsetValue( aToken
);
262 aToken
= rString
.GetToken(3, ',');
263 eLang
= static_cast<LanguageType
>(aToken
.ToInt32());
272 aToken
= rString
.GetToken(4,',');
273 nStartRow
= aToken
.ToInt32();
285 aToken
= rString
.GetToken(5,',');
286 nSub
= aToken
.GetTokenCount('/');
287 nInfoCount
= nSub
/ 2;
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();
305 // Import quoted field as text.
308 aToken
= rString
.GetToken(6, ',');
309 bQuotedFieldAsText
= aToken
.EqualsAscii("true") ? true : false;
312 // Detect special nubmers.
315 aToken
= rString
.GetToken(7, ',');
316 bDetectSpecialNumber
= aToken
.EqualsAscii("true") ? true : false;
321 String
ScAsciiOptions::WriteToString() const
330 aOutStr
.AppendAscii(pStrFix
);
331 else if ( !aFieldSeps
.Len() )
335 xub_StrLen nLen
= aFieldSeps
.Len();
336 for (xub_StrLen i
=0; i
<nLen
; i
++)
340 aOutStr
+= String::CreateFromInt32(aFieldSeps
.GetChar(i
));
342 if ( bMergeFieldSeps
)
345 aOutStr
.AppendAscii(pStrMrg
);
349 aOutStr
+= ','; // Token-Ende
355 aOutStr
+= String::CreateFromInt32(cTextSep
);
356 aOutStr
+= ','; // Token-Ende
362 if ( bCharSetSystem
) // force "SYSTEM"
363 aOutStr
+= ScGlobal::GetCharsetString( RTL_TEXTENCODING_DONTKNOW
);
365 aOutStr
+= ScGlobal::GetCharsetString( eCharSet
);
366 aOutStr
+= ','; // Token-Ende
369 aOutStr
+= String::CreateFromInt32(eLang
);
376 aOutStr
+= String::CreateFromInt32(nStartRow
);
377 aOutStr
+= ','; // Token-Ende
383 DBG_ASSERT( !nInfoCount
|| (pColStart
&& pColFormat
), "0-Zeiger in ScAsciiOptions" );
384 for (USHORT nInfo
=0; nInfo
<nInfoCount
; nInfo
++)
388 aOutStr
+= String::CreateFromInt32(pColStart
[nInfo
]);
390 aOutStr
+= String::CreateFromInt32(pColFormat
[nInfo
]);
395 // Import quoted field as text.
396 aOutStr
+= String::CreateFromAscii(bQuotedFieldAsText
? "true" : "false");
399 // Detect special nubmers.
400 aOutStr
+= String::CreateFromAscii(bDetectSpecialNumber
? "true" : "false");
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
;
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
428 for (i
=0; i
<nCount
; i
++)
430 USHORT nVal
= (USHORT
) aSemiStr
.GetToken(i
);
435 for (j
=0; j
<nFound
; j
++)
437 if ( pTemp
[j
] == nVal
)
439 else if ( pTemp
[j
] < nVal
)
445 memmove( &pTemp
[nPos
+1], &pTemp
[nPos
], (nFound
-nPos
)*sizeof(USHORT
) );
452 // Eintraege uebernehmen
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
;
473 bFixedLen
= TRUE
; // sonst macht's keinen Sinn