xref: /trunk/main/vcl/inc/vcl/strhelper.hxx (revision 86e1cf34)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _PSPRINT_STRHELPER_HXX_
24 #define _PSPRINT_STRHELPER_HXX_
25 
26 #include "vcl/dllapi.h"
27 
28 #include <tools/string.hxx>
29 #include <rtl/math.hxx>
30 
31 #include <cstring>
32 
33 namespace psp {
34 
35 String VCL_DLLPUBLIC GetCommandLineToken( int, const String& );
36 ByteString VCL_DLLPUBLIC GetCommandLineToken( int, const ByteString& );
37 // gets one token of a unix command line style string
38 // doublequote, singlequote and singleleftquote protect their respective
39 // contents
40 
41 int VCL_DLLPUBLIC GetCommandLineTokenCount( const String& );
42 int VCL_DLLPUBLIC GetCommandLineTokenCount( const ByteString& );
43 // returns number of tokens (zero if empty or whitespace only)
44 
45 String VCL_DLLPUBLIC WhitespaceToSpace( const String&, sal_Bool bProtect = sal_True );
46 ByteString VCL_DLLPUBLIC WhitespaceToSpace( const ByteString&, sal_Bool bProtect = sal_True );
47 // returns a string with multiple adjacent occurrences of whitespace
48 // converted to a single space. if bProtect is sal_True (nonzero), then
49 // doublequote, singlequote and singleleftquote protect their respective
50 // contents
51 
52 
53 // parses the first double in the string; decimal is '.' only
StringToDouble(const String & rStr)54 inline double VCL_DLLPUBLIC StringToDouble( const String& rStr )
55 {
56     rtl_math_ConversionStatus eStatus;
57     return rtl::math::stringToDouble( rStr, sal_Unicode('.'), sal_Unicode(0), &eStatus, NULL);
58 }
59 
StringToDouble(const ByteString & rStr)60 inline double VCL_DLLPUBLIC StringToDouble( const ByteString& rStr )
61 {
62     rtl_math_ConversionStatus eStatus;
63     return rtl::math::stringToDouble( rtl::OStringToOUString( rStr, osl_getThreadTextEncoding() ), sal_Unicode('.'), sal_Unicode(0), &eStatus, NULL);
64 }
65 
66 // fills a character buffer with the string representation of a double
67 // the buffer has to be long enough (e.g. 128 bytes)
68 // returns the string len
getValueOfDouble(char * pBuffer,double f,int nPrecision=0)69 inline int VCL_DLLPUBLIC getValueOfDouble( char* pBuffer, double f, int nPrecision = 0)
70 {
71     rtl::OString aStr( rtl::math::doubleToString( f, rtl_math_StringFormat_G, nPrecision, '.', true ) );
72     int nLen = aStr.getLength();
73     std::strncpy( pBuffer, aStr.getStr(), nLen+1 ); // copy string including terminating zero
74     return nLen;
75 }
76 
77 } // namespace
78 
79 #endif // _PSPRINT_STRHELPER_HXX_
80