Use c++ style constructors/destructors

This commit is contained in:
Dane Johnson 2021-12-17 16:20:48 -06:00
parent a797d269c5
commit 6aed8ed768

View File

@ -5,27 +5,34 @@
#include <cstdlib> #include <cstdlib>
#include <cassert> #include <cassert>
typedef struct page_t page_t;
typedef struct footer_t footer_t;
typedef struct statcheck_t statcheck_t; typedef struct statcheck_t statcheck_t;
typedef struct statchange_t statchange_t; typedef struct statchange_t statchange_t;
typedef struct choice_t choice_t; typedef struct choice_t choice_t;
#define FOOTER_END 1 enum class FooterType {
#define FOOTER_GOTO 2 End,
#define FOOTER_CHOICES 3 Goto,
struct footer_t { Choices
int type; };
struct Footer {
FooterType type;
union { union {
std::vector<choice_t*> choices; std::vector<choice_t*> choices;
char *link; char *link;
}; };
Footer();
~Footer();
}; };
struct page_t { struct Page {
char *id; char *id;
char *body; char *body;
footer_t footer; Footer footer;
Page();
~Page();
}; };
#define STATCHECK_GT 1 #define STATCHECK_GT 1
@ -55,18 +62,18 @@ struct choice_t {
statchange_t statchange; statchange_t statchange;
}; };
std::vector<page_t*> pages; std::vector<Page*> pages;
choice_t *make_choice(); choice_t *make_choice();
page_t *emit_ending(); Page *emit_ending();
page_t *emit_goto(char*); Page *emit_goto(char*);
page_t *emit_choices(); Page *emit_choices();
void append_body(page_t *page, char *str); void append_body(Page *page, char *str);
union value { union value {
page_t *page; Page *page;
char *string; char *string;
choice_t *choice; choice_t *choice;
std::vector<choice_t*> *choices; std::vector<choice_t*> *choices;
@ -135,11 +142,24 @@ Newline = '\r\n' | '\r' | '\n';
%% %%
static inline page_t *make_page() { Footer::Footer() {
page_t *mypage = (page_t*) malloc(sizeof(page_t)); }
mypage->id = NULL;
mypage->body = NULL; Footer::~Footer() {
return mypage; }
Page::Page() {
id = NULL;
body = NULL;
}
Page::~Page() {
if (id) {
free(id);
}
if (body) {
free(body);
}
} }
inline choice_t *make_choice() { inline choice_t *make_choice() {
@ -149,27 +169,27 @@ inline choice_t *make_choice() {
return mychoice; return mychoice;
} }
page_t *emit_goto(char *id) { Page *emit_goto(char *id) {
page_t *mypage = make_page(); Page *mypage = new Page;
mypage->footer.type = FOOTER_GOTO; mypage->footer.type = FooterType::Goto;
mypage->footer.link = id; mypage->footer.link = id;
return mypage; return mypage;
} }
page_t *emit_ending() { Page *emit_ending() {
page_t *mypage = make_page(); Page *mypage = new Page;
mypage->footer.type = FOOTER_END; mypage->footer.type = FooterType::End;
return mypage; return mypage;
} }
page_t *emit_choices() { Page *emit_choices() {
page_t *mypage = make_page(); Page *mypage = new Page;
mypage->footer.type = FOOTER_CHOICES; mypage->footer.type = FooterType::Choices;
printf("page 0x%x\n", mypage); printf("page 0x%x\n", mypage);
return mypage; return mypage;
} }
void append_body(page_t *page, char *str) { void append_body(Page *page, char *str) {
if (page->body) { if (page->body) {
int size = strlen(page->body) + strlen(str) + 1; int size = strlen(page->body) + strlen(str) + 1;
char *newstring = (char*) calloc(size, sizeof(char)); char *newstring = (char*) calloc(size, sizeof(char));
@ -182,16 +202,16 @@ void append_body(page_t *page, char *str) {
} }
} }
void print_page(page_t *page) { void print_page(Page *page) {
printf("HEADER: %s\nBODY: %s\n", page->id, page->body); printf("HEADER: %s\nBODY: %s\n", page->id, page->body);
switch (page->footer.type) { switch (page->footer.type) {
case FOOTER_END: case FooterType::End:
printf("THE END\n"); printf("THE END\n");
break; break;
case FOOTER_GOTO: case FooterType::Goto:
printf("GOTO %s\n", page->footer.link); printf("GOTO %s\n", page->footer.link);
break; break;
case FOOTER_CHOICES: case FooterType::Choices:
printf("CHOICES:\n"); printf("CHOICES:\n");
break; break;
} }
@ -202,7 +222,7 @@ int main() {
printf("Parsing Error!\n"); printf("Parsing Error!\n");
return 1; return 1;
} else { } else {
for (page_t* page: pages) { for (Page* page : pages) {
print_page(page); print_page(page);
} }
} }