2007/05/31 08:32:53 pl 1.14.40.2: #i49079# use rtl::math functions instead of creating own double<->string conversion 2007/05/21 11:00:38 pl 1.14.40.1: #i77444# remove a warning ?
		
			
				
	
	
		
			450 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			450 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*************************************************************************
 | |
|  *
 | |
|  *  OpenOffice.org - a multi-platform office productivity suite
 | |
|  *
 | |
|  *  $RCSfile: strhelper.cxx,v $
 | |
|  *
 | |
|  *  $Revision: 1.15 $
 | |
|  *
 | |
|  *  last change: $Author: rt $ $Date: 2007-07-03 13:59:14 $
 | |
|  *
 | |
|  *  The Contents of this file are made available subject to
 | |
|  *  the terms of GNU Lesser General Public License Version 2.1.
 | |
|  *
 | |
|  *
 | |
|  *    GNU Lesser General Public License Version 2.1
 | |
|  *    =============================================
 | |
|  *    Copyright 2005 by Sun Microsystems, Inc.
 | |
|  *    901 San Antonio Road, Palo Alto, CA 94303, USA
 | |
|  *
 | |
|  *    This library is free software; you can redistribute it and/or
 | |
|  *    modify it under the terms of the GNU Lesser General Public
 | |
|  *    License version 2.1, as published by the Free Software Foundation.
 | |
|  *
 | |
|  *    This library is distributed in the hope that it will be useful,
 | |
|  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | |
|  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 | |
|  *    Lesser General Public License for more details.
 | |
|  *
 | |
|  *    You should have received a copy of the GNU Lesser General Public
 | |
|  *    License along with this library; if not, write to the Free Software
 | |
|  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 | |
|  *    MA  02111-1307  USA
 | |
|  *
 | |
|  ************************************************************************/
 | |
| 
 | |
| // MARKER(update_precomp.py): autogen include statement, do not remove
 | |
| #include "precompiled_psprint.hxx"
 | |
| #include <psprint/strhelper.hxx>
 | |
| #include <sal/alloca.h>
 | |
| 
 | |
