0023418: Crash on the object displaying when running DRAW on remote station. OpenGL...
[occt.git] / src / OSD / OSD.cxx
CommitLineData
b311480e 1// Copyright (c) 1998-1999 Matra Datavision
2// Copyright (c) 1999-2012 OPEN CASCADE SAS
3//
4// The content of this file is subject to the Open CASCADE Technology Public
5// License Version 6.5 (the "License"). You may not use the content of this file
6// except in compliance with the License. Please obtain a copy of the License
7// at http://www.opencascade.org and read it completely before using this file.
8//
9// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11//
12// The Original Code and all software distributed under the License is
13// distributed on an "AS IS" basis, without warranty of any kind, and the
14// Initial Developer hereby disclaims all such warranties, including without
15// limitation, any warranties of merchantability, fitness for a particular
16// purpose or non-infringement. Please see the License for the specific terms
17// and conditions governing the rights and limitations under the License.
18
7fd59977 19
20#include <OSD.ixx>
21
22#include <stdio.h>
23#include <math.h>
24#if HAVE_IEEEFP_H
25# include <ieeefp.h>
26#endif
27#if !defined(HAVE_FINITE) && defined(isfinite)
28# define finite isfinite
29#endif
30
31static Standard_Integer DecimalPoint = 0 ;
32
33static void GetDecimalPoint() {
34 float F1 = (float ) 1.1 ;
35 char str[5] ;
36
37 sprintf(str,"%.1f",F1) ;
38 // printf("%s\n",str) ;
39 DecimalPoint = str[1] ;
40}
41
42// Convert Real to CString in format e with 16 significant digits.
43// The decimal point character is always a period.
44// The conversion is independant from current locale database
45
46Standard_Boolean OSD::RealToCString(const Standard_Real aReal,
47 Standard_PCharacter& aString)
48{
49 char *p, *q ;
50
51 // Get the local decimal point character
52
53 if (!DecimalPoint)
54 GetDecimalPoint() ;
55
56 // Substitute it
57
58// if (sprintf(aString,"%.15le",aReal) <= 0)
59 if (sprintf(aString,"%.17e",aReal) <= 0) //BUC60808
60 return Standard_False ;
61
62 if ((p = strchr(aString,DecimalPoint)))
63 *p = '.' ;
64
65 // Suppress "e+00" and unsignificant 0's
66
67 if ((p = strchr(aString,'e'))) {
68 if (!strcmp(p,"e+00"))
69 *p = 0 ;
70 for (q = p-1 ; *q == '0' ; q--) ;
71 if (q != p-1) {
72 if (*q != '.') q++ ;
73 while (*p)
74 *q++ = *p++ ;
75 *q = 0 ;
76 }
77 }
78 return Standard_True ;
79}
80
81// Make the RealToCString reciprocal conversion.
82
83Standard_Boolean OSD::CStringToReal(const Standard_CString aString,
84 Standard_Real& aReal)
85{
86 const char *p;
87 char *endptr ;
88
89
90 // Get the local decimal point character
91
92 if (!DecimalPoint)
93 GetDecimalPoint() ;
94
95 const char *str = aString;
96 char buff[1024];
97 //if((p = strchr(aString,'.')))
98 if(DecimalPoint != '.' && (p = strchr(aString,'.'))&& ((p-aString) < 1000) )
99 {
100 strncpy(buff, aString, 1000);
101 buff[p-aString] = DecimalPoint ;
102 str = buff;
103 }
104 aReal = strtod(str,&endptr) ;
105 if (*endptr)
106 return Standard_False ;
107 return Standard_True;
108}
109
110//=======================================================================
111//function : OSDSecSleep
112//purpose : Cause the process to sleep during a amount of seconds
113//=======================================================================
114
115#ifdef WNT
116# include <Windows.h>
117#if !defined(__CYGWIN32__) && !defined(__MINGW32__)
118# include <Mapiwin.h>
119#endif
120# define _DEXPLEN 11
121# define _IEEE 1
122# define DMAXEXP ((1 << _DEXPLEN - 1) - 1 + _IEEE)
123# define finite _finite
124# define SLEEP(NSEC) Sleep(1000*(NSEC))
125#else
126# define SLEEP(NSEC) sleep(NSEC)
127#endif
128
129#ifdef HAVE_VALUES_H
130//# include <values.h>
131#endif
132
133#ifdef HAVE_UNISTD_H
134# include <unistd.h>
135#endif
136
137void OSD::SecSleep(const Standard_Integer aDelay)
138{
139 SLEEP(aDelay);
140}
141
142//=======================================================================
143//function : MilliSecSleep
144//purpose : Cause the process to sleep during a amount of milliseconds
145//=======================================================================
146
147#ifdef WNT
148
149void OSD::MilliSecSleep(const Standard_Integer aDelay)
150{
151 Sleep(aDelay) ;
152}
153
154#else
155
156#ifdef HAVE_SYS_TIME_H
157# include <sys/time.h>
158#endif
159
160void OSD::MilliSecSleep(const Standard_Integer aDelay)
161{
162 int fdn ;
163 struct timeval timeout ;
164
165 timeout.tv_sec = aDelay / 1000 ;
166 timeout.tv_usec = (aDelay % 1000) * 1000 ;
167
168 fdn = select(0,NULL,NULL,NULL,&timeout) ;
169}
170
171#endif
172
173//=======================================================================
174//function : IsDivisible
175//purpose :
176//=======================================================================
177
178Standard_Boolean OSD::IsDivisible(const Standard_Real theDividend,const Standard_Real theDivisor)
179{
180 if ( theDivisor == 0. || ! finite(theDividend) ) return Standard_False;
181 //
182 // you may divide by infinity
183 //
184 if (! finite(theDivisor)) return Standard_True;
185#ifdef DEB
186// Standard_Integer aExp1, aExp2;
187// Standard_Real aMant1 = frexp(theDividend, &aExp1);
188// Standard_Real aMant2 = frexp(theDivisor, &aExp2);
189#endif
190// Code de :KL:dev:ref :
191// return ((aExp1-aExp2 < DMAXEXP) || // exponent of the result
192// (aExp1-aExp2 == DMAXEXP && aMant1 < aMant2)) ;
193
194// Code de :KAS:C30 :
195 return Standard_True;
196
197
198// this is unacceptable return for Linux because of (temporary?) undefined aExp1 and aExp2.
199// Testing both over and underflow should be (:KL:dev ) :
200
201// return ((aExp1-aExp2 < DMAXEXP) && (aExp1-aExp2 > DMINEXP) ||
202// (aExp1-aExp2 == DMAXEXP && aMant1 < aMant2) ||
203// (aExp1-aExp2 == DMINEXP && aMant1 > aMant2)) ;
204}
205
206//=======================================================================
207//function : GetExponent
208//purpose :
209//=======================================================================
210
211//Standard_Integer OSD::GetExponent(const Standard_Real aReal)
212Standard_Integer OSD::GetExponent(const Standard_Real )
213{
214// Code de :KAS:C30 :
215
216 cout << "Function OSD::GetExponent() not yet implemented." << endl;
217 return 0 ;
218
219// Code de :KL:dev:ref :
220
221// Standard_Integer Exp ;
222
223//// Standard_Real Mant = frexp(aReal, &Exp) ;
224// frexp(aReal, &Exp) ;
225// return Exp ;
226}
227
228//=======================================================================
229//function : GetMantissa
230//purpose :
231//=======================================================================
232
233//Standard_Real OSD::GetMantissa(const Standard_Real aReal)
234Standard_Real OSD::GetMantissa(const Standard_Real )
235{
236// Code de :KAS:C30 :
237 cout << "Function OSD::GetMantissa() not yet implemented." << endl;
238 return 0 ;
239
240// Code de :KL:dev:ref :
241
242// Standard_Integer Exp ;
243// return frexp(aReal, &Exp) ;
244}
245
246//=======================================================================
247// Code de :KAS:C30 :
248//=======================================================================
249Standard_Integer OSD::AvailableMemory()
250{
251 cout << "Function OSD::AvailableMemory() not yet implemented." << endl;
252 return 0 ;
253}
254
255// Code de :KL:dev:ref ??????????????? :
256#if 0
257//=======================================================================
258//function : AvailableMemory
259//purpose :
260//=======================================================================
261#include <stdlib.h>
262#include <stdio.h>
263#include <malloc.h>
264
265# if defined(SUN) || defined(__sun) || defined(SOLARIS)
266# define SIZE_MAX 0x7fffffff
267# elif defined(__osf__) || defined(DECOSF1)
268# define SIZE_MAX 0x10000000000
269# elif defined(WNT)
270# define SIZE_MAX 0x7ffdefff
271# else
272# define SIZE_MAX 0xffffffff
273# endif
274
275Standard_Integer OSD::AvailableMemory()
276{
277 size_t min = 1024 ;
278 size_t max = SIZE_MAX ;
279 size_t middle = SIZE_MAX ;
280 void * addr ;
281 int nballoc = 0 ;
282 int nbfree = 0 ;
283
284 while (min + 1024 < max) {
285 if ((addr = malloc (middle))== (void *)-1) {
286 perror("OSD::AvailableMemory()_malloc error :") ;
287 return 0 ;
288 }
289 nballoc++ ;
290 if (addr == 0)
291 max = middle ;
292 else {
293 free(addr) ;
294 nbfree++ ;
295 min = middle ;
296 }
297 middle = min + ((max - min ) >> 6) * 63 ;
298 }
299 return min >> 10 ;
300}
301
302#endif