1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
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.
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.
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.
21 #include <Standard_Stream.hxx>
27 #if !defined(HAVE_FINITE) && defined(isfinite)
28 # define finite isfinite
31 // Convert Real to CString in format e with 16 significant digits.
32 // The decimal point character is always a period.
33 // The conversion is independant from current locale database
35 Standard_Boolean OSD::RealToCString(const Standard_Real aReal,
36 Standard_PCharacter& aString)
40 if (Sprintf(aString,"%.17e",aReal) <= 0) //BUC60808
41 return Standard_False ;
43 // Suppress "e+00" and unsignificant 0's
45 p = strchr(aString,'e');
47 if (!strcmp(p,"e+00"))
49 for (q = p-1 ; *q == '0' ; q--) ;
57 return Standard_True ;
60 // Make the RealToCString reciprocal conversion.
62 Standard_Boolean OSD::CStringToReal(const Standard_CString aString,
66 aReal = Strtod(aString, &endptr);
68 return Standard_False ;
72 //=======================================================================
73 //function : OSDSecSleep
74 //purpose : Cause the process to sleep during a amount of seconds
75 //=======================================================================
79 #if !defined(__CYGWIN32__) && !defined(__MINGW32__)
80 //# include <Mapiwin.h>
84 # define DMAXEXP ((1 << _DEXPLEN - 1) - 1 + _IEEE)
85 # define finite _finite
86 # define SLEEP(NSEC) Sleep(1000*(NSEC))
88 # define SLEEP(NSEC) sleep(NSEC)
92 //# include <values.h>
99 void OSD::SecSleep(const Standard_Integer aDelay)
104 //=======================================================================
105 //function : MilliSecSleep
106 //purpose : Cause the process to sleep during a amount of milliseconds
107 //=======================================================================
111 void OSD::MilliSecSleep(const Standard_Integer aDelay)
118 #ifdef HAVE_SYS_TIME_H
119 # include <sys/time.h>
122 void OSD::MilliSecSleep(const Standard_Integer aDelay)
125 struct timeval timeout ;
127 timeout.tv_sec = aDelay / 1000 ;
128 timeout.tv_usec = (aDelay % 1000) * 1000 ;
130 fdn = select(0,NULL,NULL,NULL,&timeout) ;
135 //=======================================================================
136 //function : IsDivisible
138 //=======================================================================
140 Standard_Boolean OSD::IsDivisible(const Standard_Real theDividend,const Standard_Real theDivisor)
142 if ( theDivisor == 0. || ! finite(theDividend) ) return Standard_False;
144 // you may divide by infinity
146 if (! finite(theDivisor)) return Standard_True;
148 // Standard_Integer aExp1, aExp2;
149 // Standard_Real aMant1 = frexp(theDividend, &aExp1);
150 // Standard_Real aMant2 = frexp(theDivisor, &aExp2);
152 // Code de :KL:dev:ref :
153 // return ((aExp1-aExp2 < DMAXEXP) || // exponent of the result
154 // (aExp1-aExp2 == DMAXEXP && aMant1 < aMant2)) ;
156 // Code de :KAS:C30 :
157 return Standard_True;
160 // this is unacceptable return for Linux because of (temporary?) undefined aExp1 and aExp2.
161 // Testing both over and underflow should be (:KL:dev ) :
163 // return ((aExp1-aExp2 < DMAXEXP) && (aExp1-aExp2 > DMINEXP) ||
164 // (aExp1-aExp2 == DMAXEXP && aMant1 < aMant2) ||
165 // (aExp1-aExp2 == DMINEXP && aMant1 > aMant2)) ;
168 //=======================================================================
169 //function : GetExponent
171 //=======================================================================
173 //Standard_Integer OSD::GetExponent(const Standard_Real aReal)
174 Standard_Integer OSD::GetExponent(const Standard_Real )
176 // Code de :KAS:C30 :
178 cout << "Function OSD::GetExponent() not yet implemented." << endl;
181 // Code de :KL:dev:ref :
183 // Standard_Integer Exp ;
185 //// Standard_Real Mant = frexp(aReal, &Exp) ;
186 // frexp(aReal, &Exp) ;
190 //=======================================================================
191 //function : GetMantissa
193 //=======================================================================
195 //Standard_Real OSD::GetMantissa(const Standard_Real aReal)
196 Standard_Real OSD::GetMantissa(const Standard_Real )
198 // Code de :KAS:C30 :
199 cout << "Function OSD::GetMantissa() not yet implemented." << endl;
202 // Code de :KL:dev:ref :
204 // Standard_Integer Exp ;
205 // return frexp(aReal, &Exp) ;
208 //=======================================================================
209 // Code de :KAS:C30 :
210 //=======================================================================
211 Standard_Integer OSD::AvailableMemory()
213 cout << "Function OSD::AvailableMemory() not yet implemented." << endl;
217 // Code de :KL:dev:ref ??????????????? :
219 //=======================================================================
220 //function : AvailableMemory
222 //=======================================================================
227 # if defined(SUN) || defined(__sun) || defined(SOLARIS)
228 # define SIZE_MAX 0x7fffffff
229 # elif defined(__osf__) || defined(DECOSF1)
230 # define SIZE_MAX 0x10000000000
232 # define SIZE_MAX 0x7ffdefff
234 # define SIZE_MAX 0xffffffff
237 Standard_Integer OSD::AvailableMemory()
240 size_t max = SIZE_MAX ;
241 size_t middle = SIZE_MAX ;
246 while (min + 1024 < max) {
247 if ((addr = malloc (middle))== (void *)-1) {
248 perror("OSD::AvailableMemory()_malloc error :") ;
259 middle = min + ((max - min ) >> 6) * 63 ;