Explorar el Código

initial commit

master
Martin Dørum hace 3 años
commit
fa204ced8b
Se han modificado 4 ficheros con 179 adiciones y 0 borrados
  1. 3
    0
      .gitignore
  2. 1
    0
      build.bx
  3. 110
    0
      include/lang2/io.h
  4. 65
    0
      src/io.c

+ 3
- 0
.gitignore Ver fichero

@@ -0,0 +1,3 @@
/.clangd
/bx-out
/compile_commands.json

+ 1
- 0
build.bx Ver fichero

@@ -0,0 +1 @@
includes := include/lang2

+ 110
- 0
include/lang2/io.h Ver fichero

@@ -0,0 +1,110 @@
#ifndef L2_IO_H
#define L2_IO_H

#include <stdio.h>
#include <string.h>

#define L2_IO_BUFSIZ 1024

struct l2_io_reader {
size_t (*read)(struct l2_io_reader *self, char *buf, size_t len);
};

struct l2_io_writer {
void (*write)(struct l2_io_writer *self, const char *buf, size_t len);
};

struct l2_bufio_reader {
size_t bufsiz;
size_t len;
size_t idx;
char buf[];
};

void l2_bufio_shift(struct l2_bufio_reader *b, struct l2_io_reader *r);
int l2_bufio_shift_peek(struct l2_bufio_reader *b, struct l2_io_reader *r, size_t count);
int l2_bufio_shift_get(struct l2_bufio_reader *b, struct l2_io_reader *r);
int l2_bufio_peek(struct l2_bufio_reader *b, struct l2_io_reader *r, size_t count);
int l2_bufio_get(struct l2_bufio_reader *b, struct l2_io_reader *r);

struct l2_bufio_writer {
size_t bufsiz;
size_t idx;
char buf[];
};

void l2_bufio_flush(struct l2_bufio_writer *b, struct l2_io_writer *w);
void l2_bufio_put(struct l2_bufio_writer *b, struct l2_io_writer *w, char ch);
void l2_bufio_put_n(struct l2_bufio_writer *b, struct l2_io_writer *w, const char *ptr, size_t len);

/*
* Useful readers and writers
*/

struct l2_io_mem_reader {
struct l2_io_reader r;
size_t len;
size_t idx;
const char *mem;
};
size_t l2_io_mem_read(struct l2_io_reader *self, char *buf, size_t len);

struct l2_io_file_reader {
struct l2_io_reader r;
FILE *f;
};
size_t l2_io_file_read(struct l2_io_reader *self, char *buf, size_t len);

struct l2_io_mem_writer {
struct l2_io_writer w;
size_t len;
char *mem;
};
void l2_io_mem_write(struct l2_io_writer *self, const char *buf, size_t len);

struct l2_io_file_writer {
struct l2_io_writer w;
FILE *f;
};
void l2_io_file_write(struct l2_io_writer *self, const char *buf, size_t len);

/*
* Defined in the header to let the compiler inline
*/

inline int l2_bufio_peek(struct l2_bufio_reader *b, struct l2_io_reader *r, size_t count) {\
size_t offset = count - 1;
if (b->idx + offset < b->len) {
return b->buf[b->idx + offset];
} else {
return l2_bufio_shift_peek(b, r, count);
}
}

inline int l2_bufio_get(struct l2_bufio_reader *b, struct l2_io_reader *r) {
if (b->idx < b->bufsiz) {
return b->buf[b->idx++];
} else {
return l2_bufio_shift_get(b, r);
}
}

inline void l2_bufio_put(struct l2_bufio_writer *b, struct l2_io_writer *w, char ch) {
if (b->idx >= b->bufsiz) {
l2_bufio_flush(b, w);
}
b->buf[b->idx++] = ch;
}

inline void l2_bufio_put_n(struct l2_bufio_writer *b, struct l2_io_writer *w, const char *ptr, size_t len) {
size_t freespace = b->bufsiz - b->idx;
if (len < freespace) {
memcpy(b->buf + b->idx, ptr, len);
b->idx += len;
} else {
l2_bufio_flush(b, w);
w->write(w, ptr, len);
}
}

#endif

+ 65
- 0
src/io.c Ver fichero

@@ -0,0 +1,65 @@
#include "io.h"

#include <stdlib.h>

void l2_bufio_shift(struct l2_bufio_reader *b, struct l2_io_reader *r) {
if (b->idx > 0) {
b->len -= b->idx;
memmove(b->buf, b->buf + b->idx, b->len);
b->len += r->read(r, b->buf + b->len, b->bufsiz - b->len);
b->idx = 0;
}
}

int l2_bufio_shift_peek(struct l2_bufio_reader *b, struct l2_io_reader *r, size_t count) {
size_t offset = count - 1;
l2_bufio_shift(b, r);
if (b->len <= offset) {
return EOF;
}

return b->buf[offset];
}

int l2_bufio_shift_get(struct l2_bufio_reader *b, struct l2_io_reader *r) {
l2_bufio_shift(b, r);
if (b->len == 0) {
return EOF;
}

return b->buf[b->idx++];
}

void l2_bufio_flush(struct l2_bufio_writer *b, struct l2_io_writer *w) {
w->write(w, b->buf, b->idx);
b->idx = 0;
}

size_t l2_io_mem_read(struct l2_io_reader *self, char *buf, size_t len) {
struct l2_io_mem_reader *r = (struct l2_io_mem_reader *)self;
if (len >= r->len - r->idx) {
len = r->len - r->idx;
}

memcpy(buf, r->mem + r->idx, len);
r->idx += len;
return len;
}

size_t l2_io_file_read(struct l2_io_reader *self, char *buf, size_t len) {
struct l2_io_file_reader *r = (struct l2_io_file_reader *)self;
return fread(buf, 1, len, r->f);
}

void l2_io_mem_write(struct l2_io_writer *self, const char *buf, size_t len) {
struct l2_io_mem_writer *w = (struct l2_io_mem_writer *)self;
size_t idx = w->len;
w->len += len;
w->mem = realloc(w->mem, w->len);
memcpy(w->mem + idx, buf, len);
}

void l2_io_file_write(struct l2_io_writer *self, const char *buf, size_t len) {
struct l2_io_file_writer *w = (struct l2_io_file_writer *)self;
fwrite(buf, 1, len, w->f);
}

Cargando…
Cancelar
Guardar