forked from QW-Group/ezquake-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cl_slist.c
160 lines (124 loc) · 3.65 KB
/
cl_slist.c
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
/*
Copyright (C) 1999,2000 contributors of the QuakeForge project
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
$Id: cl_slist.c,v 1.10 2007-03-11 06:01:36 disconn3ct Exp $
*/
#include "quakedef.h"
#include "cl_slist.h"
#include "utils.h"
static qbool slist_initialised = false;
server_entry_t slist[MAX_SERVER_LIST];
void SList_Init(void) {
memset(&slist, 0, sizeof(slist));
}
void SList_Shutdown(void) {
char filename[MAX_OSPATH] = {0};
FILE *f;
if (!slist_initialised)
return;
snprintf(&filename[0], sizeof(filename), "%s/servers.txt", com_basedir);
if (!(f = fopen(filename, "w"))) {
Com_DPrintf("Couldn't open %s\n", filename);
return;
}
SList_Save(f);
fclose(f);
}
void SList_Set (int i, char *addr, char *desc) {
if (i >= MAX_SERVER_LIST || i < 0)
Sys_Error("SList_Switch: Bad index %d", i);
if (slist[i].server)
Q_free(slist[i].server);
if (slist[i].description)
Q_free(slist[i].description);
slist[i].server = Q_strdup (addr);
slist[i].description = Q_strdup (desc);
}
void SList_Reset_NoFree (int i) {
if (i >= MAX_SERVER_LIST || i < 0)
Sys_Error("SList_Switch: Bad index %d", i);
slist[i].description = slist[i].server = NULL;
}
void SList_Reset (int i) {
if (i >= MAX_SERVER_LIST || i < 0)
Sys_Error("SList_Switch: Bad index %d", i);
if (slist[i].server) {
Q_free(slist[i].server);
slist[i].server = NULL;
}
if (slist[i].description) {
Q_free(slist[i].description);
slist[i].description = NULL;
}
}
void SList_Switch (int a, int b) {
server_entry_t temp;
if (a >= MAX_SERVER_LIST || a < 0)
Sys_Error("SList_Switch: Bad index %d", a);
if (b >= MAX_SERVER_LIST || b < 0)
Sys_Error("SList_Switch: Bad index %d", b);
memcpy(&temp, &slist[a], sizeof(temp));
memcpy(&slist[a], &slist[b], sizeof(temp));
memcpy(&slist[b], &temp, sizeof(temp));
}
int SList_Length (void) {
int count;
for (count = 0; count < MAX_SERVER_LIST && slist[count].server; count++)
;
return count;
}
void SList_Load (void) {
char line[128];
char filename[MAX_OSPATH] = {0};
char *desc, *addr;
unsigned int len;
int c, argc, count;
FILE *f;
snprintf(&filename[0], sizeof(filename), "%s/servers.txt", com_basedir);
if (!(f = fopen(filename, "r")))
return;
count = len = 0;
while ((c = getc(f))) {
if (c == '\n' || c == '\r' || c == EOF) {
if (c == '\r' && (c = getc(f)) != '\n' && c != EOF)
ungetc(c, f);
line[len] = 0;
len = 0;
Cmd_TokenizeString(line);
if ((argc = Cmd_Argc()) >= 1) {
addr = Cmd_Argv(0);
desc = (argc >= 2) ? Cmd_MakeArgs(1) : "Unknown";
SList_Set (count, addr, desc);
if (++count == MAX_SERVER_LIST)
break;
}
if (c == EOF)
break; //just in case an EOF follows a '\r'
} else {
if (len + 1 < sizeof(line))
line[len++] = c;
}
}
fclose (f);
slist_initialised = true;
}
void SList_Save (FILE *f) {
int i;
char *spaces;
for (i = 0; i < MAX_SERVER_LIST; i++) {
if (!slist[i].server)
break;
spaces = CreateSpaces(32 - strlen(slist[i].server));
fprintf(f, "%s%s%s\n", slist[i].server, spaces, slist[i].description);
}
}