[ui] Make passage objects reusable

I had intended to do this for a while, but now that I want it for center
print strings, it was time to make the change.
This commit is contained in:
Bill Currie 2022-11-04 13:06:20 +09:00
parent 2a53047007
commit 1d51c05f90
3 changed files with 22 additions and 11 deletions

View file

@ -49,7 +49,8 @@ typedef struct passage_s {
struct hierarchy_s *hierarchy; ///< hierarchy of text objects
} passage_t;
passage_t *Passage_ParseText (const char *text, struct ecs_registry_s *reg);
void Passage_ParseText (passage_t *passage, const char *text);
passage_t *Passage_New (struct ecs_registry_s *reg);
void Passage_Delete (passage_t *passage);
int Passage_IsSpace (const char *text) __attribute__((pure));

View file

@ -99,16 +99,16 @@ add_entity (hierarchy_t *h, uint32_t parent)
ref->index = i;
}
VISIBLE passage_t *
Passage_ParseText (const char *text, ecs_registry_t *reg)
VISIBLE void
Passage_ParseText (passage_t *passage, const char *text)
{
passage_t *passage = malloc (sizeof (passage_t));
passage->text = text;
passage->reg = reg;
if (!*text) {
return passage;
if (passage->hierarchy) {//FIXME just empty hierarchy
Hierarchy_Delete (passage->hierarchy);
}
if (!*text) {
return;
}
passage->text = text;
unsigned num_paragraphs = 1;
unsigned num_text_objects = 1;
@ -133,7 +133,7 @@ Passage_ParseText (const char *text, ecs_registry_t *reg)
parsing_space = 0;
}
}
passage->hierarchy = Hierarchy_New (reg, &passage_type, 0);
passage->hierarchy = Hierarchy_New (passage->reg, &passage_type, 0);
Hierarchy_Reserve (passage->hierarchy,
1 + num_paragraphs + num_text_objects);
#if 0
@ -197,6 +197,15 @@ Passage_ParseText (const char *text, ecs_registry_t *reg)
to->text, to->size, to->size, text + to->text);
}
#endif
}
VISIBLE passage_t *
Passage_New (ecs_registry_t *reg)
{
passage_t *passage = malloc (sizeof (passage_t));
passage->text = 0;
passage->reg = reg;
passage->hierarchy = 0;
return passage;
}

View file

@ -66,7 +66,8 @@ main (void)
ECS_RegisterComponents (registry, test_components, 1);
registry->href_comp = test_href;
passage_t *passage = Passage_ParseText (test_text, registry);
passage_t *passage = Passage_New (registry);
Passage_ParseText (passage, test_text);
if (passage->hierarchy->childCount[0] != 3) {
ret = 1;
printf ("incorrect number of paragraphs: %d\n",