0024048: "Basic Runtime Checks" option of VS projects should be equal to "RTC1"
[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
7fd59977 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
34
35Standard_Boolean OSD::RealToCString(const Standard_Real aReal,
36 Standard_PCharacter& aString)
37{
38 char *p, *q ;
39
91322f44 40 if (Sprintf(aString,"%.17e",aReal) <= 0) //BUC60808
7fd59977 41 return Standard_False ;
42
7fd59977 43 // Suppress "e+00" and unsignificant 0's
44
45 if ((p = strchr(aString,'e'))) {
46 if (!strcmp(p,"e+00"))
47 *p = 0 ;
48 for (q = p-1 ; *q == '0' ; q--) ;
49 if (q != p-1) {
50 if (*q != '.') q++ ;
51 while (*p)
52 *q++ = *p++ ;
53 *q = 0 ;
54 }
55 }
56 return Standard_True ;
57}
58
59// Make the RealToCString reciprocal conversion.
60
61Standard_Boolean OSD::CStringToReal(const Standard_CString aString,
62 Standard_Real& aReal)
63{
7fd59977 64 char *endptr ;
91322f44 65 aReal = Strtod(aString, &endptr);
7fd59977 66 if (*endptr)
67 return Standard_False ;
68 return Standard_True;
69}
70
71//=======================================================================
72//function : OSDSecSleep
73//purpose : Cause the process to sleep during a amount of seconds
74//=======================================================================
75
76#ifdef WNT
77# include <Windows.h>
78#if !defined(__CYGWIN32__) && !defined(__MINGW32__)
fc3a37cd 79//# include <Mapiwin.h>
7fd59977 80#endif
81# define _DEXPLEN 11
82# define _IEEE 1
83# define DMAXEXP ((1 << _DEXPLEN - 1) - 1 + _IEEE)
84# define finite _finite
85# define SLEEP(NSEC) Sleep(1000*(NSEC))
86#else
87# define SLEEP(NSEC) sleep(NSEC)
88#endif
89
90#ifdef HAVE_VALUES_H
91//# include <values.h>
92#endif
93
94#ifdef HAVE_UNISTD_H
95# include <unistd.h>
96#endif
97
98void OSD::SecSleep(const Standard_Integer aDelay)
99{
100 SLEEP(aDelay);
101}
102
103//=======================================================================
104//function : MilliSecSleep
105//purpose : Cause the process to sleep during a amount of milliseconds
106//=======================================================================
107
108#ifdef WNT
109
110void OSD::MilliSecSleep(const Standard_Integer aDelay)
111{
112 Sleep(aDelay) ;
113}
114
115#else
116
117#ifdef HAVE_SYS_TIME_H
118# include <sys/time.h>
119#endif
120
121void OSD::MilliSecSleep(const Standard_Integer aDelay)
122{
123 int fdn ;
124 struct timeval timeout ;
125
126 timeout.tv_sec = aDelay / 1000 ;
127 timeout.tv_usec = (aDelay % 1000) * 1000 ;
128
129 fdn = select(0,NULL,NULL,NULL,&timeout) ;
130}
131
132#endif
133
134//=======================================================================
135//function : IsDivisible
136//purpose :
137//=======================================================================
138
139Standard_Boolean OSD::IsDivisible(const Standard_Real theDividend,const Standard_Real theDivisor)
140{
141 if ( theDivisor == 0. || ! finite(theDividend) ) return Standard_False;
142 //
143 // you may divide by infinity
144 //
145 if (! finite(theDivisor)) return Standard_True;
146#ifdef DEB
147// Standard_Integer aExp1, aExp2;
148// Standard_Real aMant1 = frexp(theDividend, &aExp1);
149// Standard_Real aMant2 = frexp(theDivisor, &aExp2);
150#endif
151// Code de :KL:dev:ref :
152// return ((aExp1-aExp2 < DMAXEXP) || // exponent of the result
153// (aExp1-aExp2 == DMAXEXP && aMant1 < aMant2)) ;
154
155// Code de :KAS:C30 :
156 return Standard_True;
157
158
159// this is unacceptable return for Linux because of (temporary?) undefined aExp1 and aExp2.
160// Testing both over and underflow should be (:KL:dev ) :
161
162// return ((aExp1-aExp2 < DMAXEXP) && (aExp1-aExp2 > DMINEXP) ||
163// (aExp1-aExp2 == DMAXEXP && aMant1 < aMant2) ||
164// (aExp1-aExp2 == DMINEXP && aMant1 > aMant2)) ;
165}
166
167//=======================================================================
168//function : GetExponent
169//purpose :
170//=======================================================================
171
172//Standard_Integer OSD::GetExponent(const Standard_Real aReal)
173Standard_Integer OSD::GetExponent(const Standard_Real )
174{
175// Code de :KAS:C30 :
176
177 cout << "Function OSD::GetExponent() not yet implemented." << endl;
178 return 0 ;
179
180// Code de :KL:dev:ref :
181
182// Standard_Integer Exp ;
183
184//// Standard_Real Mant = frexp(aReal, &Exp) ;
185// frexp(aReal, &Exp) ;
186// return Exp ;
187}
188
189//=======================================================================
190//function : GetMantissa
191//purpose :
192//=======================================================================
193
194//Standard_Real OSD::GetMantissa(const Standard_Real aReal)
195Standard_Real OSD::GetMantissa(const Standard_Real )
196{
197// Code de :KAS:C30 :
198 cout << "Function OSD::GetMantissa() not yet implemented." << endl;
199 return 0 ;
200
201// Code de :KL:dev:ref :
202
203// Standard_Integer Exp ;
204// return frexp(aReal, &Exp) ;
205}
206
207//=======================================================================
208// Code de :KAS:C30 :
209//=======================================================================
210Standard_Integer OSD::AvailableMemory()
211{
212 cout << "Function OSD::AvailableMemory() not yet implemented." << endl;
213 return 0 ;
214}
215
216// Code de :KL:dev:ref ??????????????? :
217#if 0
218//=======================================================================
219//function : AvailableMemory
220//purpose :
221//=======================================================================
222#include <stdlib.h>
223#include <stdio.h>
224#include <malloc.h>
225
226# if defined(SUN) || defined(__sun) || defined(SOLARIS)
227# define SIZE_MAX 0x7fffffff
228# elif defined(__osf__) || defined(DECOSF1)
229# define SIZE_MAX 0x10000000000
230# elif defined(WNT)
231# define SIZE_MAX 0x7ffdefff
232# else
233# define SIZE_MAX 0xffffffff
234# endif
235
236Standard_Integer OSD::AvailableMemory()
237{
238 size_t min = 1024 ;
239 size_t max = SIZE_MAX ;
240 size_t middle = SIZE_MAX ;
241 void * addr ;
242 int nballoc = 0 ;
243 int nbfree = 0 ;
244
245 while (min + 1024 < max) {
246 if ((addr = malloc (middle))== (void *)-1) {
247 perror("OSD::AvailableMemory()_malloc error :") ;
248 return 0 ;
249 }
250 nballoc++ ;
251 if (addr == 0)
252 max = middle ;
253 else {
254 free(addr) ;
255 nbfree++ ;
256 min = middle ;
257 }
258 middle = min + ((max - min ) >> 6) * 63 ;
259 }
260 return min >> 10 ;
261}
262
263#endif