b311480e |
1 | // Created on: 2002-01-25 |
2 | // Created by: doneux |
3 | // Copyright (c) 2002-2012 OPEN CASCADE SAS |
4 | // |
5 | // The content of this file is subject to the Open CASCADE Technology Public |
6 | // License Version 6.5 (the "License"). You may not use the content of this file |
7 | // except in compliance with the License. Please obtain a copy of the License |
8 | // at http://www.opencascade.org and read it completely before using this file. |
9 | // |
10 | // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its |
11 | // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France. |
12 | // |
13 | // The Original Code and all software distributed under the License is |
14 | // distributed on an "AS IS" basis, without warranty of any kind, and the |
15 | // Initial Developer hereby disclaims all such warranties, including without |
16 | // limitation, any warranties of merchantability, fitness for a particular |
17 | // purpose or non-infringement. Please see the License for the specific terms |
18 | // and conditions governing the rights and limitations under the License. |
19 | |
7fd59977 |
20 | |
7fd59977 |
21 | // Lastly modified by : |
22 | // +---------------------------------------------------------------------------+ |
23 | // ! doneux ! Creation ! 06/17/02! %V%-1! |
24 | // +---------------------------------------------------------------------------+ |
25 | |
26 | #ifdef HAVE_CONFIG_H |
27 | # include <config.h> |
28 | #endif |
29 | |
30 | #include <OSD_Real2String.ixx> |
31 | #include <stdio.h> |
32 | #if defined(HAVE_STDLIB_H) || defined(WNT) |
33 | # include <stdlib.h> |
34 | #endif |
35 | |
36 | //======================================================================= |
37 | //function : OSD_Real2String |
38 | //purpose : |
39 | //======================================================================= |
40 | |
41 | OSD_Real2String::OSD_Real2String(): |
42 | myReadDecimalPoint(0) |
43 | { |
44 | float F1 = (float ) 1.1 ; |
45 | char str[5] ; |
46 | |
47 | sprintf(str,"%.1f",F1) ; |
48 | // printf("%s\n",str) ; |
49 | myLocalDecimalPoint = str[1] ; |
50 | } |
51 | |
52 | //======================================================================= |
53 | //function : RealToCString |
54 | //purpose : |
55 | //======================================================================= |
56 | |
57 | Standard_Boolean OSD_Real2String::RealToCString(const Standard_Real theReal, |
58 | Standard_PCharacter& theString) const |
59 | { |
60 | char *p, *q ; |
61 | |
62 | if (sprintf(theString,"%.17e",theReal) <= 0) |
63 | return Standard_False ; |
64 | |
65 | // Suppress "e+00" and unsignificant 0's |
66 | |
67 | if ((p = strchr(theString,'e'))) { |
68 | if (!strcmp(p,"e+00")) |
69 | *p = 0 ; |
70 | for (q = p-1 ; *q == '0' ; q--) ; |
71 | if (q != p-1) { |
72 | |
73 | if (*q != myLocalDecimalPoint) q++ ; |
74 | |
75 | while (*p) |
76 | *q++ = *p++ ; |
77 | *q = 0 ; |
78 | } |
79 | } |
80 | return Standard_True ; |
81 | } |
82 | |
83 | //======================================================================= |
84 | //function : CStringToReal |
85 | //purpose : |
86 | //======================================================================= |
87 | |
88 | Standard_Boolean OSD_Real2String::CStringToReal(const Standard_CString theString, |
89 | Standard_Real& theReal) |
90 | { |
91 | char *endptr ; |
92 | |
93 | if (! theString) return Standard_False; |
94 | |
95 | // Get the decimal point character in the string (if any) |
96 | if (!myReadDecimalPoint) { |
97 | if (strchr(theString, ',')) myReadDecimalPoint = ','; |
98 | else if (strchr(theString, '.')) myReadDecimalPoint = '.'; |
99 | } |
100 | |
101 | |
102 | const char *str = theString; |
abd9003d |
103 | char buff[1024]; |
7fd59977 |
104 | if (myReadDecimalPoint) { |
105 | if (myReadDecimalPoint != myLocalDecimalPoint) { |
abd9003d |
106 | const char * p; |
7fd59977 |
107 | // replace the decimal point by the local one |
108 | if(myReadDecimalPoint != myLocalDecimalPoint && |
109 | (p = strchr(theString,myReadDecimalPoint))&& ((p-theString) < 1000) ) |
110 | { |
111 | strncpy(buff, theString, 1000); |
112 | buff[p-theString] = myLocalDecimalPoint; |
113 | str = buff; |
114 | } |
115 | } |
116 | } |
117 | |
118 | theReal = strtod(str,&endptr) ; |
119 | if (*endptr) |
120 | return Standard_False ; |
121 | |
122 | return Standard_True; |
123 | } |
124 | |