9f3cabc7de9bd37ef01d868df7e8e55f3760aa21
[occt.git] / src / OSD / OSD_Real2String.cxx
1 // File:        OSD_Real2String.cxx
2 // Created:     Fri Jan 25 12:58:55 2002
3 // Author:      doneux <doneux@samcef.com>
4
5 const static char sccsid[] = "@(#) OSD_Real2String.cxx    %V%-1, 06/17/02@(#)";
6 // Lastly modified by :
7 // +---------------------------------------------------------------------------+
8 // !   doneux ! Creation                                !  06/17/02!    %V%-1!
9 // +---------------------------------------------------------------------------+
10
11 #ifdef HAVE_CONFIG_H
12 # include <config.h>
13 #endif
14
15 #include <OSD_Real2String.ixx>
16 #include <stdio.h>
17 #if defined(HAVE_STDLIB_H) || defined(WNT)
18 # include <stdlib.h>
19 #endif
20
21 //=======================================================================
22 //function : OSD_Real2String
23 //purpose  : 
24 //=======================================================================
25
26 OSD_Real2String::OSD_Real2String():
27  myReadDecimalPoint(0)
28 {
29   float F1 = (float ) 1.1 ;
30   char str[5] ;
31
32   sprintf(str,"%.1f",F1) ;
33                              //  printf("%s\n",str) ;
34   myLocalDecimalPoint = str[1] ;
35 }
36
37 //=======================================================================
38 //function : RealToCString
39 //purpose  : 
40 //=======================================================================
41
42 Standard_Boolean OSD_Real2String::RealToCString(const Standard_Real theReal,
43                                                 Standard_PCharacter& theString) const
44 {
45   char *p, *q ;
46
47   if (sprintf(theString,"%.17e",theReal)  <= 0) 
48     return Standard_False ;
49
50   // Suppress "e+00" and unsignificant 0's 
51
52   if ((p = strchr(theString,'e'))) {
53     if (!strcmp(p,"e+00"))
54       *p = 0 ;
55     for (q = p-1 ; *q == '0' ; q--) ;
56     if (q != p-1) {
57  
58       if (*q != myLocalDecimalPoint) q++ ;
59
60       while (*p)
61         *q++ = *p++ ;
62       *q = 0 ;
63     }
64   }
65   return Standard_True ;
66 }
67
68 //=======================================================================
69 //function : CStringToReal
70 //purpose  : 
71 //=======================================================================
72
73 Standard_Boolean OSD_Real2String::CStringToReal(const Standard_CString theString,
74                                                 Standard_Real& theReal)
75 {
76   char *endptr ;
77
78   if (! theString) return Standard_False;
79
80    // Get the decimal point character in the string (if any)
81   if (!myReadDecimalPoint) {
82     if (strchr(theString, ',')) myReadDecimalPoint = ',';
83     else if (strchr(theString, '.')) myReadDecimalPoint = '.';
84   }
85
86
87   const char *str = theString;
88   if (myReadDecimalPoint) {
89       if (myReadDecimalPoint != myLocalDecimalPoint) {
90           const char * p;
91           char buff[1024]; 
92           // replace the decimal point by the local one
93           if(myReadDecimalPoint != myLocalDecimalPoint && 
94              (p = strchr(theString,myReadDecimalPoint))&& ((p-theString) < 1000) )
95           {
96             strncpy(buff, theString, 1000);
97             buff[p-theString] = myLocalDecimalPoint;
98             str = buff;
99           }
100       }
101   }
102
103   theReal = strtod(str,&endptr) ;
104   if (*endptr)
105     return Standard_False ;
106
107   return Standard_True;
108 }
109