0031642: Visualization - crash in Graphic3d_Structure::SetVisual() on redisplaying...
[occt.git] / src / OSD / OSD.cxx
1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
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
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.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <OSD.hxx>
16
17 // Convert Real to CString in format e with 16 significant digits.
18 // The decimal point character is always a period.
19 // The conversion is independant from current locale database
20
21 Standard_Boolean OSD::RealToCString(const Standard_Real aReal,
22                                     Standard_PCharacter& aString)
23 {
24   char *p, *q ;
25   
26   if (Sprintf(aString,"%.17e",aReal)  <= 0) //BUC60808
27     return Standard_False ;
28
29   // Suppress "e+00" and unsignificant 0's 
30
31   p = strchr(aString,'e');
32   if (p) {
33     if (!strcmp(p,"e+00"))
34       *p = 0 ;
35     for (q = p-1 ; *q == '0' ; q--) ;
36     if (q != p-1) {
37       if (*q != '.') q++ ;
38       while (*p)
39         *q++ = *p++ ;
40       *q = 0 ;
41     }
42   }
43   return Standard_True ;
44 }
45
46 // Make the RealToCString reciprocal conversion.
47
48 Standard_Boolean OSD::CStringToReal(const Standard_CString aString,
49                                     Standard_Real& aReal)
50 {
51   char *endptr ;
52   aReal = Strtod(aString, &endptr);
53   if (*endptr)
54     return Standard_False ;
55   return Standard_True;
56 }
57
58 //=======================================================================
59 //function : OSDSecSleep
60 //purpose  : Cause the process to sleep during a amount of seconds 
61 //=======================================================================
62
63 #ifdef _WIN32
64 # include <windows.h>
65 # define SLEEP(NSEC)                 Sleep(1000*(NSEC))
66 #else
67 #include <unistd.h>
68 # define SLEEP(NSEC)                 sleep(NSEC)
69 #endif
70
71 void OSD::SecSleep(const Standard_Integer aDelay)
72 {
73   SLEEP(aDelay);
74 }
75
76 //=======================================================================
77 //function : MilliSecSleep
78 //purpose  : Cause the process to sleep during a amount of milliseconds  
79 //=======================================================================
80
81 #ifdef _WIN32
82
83 void OSD::MilliSecSleep(const Standard_Integer aDelay)
84 {
85   Sleep(aDelay) ;
86 }
87
88 #else
89
90 #include <sys/time.h>
91
92 void OSD::MilliSecSleep(const Standard_Integer aDelay)
93 {
94   struct timeval timeout ;
95
96   timeout.tv_sec = aDelay / 1000 ;
97   timeout.tv_usec = (aDelay % 1000) * 1000 ;
98
99   select(0,NULL,NULL,NULL,&timeout) ;
100 }
101
102 #endif