1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
17 #include <Standard_Stream.hxx>
22 # define finite isfinite
25 // Convert Real to CString in format e with 16 significant digits.
26 // The decimal point character is always a period.
27 // The conversion is independant from current locale database
29 Standard_Boolean OSD::RealToCString(const Standard_Real aReal,
30 Standard_PCharacter& aString)
34 if (Sprintf(aString,"%.17e",aReal) <= 0) //BUC60808
35 return Standard_False ;
37 // Suppress "e+00" and unsignificant 0's
39 p = strchr(aString,'e');
41 if (!strcmp(p,"e+00"))
43 for (q = p-1 ; *q == '0' ; q--) ;
51 return Standard_True ;
54 // Make the RealToCString reciprocal conversion.
56 Standard_Boolean OSD::CStringToReal(const Standard_CString aString,
60 aReal = Strtod(aString, &endptr);
62 return Standard_False ;
66 //=======================================================================
67 //function : OSDSecSleep
68 //purpose : Cause the process to sleep during a amount of seconds
69 //=======================================================================
73 #if !defined(__CYGWIN32__) && !defined(__MINGW32__)
74 //# include <Mapiwin.h>
78 # define DMAXEXP ((1 << _DEXPLEN - 1) - 1 + _IEEE)
79 # define finite _finite
80 # define SLEEP(NSEC) Sleep(1000*(NSEC))
82 # define SLEEP(NSEC) sleep(NSEC)
88 void OSD::SecSleep(const Standard_Integer aDelay)
93 //=======================================================================
94 //function : MilliSecSleep
95 //purpose : Cause the process to sleep during a amount of milliseconds
96 //=======================================================================
100 void OSD::MilliSecSleep(const Standard_Integer aDelay)
107 #include <sys/time.h>
109 void OSD::MilliSecSleep(const Standard_Integer aDelay)
111 struct timeval timeout ;
113 timeout.tv_sec = aDelay / 1000 ;
114 timeout.tv_usec = (aDelay % 1000) * 1000 ;
116 select(0,NULL,NULL,NULL,&timeout) ;
121 //=======================================================================
122 //function : IsDivisible
124 //=======================================================================
126 Standard_Boolean OSD::IsDivisible(const Standard_Real theDividend,const Standard_Real theDivisor)
132 if ( theDivisor == 0. || ! finite(theDividend) ) return Standard_False;
134 // you may divide by infinity
136 if (! finite(theDivisor)) return Standard_True;
138 // Standard_Integer aExp1, aExp2;
139 // Standard_Real aMant1 = frexp(theDividend, &aExp1);
140 // Standard_Real aMant2 = frexp(theDivisor, &aExp2);
142 // Code de :KL:dev:ref :
143 // return ((aExp1-aExp2 < DMAXEXP) || // exponent of the result
144 // (aExp1-aExp2 == DMAXEXP && aMant1 < aMant2)) ;
146 // Code de :KAS:C30 :
147 return Standard_True;
150 // this is unacceptable return for Linux because of (temporary?) undefined aExp1 and aExp2.
151 // Testing both over and underflow should be (:KL:dev ) :
153 // return ((aExp1-aExp2 < DMAXEXP) && (aExp1-aExp2 > DMINEXP) ||
154 // (aExp1-aExp2 == DMAXEXP && aMant1 < aMant2) ||
155 // (aExp1-aExp2 == DMINEXP && aMant1 > aMant2)) ;
158 //=======================================================================
159 //function : GetExponent
161 //=======================================================================
163 //Standard_Integer OSD::GetExponent(const Standard_Real aReal)
164 Standard_Integer OSD::GetExponent(const Standard_Real )
166 // Code de :KAS:C30 :
168 cout << "Function OSD::GetExponent() not yet implemented." << endl;
171 // Code de :KL:dev:ref :
173 // Standard_Integer Exp ;
175 //// Standard_Real Mant = frexp(aReal, &Exp) ;
176 // frexp(aReal, &Exp) ;
180 //=======================================================================
181 //function : GetMantissa
183 //=======================================================================
185 //Standard_Real OSD::GetMantissa(const Standard_Real aReal)
186 Standard_Real OSD::GetMantissa(const Standard_Real )
188 // Code de :KAS:C30 :
189 cout << "Function OSD::GetMantissa() not yet implemented." << endl;
192 // Code de :KL:dev:ref :
194 // Standard_Integer Exp ;
195 // return frexp(aReal, &Exp) ;
198 //=======================================================================
199 // Code de :KAS:C30 :
200 //=======================================================================
201 Standard_Integer OSD::AvailableMemory()
203 cout << "Function OSD::AvailableMemory() not yet implemented." << endl;
207 // Code de :KL:dev:ref ??????????????? :
209 //=======================================================================
210 //function : AvailableMemory
212 //=======================================================================
217 # if defined(SUN) || defined(__sun) || defined(SOLARIS)
218 # define SIZE_MAX 0x7fffffff
219 # elif defined(__osf__) || defined(DECOSF1)
220 # define SIZE_MAX 0x10000000000
221 # elif defined(_WIN32)
222 # define SIZE_MAX 0x7ffdefff
224 # define SIZE_MAX 0xffffffff
227 Standard_Integer OSD::AvailableMemory()
230 size_t max = SIZE_MAX ;
231 size_t middle = SIZE_MAX ;
236 while (min + 1024 < max) {
237 if ((addr = malloc (middle))== (void *)-1) {
238 perror("OSD::AvailableMemory()_malloc error :") ;
249 middle = min + ((max - min ) >> 6) * 63 ;