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 | |
7fd59977 |
19 | #include <math.h> |
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 | |
29 | Standard_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 | |
56 | Standard_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 | |
88 | void 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 | |
100 | void OSD::MilliSecSleep(const Standard_Integer aDelay) |
101 | { |
102 | Sleep(aDelay) ; |
103 | } |
104 | |
105 | #else |
106 | |
03155c18 |
107 | #include <sys/time.h> |
7fd59977 |
108 | |
109 | void 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 | |
126 | Standard_Boolean OSD::IsDivisible(const Standard_Real theDividend,const Standard_Real theDivisor) |
127 | { |
128 | if ( theDivisor == 0. || ! finite(theDividend) ) return Standard_False; |
129 | // |
130 | // you may divide by infinity |
131 | // |
132 | if (! finite(theDivisor)) return Standard_True; |
0797d9d3 |
133 | #ifdef OCCT_DEBUG |
7fd59977 |
134 | // Standard_Integer aExp1, aExp2; |
135 | // Standard_Real aMant1 = frexp(theDividend, &aExp1); |
136 | // Standard_Real aMant2 = frexp(theDivisor, &aExp2); |
137 | #endif |
138 | // Code de :KL:dev:ref : |
139 | // return ((aExp1-aExp2 < DMAXEXP) || // exponent of the result |
140 | // (aExp1-aExp2 == DMAXEXP && aMant1 < aMant2)) ; |
141 | |
142 | // Code de :KAS:C30 : |
143 | return Standard_True; |
144 | |
145 | |
146 | // this is unacceptable return for Linux because of (temporary?) undefined aExp1 and aExp2. |
147 | // Testing both over and underflow should be (:KL:dev ) : |
148 | |
149 | // return ((aExp1-aExp2 < DMAXEXP) && (aExp1-aExp2 > DMINEXP) || |
150 | // (aExp1-aExp2 == DMAXEXP && aMant1 < aMant2) || |
151 | // (aExp1-aExp2 == DMINEXP && aMant1 > aMant2)) ; |
152 | } |
153 | |
154 | //======================================================================= |
155 | //function : GetExponent |
156 | //purpose : |
157 | //======================================================================= |
158 | |
159 | //Standard_Integer OSD::GetExponent(const Standard_Real aReal) |
160 | Standard_Integer OSD::GetExponent(const Standard_Real ) |
161 | { |
162 | // Code de :KAS:C30 : |
163 | |
164 | cout << "Function OSD::GetExponent() not yet implemented." << endl; |
165 | return 0 ; |
166 | |
167 | // Code de :KL:dev:ref : |
168 | |
169 | // Standard_Integer Exp ; |
170 | |
171 | //// Standard_Real Mant = frexp(aReal, &Exp) ; |
172 | // frexp(aReal, &Exp) ; |
173 | // return Exp ; |
174 | } |
175 | |
176 | //======================================================================= |
177 | //function : GetMantissa |
178 | //purpose : |
179 | //======================================================================= |
180 | |
181 | //Standard_Real OSD::GetMantissa(const Standard_Real aReal) |
182 | Standard_Real OSD::GetMantissa(const Standard_Real ) |
183 | { |
184 | // Code de :KAS:C30 : |
185 | cout << "Function OSD::GetMantissa() not yet implemented." << endl; |
186 | return 0 ; |
187 | |
188 | // Code de :KL:dev:ref : |
189 | |
190 | // Standard_Integer Exp ; |
191 | // return frexp(aReal, &Exp) ; |
192 | } |
193 | |
194 | //======================================================================= |
195 | // Code de :KAS:C30 : |
196 | //======================================================================= |
197 | Standard_Integer OSD::AvailableMemory() |
198 | { |
199 | cout << "Function OSD::AvailableMemory() not yet implemented." << endl; |
200 | return 0 ; |
201 | } |
202 | |
203 | // Code de :KL:dev:ref ??????????????? : |
204 | #if 0 |
205 | //======================================================================= |
206 | //function : AvailableMemory |
207 | //purpose : |
208 | //======================================================================= |
209 | #include <stdlib.h> |
210 | #include <stdio.h> |
211 | #include <malloc.h> |
212 | |
213 | # if defined(SUN) || defined(__sun) || defined(SOLARIS) |
214 | # define SIZE_MAX 0x7fffffff |
215 | # elif defined(__osf__) || defined(DECOSF1) |
216 | # define SIZE_MAX 0x10000000000 |
57c28b61 |
217 | # elif defined(_WIN32) |
7fd59977 |
218 | # define SIZE_MAX 0x7ffdefff |
219 | # else |
220 | # define SIZE_MAX 0xffffffff |
221 | # endif |
222 | |
223 | Standard_Integer OSD::AvailableMemory() |
224 | { |
225 | size_t min = 1024 ; |
226 | size_t max = SIZE_MAX ; |
227 | size_t middle = SIZE_MAX ; |
228 | void * addr ; |
229 | int nballoc = 0 ; |
230 | int nbfree = 0 ; |
231 | |
232 | while (min + 1024 < max) { |
233 | if ((addr = malloc (middle))== (void *)-1) { |
234 | perror("OSD::AvailableMemory()_malloc error :") ; |
235 | return 0 ; |
236 | } |
237 | nballoc++ ; |
238 | if (addr == 0) |
239 | max = middle ; |
240 | else { |
241 | free(addr) ; |
242 | nbfree++ ; |
243 | min = middle ; |
244 | } |
245 | middle = min + ((max - min ) >> 6) * 63 ; |
246 | } |
247 | return min >> 10 ; |
248 | } |
249 | |
250 | #endif |