forked from lotheac/bcproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
room.c
70 lines (63 loc) · 1.1 KB
/
room.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
#include <err.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "config.h"
#include "room.h"
struct room *
room_new(const char *mapmsg)
{
struct room *room;
const char *cur;
size_t len;
const char *end;
room = calloc(1, sizeof(struct room));
if (!room)
goto err;
cur = mapmsg;
#define SEP ";;"
#define SKIP() do {\
cur = strstr(cur, SEP);\
if (!cur)\
goto err;\
cur += strlen(SEP);\
} while(0)
#define NEXT(target) do {\
end = strstr(cur, SEP);\
if (!end)\
goto err;\
len = end - cur + 1;\
target = malloc(len);\
if (!target)\
err(1, "room_new: malloc");\
strlcpy(target, cur, len);\
cur = end + strlen(SEP);\
} while(0)
NEXT(room->area);
NEXT(room->id);
NEXT(room->direction);
sscanf(cur, "%d" SEP, &room->indoors);
SKIP();
NEXT(room->shortdesc);
NEXT(room->longdesc);
NEXT(room->exits);
#undef SEP
#undef NEXT
return room;
err:
room_free(room);
return NULL;
}
void
room_free(struct room *room)
{
if (room) {
free(room->area);
free(room->id);
free(room->direction);
free(room->shortdesc);
free(room->longdesc);
free(room->exits);
free(room);
}
}