summaryrefslogtreecommitdiff
path: root/src/OSD/OSD.cxx
blob: b0645b5995698099f6bd2d6ee4f0ab9549821218 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283

#include <OSD.ixx>

#include <stdio.h>
#include <math.h>
#if HAVE_IEEEFP_H
# include <ieeefp.h>
#endif
#if !defined(HAVE_FINITE) && defined(isfinite)
# define finite isfinite
#endif

static char DecimalPoint = 0 ;

static void GetDecimalPoint() {
  float F1 = (float ) 1.1 ;
  char str[5] ;

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

// Convert Real to CString in format e with 16 significant digits.
// The decimal point character is always a period.
// The conversion is independant from current locale database

Standard_Boolean OSD::RealToCString(const Standard_Real aReal,
				    Standard_PCharacter& aString)
{
  char *p, *q ;
  
  // Get the local decimal point character 

  if (!DecimalPoint)
    GetDecimalPoint() ;

  // Substitute it

//  if (sprintf(aString,"%.15le",aReal)  <= 0)
  if (sprintf(aString,"%.17e",aReal)  <= 0) //BUC60808
    return Standard_False ;

  if ((p = strchr(aString,DecimalPoint)) != 0)
    *p = '.' ;

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

  if ((p = strchr(aString,'e')) != 0) {
    if (!strcmp(p,"e+00"))
      *p = 0 ;
    for (q = p-1 ; *q == '0' ; q--) ;
    if (q != p-1) {
      if (*q != '.') q++ ;
      while (*p)
	*q++ = *p++ ;
      *q = 0 ;
    }
  }
  return Standard_True ;
}

// Make the RealToCString reciprocal conversion.

Standard_Boolean OSD::CStringToReal(const Standard_CString aString,
				    Standard_Real& aReal)
{
  const char *p;
  char *endptr ;
 

  // Get the local decimal point character 

  if (!DecimalPoint)
    GetDecimalPoint() ;

  const char *str = aString;
  char buff[1024];  
  //if((p = strchr(aString,'.')))
  if(DecimalPoint != '.' && ((p = strchr(aString,'.')) != NULL) && ((p-aString) < 1000) )
  {
    strncpy(buff, aString, 1000);
    buff[p-aString] = DecimalPoint ;
    str = buff;
  }
  aReal = strtod(str,&endptr) ;
  if (*endptr)
    return Standard_False ;
  return Standard_True;
}

//=======================================================================
//function : OSDSecSleep
//purpose  : Cause the process to sleep during a amount of seconds 
//=======================================================================

#ifdef WNT
# include <windows.h>
# ifdef _MSVC_VER
#  include <Mapiwin.h>
# endif
# define _DEXPLEN	             11
# define _IEEE		              1
# define DMAXEXP	             ((1 << _DEXPLEN - 1) - 1 + _IEEE)
# define finite                      _finite
# define SLEEP(NSEC)                 Sleep(1000*(NSEC))
#else
# define SLEEP(NSEC)                 sleep(NSEC)
#endif

#ifdef HAVE_VALUES_H
//# include <values.h>
#endif

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

void OSD::SecSleep(const Standard_Integer aDelay)
{
  SLEEP(aDelay);
}

//=======================================================================
//function : MilliSecSleep
//purpose  : Cause the process to sleep during a amount of milliseconds  
//=======================================================================

#ifdef WNT

void OSD::MilliSecSleep(const Standard_Integer aDelay)
{
  Sleep(aDelay) ;
}

#else

#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif

void OSD::MilliSecSleep(const Standard_Integer aDelay)
{
  struct timeval timeout ;

  timeout.tv_sec = aDelay / 1000 ;
  timeout.tv_usec = (aDelay % 1000) * 1000 ;

  select(0,NULL,NULL,NULL,&timeout) ;
}

#endif

//=======================================================================
//function : IsDivisible
//purpose  : 
//=======================================================================

Standard_Boolean OSD::IsDivisible(const Standard_Real theDividend,const Standard_Real theDivisor)
{
  if ( theDivisor == 0. || ! finite(theDividend) ) return Standard_False;
  //
  // you may divide by infinity
  //
  if (! finite(theDivisor)) return Standard_True;
#ifdef DEB
//   Standard_Integer aExp1,  aExp2;
//   Standard_Real aMant1 = frexp(theDividend, &aExp1);
//   Standard_Real aMant2 = frexp(theDivisor, &aExp2);
#endif
// Code de :KL:dev:ref :
//  return ((aExp1-aExp2 < DMAXEXP) ||        // exponent of the result
//	  (aExp1-aExp2 == DMAXEXP && aMant1 < aMant2)) ;

// Code de :KAS:C30 :
  return Standard_True;


// this is unacceptable return for Linux because of (temporary?) undefined  aExp1 and aExp2.
// Testing both over and underflow should be (:KL:dev ) :

//  return ((aExp1-aExp2 < DMAXEXP) && (aExp1-aExp2 > DMINEXP) ||
//	  (aExp1-aExp2 == DMAXEXP && aMant1 < aMant2) ||
//	  (aExp1-aExp2 == DMINEXP && aMant1 > aMant2)) ;
}

//=======================================================================
//function : GetExponent
//purpose  : 
//=======================================================================

//Standard_Integer OSD::GetExponent(const Standard_Real aReal)
Standard_Integer OSD::GetExponent(const Standard_Real )
{
// Code de :KAS:C30 :

  cout << "Function OSD::GetExponent() not yet implemented." << endl;
  return 0 ;

// Code de :KL:dev:ref :

//  Standard_Integer Exp ;

////  Standard_Real Mant = frexp(aReal, &Exp) ;
//  frexp(aReal, &Exp) ;
//  return Exp ;
}

//=======================================================================
//function : GetMantissa
//purpose  : 
//=======================================================================

//Standard_Real OSD::GetMantissa(const Standard_Real aReal)
Standard_Real OSD::GetMantissa(const Standard_Real )
{
// Code de :KAS:C30 :
  cout << "Function OSD::GetMantissa() not yet implemented." << endl;
  return 0 ;

// Code de :KL:dev:ref :

//  Standard_Integer Exp ;
//  return frexp(aReal, &Exp) ;
}

//=======================================================================
// Code de :KAS:C30 :
//=======================================================================
Standard_Integer OSD::AvailableMemory()
{
  cout << "Function OSD::AvailableMemory() not yet implemented." << endl;
  return 0 ;
}

// Code de :KL:dev:ref ??????????????? :
#if 0
//=======================================================================
//function : AvailableMemory
//purpose  : 
//=======================================================================
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>

# if defined(SUN) || defined(__sun) || defined(SOLARIS)
#  define SIZE_MAX  0x7fffffff
# elif defined(__osf__)  || defined(DECOSF1)        
#  define SIZE_MAX  0x10000000000 
# elif defined(WNT)
#  define SIZE_MAX 0x7ffdefff
# else
#  define SIZE_MAX  0xffffffff
# endif

Standard_Integer OSD::AvailableMemory()
{
  size_t min = 1024 ;
  size_t max = SIZE_MAX ;
  size_t middle = SIZE_MAX ;
  void * addr ;
  int nballoc = 0 ;
  int nbfree = 0 ;

  while (min + 1024 < max) {
    if ((addr =  malloc (middle))== (void *)-1) {
      perror("OSD::AvailableMemory()_malloc error :") ;
      return 0 ;
    }
    nballoc++ ;
    if (addr == 0)
      max = middle ;
    else {
      free(addr) ;
      nbfree++ ;
      min = middle ;
    }
    middle = min + ((max - min ) >> 6) * 63 ;
  }
  return min >> 10 ;
}

#endif