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
7 // under the terms of the GNU Lesser General Public 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.
16 #include <Standard_Stream.hxx>
22 #if !defined(HAVE_FINITE) && defined(isfinite)
23 # define finite isfinite
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
30 Standard_Boolean OSD::RealToCString(const Standard_Real aReal,
31 Standard_PCharacter& aString)
35 if (Sprintf(aString,"%.17e",aReal) <= 0) //BUC60808
36 return Standard_False ;
38 // Suppress "e+00" and unsignificant 0's
40 p = strchr(aString,'e');
42 if (!strcmp(p,"e+00"))
44 for (q = p-1 ; *q == '0' ; q--) ;
52 return Standard_True ;
55 // Make the RealToCString reciprocal conversion.
57 Standard_Boolean OSD::CStringToReal(const Standard_CString aString,
61 aReal = Strtod(aString, &endptr);
63 return Standard_False ;
67 //=======================================================================
68 //function : OSDSecSleep
69 //purpose : Cause the process to sleep during a amount of seconds
70 //=======================================================================
74 #if !defined(__CYGWIN32__) && !defined(__MINGW32__)
75 //# include <Mapiwin.h>
79 # define DMAXEXP ((1 << _DEXPLEN - 1) - 1 + _IEEE)
80 # define finite _finite
81 # define SLEEP(NSEC) Sleep(1000*(NSEC))
83 # define SLEEP(NSEC) sleep(NSEC)
87 //# include <values.h>
94 void OSD::SecSleep(const Standard_Integer aDelay)
99 //=======================================================================
100 //function : MilliSecSleep
101 //purpose : Cause the process to sleep during a amount of milliseconds
102 //=======================================================================
106 void OSD::MilliSecSleep(const Standard_Integer aDelay)
113 #ifdef HAVE_SYS_TIME_H
114 # include <sys/time.h>
117 void OSD::MilliSecSleep(const Standard_Integer aDelay)
119 struct timeval timeout ;
121 timeout.tv_sec = aDelay / 1000 ;
122 timeout.tv_usec = (aDelay % 1000) * 1000 ;
124 select(0,NULL,NULL,NULL,&timeout) ;
129 //=======================================================================
130 //function : IsDivisible
132 //=======================================================================
134 Standard_Boolean OSD::IsDivisible(const Standard_Real theDividend,const Standard_Real theDivisor)
136 if ( theDivisor == 0. || ! finite(theDividend) ) return Standard_False;
138 // you may divide by infinity
140 if (! finite(theDivisor)) return Standard_True;
142 // Standard_Integer aExp1, aExp2;
143 // Standard_Real aMant1 = frexp(theDividend, &aExp1);
144 // Standard_Real aMant2 = frexp(theDivisor, &aExp2);
146 // Code de :KL:dev:ref :
147 // return ((aExp1-aExp2 < DMAXEXP) || // exponent of the result
148 // (aExp1-aExp2 == DMAXEXP && aMant1 < aMant2)) ;
150 // Code de :KAS:C30 :
151 return Standard_True;
154 // this is unacceptable return for Linux because of (temporary?) undefined aExp1 and aExp2.
155 // Testing both over and underflow should be (:KL:dev ) :
157 // return ((aExp1-aExp2 < DMAXEXP) && (aExp1-aExp2 > DMINEXP) ||
158 // (aExp1-aExp2 == DMAXEXP && aMant1 < aMant2) ||
159 // (aExp1-aExp2 == DMINEXP && aMant1 > aMant2)) ;
162 //=======================================================================
163 //function : GetExponent
165 //=======================================================================
167 //Standard_Integer OSD::GetExponent(const Standard_Real aReal)
168 Standard_Integer OSD::GetExponent(const Standard_Real )
170 // Code de :KAS:C30 :
172 cout << "Function OSD::GetExponent() not yet implemented." << endl;
175 // Code de :KL:dev:ref :
177 // Standard_Integer Exp ;
179 //// Standard_Real Mant = frexp(aReal, &Exp) ;
180 // frexp(aReal, &Exp) ;
184 //=======================================================================
185 //function : GetMantissa
187 //=======================================================================
189 //Standard_Real OSD::GetMantissa(const Standard_Real aReal)
190 Standard_Real OSD::GetMantissa(const Standard_Real )
192 // Code de :KAS:C30 :
193 cout << "Function OSD::GetMantissa() not yet implemented." << endl;
196 // Code de :KL:dev:ref :
198 // Standard_Integer Exp ;
199 // return frexp(aReal, &Exp) ;
202 //=======================================================================
203 // Code de :KAS:C30 :
204 //=======================================================================
205 Standard_Integer OSD::AvailableMemory()
207 cout << "Function OSD::AvailableMemory() not yet implemented." << endl;
211 // Code de :KL:dev:ref ??????????????? :
213 //=======================================================================
214 //function : AvailableMemory
216 //=======================================================================
221 # if defined(SUN) || defined(__sun) || defined(SOLARIS)
222 # define SIZE_MAX 0x7fffffff
223 # elif defined(__osf__) || defined(DECOSF1)
224 # define SIZE_MAX 0x10000000000
226 # define SIZE_MAX 0x7ffdefff
228 # define SIZE_MAX 0xffffffff
231 Standard_Integer OSD::AvailableMemory()
234 size_t max = SIZE_MAX ;
235 size_t middle = SIZE_MAX ;
240 while (min + 1024 < max) {
241 if ((addr = malloc (middle))== (void *)-1) {
242 perror("OSD::AvailableMemory()_malloc error :") ;
253 middle = min + ((max - min ) >> 6) * 63 ;