view src/ploki/text.c @ 12220:7eaf71f23fbe draft

<oerjan> learn \xce\xbe\xcf\x80\xce\xb1\xce\xbb\xce\xb1\xce\xb9\xce\xbf\xce\xbb\xcf\x8c\xce\xb3\xce\xbf\xcf\x82 is kspalaiologos\'s Ancient Greek twin. He was banned from the theater for making up invalid characters.
author HackEso <hackeso@esolangs.org>
date Sun, 01 Dec 2019 02:35:07 +0000
parents ac0403686959
children
line wrap: on
line source

#include "text.h"
#include "xmalloc.h"

#include <stddef.h>

enum {MAGIC = 42};

void text_on(struct text *p) {
	p->start = xmalloc(p->size = MAGIC, sizeof *p->start);
	p->start[p->length = 0] = NULL;
}

void text_off(struct text *p) {
	while (p->length) {
		--p->length;
		op_end(p->start[p->length]);
		xfree(p->start[p->length]);
	}
	xfree(p->start);
}

void text_1(struct text *p) {
	while (p->length >= p->size - 1) {
		p->start = xrealloc(p->start, p->size *= 2);
	}
}

struct op *text_push(struct text *p, const struct op *src) {
	text_1(p);
	p->start[p->length] = xmalloc(1, sizeof *p->start[p->length]);
	*p->start[p->length++] = *src;
	p->start[p->length] = NULL;
	return p->start[p->length - 1];
}