Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / basegfx / source / tools / stringconversiontools.cxx
blob6e48c22b1f892778dd79d2f8f618b53d4f9878a2
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 static 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 static bool getDoubleChar(double& o_fRetval,
50 sal_Int32& io_rPos,
51 const OUString& rStr)
53 sal_Unicode aChar = io_rPos < rStr.getLength() ? rStr[io_rPos] : 0;
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(aChar == 'e' || aChar == 'E')
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 return ( eStatus == rtl_math_ConversionStatus_Ok );
121 return false;
124 bool importDoubleAndSpaces(double& o_fRetval,
125 sal_Int32& io_rPos,
126 const OUString& rStr,
127 const sal_Int32 nLen )
129 if( !getDoubleChar(o_fRetval, io_rPos, rStr) )
130 return false;
132 skipSpacesAndCommas(io_rPos, rStr, nLen);
134 return true;
137 bool importFlagAndSpaces(sal_Int32& o_nRetval,
138 sal_Int32& io_rPos,
139 const OUString& rStr,
140 const sal_Int32 nLen)
142 sal_Unicode aChar( rStr[io_rPos] );
144 if(aChar == '0')
146 o_nRetval = 0;
147 ++io_rPos;
149 else if (aChar == '1')
151 o_nRetval = 1;
152 ++io_rPos;
154 else
155 return false;
157 skipSpacesAndCommas(io_rPos, rStr, nLen);
159 return true;
162 } // namespace internal
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */