summaryrefslogtreecommitdiff
path: root/src/OSD/OSD_Real2String.cxx
blob: 8f9edb818b7c78d6328bb474856666e5b0c9221e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// File:	OSD_Real2String.cxx
// Created:	Fri Jan 25 12:58:55 2002
// Author:	doneux <doneux@samcef.com>

const static char sccsid[] = "@(#) OSD_Real2String.cxx    %V%-1, 06/17/02@(#)";
// Lastly modified by :
// +---------------------------------------------------------------------------+
// !   doneux ! Creation                                !  06/17/02!    %V%-1!
// +---------------------------------------------------------------------------+

#ifdef HAVE_CONFIG_H
# include <oce-config.h>
#endif

#include <OSD_Real2String.ixx>
#include <stdio.h>
#if defined(HAVE_STDLIB_H) || defined(WNT)
# include <stdlib.h>
#endif

//=======================================================================
//function : OSD_Real2String
//purpose  : 
//=======================================================================

OSD_Real2String::OSD_Real2String():
 myReadDecimalPoint(0)
{
  float F1 = (float ) 1.1 ;
  char str[5] ;

  sprintf(str,"%.1f",F1) ;
                             //  printf("%s\n",str) ;
  myLocalDecimalPoint = str[1] ;
}

//=======================================================================
//function : RealToCString
//purpose  : 
//=======================================================================

Standard_Boolean OSD_Real2String::RealToCString(const Standard_Real theReal,
						Standard_PCharacter& theString) const
{
  char *p, *q ;

  if (sprintf(theString,"%.17e",theReal)  <= 0) 
    return Standard_False ;

  // Suppress "e+00" and unsignificant 0's 

  if ((p = strchr(theString,'e')) != NULL) {
    if (!strcmp(p,"e+00"))
      *p = 0 ;
    for (q = p-1 ; *q == '0' ; q--) ;
    if (q != p-1) {
 
      if (*q != myLocalDecimalPoint) q++ ;

      while (*p)
	*q++ = *p++ ;
      *q = 0 ;
    }
  }
  return Standard_True ;
}

//=======================================================================
//function : CStringToReal
//purpose  : 
//=======================================================================

Standard_Boolean OSD_Real2String::CStringToReal(const Standard_CString theString,
						Standard_Real& theReal)
{
  char *endptr ;

  if (! theString) return Standard_False;

   // Get the decimal point character in the string (if any)
  if (!myReadDecimalPoint) {
    if (strchr(theString, ',')) myReadDecimalPoint = ',';
    else if (strchr(theString, '.')) myReadDecimalPoint = '.';
  }


  const char *str = theString;
  if (myReadDecimalPoint) {
      if (myReadDecimalPoint != myLocalDecimalPoint) {
	  const char * p;
          char buff[1024]; 
	  // replace the decimal point by the local one
          if(myReadDecimalPoint != myLocalDecimalPoint && 
             ((p = strchr(theString,myReadDecimalPoint)) != NULL) && ((p-theString) < 1000) )
          {
            strncpy(buff, theString, 1000);
            buff[p-theString] = myLocalDecimalPoint;
            str = buff;
          }
      }
  }

  theReal = strtod(str,&endptr) ;
  if (*endptr)
    return Standard_False ;

  return Standard_True;
}