2010-10-27 13:11:31 +01:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-07-17 12:30:48 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2007-04-11 11:54:30 +00:00
|
|
|
|
|
|
|
#ifndef _SBXFORM_HXX
|
|
|
|
#define _SBXFORM_HXX
|
|
|
|
|
|
|
|
//====================================================================
|
2009-04-25 00:18:20 +00:00
|
|
|
// Implementation class for Basic command: Format$( d,formatStr )
|
2007-04-11 11:54:30 +00:00
|
|
|
//====================================================================
|
|
|
|
/*
|
2009-04-25 00:18:20 +00:00
|
|
|
Grammar of format string (a try):
|
2007-04-11 11:54:30 +00:00
|
|
|
-----------------------------------------------
|
|
|
|
|
|
|
|
format_string := {\special_char} general_format | scientific_format {\special_char} {;format_string}
|
|
|
|
general_format := {#[,]}{0[,]}[.{0}{#}]
|
|
|
|
scientific_format := {0}[.{0}{#}](e | E)(+ | -){#}{0}
|
|
|
|
|
|
|
|
percent_char := '%'
|
|
|
|
special_char := \char | + | - | ( | ) | $ | space_char
|
|
|
|
char := all_ascii_chars
|
|
|
|
space_char := ' '
|
|
|
|
|
2009-04-25 00:18:20 +00:00
|
|
|
{} repeated multiple times (incl. zero times)
|
|
|
|
[] exactly one or zero times
|
|
|
|
() parenthesis, e.g. (e | E) means e or E times
|
2007-04-11 11:54:30 +00:00
|
|
|
|
2009-04-25 00:18:20 +00:00
|
|
|
Additional predefined formats for the format string:
|
2007-04-11 11:54:30 +00:00
|
|
|
"General Number"
|
|
|
|
"Currency"
|
|
|
|
"Fixed"
|
|
|
|
"Standard"
|
|
|
|
"Percent"
|
|
|
|
"Scientific"
|
|
|
|
"Yes/No"
|
|
|
|
"True/False"
|
|
|
|
"On/Off"
|
|
|
|
|
2009-04-25 00:18:20 +00:00
|
|
|
Note: invalid format string are ignored just as in VisualBasic, the output is
|
|
|
|
probably 'undefined'. ASCII letters are outputted directly.
|
2007-04-11 11:54:30 +00:00
|
|
|
|
2009-04-25 00:18:20 +00:00
|
|
|
Constraints in VisualBasic:
|
|
|
|
- the exponent (scientific syntax) has a maximum of three digits!
|
2007-04-11 11:54:30 +00:00
|
|
|
|
2009-04-25 00:18:20 +00:00
|
|
|
Constraints of new implementation:
|
|
|
|
- the '+' sign is not allowed as wildcard in the mantissa
|
2007-04-11 11:54:30 +00:00
|
|
|
|
|
|
|
TODO:
|
2009-04-25 00:18:20 +00:00
|
|
|
- Date formatting
|
|
|
|
Wildcards are: 'h', 'm', 's', 'y'
|
|
|
|
predefined String-Constants/Commands:
|
2007-04-11 11:54:30 +00:00
|
|
|
"AMPM", "Long Date", "Long Time"
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2009-04-25 00:18:20 +00:00
|
|
|
There are two possibilities to get the number of digits of a number:
|
2007-04-11 11:54:30 +00:00
|
|
|
|
2009-04-25 00:18:20 +00:00
|
|
|
a) use sprintf()
|
|
|
|
b) use log10() and pow() digit
|
2007-04-11 11:54:30 +00:00
|
|
|
*/
|
2009-04-25 00:18:20 +00:00
|
|
|
#define _with_sprintf // use a)
|
2007-04-11 11:54:30 +00:00
|
|
|
|
|
|
|
#include <tools/string.hxx>
|
2011-07-28 18:17:31 +02:00
|
|
|
#include "basicdllapi.h"
|
2007-04-11 11:54:30 +00:00
|
|
|
|
2011-07-28 18:17:31 +02:00
|
|
|
class BASIC_DLLPUBLIC SbxBasicFormater {
|
2007-04-11 11:54:30 +00:00
|
|
|
public:
|
2009-04-25 00:18:20 +00:00
|
|
|
// Constructor takes signs for decimal point, thousand separation sign
|
|
|
|
// and necessary resource strings.
|
2007-04-11 11:54:30 +00:00
|
|
|
SbxBasicFormater( sal_Unicode _cDecPoint, sal_Unicode _cThousandSep,
|
|
|
|
String _sOnStrg,
|
|
|
|
String _sOffStrg,
|
|
|
|
String _sYesStrg,
|
|
|
|
String _sNoStrg,
|
|
|
|
String _sTrueStrg,
|
|
|
|
String _sFalseStrg,
|
|
|
|
String _sCurrencyStrg,
|
|
|
|
String _sCurrencyFormatStrg );
|
|
|
|
|
2009-04-25 00:18:20 +00:00
|
|
|
/* Basic command: Format$( number,format-string )
|
2007-04-11 11:54:30 +00:00
|
|
|
|
|
|
|
Parameter:
|
2009-04-25 00:18:20 +00:00
|
|
|
dNumber : number to be formated
|
|
|
|
sFormatStrg : the Format-String, e.g. ###0.0###
|
2007-04-11 11:54:30 +00:00
|
|
|
|
2009-04-25 00:18:20 +00:00
|
|
|
Return value:
|
|
|
|
String containing the formatted output
|
2007-04-11 11:54:30 +00:00
|
|
|
*/
|
|
|
|
String BasicFormat( double dNumber, String sFormatStrg );
|
|
|
|
String BasicFormatNull( String sFormatStrg );
|
|
|
|
|
2011-01-10 14:40:57 +01:00
|
|
|
static sal_Bool isBasicFormat( String sFormatStrg );
|
2008-01-28 12:58:55 +00:00
|
|
|
|
2007-04-11 11:54:30 +00:00
|
|
|
private:
|
2011-07-28 18:17:31 +02:00
|
|
|
BASIC_DLLPRIVATE inline void ShiftString( String& sStrg, sal_uInt16 nStartPos );
|
|
|
|
BASIC_DLLPRIVATE inline void StrAppendChar( String& sStrg, sal_Unicode ch );
|
|
|
|
BASIC_DLLPRIVATE void AppendDigit( String& sStrg, short nDigit );
|
|
|
|
BASIC_DLLPRIVATE void LeftShiftDecimalPoint( String& sStrg );
|
|
|
|
BASIC_DLLPRIVATE void StrRoundDigit( String& sStrg, short nPos, sal_Bool& bOverflow );
|
|
|
|
BASIC_DLLPRIVATE void StrRoundDigit( String& sStrg, short nPos );
|
|
|
|
BASIC_DLLPRIVATE void ParseBack( String& sStrg, const String& sFormatStrg,
|
2007-04-11 11:54:30 +00:00
|
|
|
short nFormatPos );
|
|
|
|
#ifdef _with_sprintf
|
2009-04-25 00:18:20 +00:00
|
|
|
// Methods for string conversion with sprintf():
|
2011-07-28 18:17:31 +02:00
|
|
|
BASIC_DLLPRIVATE void InitScan( double _dNum );
|
|
|
|
BASIC_DLLPRIVATE void InitExp( double _dNewExp );
|
|
|
|
BASIC_DLLPRIVATE short GetDigitAtPosScan( short nPos, sal_Bool& bFoundFirstDigit );
|
|
|
|
BASIC_DLLPRIVATE short GetDigitAtPosExpScan( double dNewExponent, short nPos,
|
2011-01-10 14:40:57 +01:00
|
|
|
sal_Bool& bFoundFirstDigit );
|
2011-07-28 18:17:31 +02:00
|
|
|
BASIC_DLLPRIVATE short GetDigitAtPosExpScan( short nPos, sal_Bool& bFoundFirstDigit );
|
2007-04-11 11:54:30 +00:00
|
|
|
#else
|
2009-04-25 00:18:20 +00:00
|
|
|
// Methods for direct 'calculation' with log10() and pow():
|
2011-07-28 18:17:31 +02:00
|
|
|
BASIC_DLLPRIVATE short GetDigitAtPos( double dNumber, short nPos, double& dNextNumber,
|
2011-01-10 14:40:57 +01:00
|
|
|
sal_Bool& bFoundFirstDigit );
|
2011-07-28 18:17:31 +02:00
|
|
|
BASIC_DLLPRIVATE short RoundDigit( double dNumber );
|
2007-04-11 11:54:30 +00:00
|
|
|
#endif
|
2011-07-28 18:17:31 +02:00
|
|
|
BASIC_DLLPRIVATE String GetPosFormatString( const String& sFormatStrg, sal_Bool & bFound );
|
|
|
|
BASIC_DLLPRIVATE String GetNegFormatString( const String& sFormatStrg, sal_Bool & bFound );
|
|
|
|
BASIC_DLLPRIVATE String Get0FormatString( const String& sFormatStrg, sal_Bool & bFound );
|
|
|
|
BASIC_DLLPRIVATE String GetNullFormatString( const String& sFormatStrg, sal_Bool & bFound );
|
|
|
|
BASIC_DLLPRIVATE short AnalyseFormatString( const String& sFormatStrg,
|
2007-04-11 11:54:30 +00:00
|
|
|
short& nNoOfDigitsLeft, short& nNoOfDigitsRight,
|
|
|
|
short& nNoOfOptionalDigitsLeft,
|
|
|
|
short& nNoOfExponentDigits,
|
|
|
|
short& nNoOfOptionalExponentDigits,
|
2011-01-10 14:40:57 +01:00
|
|
|
sal_Bool& bPercent, sal_Bool& bCurrency, sal_Bool& bScientific,
|
|
|
|
sal_Bool& bGenerateThousandSeparator,
|
2007-04-11 11:54:30 +00:00
|
|
|
short& nMultipleThousandSeparators );
|
2011-07-28 18:17:31 +02:00
|
|
|
BASIC_DLLPRIVATE void ScanFormatString( double dNumber, const String& sFormatStrg,
|
2011-01-10 14:40:57 +01:00
|
|
|
String& sReturnStrg, sal_Bool bCreateSign );
|
2007-04-11 11:54:30 +00:00
|
|
|
|
2009-04-25 00:18:20 +00:00
|
|
|
//*** Data ***
|
|
|
|
sal_Unicode cDecPoint; // sign for the decimal point
|
|
|
|
sal_Unicode cThousandSep; // sign for thousand delimiter
|
|
|
|
// Text for output:
|
2007-04-11 11:54:30 +00:00
|
|
|
String sOnStrg;
|
|
|
|
String sOffStrg;
|
|
|
|
String sYesStrg;
|
|
|
|
String sNoStrg;
|
|
|
|
String sTrueStrg;
|
|
|
|
String sFalseStrg;
|
|
|
|
String sCurrencyStrg;
|
|
|
|
String sCurrencyFormatStrg;
|
|
|
|
|
2009-04-25 00:18:20 +00:00
|
|
|
//*** temporary data for scan loop ***
|
2007-04-11 11:54:30 +00:00
|
|
|
//-----------------------------------------------
|
2009-04-25 00:18:20 +00:00
|
|
|
// String containing the number in scientific format
|
2007-04-11 11:54:30 +00:00
|
|
|
String sSciNumStrg;
|
2009-04-25 00:18:20 +00:00
|
|
|
// String containing the exponent of the number
|
2007-04-11 11:54:30 +00:00
|
|
|
String sNumExpStrg;
|
2009-04-25 00:18:20 +00:00
|
|
|
double dNum; // the number that is scanned
|
|
|
|
short nNumExp; // the exponent of the number
|
|
|
|
short nExpExp; // the number of digits in the exponent
|
2007-04-11 11:54:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-10-27 13:11:31 +01:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|