summaryrefslogtreecommitdiffabout
path: root/src/txtacc.c
Unidiff
Diffstat (limited to 'src/txtacc.c') (more/less context) (show whitespace changes)
-rw-r--r--src/txtacc.c12
1 files changed, 7 insertions, 5 deletions
diff --git a/src/txtacc.c b/src/txtacc.c
index 91659f6..442e27e 100644
--- a/src/txtacc.c
+++ b/src/txtacc.c
@@ -25,26 +25,26 @@ struct txtacc_entry
25#define TXTACC_BUFSIZE 1024 25#define TXTACC_BUFSIZE 1024
26#define txtacc_entry_freesize(e) ((e)->size - (e)->len) 26#define txtacc_entry_freesize(e) ((e)->size - (e)->len)
27 27
28struct txtacc 28struct txtacc
29{ 29{
30 struct grecs_list *cur; /* Current build list */ 30 struct grecs_list *cur; /* Current build list */
31 struct grecs_list *mem; /* List of already allocated elements */ 31 struct grecs_list *mem; /* List of already allocated elements */
32}; 32};
33 33
34static struct txtacc_entry * 34static struct txtacc_entry *
35txtacc_alloc_entry (struct grecs_list *list, size_t size) 35txtacc_alloc_entry (struct grecs_list *list, size_t size)
36{ 36{
37 struct txtacc_entry *p = xmalloc (sizeof (*p)); 37 struct txtacc_entry *p = grecs_malloc (sizeof (*p));
38 p->buf = xmalloc (size); 38 p->buf = grecs_malloc (size);
39 p->size = size; 39 p->size = size;
40 p->len = 0; 40 p->len = 0;
41 grecs_list_append (list, p); 41 grecs_list_append (list, p);
42 return p; 42 return p;
43} 43}
44 44
45static struct txtacc_entry * 45static struct txtacc_entry *
46txtacc_cur_entry (struct txtacc *acc) 46txtacc_cur_entry (struct txtacc *acc)
47{ 47{
48 struct txtacc_entry *ent; 48 struct txtacc_entry *ent;
49 49
50 if (grecs_list_size (acc->cur) == 0) 50 if (grecs_list_size (acc->cur) == 0)
@@ -80,25 +80,25 @@ txtacc_entry_free (void *p)
80{ 80{
81 if (p) 81 if (p)
82 { 82 {
83 struct txtacc_entry *ent = p; 83 struct txtacc_entry *ent = p;
84 free (ent->buf); 84 free (ent->buf);
85 free (ent); 85 free (ent);
86 } 86 }
87} 87}
88 88
89struct txtacc * 89struct txtacc *
90txtacc_create () 90txtacc_create ()
91{ 91{
92 struct txtacc *acc = xmalloc (sizeof (*acc)); 92 struct txtacc *acc = grecs_malloc (sizeof (*acc));
93 acc->cur = grecs_list_create (); 93 acc->cur = grecs_list_create ();
94 acc->cur->free_entry = txtacc_entry_free; 94 acc->cur->free_entry = txtacc_entry_free;
95 acc->mem = grecs_list_create (); 95 acc->mem = grecs_list_create ();
96 acc->mem->free_entry = txtacc_entry_free; 96 acc->mem->free_entry = txtacc_entry_free;
97 return acc; 97 return acc;
98} 98}
99 99
100void 100void
101txtacc_free (struct txtacc *acc) 101txtacc_free (struct txtacc *acc)
102{ 102{
103 grecs_list_free (acc->cur); 103 grecs_list_free (acc->cur);
104 grecs_list_free (acc->mem); 104 grecs_list_free (acc->mem);
@@ -128,48 +128,50 @@ txtacc_finish (struct txtacc *acc, int steal)
128 size_t size; 128 size_t size;
129 char *p; 129 char *p;
130 130
131 switch (grecs_list_size (acc->cur)) 131 switch (grecs_list_size (acc->cur))
132 { 132 {
133 case 0: 133 case 0:
134 return NULL; 134 return NULL;
135 135
136 case 1: 136 case 1:
137 txtent = acc->cur->head->data; 137 txtent = acc->cur->head->data;
138 acc->cur->head->data = NULL; 138 acc->cur->head->data = NULL;
139 txtacc_entry_tailor (txtent); 139 txtacc_entry_tailor (txtent);
140 grecs_list_append (acc->mem, txtent);
140 break; 141 break;
141 142
142 default: 143 default:
143 size = 0; 144 size = 0;
144 for (ep = acc->cur->head; ep; ep = ep->next) 145 for (ep = acc->cur->head; ep; ep = ep->next)
145 { 146 {
146 txtent = ep->data; 147 txtent = ep->data;
147 size += txtent->len; 148 size += txtent->len;
148 } 149 }
149 150
150 txtent = txtacc_alloc_entry (acc->mem, size); 151 txtent = txtacc_alloc_entry (acc->mem, size);
151 for (ep = acc->cur->head; ep; ep = ep->next) 152 for (ep = acc->cur->head; ep; ep = ep->next)
152 { 153 {
153 struct txtacc_entry *tp = ep->data; 154 struct txtacc_entry *tp = ep->data;
154 txtacc_entry_append (txtent, tp->buf, tp->len); 155 txtacc_entry_append (txtent, tp->buf, tp->len);
155 } 156 }
156 } 157 }
157 158
158 grecs_list_clear (acc->cur); 159 grecs_list_clear (acc->cur);
159 p = txtent->buf; 160 p = txtent->buf;
160 if (steal) 161 if (steal)
162 {
163 grecs_list_remove_tail (acc->mem);
161 free (txtent); 164 free (txtent);
162 else 165 }
163 grecs_list_append (acc->mem, txtent);
164 return p; 166 return p;
165} 167}
166 168
167void 169void
168txtacc_free_string (struct txtacc *acc, char *str) 170txtacc_free_string (struct txtacc *acc, char *str)
169{ 171{
170 struct grecs_list_entry *ep; 172 struct grecs_list_entry *ep;
171 for (ep = acc->mem->head; ep; ep = ep->next) 173 for (ep = acc->mem->head; ep; ep = ep->next)
172 { 174 {
173 struct txtacc_entry *tp = ep->data; 175 struct txtacc_entry *tp = ep->data;
174 if (tp->buf == str) 176 if (tp->buf == str)
175 { 177 {

Return to:

Send suggestions and report system problems to the System administrator.