File Conn_engine_core.c changed (mode: 100644) (index 56045de..6a1675c) |
... |
... |
int Conn_addr_family(const char *addr) |
956 |
956 |
|
|
957 |
957 |
return ret; |
return ret; |
958 |
958 |
} |
} |
|
959 |
|
|
|
960 |
|
/* Splitting stuff */ |
|
961 |
|
|
|
962 |
|
/* |
|
963 |
|
* Cut in place the end \r and \n. |
|
964 |
|
*/ |
|
965 |
|
static void cut(char *s) |
|
966 |
|
{ |
|
967 |
|
int pos; |
|
968 |
|
|
|
969 |
|
pos = strlen(s) - 1; |
|
970 |
|
|
|
971 |
|
while ((s[pos] == '\r') || (s[pos] == '\n')) { |
|
972 |
|
s[pos] = '\0'; |
|
973 |
|
pos--; |
|
974 |
|
} |
|
975 |
|
} |
|
976 |
|
|
|
977 |
|
/* |
|
978 |
|
* Free a prealocated structure |
|
979 |
|
*/ |
|
980 |
|
void Conn_split_free(struct Conn_split **s) |
|
981 |
|
{ |
|
982 |
|
struct Conn_split *p, *next; |
|
983 |
|
|
|
984 |
|
if (!s) |
|
985 |
|
return; |
|
986 |
|
|
|
987 |
|
p = *s; |
|
988 |
|
if (!p) |
|
989 |
|
return; |
|
990 |
|
|
|
991 |
|
while (p) { |
|
992 |
|
free(p->r); |
|
993 |
|
free(p->l); |
|
994 |
|
next = p->next; |
|
995 |
|
free(p); |
|
996 |
|
p = next; |
|
997 |
|
} |
|
998 |
|
|
|
999 |
|
*s = NULL; |
|
1000 |
|
} |
|
1001 |
|
|
|
1002 |
|
/* |
|
1003 |
|
* Split a buffer pointed by C to var,value pairs. |
|
1004 |
|
*/ |
|
1005 |
|
struct Conn_split *Conn_split(char *line) |
|
1006 |
|
{ |
|
1007 |
|
char *p; |
|
1008 |
|
struct Conn_split *ret = NULL, *q, *last; |
|
1009 |
|
char l[128], r[4096]; |
|
1010 |
|
unsigned int i; |
|
1011 |
|
char search_for; |
|
1012 |
|
|
|
1013 |
|
cut(line); |
|
1014 |
|
|
|
1015 |
|
/* do the spliting */ |
|
1016 |
|
p = line; |
|
1017 |
|
while (*p != '\0') { |
|
1018 |
|
/* skip empty space */ |
|
1019 |
|
while ((*p == ' ') || (*p == '\t')) |
|
1020 |
|
p++; |
|
1021 |
|
|
|
1022 |
|
/* Building left */ |
|
1023 |
|
i = 0; |
|
1024 |
|
while ((i < sizeof(l) - 1) && (*p != '\0') && (*p != '=') |
|
1025 |
|
&& (*p != '\r')) { |
|
1026 |
|
l[i++] = *p; |
|
1027 |
|
p++; |
|
1028 |
|
} |
|
1029 |
|
l[i] = '\0'; |
|
1030 |
|
|
|
1031 |
|
/* Building right */ |
|
1032 |
|
r[0] = '\0'; |
|
1033 |
|
if (*p != '\0') { |
|
1034 |
|
/* skip '=' */ |
|
1035 |
|
p++; |
|
1036 |
|
|
|
1037 |
|
search_for = ' '; |
|
1038 |
|
if (*p == '"') { |
|
1039 |
|
search_for = '"'; |
|
1040 |
|
p++; |
|
1041 |
|
} |
|
1042 |
|
|
|
1043 |
|
i = 0; |
|
1044 |
|
while ((i < sizeof(r) - 1) && (*p != '\r') && (*p != '\0') |
|
1045 |
|
&& (*p != search_for)) { |
|
1046 |
|
r[i++] = *p; |
|
1047 |
|
p++; |
|
1048 |
|
} |
|
1049 |
|
r[i] = '\0'; |
|
1050 |
|
|
|
1051 |
|
if (*p == search_for) |
|
1052 |
|
p++; |
|
1053 |
|
} |
|
1054 |
|
|
|
1055 |
|
/* alloc data and fill it */ |
|
1056 |
|
q = (struct Conn_split *) calloc(1, sizeof(struct Conn_split)); |
|
1057 |
|
if (!q) { |
|
1058 |
|
snprintf(Conn_error, sizeof(Conn_error), |
|
1059 |
|
"cannot alloc memory!\n"); |
|
1060 |
|
goto out_free; |
|
1061 |
|
} |
|
1062 |
|
q->l = strdup(l); |
|
1063 |
|
if (!q->l) { |
|
1064 |
|
free(q); |
|
1065 |
|
goto out_free; |
|
1066 |
|
} |
|
1067 |
|
q->r = strdup(r); |
|
1068 |
|
if (!q->r) { |
|
1069 |
|
free(q->l); |
|
1070 |
|
free(q); |
|
1071 |
|
goto out_free; |
|
1072 |
|
} |
|
1073 |
|
q->len = strlen(q->r); |
|
1074 |
|
|
|
1075 |
|
if (ret == NULL) { |
|
1076 |
|
ret = q; |
|
1077 |
|
last = ret; |
|
1078 |
|
} else { |
|
1079 |
|
last->next = q; |
|
1080 |
|
last = q; |
|
1081 |
|
} |
|
1082 |
|
} |
|
1083 |
|
|
|
1084 |
|
return ret; |
|
1085 |
|
|
|
1086 |
|
out_free: |
|
1087 |
|
Conn_split_free(&ret); |
|
1088 |
|
|
|
1089 |
|
return NULL; |
|
1090 |
|
} |
|
1091 |
|
|
|
1092 |
|
/* |
|
1093 |
|
* Search for a string and return the value |
|
1094 |
|
*/ |
|
1095 |
|
char *Conn_split_get_size(const struct Conn_split *s, const char *l, |
|
1096 |
|
unsigned int *size) |
|
1097 |
|
{ |
|
1098 |
|
while (s) { |
|
1099 |
|
if (strcmp(l, s->l) == 0) { |
|
1100 |
|
if (size != NULL) |
|
1101 |
|
*size = s->len; |
|
1102 |
|
return s->r; |
|
1103 |
|
} |
|
1104 |
|
s = s->next; |
|
1105 |
|
} |
|
1106 |
|
|
|
1107 |
|
return NULL; |
|
1108 |
|
} |
|
1109 |
|
|
|
1110 |
|
/* |
|
1111 |
|
* Search for a string and return the value |
|
1112 |
|
*/ |
|
1113 |
|
char *Conn_split_get(const struct Conn_split *s, const char *l) |
|
1114 |
|
{ |
|
1115 |
|
return Conn_split_get_size(s, l, NULL); |
|
1116 |
|
} |
|
1117 |
|
|
|
1118 |
|
/* |
|
1119 |
|
* Search for a string and return the value or "" if not found |
|
1120 |
|
*/ |
|
1121 |
|
char *Conn_split_get_e(const struct Conn_split *s, const char *l, |
|
1122 |
|
unsigned int *size) |
|
1123 |
|
{ |
|
1124 |
|
char *r; |
|
1125 |
|
|
|
1126 |
|
r = Conn_split_get_size(s, l, size); |
|
1127 |
|
if (!r) |
|
1128 |
|
r = ""; |
|
1129 |
|
|
|
1130 |
|
return r; |
|
1131 |
|
} |
|
1132 |
|
|
|
1133 |
|
/* |
|
1134 |
|
* Return a value as unsigned long |
|
1135 |
|
*/ |
|
1136 |
|
unsigned long Conn_split_get_ul(const struct Conn_split *s, const char *l, |
|
1137 |
|
unsigned int base) |
|
1138 |
|
{ |
|
1139 |
|
char *r; |
|
1140 |
|
unsigned long ret = 0; |
|
1141 |
|
|
|
1142 |
|
r = Conn_split_get(s, l); |
|
1143 |
|
if (r) |
|
1144 |
|
ret = strtoul(r, NULL, base); |
|
1145 |
|
|
|
1146 |
|
return ret; |
|
1147 |
|
} |
|
1148 |
|
|
|
1149 |
|
/* |
|
1150 |
|
* Return a value as double |
|
1151 |
|
*/ |
|
1152 |
|
double Conn_split_get_d(const struct Conn_split *s, const char *l) |
|
1153 |
|
{ |
|
1154 |
|
char *r; |
|
1155 |
|
double ret = 0; |
|
1156 |
|
|
|
1157 |
|
r = Conn_split_get(s, l); |
|
1158 |
|
if (r) |
|
1159 |
|
ret = strtod(r, NULL); |
|
1160 |
|
|
|
1161 |
|
return ret; |
|
1162 |
|
} |
File Conn_engine_core.h changed (mode: 100644) (index dab1c50..948c165) |
... |
... |
struct Conn_queue |
181 |
181 |
struct Conn_queue_entry *head, *tail; |
struct Conn_queue_entry *head, *tail; |
182 |
182 |
}; |
}; |
183 |
183 |
|
|
|
184 |
|
|
|
185 |
|
/* For parsing */ |
|
186 |
|
struct Conn_split |
|
187 |
|
{ |
|
188 |
|
struct Conn_split *next; |
|
189 |
|
char *l; |
|
190 |
|
char *r; |
|
191 |
|
unsigned int len; |
|
192 |
|
}; |
|
193 |
|
|
|
194 |
|
|
184 |
195 |
/* Variables */ |
/* Variables */ |
185 |
196 |
extern void (*Conn_accept_cb)(struct Conn *C); |
extern void (*Conn_accept_cb)(struct Conn *C); |
186 |
197 |
extern void (*Conn_recv_cb)(struct Conn *C); |
extern void (*Conn_recv_cb)(struct Conn *C); |
|
... |
... |
extern int Conn_queue_add(struct Conn_queue *q, |
285 |
296 |
const unsigned int slot); |
const unsigned int slot); |
286 |
297 |
extern void Conn_queue_destroy(struct Conn_queue *q); |
extern void Conn_queue_destroy(struct Conn_queue *q); |
287 |
298 |
|
|
|
299 |
|
/* splitting stuff */ |
|
300 |
|
extern struct Conn_split *Conn_split(char *line); |
|
301 |
|
extern char *Conn_split_get_size(const struct Conn_split *s, |
|
302 |
|
const char *l, unsigned int *len); |
|
303 |
|
extern char *Conn_split_get(const struct Conn_split *s, |
|
304 |
|
const char *l); |
|
305 |
|
extern char *Conn_split_get_e(const struct Conn_split *s, |
|
306 |
|
const char *l, unsigned int *size); |
|
307 |
|
extern unsigned long Conn_split_get_ul(const struct Conn_split *s, |
|
308 |
|
const char *l, unsigned int base); |
|
309 |
|
extern double Conn_split_get_d(const struct Conn_split *s, |
|
310 |
|
const char *l); |
|
311 |
|
extern void Conn_split_free(struct Conn_split **s); |
|
312 |
|
|
288 |
313 |
#endif |
#endif |
|
314 |
|
|
File tests/t2.c added (mode: 100644) (index 0000000..a5fb690) |
|
1 |
|
/* |
|
2 |
|
* Testing splitting functions |
|
3 |
|
*/ |
|
4 |
|
|
|
5 |
|
#include <Conn.h> |
|
6 |
|
|
|
7 |
|
int main(void) |
|
8 |
|
{ |
|
9 |
|
char *s = "a=b c= d= sasasasdasd=", *x; |
|
10 |
|
int ret = 0; |
|
11 |
|
struct Conn_split *sp; |
|
12 |
|
|
|
13 |
|
sp = Conn_split(s); |
|
14 |
|
if (!sp) { |
|
15 |
|
printf("Cannot split (%s)!\n", Conn_strerror()); |
|
16 |
|
ret++; |
|
17 |
|
} else { |
|
18 |
|
x = Conn_split_get(sp, "a"); |
|
19 |
|
if (!x || strcmp(x, "b") != 0) { |
|
20 |
|
printf("ERROR: a != %s.\n", x); |
|
21 |
|
ret++; |
|
22 |
|
} |
|
23 |
|
|
|
24 |
|
x = Conn_split_get(sp, "c"); |
|
25 |
|
if (!x || strcmp(x, "") != 0) { |
|
26 |
|
printf("ERROR: \"\" != \"%s\".\n", x); |
|
27 |
|
ret++; |
|
28 |
|
} |
|
29 |
|
} |
|
30 |
|
|
|
31 |
|
Conn_split_free(&sp); |
|
32 |
|
|
|
33 |
|
return ret; |
|
34 |
|
} |