Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / basegfx / source / tools / stringconversiontools.cxx
blob211f8991d490746c0d6a04c1685d8297fc84f4f2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <stringconversiontools.hxx>
21 #include <rtl/math.hxx>
23 namespace basegfx
25 namespace internal
27 void lcl_skipSpaces(sal_Int32& io_rPos,
28 const OUString& rStr,
29 const sal_Int32 nLen)
31 while( io_rPos < nLen &&
32 ' ' == rStr[io_rPos] )
34 ++io_rPos;
38 void lcl_skipSpacesAndCommas(sal_Int32& io_rPos,
39 const OUString& rStr,
40 const sal_Int32 nLen)
42 while(io_rPos < nLen
43 && (' ' == rStr[io_rPos] || ',' == rStr[io_rPos]))
45 ++io_rPos;
49 bool lcl_getDoubleChar(double& o_fRetval,
50 sal_Int32& io_rPos,
51 const OUString& rStr)
53 sal_Unicode aChar( rStr[io_rPos] );
54 OUStringBuffer sNumberString;
55 bool separator_seen=false;
57 if('+' == aChar || '-' == aChar)
59 sNumberString.append(rStr[io_rPos]);
60 aChar = rStr[++io_rPos];
63 while(('0' <= aChar && '9' >= aChar)
64 || (!separator_seen && '.' == aChar))
66 if ('.' == aChar) separator_seen = true;
67 sNumberString.append(rStr[io_rPos]);
68 io_rPos++;
69 aChar = io_rPos < rStr.getLength() ? rStr[io_rPos] : 0;
72 if('e' == aChar || 'E' == aChar)
74 sNumberString.append(rStr[io_rPos]);
75 io_rPos++;
76 aChar = io_rPos < rStr.getLength() ? rStr[io_rPos] : 0;
78 if('+' == aChar || '-' == aChar)
80 sNumberString.append(rStr[io_rPos]);
81 io_rPos++;
82 aChar = io_rPos < rStr.getLength() ? rStr[io_rPos] : 0;
85 while('0' <= aChar && '9' >= aChar)
87 sNumberString.append(rStr[io_rPos]);
88 io_rPos++;
89 aChar = io_rPos < rStr.getLength() ? rStr[io_rPos] : 0;
93 if(sNumberString.getLength())
95 rtl_math_ConversionStatus eStatus;
96 o_fRetval = ::rtl::math::stringToDouble( sNumberString.makeStringAndClear(),
97 '.',
98 ',',
99 &eStatus,
100 NULL );
101 return ( eStatus == rtl_math_ConversionStatus_Ok );
104 return false;
107 bool lcl_importDoubleAndSpaces( double& o_fRetval,
108 sal_Int32& io_rPos,
109 const OUString& rStr,
110 const sal_Int32 nLen )
112 if( !lcl_getDoubleChar(o_fRetval, io_rPos, rStr) )
113 return false;
115 lcl_skipSpacesAndCommas(io_rPos, rStr, nLen);
117 return true;
120 bool lcl_importFlagAndSpaces(sal_Int32& o_nRetval,
121 sal_Int32& io_rPos,
122 const OUString& rStr,
123 const sal_Int32 nLen)
125 sal_Unicode aChar( rStr[io_rPos] );
127 if('0' == aChar)
129 o_nRetval = 0;
130 ++io_rPos;
132 else if ('1' == aChar)
134 o_nRetval = 1;
135 ++io_rPos;
137 else
138 return false;
140 lcl_skipSpacesAndCommas(io_rPos, rStr, nLen);
142 return true;
145 void lcl_putNumberCharWithSpace( OUStringBuffer& rStr,
146 double fValue,
147 double fOldValue,
148 bool bUseRelativeCoordinates )
150 if( bUseRelativeCoordinates )
151 fValue -= fOldValue;
153 const sal_Int32 aLen( rStr.getLength() );
154 if(aLen)
156 if( lcl_isOnNumberChar(rStr[aLen - 1], false) &&
157 fValue >= 0.0 )
159 rStr.append( ' ' );
163 lcl_putNumberChar(rStr, fValue);
166 } // namespace internal
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */