summaryrefslogtreecommitdiff
path: root/src/base/main.c
blob: 4163cac238e7080f9564997280a9c59b018e00d3 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <assert.h>
#include <sys/stat.h>

#ifdef __WIN32__
# include <windows.h>
# include <wininet.h>
#endif

#include "main.h"
#include "tux.h"

#ifndef PUBLIC_SERVER
#include "game.h"
#endif

#ifdef PUBLIC_SERVER
#include "publicServer.h"
#endif

static int my_argc;
static char **my_argv;

char *
getParam(char *s)
{
    int i;
    int len;

    len = strlen(s);

    for (i = 1; i < my_argc; i++) {
        //printf("%s %s\n", s, my_argv[i]);

        if (strlen(my_argv[i]) < len) {
            continue;
        }

        if (strncmp(s, my_argv[i], len) == 0) {
            return strchr(my_argv[i], '=') + 1;
        }
    }

    return NULL;
}

char *
getParamElse(char *s1, char *s2)
{
    char *ret;
    ret = getParam(s1);

    if (ret == NULL) {
        return s2;
    }

    return ret;
}

bool_t
isParamFlag(char *s)
{
    int i;

    for (i = 0; i < my_argc; i++) {
        if (strcmp(s, my_argv[i]) == 0) {
            return TRUE;
        }
    }

    return FALSE;
}

char *
getString(int n)
{
    char str[STR_NUM_SIZE];
    sprintf(str, "%d", n);
    return strdup(str);
}

int *
newInt(int x)
{
    int *new;
    new = malloc(sizeof(int));
    *new = x;
    return new;
}

void
accessExistFile(const char *s)
{
    if (access(s, F_OK) != 0) {
        fprintf(stderr, _("File %s not found !\nProgram shutdown !\n"), s);
        exit(-1);
    }
}

int
tryExistFile(const char *s)
{
    if (access(s, F_OK) != 0)
        return 1;
    else
        return 0;
}

int
isFillPath(const char *path)
{
    assert(path != NULL);

#ifndef __WIN32__
    if (path[0] == '/')         // for Unix-like systems ;)
#else
    if (path[1] == ':')         // for Windows-like systems ;)
#endif
    {
        return 1;
    }

    return 0;
}

#ifdef __WIN32__
int WINAPI
WinMain(HINSTANCE hInstance,
        HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
#else
int
main(int argc, char *argv[])
#endif
{
#ifndef __WIN32__
    signal(SIGPIPE, SIG_IGN);
#endif

    srand((unsigned) time(NULL));


#ifdef NLS
    setlocale(LC_ALL, "");
    bindtextdomain(PACKAGE, PATH_LOCALE);
    bind_textdomain_codeset(PACKAGE, "UTF-8");
    textdomain(PACKAGE);
#endif
#ifndef __WIN32__
    my_argc = argc;
    my_argv = argv;
#endif
/*
	test_space();
	exit(0);
*/

#ifdef __WIN32__
    WORD wVersionRequested = MAKEWORD(1, 1);    // WinSock version
    WSADATA data;               // WinSock information structure

    /* Let's initialize WinSock */
    if (WSAStartup(wVersionRequested, &data) != 0) {
        fprintf(stderr,
                _("WinSock initialization failed !\nProgram shutdown !\n"));
        exit(-1);
    }
#endif

#ifndef PUBLIC_SERVER
    startGame();
#endif

#ifdef PUBLIC_SERVER
    startPublicServer();
#endif

    return 0;
}