Files
rifle/core/state.c
2026-02-15 22:17:40 -05:00

63 lines
1.3 KiB
C

#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include "rifle/state.h"
int
rifle_state_init(struct rifle_state *state, const char *in_path)
{
if (state == NULL || in_path == NULL) {
return -1;
}
memset(state, 0, sizeof(*state));
state->in_fd = open(in_path, O_RDONLY);
if (state->in_fd < 0) {
return -1;
}
state->out_fp = fopen(ASMOUT, "w");
if (state->out_fp == NULL) {
close(state->in_fd);
return -1;
}
if (ptrbox_init(&state->ptrbox) < 0) {
close(state->in_fd);
fclose(state->out_fp);
return -1;
}
if (symbol_table_init(&state->symtab) < 0) {
close(state->in_fd);
fclose(state->out_fp);
ptrbox_destroy(&state->ptrbox);
return -1;
}
if (tokbuf_init(&state->tokbuf) < 0) {
ptrbox_destroy(&state->ptrbox);
fclose(state->out_fp);
symbol_table_destroy(&state->symtab);
close(state->in_fd);
return -1;
}
state->line_num = 1;
return 0;
}
void
rifle_state_destroy(struct rifle_state *state)
{
if (state == NULL) {
return;
}
close(state->in_fd);
state->in_fd = -1;
tokbuf_destroy(&state->tokbuf);
symbol_table_destroy(&state->symtab);
fclose(state->out_fp);
}