Files
Cescal/core/state.c
T

53 lines
976 B
C
Raw Normal View History

2026-05-23 03:00:43 -04:00
/*
* Copyright (c) 2026, Chloe M.
* Provided under the BSD-3 clause
*/
2026-05-23 01:28:05 -04:00
#include <stdint.h>
#include <stddef.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "cescal/state.h"
int
state_init(struct cescal_state *state, const char *pathname)
{
if (state == NULL || pathname == NULL) {
errno = EINVAL;
return -1;
}
2026-05-23 18:08:17 -04:00
memset(state, 0, sizeof(*state));
2026-05-23 01:28:05 -04:00
state->in_fd = open(pathname, O_RDONLY);
if (state->in_fd < 0) {
return -1;
}
if (readbuf_init(&state->rb) < 0) {
close(state->in_fd);
return -1;
}
2026-05-23 02:57:50 -04:00
if (tokbuf_init(&state->tokbuf) < 0) {
close(state->in_fd);
return -1;
}
2026-05-23 06:03:23 -04:00
if (ptrbox_init(&state->ptrbox) < 0) {
close(state->in_fd);
return -1;
}
2026-05-23 01:28:05 -04:00
return 0;
}
void
state_close(struct cescal_state *state)
{
close(state->in_fd);
state->in_fd = -1;
2026-05-23 06:03:23 -04:00
ptrbox_destroy(&state->ptrbox);
tokbuf_destroy(&state->tokbuf);
2026-05-23 01:28:05 -04:00
}