aboutsummaryrefslogtreecommitdiff
path: root/src/txtacc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/txtacc.c')
-rw-r--r--src/txtacc.c124
1 files changed, 62 insertions, 62 deletions
diff --git a/src/txtacc.c b/src/txtacc.c
index 5b9a197..c753f21 100644
--- a/src/txtacc.c
+++ b/src/txtacc.c
@@ -19,51 +19,51 @@
19#endif 19#endif
20#include <string.h> 20#include <string.h>
21#include <stdlib.h> 21#include <stdlib.h>
22#include "grecs.h" 22#include "argot.h"
23 23
24struct grecs_txtacc_entry 24struct argot_txtacc_entry
25{ 25{
26 char *buf; /* Text buffer */ 26 char *buf; /* Text buffer */
27 size_t size; /* Buffer size */ 27 size_t size; /* Buffer size */
28 size_t len; /* Actual number of bytes in buffer */ 28 size_t len; /* Actual number of bytes in buffer */
29}; 29};
30#define TXTACC_BUFSIZE 1024 30#define TXTACC_BUFSIZE 1024
31#define grecs_txtacc_entry_freesize(e) ((e)->size - (e)->len) 31#define argot_txtacc_entry_freesize(e) ((e)->size - (e)->len)
32 32
33struct grecs_txtacc 33struct argot_txtacc
34{ 34{
35 struct grecs_list *cur; /* Current build list */ 35 struct argot_list *cur; /* Current build list */
36 struct grecs_list *mem; /* List of already allocated elements */ 36 struct argot_list *mem; /* List of already allocated elements */
37}; 37};
38 38
39static struct grecs_txtacc_entry * 39static struct argot_txtacc_entry *
40grecs_txtacc_alloc_entry(struct grecs_list *list, size_t size) 40argot_txtacc_alloc_entry(struct argot_list *list, size_t size)
41{ 41{
42 struct grecs_txtacc_entry *p = grecs_malloc(sizeof (*p)); 42 struct argot_txtacc_entry *p = argot_malloc(sizeof (*p));
43 p->buf = grecs_malloc(size); 43 p->buf = argot_malloc(size);
44 p->size = size; 44 p->size = size;
45 p->len = 0; 45 p->len = 0;
46 grecs_list_append(list, p); 46 argot_list_append(list, p);
47 return p; 47 return p;
48} 48}
49 49
50static struct grecs_txtacc_entry * 50static struct argot_txtacc_entry *
51grecs_txtacc_cur_entry(struct grecs_txtacc *acc) 51argot_txtacc_cur_entry(struct argot_txtacc *acc)
52{ 52{
53 struct grecs_txtacc_entry *ent; 53 struct argot_txtacc_entry *ent;
54 54
55 if (grecs_list_size(acc->cur) == 0) 55 if (argot_list_size(acc->cur) == 0)
56 return grecs_txtacc_alloc_entry(acc->cur, 56 return argot_txtacc_alloc_entry(acc->cur,
57 GRECS_TXTACC_BUFSIZE); 57 ARGOT_TXTACC_BUFSIZE);
58 ent = acc->cur->tail->data; 58 ent = acc->cur->tail->data;
59 if (grecs_txtacc_entry_freesize(ent) == 0) 59 if (argot_txtacc_entry_freesize(ent) == 0)
60 ent = grecs_txtacc_alloc_entry(acc->cur, 60 ent = argot_txtacc_alloc_entry(acc->cur,
61 GRECS_TXTACC_BUFSIZE); 61 ARGOT_TXTACC_BUFSIZE);
62 return ent; 62 return ent;
63} 63}
64 64
65static void 65static void
66grecs_txtacc_entry_append(struct grecs_txtacc_entry *ent, 66argot_txtacc_entry_append(struct argot_txtacc_entry *ent,
67 const char *p, size_t size) 67 const char *p, size_t size)
68{ 68{
69 memcpy(ent->buf + ent->len, p, size); 69 memcpy(ent->buf + ent->len, p, size);
@@ -71,10 +71,10 @@ grecs_txtacc_entry_append(struct grecs_txtacc_entry *ent,
71} 71}
72 72
73static void 73static void
74grecs_txtacc_entry_tailor(struct grecs_txtacc_entry *ent) 74argot_txtacc_entry_tailor(struct argot_txtacc_entry *ent)
75{ 75{
76 if (ent->size > ent->len) { 76 if (ent->size > ent->len) {
77 char *p = grecs_realloc(ent->buf, ent->len); 77 char *p = argot_realloc(ent->buf, ent->len);
78 if (!p) 78 if (!p)
79 return; 79 return;
80 ent->buf = p; 80 ent->buf = p;
@@ -83,83 +83,83 @@ grecs_txtacc_entry_tailor(struct grecs_txtacc_entry *ent)
83} 83}
84 84
85static void 85static void
86grecs_txtacc_entry_free(void *p) 86argot_txtacc_entry_free(void *p)
87{ 87{
88 if (p) { 88 if (p) {
89 struct grecs_txtacc_entry *ent = p; 89 struct argot_txtacc_entry *ent = p;
90 free(ent->buf); 90 free(ent->buf);
91 free(ent); 91 free(ent);
92 } 92 }
93} 93}
94 94
95struct grecs_txtacc * 95struct argot_txtacc *
96grecs_txtacc_create() 96argot_txtacc_create()
97{ 97{
98 struct grecs_txtacc *acc = grecs_malloc(sizeof (*acc)); 98 struct argot_txtacc *acc = argot_malloc(sizeof (*acc));
99 acc->cur = grecs_list_create(); 99 acc->cur = argot_list_create();
100 acc->cur->free_entry = grecs_txtacc_entry_free; 100 acc->cur->free_entry = argot_txtacc_entry_free;
101 acc->mem = grecs_list_create(); 101 acc->mem = argot_list_create();
102 acc->mem->free_entry = grecs_txtacc_entry_free; 102 acc->mem->free_entry = argot_txtacc_entry_free;
103 return acc; 103 return acc;
104} 104}
105 105
106void 106void
107grecs_txtacc_free(struct grecs_txtacc *acc) 107argot_txtacc_free(struct argot_txtacc *acc)
108{ 108{
109 if (acc) { 109 if (acc) {
110 grecs_list_free(acc->cur); 110 argot_list_free(acc->cur);
111 grecs_list_free(acc->mem); 111 argot_list_free(acc->mem);
112 free(acc); 112 free(acc);
113 } 113 }
114} 114}
115 115
116void 116void
117grecs_txtacc_grow(struct grecs_txtacc *acc, const char *buf, size_t size) 117argot_txtacc_grow(struct argot_txtacc *acc, const char *buf, size_t size)
118{ 118{
119 while (size) { 119 while (size) {
120 struct grecs_txtacc_entry *ent = grecs_txtacc_cur_entry(acc); 120 struct argot_txtacc_entry *ent = argot_txtacc_cur_entry(acc);
121 size_t rest = grecs_txtacc_entry_freesize(ent); 121 size_t rest = argot_txtacc_entry_freesize(ent);
122 if (rest > size) 122 if (rest > size)
123 rest = size; 123 rest = size;
124 grecs_txtacc_entry_append(ent, buf, rest); 124 argot_txtacc_entry_append(ent, buf, rest);
125 buf += rest; 125 buf += rest;
126 size -= rest; 126 size -= rest;
127 } 127 }
128} 128}
129 129
130void 130void
131grecs_txtacc_grow_string(struct grecs_txtacc *acc, const char *buf) 131argot_txtacc_grow_string(struct argot_txtacc *acc, const char *buf)
132{ 132{
133 grecs_txtacc_grow(acc, buf, strlen(buf)); 133 argot_txtacc_grow(acc, buf, strlen(buf));
134} 134}
135 135
136void 136void
137grecs_txtacc_grow_string_escape(struct grecs_txtacc *acc, const char *buf) 137argot_txtacc_grow_string_escape(struct argot_txtacc *acc, const char *buf)
138{ 138{
139 for (; *buf; buf++) { 139 for (; *buf; buf++) {
140 if (strchr(" \t\n\"\'\\", *buf)) 140 if (strchr(" \t\n\"\'\\", *buf))
141 grecs_txtacc_grow_char(acc, '\\'); 141 argot_txtacc_grow_char(acc, '\\');
142 grecs_txtacc_grow_char(acc, *buf); 142 argot_txtacc_grow_char(acc, *buf);
143 } 143 }
144} 144}
145 145
146char * 146char *
147grecs_txtacc_finish(struct grecs_txtacc *acc, int steal) 147argot_txtacc_finish(struct argot_txtacc *acc, int steal)
148{ 148{
149 struct grecs_list_entry *ep; 149 struct argot_list_entry *ep;
150 struct grecs_txtacc_entry *txtent; 150 struct argot_txtacc_entry *txtent;
151 size_t size; 151 size_t size;
152 char *p; 152 char *p;
153 153
154 switch (grecs_list_size(acc->cur)) { 154 switch (argot_list_size(acc->cur)) {
155 case 0: 155 case 0:
156 return NULL; 156 return NULL;
157 157
158 case 1: 158 case 1:
159 txtent = acc->cur->head->data; 159 txtent = acc->cur->head->data;
160 acc->cur->head->data = NULL; 160 acc->cur->head->data = NULL;
161 grecs_txtacc_entry_tailor(txtent); 161 argot_txtacc_entry_tailor(txtent);
162 grecs_list_append(acc->mem, txtent); 162 argot_list_append(acc->mem, txtent);
163 break; 163 break;
164 164
165 default: 165 default:
@@ -169,38 +169,38 @@ grecs_txtacc_finish(struct grecs_txtacc *acc, int steal)
169 size += txtent->len; 169 size += txtent->len;
170 } 170