/
EX_misc.c
577 lines (470 loc) · 11.6 KB
/
EX_misc.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#include "quakedef.h"
#ifdef _WIN32
#include "winquake.h"
#include <windows.h>
#endif
#include "EX_misc.h"
#include "EX_FunNames.h"
#if defined (__linux__)
#define CLIPBOARDSIZE 1024
static char clipboard[CLIPBOARDSIZE] = "\0"; // for clipboard implementation
#endif
// make any intermediate directories for given filename path
void MakeIntermediatePath(char *_name)
{
char *slash;
char name[2000];
char buf[2000];
strcpy(name, _name);
for (slash = name; *slash; slash++)
if (*slash == '/' || *slash == '\\')
*slash = PATH_SEPARATOR;
slash = strchr(name, PATH_SEPARATOR);
while (slash != NULL)
{
strncpy(buf, name, slash-name);
buf[slash-name] = 0;
Sys_mkdir(buf);
slash = strchr(slash+1, PATH_SEPARATOR);
}
}
// copies given text to clipboard
void CopyToClipboard(const char *text)
{
#ifdef _WIN32
if (OpenClipboard(NULL))
{
HANDLE i;
LPTSTR lptstrCopy;
HGLOBAL hglbCopy;
EmptyClipboard();
hglbCopy = GlobalAlloc(GMEM_DDESHARE, strlen(text)+1);
lptstrCopy = GlobalLock(hglbCopy);
strcpy((char *)lptstrCopy, text);
GlobalUnlock(hglbCopy);
i = SetClipboardData(CF_TEXT, hglbCopy);
CloseClipboard();
}
#else
strncpy(clipboard, text, CLIPBOARDSIZE);
clipboard[CLIPBOARDSIZE-1] = 0;
#endif
}
// reads from clipboard
char *ReadFromClipboard(void)
{
#ifdef _WIN32
static char clipbuf[1024];
int i;
HANDLE th;
char *clipText;
clipbuf[0] = 0;
if (OpenClipboard(NULL))
{
th = GetClipboardData(CF_TEXT);
if (th)
{
clipText = GlobalLock(th);
if (clipText)
{
strncpy(clipbuf, clipText, 1023);
clipbuf[1023] = 0;
for (i=0; i < strlen(clipbuf); i++)
if (clipbuf[i]=='\n' || clipbuf[i]=='\t' || clipbuf[i]=='\b')
clipbuf[i] = ' ';
}
GlobalUnlock(th);
}
CloseClipboard();
}
return clipbuf;
#else
return clipboard;
#endif
}
// compares two fun strings
int funcmp(const char *s1, const char *s2)
{
char *t1, *t2;
int ret;
if (s1 == NULL && s2 == NULL)
return 0;
if (s1 == NULL)
return -1;
if (s2 == NULL)
return 1;
t1 = Q_strdup(s1);
t2 = Q_strdup(s2);
FunToSort(t1);
FunToSort(t2);
ret = strcmp(t1, t2);
Q_free(t1);
Q_free(t2);
return ret;
}
//-------------------------------------
//
// case insensitive strstr (bool return)
// assumes sub is lowercase
//
int stristr(const char *s, const char *sub)
{
int i;
char *tmp;
tmp = Q_strdup(s);
for (i=0; i < strlen(tmp); i++)
tmp[i] = tolower(tmp[i]);
i = (int)strstr(tmp, sub);
Q_free(tmp);
return i;
}
//-------------------------------------
//
// find view entity
// = playernum+1 if player, or tracked person if spectator
// minus if free track
//
int GetViewEntity(void)
{
extern cvar_t cl_chasecam;
if (cl.spectator)
{
if (WhoIsSpectated() < 0 || cl_chasecam.value == 0)
return -1;
else
return WhoIsSpectated() + 1;
}
else
return cl.playernum + 1;
}
//-------------------------------------
//
// separate chat message
// finds, who said that, where is the message and type of it
// returns client number (or MAX_CLIENTS for 'console' user)
//
void Tmp_MakeRed(char *s)
{
while (*s)
*s++ |= 128;
}
int SeparateChat(char *chat, int *out_type, char **out_msg)
{
int server_cut = 31; // maximum characters sent in nick by server
int i;
int classified = -1;
int type;
char *msg;
for (i=0; i <= MAX_CLIENTS; i++)
{
int client = -1;
char buf[512];
if (i == MAX_CLIENTS)
{
strcpy(buf, "console: ");
//Tmp_MakeRed(buf);
if (!strncmp(chat, buf, strlen(buf)))
{
client = i;
type = CHAT_MM1;
msg = chat + strlen(buf);
}
}
else
{
if (!cl.players[i].name[0])
continue;
sprintf(buf, "%.*s: ", server_cut, Info_ValueForKey(cl.players[i].userinfo, "name"));
//Tmp_MakeRed(buf);
if (!strncmp(chat, buf, strlen(buf)))
{
client = i;
type = CHAT_MM1;
msg = chat + strlen(buf);
}
sprintf(buf, "(%.*s): ", server_cut, Info_ValueForKey(cl.players[i].userinfo, "name"));
//Tmp_MakeRed(buf);
if (!strncmp(chat, buf, strlen(buf)))
{
client = i;
type = CHAT_MM2;
msg = chat + strlen(buf);
}
sprintf(buf, "[SPEC] %.*s: ", server_cut, Info_ValueForKey(cl.players[i].userinfo, "name"));
//Tmp_MakeRed(buf);
if (!strncmp(chat, buf, strlen(buf)))
{
client = i;
type = CHAT_SPEC;
msg = chat + strlen(buf);
}
}
if (client >= 0)
{
if (classified < 0)
classified = client;
else
return -1;
}
}
if (classified < 0)
return -1;
if (out_msg)
*out_msg = msg;
if (out_type)
*out_type = type;
return classified;
}
//-------------------------------------
//
// find user number by string, string can be:
// #num - user number
// num - user id
// XXX - exact client name
//
int GetUserNum(char *t)
{
int client = -1;
if (*t == '#')
client = atoi(t+1);
else
{
int uid, i;
uid = atoi(t);
for (i=0; i < MAX_CLIENTS; i++)
if (cl.players[i].name[0] && cl.players[i].userid == uid)
{
client = i;
break;
}
if (client < 0)
for (i=0; i < MAX_CLIENTS; i++)
if (cl.players[i].name[0] && !strcmp(cl.players[i].name, t))
{
client = i;
break;
}
}
if (client >= 0 && cl.players[client].name[0])
return client;
else
return -1;
}
// ----------------------------------------------
//
// myRemoveColors
// convert string to non-coloured form
// alse remove characters, which cannot be in a filename
//
void myRemoveColors(char *str)
{
//char dest[2000];
char *s = str;
if (str[0] == 0)
return;
while (*s)
{
*s &= 127; // remove red colour
if (*s < 16) *s = '_'; // bochomazy
else if (*s == 16) *s = '[';
else if (*s == 17) *s = ']';
else if (*s >= 18 && *s <= 27) *s += 30;
else if (*s >= 28 && *s <= 31) *s = '_';
else if (*s == 127) *s = '_';
// remove disallowed characters
if (*s=='\\' || *s=='/' || *s==':' || *s=='*' || *s=='?' ||
*s=='\"' || *s=='<' || *s=='>' || *s=='|')
*s = '_';
s++;
}
// remove spaces from the beginning
s = str;
while (*s == '_' || *s == ' ')
s++;
memmove(str, s, strlen(s) + 1);
// remove spaces from the end
s = str + strlen(str);
while (s > str && (*(s-1) == '_' || *(s-1) == ' '))
s--;
*s = 0;
if (str[0] == 0)
strcpy(str, "_");
// done
}
// overwrites, always generates same length or shorter string
void StripName(char *namebuf)
{
int i;
char *s;
for (i=0; i < strlen(namebuf); i++)
if (namebuf[i] == '_')
namebuf[i] = ' ';
// remove spaces from the beginning
s = namebuf;
while (*s == ' ')
s++;
memmove(namebuf, s, strlen(s) + 1);
// remove spaces from the end
s = namebuf + strlen(namebuf);
while (s > namebuf && *(s-1) == ' ')
s--;
*s = 0;
// remove few spaces in a row
s = namebuf;
while (s < namebuf + strlen(namebuf))
{
if (*s == ' ')
{
char *q = s+1;
while (*q == ' ')
q++;
if (q > s+1)
memmove(s+1, q, strlen(q)+1);
}
s++;
}
if (namebuf[0] == 0)
strcpy(namebuf, "_");
// replace all non-standard characters with '_'
for (i=0; i < strlen(namebuf); i++)
{
if (namebuf[i] < ' ' || namebuf[i] > '~')
namebuf[i] = '_';
}
}
// get float value of string, false on error
qbool getFloatValue(char *string, float *val)
{
// [sign] [digits] [.digits] [ {d | D | e | E }[sign]digits]
char *s;
qbool percent = false;
s = string;
//
// first validate
//
// skip white spaces
while (isspace2(*s))
s++;
if (*s == 0) // empty string
return false;
// sign
if (*s == '-' || *s == '+')
s++;
// digits
if ((*s >= 0 && *s <= '9') || *s == '.')
{
while (*s >= '0' && *s <= '9')
s++;
// .digits
if (*s == '.')
{
s++;
if (*s >= '0' && *s <= '9')
{
while (*s >= '0' && *s <= '9')
s++;
}
else
return false; // no digits after '.' ?
}
}
else
return false; // no value?
// [ {d | D | e | E }[sign]digits]
if (tolower(*s) == 'd' || tolower(*s) == 'e')
{
s++;
if (*s == '-' || *s == '+')
s++;
if (*s >= '0' && *s <= '9')
{
while (*s >= '0' && *s <= '9')
s++;
}
else
return false;
}
// check for percent sign
if (*s == '%')
{
percent = true;
s++;
}
// skip white spaces
while (isspace2(*s))
s++;
// we should be at the end now
if (*s != 0)
return false;
//
// all OK, calculate value
//
*val = atof(string);
if (percent)
*val /= 100.0;
return true;
}
// get integer value of string, false on error
qbool getIntegerValue(char *string, int *val)
{
// [sign]digits || "0x" hex-digits
char *s;
s = string;
//
// first validate
//
// skip white spaces
while (isspace2(*s))
s++;
if (*s == 0) // empty string
return false;
if (*s == '0' && tolower(*(s+1)) == 'x')
{
// hexadecimal
s += 2;
// hex digits
if ((*s >= '0' && *s <= '9') || (tolower(*s) >= 'a' && tolower(*s) <= 'f'))
{
while ((*s >= '0' && *s <= '9') || (tolower(*s) >= 'a' && tolower(*s) <= 'f'))
s++;
}
else
return false; // at least one required
// skip white spaces
while (isspace2(*s))
s++;
// we should be at the end now
if (*s != 0)
return false;
//
// calculate value
//
sscanf(string+2, "%x", val);
}
else
{
// decimal
// sign
if (*s == '-' || *s == '+')
s++;
// digits
if (*s >= '0' && *s <= '9')
{
while (*s >= '0' && *s <= '9')
s++;
}
else
return false; // at least one digit required
// skip white spaces
while (isspace2(*s))
s++;
// we should be at the end now
if (*s != 0)
return false;
//
// calculate value
//
*val = atoi(string);
}
return true;
}