Bump version to 5.0-14
[LibreOffice.git] / basegfx / source / tools / stringconversiontools.cxx
blob4fcbbc77e9c212eb11cb2f909f778619a623da7e
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 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 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 getDoubleChar(double& o_fRetval,
50 sal_Int32& io_rPos,
51 const OUString& rStr)
53 sal_Unicode aChar( rStr[io_rPos] );
54 OUStringBuffer sNumberString;
56 // sign
57 if('+' == aChar || '-' == aChar)
59 sNumberString.append(rStr[io_rPos]);
60 aChar = rStr[++io_rPos];
63 // numbers before point
64 while('0' <= aChar && '9' >= aChar)
66 sNumberString.append(rStr[io_rPos]);
67 io_rPos++;
68 aChar = io_rPos < rStr.getLength() ? rStr[io_rPos] : 0;
71 // point
72 if('.' == aChar)
74 sNumberString.append(rStr[io_rPos]);
75 io_rPos++;
76 aChar = io_rPos < rStr.getLength() ? rStr[io_rPos] : 0;
79 // numbers after point
80 while ('0' <= aChar && '9' >= aChar)
82 sNumberString.append(rStr[io_rPos]);
83 io_rPos++;
84 aChar = io_rPos < rStr.getLength() ? rStr[io_rPos] : 0;
87 // 'e'
88 if('e' == aChar || 'E' == aChar)
90 sNumberString.append(rStr[io_rPos]);
91 io_rPos++;
92 aChar = io_rPos < rStr.getLength() ? rStr[io_rPos] : 0;
94 // sign for 'e'
95 if('+' == aChar || '-' == aChar)
97 sNumberString.append(rStr[io_rPos]);
98 io_rPos++;
99 aChar = io_rPos < rStr.getLength() ? rStr[io_rPos] : 0;
102 // number for 'e'
103 while('0' <= aChar && '9' >= aChar)
105 sNumberString.append(rStr[io_rPos]);
106 io_rPos++;
107 aChar = io_rPos < rStr.getLength() ? rStr[io_rPos] : 0;
111 if(sNumberString.getLength())
113 rtl_math_ConversionStatus eStatus;
114 o_fRetval = ::rtl::math::stringToDouble( sNumberString.makeStringAndClear(),
115 '.',
116 ',',
117 &eStatus,
118 NULL );
119 return ( eStatus == rtl_math_ConversionStatus_Ok );
122 return false;
125 bool importDoubleAndSpaces(double& o_fRetval,
126 sal_Int32& io_rPos,
127 const OUString& rStr,
128 const sal_Int32 nLen )
130 if( !getDoubleChar(o_fRetval, io_rPos, rStr) )
131 return false;
133 skipSpacesAndCommas(io_rPos, rStr, nLen);
135 return true;
138 bool importFlagAndSpaces(sal_Int32& o_nRetval,
139 sal_Int32& io_rPos,
140 const OUString& rStr,
141 const sal_Int32 nLen)
143 sal_Unicode aChar( rStr[io_rPos] );
145 if('0' == aChar)
147 o_nRetval = 0;
148 ++io_rPos;
150 else if ('1' == aChar)
152 o_nRetval = 1;
153 ++io_rPos;
155 else
156 return false;
158 skipSpacesAndCommas(io_rPos, rStr, nLen);
160 return true;
163 void putNumberCharWithSpace(OUStringBuffer& rStr,
164 double fValue,
165 double fOldValue,
166 bool bUseRelativeCoordinates )
168 if( bUseRelativeCoordinates )
169 fValue -= fOldValue;
171 const sal_Int32 aLen( rStr.getLength() );
172 if(aLen)
174 if( isOnNumberChar(rStr[aLen - 1], false, true) &&
175 fValue >= 0.0 )
177 rStr.append( ' ' );
181 rStr.append(fValue);
183 } // namespace internal
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */