0025181: Incrementation of OCCT version up to 6.8.0.dev
[occt.git] / src / OSD / OSD.cxx
CommitLineData
b311480e 1// Copyright (c) 1998-1999 Matra Datavision
973c2be1 2// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
b311480e 5//
d5f74e42 6// This library is free software; you can redistribute it and/or modify it under
7// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 8// by the Free Software Foundation, with special exception defined in the file
9// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10// distribution for complete text of the license and disclaimer of any warranty.
b311480e 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
7fd59977 14
15#include <OSD.ixx>
89c4bca8 16#include <Standard_Stream.hxx>
7fd59977 17#include <stdio.h>
18#include <math.h>
19#if HAVE_IEEEFP_H
20# include <ieeefp.h>
21#endif
22#if !defined(HAVE_FINITE) && defined(isfinite)
23# define finite isfinite
24#endif
25
7fd59977 26// Convert Real to CString in format e with 16 significant digits.
27// The decimal point character is always a period.
28// The conversion is independant from current locale database
29
30Standard_Boolean OSD::RealToCString(const Standard_Real aReal,
31 Standard_PCharacter& aString)
32{
33 char *p, *q ;
34
91322f44 35 if (Sprintf(aString,"%.17e",aReal) <= 0) //BUC60808
7fd59977 36 return Standard_False ;
37
7fd59977 38 // Suppress "e+00" and unsignificant 0's
39
302f96fb 40 p = strchr(aString,'e');
41 if (p) {
7fd59977 42 if (!strcmp(p,"e+00"))
43 *p = 0 ;
44 for (q = p-1 ; *q == '0' ; q--) ;
45 if (q != p-1) {
46 if (*q != '.') q++ ;
47 while (*p)
48 *q++ = *p++ ;
49 *q = 0 ;
50 }
51 }
52 return Standard_True ;
53}
54
55// Make the RealToCString reciprocal conversion.
56
57Standard_Boolean OSD::CStringToReal(const Standard_CString aString,
58 Standard_Real& aReal)
59{
7fd59977 60 char *endptr ;
91322f44 61 aReal = Strtod(aString, &endptr);
7fd59977 62 if (*endptr)
63 return Standard_False ;
64 return Standard_True;
65}
66
67//=======================================================================
68//function : OSDSecSleep
69//purpose : Cause the process to sleep during a amount of seconds
70//=======================================================================
71
72#ifdef WNT
73# include <Windows.h>
74#if !defined(__CYGWIN32__) && !defined(__MINGW32__)
fc3a37cd 75//# include <Mapiwin.h>
7fd59977 76#endif
77# define _DEXPLEN 11
78# define _IEEE 1
79# define DMAXEXP ((1 << _DEXPLEN - 1) - 1 + _IEEE)
80# define finite _finite
81# define SLEEP(NSEC) Sleep(1000*(NSEC))
82#else
83# define SLEEP(NSEC) sleep(NSEC)
84#endif
85
86#ifdef HAVE_VALUES_H
87//# include <values.h>
88#endif
89
90#ifdef HAVE_UNISTD_H
91# include <unistd.h>
92#endif
93
94void OSD::SecSleep(const Standard_Integer aDelay)
95{
96 SLEEP(aDelay);
97}
98
99//=======================================================================
100//function : MilliSecSleep
101//purpose : Cause the process to sleep during a amount of milliseconds
102//=======================================================================
103
104#ifdef WNT
105
106void OSD::MilliSecSleep(const Standard_Integer aDelay)
107{
108 Sleep(aDelay) ;
109}
110
111#else
112
113#ifdef HAVE_SYS_TIME_H
114# include <sys/time.h>
115#endif
116
117void OSD::MilliSecSleep(const Standard_Integer aDelay)
118{
7fd59977 119 struct timeval timeout ;
120
121 timeout.tv_sec = aDelay / 1000 ;
122 timeout.tv_usec = (aDelay % 1000) * 1000 ;
123
96a95605 124 select(0,NULL,NULL,NULL,&timeout) ;
7fd59977 125}
126
127#endif
128
129//=======================================================================
130//function : IsDivisible
131//purpose :
132//=======================================================================
133
134Standard_Boolean OSD::IsDivisible(const Standard_Real theDividend,const Standard_Real theDivisor)
135{
136 if ( theDivisor == 0. || ! finite(theDividend) ) return Standard_False;
137 //
138 // you may divide by infinity
139 //
140 if (! finite(theDivisor)) return Standard_True;
141#ifdef DEB
142// Standard_Integer aExp1, aExp2;
143// Standard_Real aMant1 = frexp(theDividend, &aExp1);
144// Standard_Real aMant2 = frexp(theDivisor, &aExp2);
145#endif
146// Code de :KL:dev:ref :
147// return ((aExp1-aExp2 < DMAXEXP) || // exponent of the result
148// (aExp1-aExp2 == DMAXEXP && aMant1 < aMant2)) ;
149
150// Code de :KAS:C30 :
151 return Standard_True;
152
153
154// this is unacceptable return for Linux because of (temporary?) undefined aExp1 and aExp2.
155// Testing both over and underflow should be (:KL:dev ) :
156
157// return ((aExp1-aExp2 < DMAXEXP) && (aExp1-aExp2 > DMINEXP) ||
158// (aExp1-aExp2 == DMAXEXP && aMant1 < aMant2) ||
159// (aExp1-aExp2 == DMINEXP && aMant1 > aMant2)) ;
160}
161
162//=======================================================================
163//function : GetExponent
164//purpose :
165//=======================================================================
166
167//Standard_Integer OSD::GetExponent(const Standard_Real aReal)
168Standard_Integer OSD::GetExponent(const Standard_Real )
169{
170// Code de :KAS:C30 :
171
172 cout << "Function OSD::GetExponent() not yet implemented." << endl;
173 return 0 ;
174
175// Code de :KL:dev:ref :
176
177// Standard_Integer Exp ;
178
179//// Standard_Real Mant = frexp(aReal, &Exp) ;
180// frexp(aReal, &Exp) ;
181// return Exp ;
182}
183
184//=======================================================================
185//function : GetMantissa
186//purpose :
187//=======================================================================
188
189//Standard_Real OSD::GetMantissa(const Standard_Real aReal)
190Standard_Real OSD::GetMantissa(const Standard_Real )
191{
192// Code de :KAS:C30 :
193 cout << "Function OSD::GetMantissa() not yet implemented." << endl;
194 return 0 ;
195
196// Code de :KL:dev:ref :
197
198// Standard_Integer Exp ;
199// return frexp(aReal, &Exp) ;
200}
201
202//=======================================================================
203// Code de :KAS:C30 :
204//=======================================================================
205Standard_Integer OSD::AvailableMemory()
206{
207 cout << "Function OSD::AvailableMemory() not yet implemented." << endl;
208 return 0 ;
209}
210
211// Code de :KL:dev:ref ??????????????? :
212#if 0
213//=======================================================================
214//function : AvailableMemory
215//purpose :
216//=======================================================================
217#include <stdlib.h>
218#include <stdio.h>
219#include <malloc.h>
220
221# if defined(SUN) || defined(__sun) || defined(SOLARIS)
222# define SIZE_MAX 0x7fffffff
223# elif defined(__osf__) || defined(DECOSF1)
224# define SIZE_MAX 0x10000000000
225# elif defined(WNT)
226# define SIZE_MAX 0x7ffdefff
227# else
228# define SIZE_MAX 0xffffffff
229# endif
230
231Standard_Integer OSD::AvailableMemory()
232{
233 size_t min = 1024 ;
234 size_t max = SIZE_MAX ;
235 size_t middle = SIZE_MAX ;
236 void * addr ;
237 int nballoc = 0 ;
238 int nbfree = 0 ;
239
240 while (min + 1024 < max) {
241 if ((addr = malloc (middle))== (void *)-1) {
242 perror("OSD::AvailableMemory()_malloc error :") ;
243 return 0 ;
244 }
245 nballoc++ ;
246 if (addr == 0)
247 max = middle ;
248 else {
249 free(addr) ;
250 nbfree++ ;
251 min = middle ;
252 }
253 middle = min + ((max - min ) >> 6) * 63 ;
254 }
255 return min >> 10 ;
256}
257
258#endif