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