Initial revision
[TestXSLT.git] / libsablot / src / engine / platform.cpp
1 /*
2  * The contents of this file are subject to the Mozilla Public
3  * License Version 1.1 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of
5  * the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS
8  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9  * implied. See the License for the specific language governing
10  * rights and limitations under the License.
11  *
12  * The Original Code is the Sablotron XSLT Processor.
13  *
14  * The Initial Developer of the Original Code is Ginger Alliance Ltd.
15  * Portions created by Ginger Alliance are Copyright (C) 2000-2002
16  * Ginger Alliance Ltd. All Rights Reserved.
17  *
18  * Contributor(s):
19  *
20  * Alternatively, the contents of this file may be used under the
21  * terms of the GNU General Public License Version 2 or later (the
22  * "GPL"), in which case the provisions of the GPL are applicable
23  * instead of those above.  If you wish to allow use of your
24  * version of this file only under the terms of the GPL and not to
25  * allow others to use your version of this file under the MPL,
26  * indicate your decision by deleting the provisions above and
27  * replace them with the notice and other provisions required by
28  * the GPL.  If you do not delete the provisions above, a recipient
29  * may use your version of this file under either the MPL or the
30  * GPL.
31  */
32
33 // platform.cpp
34
35 // GP: clean
36
37 #include <float.h>
38 #include <math.h>
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 // includes for time measurement
45 #include <time.h>      // needed by <sys/timeb.h> for definition of time_t
46
47 #if defined(HAVE_SYS_TIMEB_H) || defined(WIN32)
48 #include <sys/timeb.h>
49 #endif
50
51 #ifdef HAVE_SYS_TIME_H
52 #include <sys/time.h>
53 #endif
54
55 #ifdef HAVE_IEEEFP_H
56 #include <ieeefp.h>
57 #endif
58
59 /* needed for isinf on certain platforms */
60 #ifdef HAVE_SUNMATH_H
61 #include <sunmath.h>
62 #endif
63
64 /* needed for wcscmp__ */
65 #ifdef HAVE_WCHAR_H
66 #include <wchar.h>
67 #endif
68
69 /*
70   include direct.h for getcwd()
71 */
72 #ifdef HAVE_UNISTD_H
73 #include <unistd.h>
74 #elif defined(WIN32)
75 #include <direct.h>
76 #elif defined(__BORLANDC__)
77 #include <dir.h>
78 #else
79 #error "Can't find appropriate include (unistd.h & Co.)"
80 #endif
81
82 #include "datastr.h"
83
84 int isnan__(double x)
85 {
86 #ifdef HAVE_ISNAN
87     return isnan(x);
88 #elif defined(WIN32)
89     return _isnan(x);
90 #else
91 #    error "Don't know how to detect NaN on this platform"
92 #endif
93 }
94
95 int isinf__(double x)
96 {
97 #ifdef HAVE_ISINF
98     return isinf(x);
99 #elif defined (HAVE_FINITE)
100     return ! finite(x) && x == x;
101 #elif defined (WIN32)
102     return ! _finite(x) && x == x;
103 #else
104 #    error "Don't know how to detect Infinity on this platform"
105 #endif
106 }
107
108 // time: get the number of milliseconds
109 double getMillisecs()
110 {
111     double ret;
112 #if defined (WIN32)
113     struct _timeb theTime;
114     _ftime(&theTime);
115     ret = theTime.time + theTime.millitm/1000.0;
116 #elif defined (HAVE_FTIME)
117     struct timeb theTime;
118     ftime(&theTime);
119     ret = theTime.time + theTime.millitm/1000.0;
120 #elif defined (HAVE_GETTIMEOFDAY)
121     timeval theTime;
122     gettimeofday(&theTime, NULL);
123     ret = theTime.tv_sec + theTime.tv_usec/1000000.0;
124 #else
125 #error "Can't find function ftime() or similar on this platform"
126 #endif
127     return ret;
128 }
129
130 //wcs compare
131 int wcscmp__(const char* a, const char* b)
132 {
133 #if defined (SAB_WCS_COLLATE)
134   return wcscmp((const wchar_t*) a, (const wchar_t*) b);
135 #else
136   return strcmp(a, b);
137 #endif
138 }
139
140 // get current working directory
141 void my_getcwd(DStr &dir)
142 {
143 char buf[256];
144 #if defined(WIN32)
145     _getcwd(buf, 256);
146 #else //if defined(__linux__) || defined(__BORLANDC__) || defined(__unix)
147     getcwd(buf,256);
148 #endif
149     dir = buf;
150     if (!(dir == (const char*) "/"))
151         dir += '/';
152 }
153
154 //set line buffered output
155 void setlinebuf__(FILE *file)
156 {
157 #if defined(HAVE_SETVBUF) || defined(_MSC_VER)
158   setvbuf(file, NULL, _IOLBF, 0);
159 #endif
160 }
161