-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr.c
73 lines (58 loc) · 989 Bytes
/
str.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
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "str.h"
#include "util.h"
#include "vec.h"
int
str_append(char **dst, char src)
{
int err = 0;
size_t avail = 0;
avail = vec_mem(*dst) - vec_len(*dst);
if (avail < 3) {
err = vec_concat(dst, "\0\0", 2);
if (err) return err;
}
return vec_append(dst, &src);
}
int
str_readline(char **dst, FILE *src)
{
char c = 0;
int err = 0;
size_t avail = 0;
vec_truncat(dst, 0);
while (fread(&c,1,1,src) && c != '\n') {
avail = vec_mem(*dst) - vec_len(*dst);
if (avail < 3) {
err = vec_concat(dst, "\0\0", 2);
if (err) return err;
}
vec_append(dst, &c);
}
if (feof(src)) return -1;
if (ferror(src)) return errno;
return 0;
}
void
str_chomp(char **str)
{
char *nl = 0;
char *s = *str;
do if (*s == '\n') nl = s; while (*s++);
vec_truncat(str, nl - s);
if (nl) *nl = 0;
}
void
str_free(char *str)
{
vec_free(str);
}
char *
str_alloc(void)
{
char *ret;
vec_ctor(ret);
return ret;
}