forked from aburch/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimsys.cc
110 lines (93 loc) · 2.21 KB
/
simsys.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "macros.h"
#include "simmain.h"
#include "simsys.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <direct.h>
# include <windows.h>
# define PATH_MAX MAX_PATH
#else
# include <limits.h>
# if !defined __AMIGA__ && !defined __BEOS__
# include <unistd.h>
# endif
#endif
struct sys_event sys_event;
void dr_mkdir(char const* const path)
{
#ifdef _WIN32
mkdir(path);
#else
mkdir(path, 0777);
#endif
}
char const* dr_query_homedir()
{
static char buffer[PATH_MAX];
#if defined _WIN32
DWORD len = PATH_MAX - 24;
HKEY hHomeDir;
if (RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", 0, KEY_READ, &hHomeDir) != ERROR_SUCCESS)
return 0;
RegQueryValueExA(hHomeDir, "Personal", 0, 0, (BYTE*)buffer, &len);
strcat(buffer,"\\Simutrans");
#elif defined __APPLE__
sprintf(buffer, "%s/Library/Simutrans", getenv("HOME"));
#else
sprintf(buffer, "%s/simutrans", getenv("HOME"));
#endif
dr_mkdir(buffer);
// create other subdirectories
#ifdef _WIN32
strcat(buffer, "\\");
#else
strcat(buffer, "/");
#endif
char b2[PATH_MAX];
sprintf(b2, "%smaps", buffer);
dr_mkdir(b2);
sprintf(b2, "%ssave", buffer);
dr_mkdir(b2);
sprintf(b2, "%sscreenshot", buffer);
dr_mkdir(b2);
return buffer;
}
void dr_fatal_notify(char const* const msg)
{
#ifdef _WIN32
MessageBoxA(0, msg, "Fatal Error", MB_ICONEXCLAMATION);
#else
fputs(msg, stderr);
#endif
}
int sysmain(int const argc, char** const argv)
{
#ifdef _WIN32
char pathname[1024];
GetModuleFileNameA(GetModuleHandle(0), pathname, lengthof(pathname));
argv[0] = pathname;
#elif !defined __BEOS__
# if defined __GLIBC__ && !defined __AMIGA__
/* glibc has a non-standard extension */
char* buffer2 = 0;
# else
char buffer2[PATH_MAX];
# endif
# ifndef __AMIGA__
char buffer[PATH_MAX];
int length = readlink("/proc/self/exe", buffer, lengthof(buffer) - 1);
if (length != -1) {
buffer[length] = '\0'; /* readlink() does not NUL-terminate */
argv[0] = buffer;
}
# endif
// no process file system => need to parse argv[0]
/* should work on most unix or gnu systems */
argv[0] = realpath(argv[0], buffer2);
#endif
return simu_main(argc, argv);
}