LCOV - code coverage report
Current view: top level - libreoffice/svl/source/numbers - zforfind.hxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 7 12 58.3 %
Date: 2012-12-17 Functions: 4 6 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : #ifndef _ZFORFIND_HXX
      21             : #define _ZFORFIND_HXX
      22             : 
      23             : #include <com/sun/star/uno/Sequence.hxx>
      24             : #include <rtl/ustring.hxx>
      25             : 
      26             : class Date;
      27             : class SvNumberformat;
      28             : class SvNumberFormatter;
      29             : 
      30             : #define SV_MAX_ANZ_INPUT_STRINGS  20    // max count of substrings in input scanner
      31             : 
      32             : class ImpSvNumberInputScan
      33             : {
      34             : public:
      35             :     ImpSvNumberInputScan( SvNumberFormatter* pFormatter );
      36             :     ~ImpSvNumberInputScan();
      37             : 
      38             : /*!*/   void ChangeIntl();                      // MUST be called if language changes
      39             : 
      40             :     /// set reference date for offset calculation
      41             :     void ChangeNullDate( const sal_uInt16 nDay,
      42             :                          const sal_uInt16 nMonth,
      43             :                          const sal_uInt16 nYear );
      44             : 
      45             :     /// convert input string to number
      46             :     bool IsNumberFormat( const OUString& rString,            /// input string
      47             :                          short& F_Type,                      /// format type (in + out)
      48             :                          double& fOutNumber,                 /// value determined (out)
      49             :                          const SvNumberformat* pFormat = NULL);  /// optional a number format to which compare against
      50             : 
      51             :     /// after IsNumberFormat: get decimal position
      52          92 :     short   GetDecPos() const { return nDecPos; }
      53             :     /// after IsNumberFormat: get count of numeric substrings in input string
      54           0 :     sal_uInt16  GetAnzNums() const { return nAnzNums; }
      55             : 
      56             :     /// set threshold of two-digit year input
      57        1534 :     void    SetYear2000( sal_uInt16 nVal ) { nYear2000 = nVal; }
      58             :     /// get threshold of two-digit year input
      59           4 :     sal_uInt16  GetYear2000() const { return nYear2000; }
      60             : 
      61             :     /** Whether input can be forced to ISO 8601 format.
      62             : 
      63             :         Depends on locale's date separator and a specific date format order.
      64             : 
      65             :         @param eDateFormat
      66             :             Evaluated only on first call during one scan process, subsequent
      67             :             calls return state of nCanForceToIso8601!
      68             : 
      69             :         @see nCanForceToIso8601
      70             :      */
      71             :     bool CanForceToIso8601( DateFormat eDateFormat );
      72             : 
      73             :     void InvalidateDateAcceptancePatterns();
      74             : 
      75             : private:
      76             :     SvNumberFormatter*  pFormatter;
      77             :     OUString* pUpperMonthText;                  //* Array of month names, uppercase
      78             :     OUString* pUpperAbbrevMonthText;            //* Array of month names, abbreviated, uppercase
      79             :     OUString* pUpperGenitiveMonthText;          //* Array of genitive month names, uppercase
      80             :     OUString* pUpperGenitiveAbbrevMonthText;    //* Array of genitive month names, abbreviated, uppercase
      81             :     OUString* pUpperPartitiveMonthText;         //* Array of partitive month names, uppercase
      82             :     OUString* pUpperPartitiveAbbrevMonthText;   //* Array of partitive month names, abbreviated, uppercase
      83             :     OUString* pUpperDayText;                    //* Array of day of week names, uppercase
      84             :     OUString* pUpperAbbrevDayText;              //* Array of day of week names, abbreviated, uppercase
      85             :     OUString  aUpperCurrSymbol;                 //* Currency symbol, uppercase
      86             :     bool    bTextInitialized;                   //* Whether days and months are initialized
      87             :     bool    bScanGenitiveMonths;                //* Whether to scan an input for genitive months
      88             :     bool    bScanPartitiveMonths;               //* Whether to scan an input for partitive months
      89             :     Date* pNullDate;                            //* 30Dec1899
      90             :     // Variables for provisional results:
      91             :     OUString sStrArray[SV_MAX_ANZ_INPUT_STRINGS]; //* Array of scanned substrings
      92             :     bool       IsNum[SV_MAX_ANZ_INPUT_STRINGS]; //* Whether a substring is numeric
      93             :     sal_uInt16 nNums[SV_MAX_ANZ_INPUT_STRINGS]; //* Sequence of offsets to numeric strings
      94             :     sal_uInt16 nAnzStrings;                     //* Total count of scanned substrings
      95             :     sal_uInt16 nAnzNums;                        //* Count of numeric substrings
      96             :     bool   bDecSepInDateSeps;                   //* True <=> DecSep in {.,-,/,DateSep}
      97             :     sal_uInt8   nMatchedAllStrings;             //* Scan...String() matched all substrings,
      98             : 
      99             :     // bit mask of nMatched... constants
     100             :     static const sal_uInt8 nMatchedEndString;        // 0x01
     101             :     static const sal_uInt8 nMatchedMidString;        // 0x02
     102             :     static const sal_uInt8 nMatchedStartString;      // 0x04
     103             :     static const sal_uInt8 nMatchedVirgin;           // 0x08
     104             :     static const sal_uInt8 nMatchedUsedAsReturn;     // 0x10
     105             : 
     106             :     int    nSign;                               // Sign of number
     107             :     short  nMonth;                              // Month (1..x) if date
     108             :                                                 // negative => short format
     109             :     short  nMonthPos;                           // 1 = front, 2 = middle
     110             :                                                 // 3 = end
     111             :     sal_uInt16 nTimePos;                        // Index of first time separator (+1)
     112             :     short  nDecPos;                             // Index of substring containing "," (+1)
     113             :     short  nNegCheck;                           // '( )' for negative
     114             :     short  nESign;                              // Sign of exponent
     115             :     short  nAmPm;                               // +1 AM, -1 PM, 0 if none
     116             :     short  nLogical;                            // -1 => False, 1 => True
     117             :     sal_uInt16 nThousand;                       // Count of group (AKA thousand) separators
     118             :     sal_uInt16 nPosThousandString;              // Position of concatenaded 000,000,000 string
     119             :     short  eScannedType;                        // Scanned type
     120             :     short  eSetType;                            // Preset Type
     121             : 
     122             :     sal_uInt16 nStringScanNumFor;               // Fixed strings recognized in
     123             :                                                 // pFormat->NumFor[nNumForStringScan]
     124             :     short  nStringScanSign;                     // Sign resulting of FixString
     125             :     sal_uInt16 nYear2000;                       // Two-digit threshold
     126             :                                                 // Year as 20xx
     127             :                                                 // default 18
     128             :                                                 // number <= nYear2000 => 20xx
     129             :                                                 // number >  nYear2000 => 19xx
     130             :     sal_uInt16  nTimezonePos;                   // Index of timezone separator (+1)
     131             : 
     132             :     /** State of ISO 8601 detection.
     133             : 
     134             :         0:= don't know yet
     135             :         1:= no
     136             :         2:= yes, <=2 digits in year
     137             :         3:= yes,   3 digits in year
     138             :         4:= yes, >=4 digits in year
     139             : 
     140             :         @see MayBeIso8601()
     141             :      */
     142             :     sal_uInt8    nMayBeIso8601;
     143             : 
     144             :     /** State of ISO 8601 can be forced.
     145             : 
     146             :         0:= don't know yet
     147             :         1:= no
     148             :         2:= yes
     149             : 
     150             :         @see CanForceToIso8601()
     151             :      */
     152             :     sal_uInt8   nCanForceToIso8601;
     153             : 
     154             :     /** State of dd-month-yy or yy-month-dd detection, with month name.
     155             : 
     156             :         0:= don't know yet
     157             :         1:= no
     158             :         2:= yes, dd-month-yy
     159             :         3:= yes, yy-month-dd
     160             : 
     161             :         @see MayBeMonthDate()
     162             :      */
     163             :     sal_uInt8   nMayBeMonthDate;
     164             : 
     165             :     /** Input matched this locale dependent date acceptance pattern.
     166             :         -2 if not checked yet, -1 if no match, >=0 matched pattern.
     167             : 
     168             :         @see IsAcceptedDatePattern()
     169             :      */
     170             :     sal_Int32   nAcceptedDatePattern;
     171             :     com::sun::star::uno::Sequence< OUString >  sDateAcceptancePatterns;
     172             : 
     173             :     /** If input matched a date acceptance pattern that starts at input
     174             :         particle sStrArray[nDatePatternStart].
     175             : 
     176             :         @see IsAcceptedDatePattern()
     177             :      */
     178             :     sal_uInt16  nDatePatternStart;
     179             : 
     180             : #ifdef _ZFORFIND_CXX        // methods private to implementation
     181             :     void Reset();                               // Reset all variables before start of analysis
     182             : 
     183             :     void InitText();                            // Init of months and days of week
     184             : 
     185             :     // Convert string to double.
     186             :     // Only simple unsigned floating point values without any error detection,
     187             :     // decimal separator has to be '.'
     188             :     // If bForceFraction==true the string is taken to be the fractional part
     189             :     // of 0.1234 without the leading 0. (thus being just "1234").
     190             :     double StringToDouble( const OUString& rStr,
     191             :                            bool bForceFraction = false );
     192             : 
     193             :     // Next number/string symbol
     194             :     bool NextNumberStringSymbol( const sal_Unicode*& pStr,
     195             :                                  OUString& rSymbol );
     196             : 
     197             :     // Concatenate ,000,23 blocks
     198             :     // in input to 000123
     199             :     bool SkipThousands( const sal_Unicode*& pStr, OUString& rSymbol );
     200             : 
     201             :     // Divide numbers/strings into
     202             :     // arrays and variables above.
     203             :     // Leading blanks and blanks
     204             :     // after numbers are thrown away
     205             :     void NumberStringDivision( const OUString& rString );
     206             : 
     207             : 
     208             :     // optimized substring versions
     209             : 
     210             :     // Whether rString contains rWhat at nPos
     211       22128 :     static inline bool StringContains( const OUString& rWhat,
     212             :                                        const OUString& rString,
     213             :                                        sal_Int32 nPos )
     214             :         {
     215             :             // mostly used with one character
     216       22128 :             if ( rWhat[ 0 ] != rString[ nPos ] )
     217             :             {
     218       21324 :                 return false;
     219             :             }
     220         804 :             return StringContainsImpl( rWhat, rString, nPos );
     221             :         }
     222             : 
     223             :     // Whether pString contains rWhat at nPos
     224           0 :     static inline bool StringPtrContains( const OUString& rWhat,
     225             :                                           const sal_Unicode* pString,
     226             :                                           sal_Int32 nPos ) // nPos MUST be a valid offset from pString
     227             :         {
     228             :             // mostly used with one character
     229           0 :             if ( rWhat[ 0 ] != pString[ nPos ] )
     230             :             {
     231           0 :                 return false;
     232             :             }
     233           0 :             return StringPtrContainsImpl( rWhat, pString, nPos );
     234             :         }
     235             : 
     236             :     //! DO NOT use directly
     237             :     static bool StringContainsImpl( const OUString& rWhat,
     238             :                                     const OUString& rString,
     239             :                                     sal_Int32 nPos );
     240             :     //! DO NOT use directly
     241             :     static bool StringPtrContainsImpl( const OUString& rWhat,
     242             :                                        const sal_Unicode* pString,
     243             :                                        sal_Int32 nPos );
     244             : 
     245             :     // Skip a special character
     246             :     static inline bool SkipChar( sal_Unicode c,
     247             :                                  const OUString& rString,
     248             :                                  sal_Int32& nPos );
     249             : 
     250             :     // Skip blank
     251             :     static inline void SkipBlanks( const OUString& rString,
     252             :                                    sal_Int32& nPos );
     253             : 
     254             :     // Jump over rWhat in rString at nPos
     255             :     static inline bool SkipString( const OUString& rWhat,
     256             :                                    const OUString& rString,
     257             :                                    sal_Int32& nPos );
     258             : 
     259             :     // Recognizes exactly ,111 as group separator
     260             :     inline bool GetThousandSep( const OUString& rString,
     261             :                                 sal_Int32& nPos,
     262             :                                 sal_uInt16 nStringPos );
     263             :     // Get boolean value
     264             :     short GetLogical( const OUString& rString );
     265             : 
     266             :     // Get month and advance string position
     267             :     short GetMonth( const OUString& rString,
     268             :                     sal_Int32& nPos );
     269             : 
     270             :     // Get day of week and advance string position
     271             :     int GetDayOfWeek( const OUString& rString,
     272             :                       sal_Int32& nPos );
     273             : 
     274             :     // Get currency symbol and advance string position
     275             :     bool GetCurrency( const OUString& rString,
     276             :                       sal_Int32& nPos,
     277             :                       const SvNumberformat* pFormat = NULL ); // optional number format to match against
     278             : 
     279             :     // Get symbol AM or PM and advance string position
     280             :     bool GetTimeAmPm( const OUString& rString,
     281             :                       sal_Int32& nPos );
     282             : 
     283             :     // Get decimal separator and advance string position
     284             :     inline bool GetDecSep( const OUString& rString,
     285             :                            sal_Int32& nPos );
     286             : 
     287             :     // Get hundredth seconds separator and advance string position
     288             :     inline bool GetTime100SecSep( const OUString& rString,
     289             :                                   sal_Int32& nPos );
     290             : 
     291             :     // Get sign  and advance string position
     292             :     // Including special case '('
     293             :     int GetSign( const OUString& rString,
     294             :                  sal_Int32& nPos );
     295             : 
     296             :     // Get sign of exponent and advance string position
     297             :     short GetESign( const OUString& rString,
     298             :                     sal_Int32& nPos );
     299             : 
     300             :     // Get next number as array offset
     301             :     inline bool GetNextNumber( sal_uInt16& i,
     302             :                                sal_uInt16& j );
     303             : 
     304             :     /** Converts time -> double (only decimals)
     305             : 
     306             :         @return TRUE if time, FALSE if not (e.g. hours >12 with AM/PM)
     307             :      */
     308             :     bool GetTimeRef( double& fOutNumber,                     // result as double
     309             :                      sal_uInt16 nIndex,                      // Index of hour in input
     310             :                      sal_uInt16 nAnz );                      // Count of time substrings in input
     311             :     sal_uInt16 ImplGetDay  ( sal_uInt16 nIndex );   // Day input, 0 if no match
     312             :     sal_uInt16 ImplGetMonth( sal_uInt16 nIndex );   // Month input, zero based return, NumberOfMonths if no match
     313             :     sal_uInt16 ImplGetYear ( sal_uInt16 nIndex );   // Year input, 0 if no match
     314             : 
     315             :     // Conversion of date to number
     316             :     bool GetDateRef( double& fDays,                          // OUT: days diff to null date
     317             :                      sal_uInt16& nCounter,                   // Count of date substrings
     318             :                      const SvNumberformat* pFormat = NULL ); // optional number format to match against
     319             : 
     320             :     // Analyze start of string
     321             :     bool ScanStartString( const OUString& rString,
     322             :                           const SvNumberformat* pFormat = NULL );
     323             : 
     324             :     // Analyze middle substring
     325             :     bool ScanMidString( const OUString& rString,
     326             :                         sal_uInt16 nStringPos,
     327             :                         const SvNumberformat* pFormat = NULL );
     328             : 
     329             : 
     330             :     // Analyze end of string
     331             :     bool ScanEndString( const OUString& rString,
     332             :                         const SvNumberformat* pFormat = NULL );
     333             : 
     334             :     // Compare rString to substring of array indexed by nString
     335             :     // nString == 0xFFFF => last substring
     336             :     bool ScanStringNumFor( const OUString& rString,
     337             :                            sal_Int32 nPos,
     338             :                            const SvNumberformat* pFormat,
     339             :                            sal_uInt16 nString,
     340             :                            bool bDontDetectNegation = false );
     341             : 
     342             :     // if nMatchedAllStrings set nMatchedUsedAsReturn and return true,
     343             :     // else do nothing and return false
     344             :     bool MatchedReturn();
     345             : 
     346             :     //! Be sure that the string to be analyzed is already converted to upper
     347             :     //! case and if it contained native humber digits that they are already
     348             :     //! converted to ASCII.
     349             : 
     350             :     // Main anlyzing function
     351             :     bool IsNumberFormatMain( const OUString& rString,
     352             :                              const SvNumberformat* pFormat = NULL);    // optional number format to match against
     353             : 
     354             :     static inline bool MyIsdigit( sal_Unicode c );
     355             : 
     356             :     // native number transliteration if necessary
     357             :     void TransformInput( OUString& rString );
     358             : 
     359             :     /** Whether input matches locale dependent date acceptance pattern.
     360             : 
     361             :         @param nStartPatternAt
     362             :                The pattern matching starts at input particle
     363             :                sStrArray[nStartPatternAt].
     364             : 
     365             :         NOTE: once called the result is remembered, subsequent calls with
     366             :         different parameters do not check for a match and do not lead to a
     367             :         different result.
     368             :      */
     369             :     bool IsAcceptedDatePattern( sal_uInt16 nStartPatternAt );
     370             : 
     371             :     /** Sets (not advances!) rPos to sStrArray[nParticle].getLength() if string
     372             :         matches separator in pattern at nParticle.
     373             : 
     374             :         @returns TRUE if separator matched.
     375             :      */
     376             :     bool SkipDatePatternSeparator( sal_uInt16 nParticle, sal_Int32 & rPos );
     377             : 
     378             :     /** Obtain order of accepted date pattern coded as, for example,
     379             :         ('D'<<16)|('M'<<8)|'Y'
     380             :     */
     381             :     sal_uInt32 GetDatePatternOrder();
     382             : 
     383             :     /** Obtain date format order, from accepted date pattern if available or
     384             :         otherwise the locale's default order.
     385             :      */
     386             :     DateFormat GetDateOrder();
     387             : 
     388             :     /** Whether input may be an ISO 8601 date format, yyyy-mm-dd...
     389             : 
     390             :         Checks if input has at least 3 numbers for yyyy-mm-dd and the separator
     391             :         is '-', and 1<=mm<=12 and 1<=dd<=31.
     392             : 
     393             :         @see nMayBeIso8601
     394             :      */
     395             :     bool MayBeIso8601();
     396             : 
     397             :     /** Whether input may be a dd-month-yy format, with month name, not
     398             :         number.
     399             : 
     400             :         @see nMayBeMonthDate
     401             :      */
     402             :     bool MayBeMonthDate();
     403             : 
     404             : #endif  // _ZFORFIND_CXX
     405             : };
     406             : 
     407             : 
     408             : 
     409             : #endif  // _ZFORFIND_HXX
     410             : 
     411             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10