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