forked from blackav/ejudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallowed_list.c
120 lines (105 loc) · 2.35 KB
/
allowed_list.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
/* -*- mode: c -*- */
/* $Id$ */
/* Copyright (C) 2007-2014 Alexander Chernov <cher@ejudge.ru> */
/*
* 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.
*/
#include "ejudge/misctext.h"
#include "ejudge/xalloc.h"
#include <stdlib.h>
#include <ctype.h>
void
allowed_list_parse(
const unsigned char *str,
int separator,
unsigned char ***pv,
size_t *pu)
{
const unsigned char *s, *q;
unsigned char *p;
int i;
size_t sz;
unsigned char **v = 0;
size_t u = 0;
if (separator <= 0) separator = ',';
*pv = 0;
*pu = 0;
if (!str) return;
for (s = str; *s; s++)
if (*s == separator)
u++;
u++;
XCALLOC(v, u);
s = str;
for (i = 0; i < u && *s;) {
while (*s && isspace(*s)) s++;
if (*s == separator) {
s++;
continue;
}
if (!*s) break;
q = strchr(s, separator);
if (!q) q = s + strlen(s);
v[i] = p = xmemdup(s, q - s);
sz = strlen(p);
while (sz > 0 && isspace(p[sz - 1])) p[--sz] = 0;
if (!sz) {
xfree(p);
v[i] = 0;
} else {
i++;
}
if (*s) s = q + 1;
}
u = i;
if (!u) {
xfree(v); v = 0;
}
*pv = v;
*pu = u;
}
unsigned char **
allowed_list_free(unsigned char **pv, size_t u)
{
size_t i;
if (!pv || !u) return 0;
for (i = 0; i < u; i++)
xfree(pv[i]);
xfree(pv);
return 0;
}
void
allowed_list_map(
const unsigned char *user_langs,
int separator,
unsigned char **pv,
size_t pu,
int **pmap)
{
int *map = 0;
unsigned char **langs = 0;
size_t langs_u = 0;
int i, j;
*pmap = 0;
if (!pv || !pu) return;
XCALLOC(map, pu);
*pmap = map;
allowed_list_parse(user_langs, separator, &langs, &langs_u);
if (!langs || !langs_u) return;
for (i = 0; i < pu; i++) {
for (j = 0; j < langs_u; j++)
if (!strcmp(pv[i], langs[j]))
break;
if (j < langs_u)
map[i] = 1;
}
allowed_list_free(langs, langs_u);
}