tcc-stupidos/libtcc/io.c
2025-02-14 17:47:45 +01:00

53 lines
1.1 KiB
C

#include "libtcc.h"
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */
#include <tcc.h>
#include <tcc/memory.h>
#include <tcc/io.h>
#include "utils/string.h"
#include "cc/cc.h"
void
tcc_open_bf(TCCState *s1, const char *filename, int initlen)
{
BufferedFile *bf;
int buflen;
buflen = initlen ? initlen : IO_BUF_SIZE;
bf = tcc_mallocz(sizeof(BufferedFile) + buflen);
bf->buf_ptr = bf->buffer;
bf->buf_end = bf->buffer + initlen;
bf->buf_end[0] = CH_EOB; /* put eob symbol */
pstrcpy(bf->filename, sizeof(bf->filename), filename);
bf->true_filename = bf->filename;
bf->line_num = 1;
bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
bf->fd = -1;
bf->prev = file;
bf->prev_tok_flags = tok_flags;
file = bf;
tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
}
void tcc_close(void)
{
TCCState *s1 = tcc_state;
BufferedFile *bf = file;
if (bf->fd > 0)
{
close(bf->fd);
total_lines += bf->line_num - 1;
}
if (bf->true_filename != bf->filename)
{
tcc_free(bf->true_filename);
}
file = bf->prev;
tok_flags = bf->prev_tok_flags;
tcc_free(bf);
}