1 #include <cstring>
2 #include <cstdlib>
3
4 #include "config.h"
5 #include "xio.h"
6 #include "misc-f.h"
7
8 #ifdef USE_FLOAT128
9 #include <quadmath.h>
10 #endif
11
12 struct fr {
13 char *string;
14 int pos;
15 int allocedsize;
16 int stringsize;
17 };
18
xstrtonum(const char * s,char ** sp)19 number_t xstrtonum(const char *s, char **sp)
20 {
21 #ifdef USE_FLOAT128
22 return strtoflt128(s, sp);
23 #else
24 #ifdef USE_LONG_DOUBLE
25 return strtold(s, sp);
26 #else
27 return strtod(s, sp);
28 #endif
29 #endif
30 }
31
mystrdup(const char * c)32 char *mystrdup(const char *c)
33 {
34 int l = strlen(c);
35 char *d = (char *)malloc(l + 1);
36 if (!d)
37 return NULL;
38 memcpy(d, c, l + 1);
39 return d;
40 }
41
sputc(int c,xio_file s)42 static int sputc(int c, xio_file s)
43 {
44 struct fr *f = (struct fr *)s->data;
45 if (f->pos >= f->allocedsize - 1) {
46 char *c = (char *)realloc(f->string, f->allocedsize * 2);
47 if (!c)
48 return XIO_EOF;
49 f->string = c;
50 f->allocedsize *= 2;
51 }
52 f->string[f->pos++] = c;
53 if (f->pos >= f->stringsize)
54 f->string[f->pos] = 0, f->stringsize = f->pos;
55 return 0;
56 }
57
sputs(const char * c,xio_file s)58 static int sputs(const char *c, xio_file s)
59 {
60 int l = strlen(c);
61 struct fr *f = (struct fr *)s->data;
62 while (f->pos + l >= f->allocedsize - 1) {
63 char *c = (char *)realloc(f->string, f->allocedsize * 2);
64 if (!c)
65 return XIO_EOF;
66 f->string = c;
67 f->allocedsize *= 2;
68 }
69 memcpy(f->string + f->pos, c, l);
70 f->pos += l;
71 if (f->pos >= f->stringsize)
72 f->string[f->pos] = 0, f->stringsize = f->pos;
73 return 0;
74 }
75
sungetc(int,xio_file s)76 static int sungetc(int /*c*/, xio_file s)
77 {
78 struct fr *f = (struct fr *)s->data;
79 f->pos--;
80 /*f->string[f->pos]=c; */
81 return 0;
82 }
83
sgetc(xio_file s)84 static int sgetc(xio_file s)
85 {
86 struct fr *f = (struct fr *)s->data;
87 if (f->pos == f->stringsize)
88 return XIO_EOF;
89 return f->string[f->pos++];
90 }
91
sfeof(xio_file s)92 static int sfeof(xio_file s)
93 {
94 struct fr *f = (struct fr *)s->data;
95 return (f->pos == f->stringsize);
96 }
97
srclose(xio_file s)98 static int srclose(xio_file s)
99 {
100 struct fr *f = (struct fr *)s->data;
101 free(f->string);
102 free(f);
103 free(s);
104 return 0;
105 }
106
swclose(xio_file s)107 static int swclose(xio_file s)
108 {
109 struct fr *f = (struct fr *)s->data;
110 f->string = (char *)realloc(f->string, f->stringsize + 1);
111 /*free(s);
112 free(f); */
113 return 0;
114 }
115
xio_getstring(xio_file s)116 char *xio_getstring(xio_file s)
117 {
118 struct fr *f = (struct fr *)s->data;
119 char *c = f->string;
120 free(f);
121 free(s);
122 return c;
123 }
124
xio_strropen(const char * string)125 xio_file xio_strropen(const char *string)
126 {
127 xio_file s = (xio_file)calloc(1, sizeof(*s));
128 struct fr *f = (struct fr *)calloc(1, sizeof(*f));
129 s->data = f;
130 f->pos = 0;
131 f->string = (char *)string;
132 f->stringsize = strlen(string);
133 s->fclose = srclose;
134 s->xeof = sfeof;
135 s->fgetc = sgetc;
136 s->fungetc = sungetc;
137 return s;
138 }
139
140 #define PAGE 4096
xio_strwopen(void)141 xio_file xio_strwopen(void)
142 {
143 xio_file s = (xio_file)calloc(1, sizeof(*s));
144 struct fr *f = (struct fr *)calloc(1, sizeof(*f));
145 s->data = f;
146 f->pos = 0;
147 f->string = (char *)malloc(PAGE);
148 f->allocedsize = PAGE;
149 f->stringsize = 0;
150 s->fputc = sputc;
151 s->fputs = sputs;
152 s->fclose = swclose;
153 s->flush = NULL;
154 return s;
155 }
156