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