From 1d51c05f906904d2aeb99fed6c426ada24e59dae Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Fri, 4 Nov 2022 13:06:20 +0900 Subject: [PATCH] [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. --- include/QF/ui/passage.h | 3 ++- libs/ui/passage.c | 27 ++++++++++++++++++--------- libs/ui/test/test-passage.c | 3 ++- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/include/QF/ui/passage.h b/include/QF/ui/passage.h index 52a105cdd..09a47b9ee 100644 --- a/include/QF/ui/passage.h +++ b/include/QF/ui/passage.h @@ -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)); diff --git a/libs/ui/passage.c b/libs/ui/passage.c index 02e96a6dc..fd112c56e 100644 --- a/libs/ui/passage.c +++ b/libs/ui/passage.c @@ -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; } diff --git a/libs/ui/test/test-passage.c b/libs/ui/test/test-passage.c index e67223ee0..d6bbe8f57 100644 --- a/libs/ui/test/test-passage.c +++ b/libs/ui/test/test-passage.c @@ -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",