Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
macosx_math.cpp
Go to the documentation of this file.
1 #include <macosx_math.h>
2 #include <string>
3 #include "posh.h"
4 #if defined (__APPLE__)
5 //these stuffs are included in OSX 10.4 and above--so just check for x86
6 #include <stdio.h>
7  #include <string.h>
8  #include <assert.h>
9  #include <sys/param.h>
10  #include <stdlib.h>
11  #include <crt_externs.h>
12  #include <errno.h>
13  #include <mach-o/dyld.h>
14 
15 typedef int (*NSGetExecutablePathProcPtr)( char *buf, size_t *bufsize );
16 
17 static int NSGetExecutablePathOnTenOneAndEarlierOnly( char *execPath, size_t *execPathSize )
18 {
19  int err = 0;
20  char **cursor;
21  char *possiblyRelativePath;
22  char absolutePath[MAXPATHLEN];
23  size_t absolutePathSize;
24 
25  assert( execPath != NULL );
26  assert( execPathSize != NULL );
27 
28  cursor = (char**) ( *( _NSGetArgv() )+*( _NSGetArgc() ) );
29  //There should be a NULL after the argv array.
30  //If not, error out.
31  if (*cursor != 0)
32  err = -1;
33  if (err == 0) {
34  //Skip past the NULL after the argv array.
35 
36  cursor += 1;
37  //Now, skip over the entire kernel-supplied environment,
38  //which is an array of char * terminated by a NULL.
39  while (*cursor != 0)
40  cursor += 1;
41  //Skip over the NULL terminating the environment.
42 
43  cursor += 1;
44 
45  //Now we have the path that was passed to exec
46  //(not the argv[0] path, but the path that the kernel
47  //actually executed).
48 
49  possiblyRelativePath = *cursor;
50  //Convert the possibly relative path to an absolute
51  //path. We use realpath for expedience, although
52  //the real implementation of _NSGetExecutablePath
53  //uses getcwd and can return a path with symbolic links
54  //etc in it.
55  if (realpath( possiblyRelativePath, absolutePath ) == NULL)
56  err = -1;
57  }
58  //Now copy the path out into the client's buffer, returning
59  //an error if the buffer isn't big enough.
60  if (err == 0) {
61  absolutePathSize = (strlen( absolutePath )+1);
62  if (absolutePathSize <= *execPathSize)
63  strcpy( execPath, absolutePath );
64  else
65  err = -1;
66  *execPathSize = absolutePathSize;
67  }
68  return err;
69 }
70 
71 extern "C" {
72 int XNSGetExecutablePath( char *execPath, size_t *execPathSize )
73 {
74  if ( NSIsSymbolNameDefined( "__NSGetExecutablePath" ) )
75  return ( (NSGetExecutablePathProcPtr) NSAddressOfSymbol( NSLookupAndBindSymbol( "__NSGetExecutablePath" ) ) )( execPath,
76  execPathSize );
77  else
78  //The function _NSGetExecutablePath() is new in Jaguar, so use this custom version when running on 10.1.x and earlier.
79  return NSGetExecutablePathOnTenOneAndEarlierOnly( execPath, execPathSize );
80 }
81 }
82 
83 #endif
84 
85 int float_to_int( float a )
86 {
87  int maxint = 0x7ffffff;
88  int minint = -0x8000000;
89  if ( (a < maxint) && (a > minint) ) return int(a);
90  if (a > 0) return maxint;
91  if (a < 0) return minint;
92  return 0;
93 }
94 int double_to_int( double a )
95 {
96  int maxint = 0x7ffffff;
97  int minint = -0x8000000;
98  if ( (a < maxint) && (a > minint) ) return int(a);
99  if (a > 0) return maxint;
100  if (a < 0) return minint;
101  return 0;
102 }
103 #ifdef WITH_MACOSX_BUNDLE
104 #if (defined (__APPLE__) || defined (MACOSX ) ) && !defined (POSH_LITTLE_ENDIAN)
105 extern "C" {
106 char * ctermid_r( char *buf )
107 {
108  if (buf) {
109  buf[0] = '/';
110  buf[1] = 'd';
111  buf[2] = 'e';
112  buf[3] = 'v';
113  buf[4] = '/';
114  buf[5] = 't';
115  buf[6] = 't';
116  buf[7] = 'y';
117  buf[8] = '\0';
118  } else {
119  static char ret[] = "/dev/tty";
120  return ret;
121  }
122 }
123 
124 float acosf( float a )
125 {
126  return acos( a );
127 }
128 float asinf( float a )
129 {
130  return asin( a );
131 }
132 long lrintf( float a )
133 {
134  return (long) round( a );
135 }
136 long lrint( double a )
137 {
138  return (long) round( a );
139 }
140 long long llrintf( float a )
141 {
142  return (long) round( a );
143 }
144 long long llrint( double a )
145 {
146  return (long) round( a );
147 }
148 long long llrintl( long double a )
149 {
150  return (long) round( a );
151 }
152 
153 float sqrtf( float v )
154 {
155  return (float) sqrt( (double) v );
156 }
157 
158 float cosf( float v )
159 {
160  return (float) cos( (double) v );
161 }
162 
163 float sinf( float v )
164 {
165  return (float) sin( (double) v );
166 }
167 
168 float tanf( float v )
169 {
170  return (float) tan( (double) v );
171 }
172 
173 float powf( float v, float p )
174 {
175  return (float) pow( (double) v, (double) p );
176 }
177 long long atoll( const char *a )
178 {
179  long long retval = 0;
180  bool negatory = false;
181  if (a[0] == '-') negatory = true;
182  ++a;
183  while (*a) {
184  retval *= 10;
185  retval += *a-'0';
186  ++a;
187  }
188  return retval;
189 }
190 #define sockaddr void
191 #define socklen_t size_t
192 #define restrict
193 int getnameinfo( const sockaddr*restrict sa,
194  socklen_t salen,
195  char*restrict node,
196  socklen_t nodelen,
197  char*restrict service,
198  socklen_t servicelen,
199  int flags )
200 {
201  return 1;
202 }
203 }
204 #endif
205 #endif
206