Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/util/xstring.c
blob: 02959f6a77dd62004068713bfc6837a7d2a4af1c (plain)
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
#ifndef _plan9_
#include <string.h>
#include <stdlib.h>
#else
#include <u.h>
#include <libc.h>
#endif

#include <xio.h>
#include <misc-f.h>
struct fr {
    char *string;
    int pos;
    int allocedsize;
    int stringsize;
};

char *mystrdup(const char *c)
{
    int l = strlen(c);
    char *d = malloc(l + 1);
    if (!d)
	return NULL;
    memcpy(d, c, l + 1);
    return d;
}

static int sputc(int c, xio_file s)
{
    struct fr *f = (struct fr *) s->data;
    if (f->pos >= f->allocedsize - 1) {
	char *c = (char *) realloc(f->string, f->allocedsize * 2);
	if (!c)
	    return XIO_EOF;
	f->string = c;
	f->allocedsize *= 2;
    }
    f->string[f->pos++] = c;
    if (f->pos >= f->stringsize)
	f->string[f->pos] = 0, f->stringsize = f->pos;
    return 0;
}

static int sputs(CONST char *c, xio_file s)
{
    int l = strlen(c);
    struct fr *f = (struct fr *) s->data;
    while (f->pos + l >= f->allocedsize - 1) {
	char *c = (char *) realloc(f->string, f->allocedsize * 2);
	if (!c)
	    return XIO_EOF;
	f->string = c;
	f->allocedsize *= 2;
    }
    memcpy(f->string + f->pos, c, l);
    f->pos += l;
    if (f->pos >= f->stringsize)
	f->string[f->pos] = 0, f->stringsize = f->pos;
    return 0;
}

static int sungetc(int c, xio_file s)
{
    struct fr *f = (struct fr *) s->data;
    f->pos--;
    /*f->string[f->pos]=c; */
    return 0;
}

static int sgetc(xio_file s)
{
    struct fr *f = (struct fr *) s->data;
    if (f->pos == f->stringsize)
	return XIO_EOF;
    return f->string[f->pos++];
}

static int sfeof(xio_file s)
{
    struct fr *f = (struct fr *) s->data;
    return (f->pos == f->stringsize);
}

static int srclose(xio_file s)
{
    struct fr *f = (struct fr *) s->data;
    free(f->string);
    free(f);
    free(s);
    return 0;
}

static int swclose(xio_file s)
{
    struct fr *f = (struct fr *) s->data;
    f->string = (char *) realloc(f->string, f->stringsize + 1);
    /*free(s);
       free(f); */
    return 0;
}

char *xio_getstring(xio_file s)
{
    struct fr *f = (struct fr *) s->data;
    char *c = f->string;
    free(f);
    free(s);
    return c;
}

xio_file xio_strropen(CONST char *string)
{
    xio_file s = (xio_file) calloc(1, sizeof(*s));
    struct fr *f = (struct fr *) calloc(1, sizeof(*f));
    s->data = f;
    f->pos = 0;
    f->string = (char *) string;
    f->stringsize = strlen(string);
    s->fclose = srclose;
    s->xeof = sfeof;
    s->fgetc = sgetc;
    s->fungetc = sungetc;
    return s;
}

#define PAGE 4096
xio_file xio_strwopen(void)
{
    xio_file s = (xio_file) calloc(1, sizeof(*s));
    struct fr *f = (struct fr *) calloc(1, sizeof(*f));
    s->data = f;
    f->pos = 0;
    f->string = (char *) malloc(PAGE);
    f->allocedsize = PAGE;
    f->stringsize = 0;
    s->fputc = sputc;
    s->fputs = sputs;
    s->fclose = swclose;
    s->flush = NULL;
    return s;
}