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