-
Notifications
You must be signed in to change notification settings - Fork 19
/
state.h
83 lines (77 loc) · 1.95 KB
/
state.h
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
/*
* state.h
*
* (C) Copyright 2014 Ulrich Hecht
*
* This file is part of CASCADE. CASCADE is almost free software; you can
* redistribute it and/or modify it under the terms of the Cascade Public
* License 1.0. Read the file "LICENSE" for details.
*/
#ifndef _STATE_H
#define _STATE_H
#include <stdio.h>
#include "debug.h"
#ifdef EVENT_COMPRESSED
#include <zlib.h>
#endif
/* any attempt to make this more ceeplusplussy bloated it with needless complexity,
so we let the preprocessor do the job instead */
#ifdef EVENT_COMPRESSED
typedef gzFile statefile_t;
#define state_open gzopen
#define state_tell gztell
#define state_seek gzseek
#define state_eof gzeof
#define state_close gzclose
#define STATE_RW(x) write ? gzwrite(fp, &x, sizeof(x)) : gzread(fp, &x, sizeof(x))
#define STATE_RWBUF(x, n) write ? gzwrite(fp, x, n) : gzread(fp, x, n)
#else
typedef FILE* statefile_t;
#define state_open fopen
#define state_tell ftell
#define state_seek fseek
#define state_eof feof
#define state_close gzclose
#define STATE_RW(x) write ? fwrite(&x, sizeof(x), 1, fp) : fread(&x, sizeof(x), 1, fp)
#define STATE_RWBUF(x, n) write ? fwrite(x, n, 1, fp) : fread(x, n, 1, fp)
#endif
#ifdef EVENT_COMPRESSED
#define STATE_RWSTRING(s) { \
if (write) { \
if (s) gzputs(fp, s); \
gzputc(fp, '\n'); \
} \
else { \
char n[256]; \
gzgets(fp, n, 256); \
n[strlen(n)-1] = 0; /* strip LF */ \
if (s) \
free(s); \
if (strlen(n)) \
s = strdup(n); \
else \
s = NULL; \
DEBUG(WARN, "rwstring read %s\n", s); \
} \
}
#else
#define STATE_RWSTRING(s) { \
if (write) { \
if (s) fputs(s, fp); \
putc('\n', fp); \
} \
else { \
char n[256]; \
fgets(n, 256, fp); \
n[strlen(n)-1] = 0; /* strip LF */ \
if (s) \
free(s); \
if (strlen(n)) \
s = strdup(n); \
else \
s = NULL; \
DEBUG(WARN, "rwstring read %s\n", s); \
} \
}
#endif
#endif