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))
{
ret = p->text;
p->text = NULL;
ret = buffer_detach(&p->text);
}
else
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]);
/* stole text */
data->text = p->text;
p->text = NULL;
data->text = buffer_detach(&p->text);
markup_parser_data_delete(&p);
return true;

View file

@ -47,9 +47,7 @@ markup_parse_start_element(GMarkupParseContext *context __attribute__ ((unused))
GError **error __attribute__ ((unused)))
{
markup_parser_data_t *p = (markup_parser_data_t *) user_data;
char *newtext;
int i, j;
ssize_t len;
for(i = 0; p->elements[i]; 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])
{
asprintf(&newtext, "%s%s", NONULL(p->text), p->elements_sub[i]);
p_delete(&p->text);
p->text = newtext;
buffer_adds(&p->text, p->elements_sub[i]);
}
return;
@ -76,22 +72,13 @@ markup_parse_start_element(GMarkupParseContext *context __attribute__ ((unused))
if(a_strcmp(element_name, "markup"))
{
if(!(len = asprintf(&newtext, "%s<%s ", NONULL(p->text), element_name)))
return;
len++; /* add \0 that asprintf does not return in len */
buffer_addf(&p->text, "<%s", element_name);
for(i = 0; attribute_names[i]; i++)
{
len += a_strlen(attribute_names[i]) + a_strlen(attribute_values[i]) + 5;
p_realloc(&newtext, len);
a_strcat(newtext, len, attribute_names[i]);
a_strcat(newtext, len, "=\"");
a_strcat(newtext, len, attribute_values[i]);
a_strcat(newtext, len, "\" ");
buffer_addf(&p->text, " %s=\"%s\"", attribute_names[i],
attribute_values[i]);
}
p_realloc(&newtext, ++len);
a_strcat(newtext, len, ">");
p_delete(&p->text);
p->text = newtext;
buffer_addc(&p->text, '>');
}
}
@ -110,7 +97,6 @@ markup_parse_end_element(GMarkupParseContext *context __attribute__ ((unused)),
GError **error __attribute__ ((unused)))
{
markup_parser_data_t *p = (markup_parser_data_t *) user_data;
char *newtext;
int i;
for(i = 0; p->elements[i]; i++)
@ -118,11 +104,7 @@ markup_parse_end_element(GMarkupParseContext *context __attribute__ ((unused)),
return;
if(a_strcmp(element_name, "markup"))
{
asprintf(&newtext, "%s</%s>", p->text, element_name);
p_delete(&p->text);
p->text = newtext;
}
buffer_addf(&p->text, "</%s>", element_name);
}
/** 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)))
{
markup_parser_data_t *p = (markup_parser_data_t *) user_data;
ssize_t rlen;
char *esc;
esc = g_markup_escape_text(text, text_len);
text_len = a_strlen(esc);
rlen = a_strlen(p->text) + 1 + text_len;
p_realloc(&p->text, rlen);
a_strncat(p->text, rlen, esc, text_len);
buffer_adds(&p->text, 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_values);
buffer_wipe(&p->text);
buffer_wipe(&(*p)->text);
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))
{
ret = p->text;
p->text = NULL;
ret = buffer_detach(&p->text);
}
else
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);
break;
}
p->text = NULL;
markup_parser_data_delete(&p);
if((image = draw_image_new(c->icon_path)))