Use extensible buffers in markup parsing.

This should avoid quite a few malloc calls.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
This commit is contained in:
Pierre Habouzit 2008-06-22 15:12:42 +02:00
parent e25a50774b
commit 5c601dfd19
5 changed files with 11 additions and 38 deletions

View File

@ -778,8 +778,7 @@ client_markup_parse(client_t *c, const char *str, ssize_t len)
if(markup_parse(p, str, len)) if(markup_parse(p, str, len))
{ {
ret = p->text; ret = buffer_detach(&p->text);
p->text = NULL;
} }
else else
ret = a_strdup(str); ret = a_strdup(str);

View File

@ -261,9 +261,7 @@ draw_text_markup_expand(draw_parser_data_t *data,
data->margin.right = atoi(p->attribute_values[2][i]); data->margin.right = atoi(p->attribute_values[2][i]);
/* stole text */ /* stole text */
data->text = p->text; data->text = buffer_detach(&p->text);
p->text = NULL;
markup_parser_data_delete(&p); markup_parser_data_delete(&p);
return true; return true;

View File

@ -47,9 +47,7 @@ markup_parse_start_element(GMarkupParseContext *context __attribute__ ((unused))
GError **error __attribute__ ((unused))) GError **error __attribute__ ((unused)))
{ {
markup_parser_data_t *p = (markup_parser_data_t *) user_data; markup_parser_data_t *p = (markup_parser_data_t *) user_data;
char *newtext;
int i, j; int i, j;
ssize_t len;
for(i = 0; p->elements[i]; i++) for(i = 0; p->elements[i]; i++)
if(!a_strcmp(element_name, p->elements[i])) if(!a_strcmp(element_name, p->elements[i]))
@ -66,9 +64,7 @@ markup_parse_start_element(GMarkupParseContext *context __attribute__ ((unused))
if(p->elements_sub && p->elements_sub[i]) if(p->elements_sub && p->elements_sub[i])
{ {
asprintf(&newtext, "%s%s", NONULL(p->text), p->elements_sub[i]); buffer_adds(&p->text, p->elements_sub[i]);
p_delete(&p->text);
p->text = newtext;
} }
return; return;
@ -76,22 +72,13 @@ markup_parse_start_element(GMarkupParseContext *context __attribute__ ((unused))
if(a_strcmp(element_name, "markup")) if(a_strcmp(element_name, "markup"))
{ {
if(!(len = asprintf(&newtext, "%s<%s ", NONULL(p->text), element_name))) buffer_addf(&p->text, "<%s", element_name);
return;
len++; /* add \0 that asprintf does not return in len */
for(i = 0; attribute_names[i]; i++) for(i = 0; attribute_names[i]; i++)
{ {
len += a_strlen(attribute_names[i]) + a_strlen(attribute_values[i]) + 5; buffer_addf(&p->text, " %s=\"%s\"", attribute_names[i],
p_realloc(&newtext, len); attribute_values[i]);
a_strcat(newtext, len, attribute_names[i]);
a_strcat(newtext, len, "=\"");
a_strcat(newtext, len, attribute_values[i]);
a_strcat(newtext, len, "\" ");
} }
p_realloc(&newtext, ++len); buffer_addc(&p->text, '>');
a_strcat(newtext, len, ">");
p_delete(&p->text);
p->text = newtext;
} }
} }
@ -110,7 +97,6 @@ markup_parse_end_element(GMarkupParseContext *context __attribute__ ((unused)),
GError **error __attribute__ ((unused))) GError **error __attribute__ ((unused)))
{ {
markup_parser_data_t *p = (markup_parser_data_t *) user_data; markup_parser_data_t *p = (markup_parser_data_t *) user_data;
char *newtext;
int i; int i;
for(i = 0; p->elements[i]; i++) for(i = 0; p->elements[i]; i++)
@ -118,11 +104,7 @@ markup_parse_end_element(GMarkupParseContext *context __attribute__ ((unused)),
return; return;
if(a_strcmp(element_name, "markup")) if(a_strcmp(element_name, "markup"))
{ buffer_addf(&p->text, "</%s>", element_name);
asprintf(&newtext, "%s</%s>", p->text, element_name);
p_delete(&p->text);
p->text = newtext;
}
} }
/** Callback to invoke when some text is seen (text is always inside an /** Callback to invoke when some text is seen (text is always inside an
@ -144,14 +126,10 @@ markup_parse_text(GMarkupParseContext *context __attribute__ ((unused)),
GError **error __attribute__ ((unused))) GError **error __attribute__ ((unused)))
{ {
markup_parser_data_t *p = (markup_parser_data_t *) user_data; markup_parser_data_t *p = (markup_parser_data_t *) user_data;
ssize_t rlen;
char *esc; char *esc;
esc = g_markup_escape_text(text, text_len); esc = g_markup_escape_text(text, text_len);
text_len = a_strlen(esc); buffer_adds(&p->text, esc);
rlen = a_strlen(p->text) + 1 + text_len;
p_realloc(&p->text, rlen);
a_strncat(p->text, rlen, esc, text_len);
p_delete(&esc); p_delete(&esc);
} }
@ -202,7 +180,7 @@ markup_parser_data_delete(markup_parser_data_t **p)
p_delete(&(*p)->attribute_names); p_delete(&(*p)->attribute_names);
p_delete(&(*p)->attribute_values); p_delete(&(*p)->attribute_values);
buffer_wipe(&p->text); buffer_wipe(&(*p)->text);
p_delete(p); p_delete(p);
} }

View File

@ -67,8 +67,7 @@ tag_markup_parse(tag_t *t, const char *str, ssize_t len)
if(markup_parse(p, str, len)) if(markup_parse(p, str, len))
{ {
ret = p->text; ret = buffer_detach(&p->text);
p->text = NULL;
} }
else else
ret = a_strdup(str); ret = a_strdup(str);

View File

@ -131,7 +131,6 @@ tasklist_draw(draw_context_t *ctx, int screen,
draw_rectangle(ctx, area, 1.0, true, bg_color); draw_rectangle(ctx, area, 1.0, true, bg_color);
break; break;
} }
p->text = NULL;
markup_parser_data_delete(&p); markup_parser_data_delete(&p);
if((image = draw_image_new(c->icon_path))) if((image = draw_image_new(c->icon_path)))