00001
00002
00005 #include "stdafx.h"
00006 #include "openttd.h"
00007 #include "variables.h"
00008 #include "textbuf_gui.h"
00009 #include "functions.h"
00010 #include "core/random_func.hpp"
00011
00012 #include "table/strings.h"
00013
00014 #include <dirent.h>
00015 #include <unistd.h>
00016 #include <sys/stat.h>
00017 #include <time.h>
00018 #include <signal.h>
00019
00020 #if (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__)
00021 #define HAS_STATVFS
00022 #endif
00023
00024 #ifdef HAS_STATVFS
00025 #include <sys/statvfs.h>
00026 #endif
00027
00028
00029 #ifdef __MORPHOS__
00030 #include <exec/types.h>
00031 ULONG __stack = (1024*1024)*2;
00032
00033
00034 #undef SIG_IGN
00035 #define SIG_IGN (void (*)(int))1
00036 #endif
00037
00038 #ifdef __AMIGA__
00039 #warning add stack symbol to avoid that user needs to set stack manually (tokai)
00040
00041 #endif
00042
00043 #if defined(__APPLE__)
00044 #if defined(WITH_SDL)
00045
00046 #include <SDL.h>
00047 #endif
00048 #endif
00049
00050 bool FiosIsRoot(const char *path)
00051 {
00052 #if !defined(__MORPHOS__) && !defined(__AMIGAOS__)
00053 return path[1] == '\0';
00054 #else
00055
00056 const char *s = strchr(path, ':');
00057 return s != NULL && s[1] == '\0';
00058 #endif
00059 }
00060
00061 void FiosGetDrives()
00062 {
00063 return;
00064 }
00065
00066 bool FiosGetDiskFreeSpace(const char *path, uint32 *tot)
00067 {
00068 uint32 free = 0;
00069
00070 #ifdef HAS_STATVFS
00071 # ifdef __APPLE__
00072
00073 if (MacOSVersionIsAtLeast(10, 4, 0))
00074 # endif
00075 {
00076 struct statvfs s;
00077
00078 if (statvfs(path, &s) != 0) return false;
00079 free = (uint64)s.f_frsize * s.f_bavail >> 20;
00080 }
00081 #endif
00082 if (tot != NULL) *tot = free;
00083 return true;
00084 }
00085
00086 bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb)
00087 {
00088 char filename[MAX_PATH];
00089
00090 #if defined(__MORPHOS__) || defined(__AMIGAOS__)
00091
00092 if (FiosIsRoot(path)) {
00093 snprintf(filename, lengthof(filename), "%s:%s", path, ent->d_name);
00094 } else
00095 #else
00096 assert(path[strlen(path) - 1] == PATHSEPCHAR);
00097 if (strlen(path) > 2) assert(path[strlen(path) - 2] != PATHSEPCHAR);
00098 #endif
00099 snprintf(filename, lengthof(filename), "%s%s", path, ent->d_name);
00100
00101 return stat(filename, sb) == 0;
00102 }
00103
00104 bool FiosIsHiddenFile(const struct dirent *ent)
00105 {
00106 return ent->d_name[0] == '.';
00107 }
00108
00109 void ShowInfo(const char *str)
00110 {
00111 fprintf(stderr, "%s\n", str);
00112 }
00113
00114 void ShowOSErrorBox(const char *buf)
00115 {
00116 #if defined(__APPLE__)
00117
00118
00119 ShowMacDialog( buf, "See readme for more info\nMost likely you are missing files from the original TTD", "Quit" );
00120 #else
00121
00122 fprintf(stderr, "\033[1;31mError: %s\033[0;39m\n", buf);
00123 #endif
00124 }
00125
00126 #ifdef WITH_COCOA
00127 void cocoaSetupAutoreleasePool();
00128 void cocoaReleaseAutoreleasePool();
00129 #endif
00130
00131 int CDECL main(int argc, char* argv[])
00132 {
00133 int ret;
00134
00135 #ifdef WITH_COCOA
00136 cocoaSetupAutoreleasePool();
00137
00138 if (argc >= 2 && strncmp(argv[1], "-psn", 4) == 0) {
00139 argv[1] = NULL;
00140 argc = 1;
00141 }
00142 #endif
00143
00144 SetRandomSeed(time(NULL));
00145
00146 signal(SIGPIPE, SIG_IGN);
00147
00148 ret = ttd_main(argc, argv);
00149
00150 #ifdef WITH_COCOA
00151 cocoaReleaseAutoreleasePool();
00152 #endif
00153
00154 return ret;
00155 }
00156
00157 bool InsertTextBufferClipboard(Textbuf *tb)
00158 {
00159 return false;
00160 }
00161
00162
00163
00164
00165 #ifdef __AMIGA__
00166
00167 # include <devices/timer.h>
00168 # include <dos/dos.h>
00169
00170 extern struct Device *TimerBase = NULL;
00171 extern struct MsgPort *TimerPort = NULL;
00172 extern struct timerequest *TimerRequest = NULL;
00173 #endif // __AMIGA__
00174
00175 void CSleep(int milliseconds)
00176 {
00177 #if defined(PSP)
00178 sceKernelDelayThread(milliseconds * 1000);
00179 #elif defined(__BEOS__)
00180 snooze(milliseconds * 1000);
00181 #elif defined(__AMIGA__)
00182 {
00183 ULONG signals;
00184 ULONG TimerSigBit = 1 << TimerPort->mp_SigBit;
00185
00186
00187 TimerRequest->tr_node.io_Command = TR_ADDREQUEST;
00188 TimerRequest->tr_time.tv_secs = (milliseconds * 1000) / 1000000;
00189 TimerRequest->tr_time.tv_micro = (milliseconds * 1000) % 1000000;
00190 SendIO((struct IORequest *)TimerRequest);
00191
00192 if (!((signals = Wait(TimerSigBit | SIGBREAKF_CTRL_C)) & TimerSigBit) ) {
00193 AbortIO((struct IORequest *)TimerRequest);
00194 }
00195 WaitIO((struct IORequest *)TimerRequest);
00196 }
00197 #else
00198 usleep(milliseconds * 1000);
00199 #endif
00200 }
00201
00202 #ifdef WITH_ICONV
00203
00204 #include <iconv.h>
00205 #include <errno.h>
00206 #include "debug.h"
00207 #include "string_func.h"
00208
00209 #define INTERNALCODE "UTF-8"
00210
00214 static const char *GetLocalCode()
00215 {
00216 #if defined(__APPLE__)
00217 return "UTF-8-MAC";
00218 #else
00219
00220 const char *locale = GetCurrentLocale("LC_CTYPE");
00221 if (locale != NULL) locale = strchr(locale, '.');
00222
00223 return (locale == NULL) ? "" : locale + 1;
00224 #endif
00225 }
00226
00232 static const char *convert_tofrom_fs(iconv_t convd, const char *name)
00233 {
00234 static char buf[1024];
00235
00236
00237
00238 #ifdef HAVE_BROKEN_ICONV
00239 char *inbuf = (char*)name;
00240 #else
00241 const char *inbuf = name;
00242 #endif
00243
00244 char *outbuf = buf;
00245 size_t outlen = sizeof(buf) - 1;
00246 size_t inlen = strlen(name);
00247
00248 ttd_strlcpy(outbuf, name, sizeof(buf));
00249
00250 iconv(convd, NULL, NULL, NULL, NULL);
00251 if (iconv(convd, &inbuf, &inlen, &outbuf, &outlen) == (size_t)(-1)) {
00252 DEBUG(misc, 0, "[iconv] error converting '%s'. Errno %d", name, errno);
00253 }
00254
00255 *outbuf = '\0';
00256
00257 return buf;
00258 }
00259
00263 const char *OTTD2FS(const char *name)
00264 {
00265 static iconv_t convd = (iconv_t)(-1);
00266
00267 if (convd == (iconv_t)(-1)) {
00268 const char *env = GetLocalCode();
00269 convd = iconv_open(env, INTERNALCODE);
00270 if (convd == (iconv_t)(-1)) {
00271 DEBUG(misc, 0, "[iconv] conversion from codeset '%s' to '%s' unsupported", INTERNALCODE, env);
00272 return name;
00273 }
00274 }
00275
00276 return convert_tofrom_fs(convd, name);
00277 }
00278
00282 const char *FS2OTTD(const char *name)
00283 {
00284 static iconv_t convd = (iconv_t)(-1);
00285
00286 if (convd == (iconv_t)(-1)) {
00287 const char *env = GetLocalCode();
00288 convd = iconv_open(INTERNALCODE, env);
00289 if (convd == (iconv_t)(-1)) {
00290 DEBUG(misc, 0, "[iconv] conversion from codeset '%s' to '%s' unsupported", env, INTERNALCODE);
00291 return name;
00292 }
00293 }
00294
00295 return convert_tofrom_fs(convd, name);
00296 }
00297
00298 #else
00299 const char *FS2OTTD(const char *name) {return name;}
00300 const char *OTTD2FS(const char *name) {return name;}
00301 #endif