add basic read syscall wrappers for terminfo db
This commit is contained in:
parent
87f06f7fb6
commit
2033721c22
2 changed files with 57 additions and 0 deletions
51
src/read.c
Normal file
51
src/read.c
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "read.h"
|
||||
|
||||
static struct ti_db {
|
||||
const char *restrict const file;
|
||||
// const FILE *restrict const stream;
|
||||
const long size;
|
||||
};
|
||||
|
||||
/*
|
||||
* Returns 0 on success, else 1 on failure and sets errno.
|
||||
*/
|
||||
static inline int fsize(FILE *restrict const stream, long *restrict const size,
|
||||
const bool offset) {
|
||||
long initpos = (offset) ? ftell(stream) : 0L;
|
||||
|
||||
if (fseek(stream, 0L, SEEK_END))
|
||||
return 1;
|
||||
*size = ftell(stream);
|
||||
if (offset && fseek(stream, initpos, SEEK_SET))
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Read the entire file at once to avoid half a million individual read syscalls.
|
||||
*/
|
||||
static inline int freaddb(const char *restrict const file, char *restrict *restrict const db,
|
||||
long *restrict const size) {
|
||||
FILE *restrict const stream = fopen(file, "rb");
|
||||
if (fsize(stream, size, false))
|
||||
return 1;
|
||||
|
||||
*db = (char*)malloc(*size);
|
||||
if (fread(*db, *size, 1UL, stream) != *size)
|
||||
return 2;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
*/
|
||||
static inline int dbread(char *restrict const db, void *buf, const size_t size) {
|
||||
|
||||
}
|
||||
|
||||
int tireadi16() {}
|
||||
6
src/read.h
Normal file
6
src/read.h
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef CURSETREE_READ_H
|
||||
#define CURSETREE_READ_H
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#endif /* CURSETREE_READ_H */
|
||||
Loading…
Add table
Add a link
Reference in a new issue