-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbits.c
214 lines (196 loc) · 3.08 KB
/
bits.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
/*
harris - a strategy game
Copyright (C) 2012-2015 Edward Cree
licensed under GPLv3+ - see top of harris.c for details
bits: misc. functions, mostly for string buffers
*/
#include "bits.h"
#include <stdbool.h>
#include <ctype.h>
char *fgetl(FILE *fp)
{
bool eof=false;
string s=init_string();
signed int c;
while(!feof(fp))
{
c=fgetc(fp);
if(c==EOF)
{
eof=true;
break;
}
if(c=='\n')
break;
if(c!=0)
{
append_char(&s, c);
}
}
if(eof&&!s.i)
{
free(s.buf);
return(NULL);
}
return(s.buf);
}
char *slurp(FILE *fp)
{
string s=init_string();
signed int c;
while(!feof(fp))
{
c=fgetc(fp);
if(c==EOF)
break;
if(c!=0)
append_char(&s, c);
}
return(s.buf);
}
string sslurp(FILE *fp)
{
string s=init_string();
signed int c;
while(!feof(fp))
{
c=fgetc(fp);
if(c==EOF)
break;
append_char(&s, c);
}
return(s);
}
void append_char(string *s, char c)
{
if(s->buf)
{
s->buf[s->i++]=c;
}
else
{
*s=init_string();
append_char(s, c);
}
char *nbuf=s->buf;
if(s->i>=s->l)
{
if(s->i)
s->l=s->i*2;
else
s->l=80;
nbuf=(char *)realloc(s->buf, s->l);
}
if(nbuf)
{
s->buf=nbuf;
s->buf[s->i]=0;
}
else
{
free(s->buf);
*s=init_string();
}
}
void append_str(string *s, const char *str)
{
while(str && *str) // not the most tremendously efficient implementation, but conceptually simple at least
{
append_char(s, *str++);
}
}
void append_string(string *s, const string t)
{
if(!s) return;
size_t i=s->i+t.i;
if(i>=s->l)
{
size_t l=i+1;
char *nbuf=(char *)realloc(s->buf, l);
if(!nbuf) return;
s->l=l;
s->buf=nbuf;
}
memcpy(s->buf+s->i, t.buf, t.i);
s->buf[i]=0;
s->i=i;
}
string init_string(void)
{
string s;
s.buf=(char *)malloc(s.l=80);
if(s.buf)
s.buf[0]=0;
else
s.l=0;
s.i=0;
return(s);
}
string null_string(void)
{
return((string){.buf=NULL, .l=0, .i=0});
}
string make_string(const char *str)
{
string s=init_string();
append_str(&s, str);
return(s);
}
void free_string(string *s)
{
free(s->buf);
}
const char hex[16]="0123456789abcdef";
void pacid(acid id, char buf[9])
{
for(size_t i=0;i<8;i++)
buf[i]=hex[(id>>(i<<2))&0xf];
buf[8]=0;
}
int gacid(const char from[8], acid *buf)
{
if(!buf) return(1);
*buf=0;
unsigned int val;
char fbuf[2]={0, 0};
for(size_t i=0;i<8;i++)
{
if(!isxdigit(from[i])) return(1);
fbuf[0]=from[i];
sscanf(fbuf, "%x", &val);
*buf|=val<<(i<<2);
}
return(0);
}
void pcmid(cmid id, char buf[17])
{
for(size_t i=0;i<16;i++)
buf[i]=hex[(id>>(i<<2))&0xf];
buf[16]=0;
}
int gcmid(const char from[16], cmid *buf)
{
if(!buf) return(1);
*buf=0;
unsigned int val;
char fbuf[2]={0, 0};
for(size_t i=0;i<16;i++)
{
if(!from[i]) break; // Allow short strings. It's fine because we're little endian
if(!isxdigit(from[i])) return(1);
fbuf[0]=from[i];
sscanf(fbuf, "%x", &val);
*buf|=((uint64_t)val)<<(i<<2);
}
return(0);
}
#ifdef WINDOWS /* doesn't have strndup, we need to implement one */
char *strndup(const char *s, size_t size)
{
char *rv=(char *)malloc(size+1);
if(rv==NULL) return(NULL);
strncpy(rv, s, size);
rv[size]=0;
return(rv);
}
#endif