| namespace psp {
 | |
| 
 | |
| inline int isSpace( char cChar )
 | |
| {
 | |
|     return
 | |
|         cChar == ' '    || cChar == '\t'    ||
 | |
|         cChar == '\r'   || cChar == '\n'    ||
 | |
|         cChar == 0x0c   || cChar == 0x0b;
 | |
| }
 | |
| 
 | |
| inline int isSpace( sal_Unicode cChar )
 | |
| {
 | |
|     return
 | |
|         cChar == ' '    || cChar == '\t'    ||
 | |
|         cChar == '\r'   || cChar == '\n'    ||
 | |
|         cChar == 0x0c   || cChar == 0x0b;
 | |
| }
 | |
| 
 | |
| inline int isProtect( char cChar )
 | |
| {
 | |
|     return cChar == '`' || cChar == '\'' || cChar == '"';
 | |
| }
 | |
| 
 | |
| inline int isProtect( sal_Unicode cChar )
 | |
| {
 | |
|     return cChar == '`' || cChar == '\'' || cChar == '"';
 | |
| }
 | |
| 
 | |
| inline void CopyUntil( char*& pTo, const char*& pFrom, char cUntil, int bIncludeUntil = 0 )
 | |
| {
 | |
|     do
 | |
|     {
 | |
|         if( *pFrom == '\\' )
 | |
|         {
 | |
|             pFrom++;
 | |
|             if( *pFrom )
 | |
|             {
 | |
|                 *pTo = *pFrom;
 | |
|                 pTo++;
 | |
|             }
 | |
|         }
 | |
|         else if( bIncludeUntil || ! isProtect( *pFrom ) )
 | |
|         {
 | |
|             *pTo = *pFrom;
 | |
|             pTo++;
 | |
|         }
 | |
|         pFrom++;
 | |
|     } while( *pFrom && *pFrom != cUntil );
 | |
|     // copy the terminating character unless zero or protector
 | |
|     if( ! isProtect( *pFrom ) || bIncludeUntil )
 | |
|     {
 | |
|         *pTo = *pFrom;
 | |
|         if( *pTo )
 | |
|             pTo++;
 | |
|     }
 | |
|     if( *pFrom )
 | |
|         pFrom++;
 | |
| }
 | |
| 
 | |
| inline void CopyUntil( sal_Unicode*& pTo, const sal_Unicode*& pFrom, sal_Unicode cUntil, int bIncludeUntil = 0 )
 | |
| {
 | |
|     do
 | |
|     {
 | |
|         if( *pFrom == '\\' )
 | |
|         {
 | |
|             pFrom++;
 | |
|             if( *pFrom )
 | |
|             {
 | |
|                 *pTo = *pFrom;
 | |
|                 pTo++;
 | |
|             }
 | |
|         }
 | |
|         else if( bIncludeUntil || ! isProtect( *pFrom ) )
 | |
|         {
 | |
|             *pTo = *pFrom;
 | |
|             pTo++;
 | |
|         }
 | |
|         pFrom++;
 | |
|     } while( *pFrom && *pFrom != cUntil );
 | |
|     // copy the terminating character unless zero or protector
 | |
|     if( ! isProtect( *pFrom ) || bIncludeUntil )
 | |
|     {
 | |
|         *pTo = *pFrom;
 | |
|         if( *pTo )
 | |
|             pTo++;
 | |
|     }
 | |
|     if( *pFrom )
 | |
|         pFrom++;
 | |
| }
 | |
| 
 | |
| String GetCommandLineToken( int nToken, const String& rLine )
 | |
| {
 | |
|     int nLen = rLine.Len();
 | |
|     if( ! nLen )
 | |
|         return String();
 | |
| 
 | |
|     int nActualToken = 0;
 | |
|     sal_Unicode* pBuffer = (sal_Unicode*)alloca( sizeof(sal_Unicode)*( nLen + 1 ) );
 | |
|     const sal_Unicode* pRun = rLine.GetBuffer();
 | |
|     sal_Unicode* pLeap = NULL;
 | |
| 
 | |
|     while( *pRun && nActualToken <= nToken )
 | |
|     {
 | |
|         while( *pRun && isSpace( *pRun ) )
 | |
|             pRun++;
 | |
|         pLeap = pBuffer;
 | |
|         while( *pRun && ! isSpace( *pRun ) )
 | |
|         {
 | |
|             if( *pRun == '\\' )
 | |
|             {
 | |
|                 // escapement
 | |
|                 pRun++;
 | |
|                 *pLeap = *pRun;
 | |
|                 pLeap++;
 | |
|                 if( *pRun )
 | |
|                     pRun++;
 | |
|             }
 | |
|             else if( *pRun == '`' )
 | |
|                 CopyUntil( pLeap, pRun, '`' );
 | |
|             else if( *pRun == '\'' )
 | |
|                 CopyUntil( pLeap, pRun, '\'' );
 | |
|             else if( *pRun == '"' )
 | |
|                 CopyUntil( pLeap, pRun, '"' );
 | |
|             else
 | |
|             {
 | |
|                 *pLeap = *pRun;
 | |
|                 pLeap++;
 | |
|                 pRun++;
 | |
|             }
 | |
|         }
 | |
|         if( nActualToken != nToken )
 | |
|             pBuffer[0] = 0;
 | |
|         nActualToken++;
 | |
|     }
 | |
| 
 | |
|     *pLeap = 0;
 | |
| 
 | |
|     String aRet( pBuffer );
 | |
|     return aRet;
 | |
| }
 | |
| 
 | |
| ByteString GetCommandLineToken( int nToken, const ByteString& rLine )
 | |
| {
 | |
|     int nLen = rLine.Len();
 | |
|     if( ! nLen )
 | |
|         return ByteString();
 | |
| 
 | |
|     int nActualToken = 0;
 | |
|     char* pBuffer = (char*)alloca( nLen + 1 );
 | |
|     const char* pRun = rLine.GetBuffer();
 | |
|     char* pLeap = NULL;
 | |
| 
 | |
|     while( *pRun && nActualToken <= nToken )
 | |
|     {
 | |
|         while( *pRun && isSpace( *pRun ) )
 | |
|             pRun++;
 | |
|         pLeap = pBuffer;
 | |
|         while( *pRun && ! isSpace( *pRun ) )
 | |
|         {
 | |
|             if( *pRun == '\\' )
 | |
|             {
 | |
|                 // escapement
 | |
|                 pRun++;
 | |
|                 *pLeap = *pRun;
 | |
|                 pLeap++;
 | |
|                 if( *pRun )
 | |
|                     pRun++;
 | |
|             }
 | |
|             else if( *pRun == '`' )
 | |
|                 CopyUntil( pLeap, pRun, '`' );
 | |
|             else if( *pRun == '\'' )
 | |
|                 CopyUntil( pLeap, pRun, '\'' );
 | |
|             else if( *pRun == '"' )
 | |
|                 CopyUntil( pLeap, pRun, '"' );
 | |
|             else
 | |
|             {
 | |
|                 *pLeap = *pRun;
 | |
|                 pLeap++;
 | |
|                 pRun++;
 | |
|             }
 | |
|         }
 | |
|         if( nActualToken != nToken )
 | |
|             pBuffer[0] = 0;
 | |
|         nActualToken++;
 | |
|     }
 | |
| 
 | |
|     *pLeap = 0;
 | |
| 
 | |
|     ByteString aRet( pBuffer );
 | |
|     return aRet;
 | |
| }
 | |
| 
 | |
| int GetCommandLineTokenCount( const String& rLine )
 | |
| {
 | |
|     if( ! rLine.Len() )
 | |
|         return 0;
 | |
| 
 | |
|     int nTokenCount = 0;
 | |
|     const sal_Unicode *pRun = rLine.GetBuffer();
 | |
| 
 | |
| 
 | |
|     while( *pRun )
 | |
|     {
 | |
|         while( *pRun && isSpace( *pRun ) )
 | |
|             pRun++;
 | |
|         if( ! *pRun )
 | |
|             break;
 | |
|         while( *pRun && ! isSpace( *pRun ) )
 | |
|         {
 | |
|             if( *pRun == '\\' )
 | |
|             {
 | |
|                 // escapement
 | |
|                 pRun++;
 | |
|                 if( *pRun )
 | |
|                     pRun++;
 | |
|             }
 | |
|             else if( *pRun == '`' )
 | |
|             {
 | |
|                 do pRun++; while( *pRun && *pRun != '`' );
 | |
|                 if( *pRun )
 | |
|                     pRun++;
 | |
|             }
 | |
|             else if( *pRun == '\'' )
 | |
|             {
 | |
|                 do pRun++; while( *pRun && *pRun != '\'' );
 | |
|                 if( *pRun )
 | |
|                     pRun++;
 | |
|             }
 | |
|             else if( *pRun == '"' )
 | |
|             {
 | |
|                 do pRun++; while( *pRun && *pRun != '"' );
 | |
|                 if( *pRun )
 | |
|                     pRun++;
 | |
|             }
 | |
|             else
 | |
|                 pRun++;
 | |
|         }
 | |
|         nTokenCount++;
 | |
|     }
 | |
| 
 | |
|     return nTokenCount;
 | |
| }
 | |
| 
 | |
| int GetCommandLineTokenCount( const ByteString& rLine )
 | |
| {
 | |
|     if( ! rLine.Len() )
 | |
|         return 0;
 | |
| 
 | |
|     int nTokenCount = 0;
 | |
|     const char *pRun = rLine.GetBuffer();
 | |
| 
 | |
| 
 | |
|     while( *pRun )
 | |
|     {
 | |
|         while( *pRun && isSpace( *pRun ) )
 | |
|             pRun++;
 | |
|         if( ! *pRun )
 | |
|             break;
 | |
|         while( *pRun && ! isSpace( *pRun ) )
 | |
|         {
 | |
|             if( *pRun == '\\' )
 | |
|             {
 | |
|                 // escapement
 | |
|                 pRun++;
 | |
|                 if( *pRun )
 | |
|                     pRun++;
 | |
|             }
 | |
|             else if( *pRun == '`' )
 | |
|             {
 | |
|                 do pRun++; while( *pRun && *pRun != '`' );
 | |
|                 if( *pRun )
 | |
|                     pRun++;
 | |
|             }
 | |
|             else if( *pRun == '\'' )
 | |
|             {
 | |
|                 do pRun++; while( *pRun && *pRun != '\'' );
 | |
|                 if( *pRun )
 | |
|                     pRun++;
 | |
|             }
 | |
|             else if( *pRun == '"' )
 | |
|             {
 | |
|                 do pRun++; while( *pRun && *pRun != '"' );
 | |
|                 if( *pRun )
 | |
|                     pRun++;
 | |
|             }
 | |
|             else
 | |
|                 pRun++;
 | |
|         }
 | |
|         nTokenCount++;
 | |
|     }
 | |
| 
 | |
|     return nTokenCount;
 | |
| }
 | |
| 
 | |
| String WhitespaceToSpace( const String& rLine, BOOL bProtect )
 | |
| {
 | |
|     int nLen = rLine.Len();
 | |
|     if( ! nLen )
 | |
|         return String();
 | |
| 
 | |
|     sal_Unicode *pBuffer = (sal_Unicode*)alloca( sizeof(sal_Unicode)*(nLen + 1) );
 | |
|     const sal_Unicode *pRun = rLine.GetBuffer();
 | |
|     sal_Unicode *pLeap = pBuffer;
 | |
| 
 | |
|     while( *pRun )
 | |
|     {
 | |
|         if( *pRun && isSpace( *pRun ) )
 | |
|         {
 | |
|             *pLeap = ' ';
 | |
|             pLeap++;
 | |
|             pRun++;
 | |
|         }
 | |
|         while( *pRun && isSpace( *pRun ) )
 | |
|             pRun++;
 | |
|         while( *pRun && ! isSpace( *pRun ) )
 | |
|         {
 | |
|             if( *pRun == '\\' )
 | |
|             {
 | |
|                 // escapement
 | |
|                 pRun++;
 | |
|                 *pLeap = *pRun;
 | |
|                 pLeap++;
 | |
|                 if( *pRun )
 | |
|                     pRun++;
 | |
|             }
 | |
|             else if( bProtect && *pRun == '`' )
 | |
|                 CopyUntil( pLeap, pRun, '`', TRUE );
 | |
|             else if( bProtect && *pRun == '\'' )
 | |
|                 CopyUntil( pLeap, pRun, '\'', TRUE );
 | |
|             else if( bProtect && *pRun == '"' )
 | |
|                 CopyUntil( pLeap, pRun, '"', TRUE );
 | |
|             else
 | |
|             {
 | |
|                 *pLeap = *pRun;
 | |
|                 *pLeap++;
 | |
|                 *pRun++;
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     *pLeap = 0;
 | |
| 
 | |
|     // there might be a space at beginning or end
 | |
|     pLeap--;
 | |
|     if( *pLeap == ' ' )
 | |
|         *pLeap = 0;
 | |
| 
 | |
|     String aRet( *pBuffer == ' ' ? pBuffer+1 : pBuffer );
 | |
|     return aRet;
 | |
| }
 | |
| 
 | |
| ByteString WhitespaceToSpace( const ByteString& rLine, BOOL bProtect )
 | |
| {
 | |
|     int nLen = rLine.Len();
 | |
|     if( ! nLen )
 | |
|         return ByteString();
 | |
| 
 | |
|     char *pBuffer = (char*)alloca( nLen + 1 );
 | |
|     const char *pRun = rLine.GetBuffer();
 | |
|     char *pLeap = pBuffer;
 | |
| 
 | |
|     while( *pRun )
 | |
|     {
 | |
|         if( *pRun && isSpace( *pRun ) )
 | |
|         {
 | |
|             *pLeap = ' ';
 | |
|             pLeap++;
 | |
|             pRun++;
 | |
|         }
 | |
|         while( *pRun && isSpace( *pRun ) )
 | |
|             pRun++;
 | |
|         while( *pRun && ! isSpace( *pRun ) )
 | |
|         {
 | |
|             if( *pRun == '\\' )
 | |
|             {
 | |
|                 // escapement
 | |
|                 pRun++;
 | |
|                 *pLeap = *pRun;
 | |
|                 pLeap++;
 | |
|                 if( *pRun )
 | |
|                     pRun++;
 | |
|             }
 | |
|             else if( bProtect && *pRun == '`' )
 | |
|                 CopyUntil( pLeap, pRun, '`', TRUE );
 | |
|             else if( bProtect && *pRun == '\'' )
 | |
|                 CopyUntil( pLeap, pRun, '\'', TRUE );
 | |
|             else if( bProtect && *pRun == '"' )
 | |
|                 CopyUntil( pLeap, pRun, '"', TRUE );
 | |
|             else
 | |
|             {
 | |
|                 *pLeap = *pRun;
 | |
|                 *pLeap++;
 | |
|                 *pRun++;
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     *pLeap = 0;
 | |
| 
 | |
|     // there might be a space at beginning or end
 | |
|     pLeap--;
 | |
|     if( *pLeap == ' ' )
 | |
|         *pLeap = 0;
 | |
| 
 | |
|     ByteString aRet( *pBuffer == ' ' ? pBuffer+1 : pBuffer );
 | |
|     return aRet;
 | |
| }
 | |
| 
 | |
| } // namespace
 |