factor file collection out of main.c

This commit is contained in:
Lephenixnoir 2023-08-07 18:27:28 +02:00
parent 4f28a77294
commit eb5b7383f2
Signed by: Lephenixnoir
GPG key ID: 1BBA026E13FC0495
4 changed files with 135 additions and 41 deletions

View file

@ -11,6 +11,7 @@ find_package(Gint 2.9 REQUIRED)
set(SOURCES
src/brk.c
src/document.c
src/file.c
src/main.c
src/parser.c
src/reader.c)

80
src/file.c Normal file
View file

@ -0,0 +1,80 @@
#include "file.h"
#include <stdlib.h>
#include <string.h>
void file_free(struct file const *file)
{
free(file->source);
free(file->str);
document_free(file->doc);
}
struct fileset *fileset_new(void)
{
return calloc(1, sizeof(struct fileset));
}
void fileset_add_file(struct fileset *fs, struct file const *file)
{
int new_count = fs->file_count + 1;
struct file *new_buf = realloc(fs->files, new_count * sizeof *new_buf);
if(!new_buf)
return;
new_buf[new_count - 1] = *file;;
fs->files = new_buf;
fs->file_count = new_count;
}
void fileset_free(struct fileset *fs)
{
for(int i = 0; i < fs->file_count; i++)
file_free(&fs->files[i]);
free(fs->files);
free(fs);
}
//======= Builtin files =======//
#define ENUM_FILES(X) \
X(file_01_suites_tex) \
X(file_02_complexes_tex) \
X(file_03_limites_tex) \
X(file_04_derivation_tex) \
X(file_05_exponentielle_tex) \
X(file_06_logarithme_tex) \
X(file_07_trigo_tex) \
X(file_08_integration_tex) \
X(file_09_geometrie_tex) \
X(file_10_probabilites_tex) \
X(file_11_loisprobas_tex) \
X(file_12_stats_tex) \
X(file_13_arithmetique_tex) \
static void add_builtin(struct fileset *fs,
char const *str, int len, char const *name)
{
struct file file = {
.source = strdup(name),
.str = (char *)str,
.len = len,
.read_only = true,
.doc = NULL,
};
fileset_add_file(fs, &file);
}
void fileset_add_builtins(struct fileset *fs)
{
#define DO(NAME) \
extern char const NAME[]; \
extern char const NAME ## _size; \
add_builtin(fs, NAME, (int)&NAME ## _size, #NAME);
ENUM_FILES(DO)
}
//======= Storage memory files =======//
void fileset_add_root_tex_files(struct fileset *fs)
{
}

42
src/file.h Normal file
View file

@ -0,0 +1,42 @@
#pragma once
#include "document.h"
#include <stddef.h>
#include <stdbool.h>
struct file {
/* Filepath or builtin filename (owned) */
char *source;
/* Contents (owned unless read_only is set) */
char *str;
/* Length of contents */
int len;
/* Whether str is a read-only buffer from the add-in itself */
bool read_only;
/* Associated compiled document */
struct document *doc;
};
/* Just a vector. Yeah, this is boring. */
struct fileset {
int file_count;
struct file *files;
};
//======= Basic functions for manipulating files and filesets =======//
void file_free(struct file const *file);
struct fileset *fileset_new(void);
/* Add a file to the set. The file structure is moved and the set becomes its
owner. *file should no longer be used after this call. */
void fileset_add_file(struct fileset *fs, struct file const *file);
void fileset_free(struct fileset *fs);
//======= Functions for collecting files =======//
void fileset_add_builtins(struct fileset *fs);
void fileset_add_root_tex_files(struct fileset *fs);

View file

@ -4,37 +4,7 @@
#include <TeX/env.h>
#include "document.h"
#include "reader.h"
#define ENUM_FILES(X) \
X(file_01_suites_tex) \
X(file_02_complexes_tex) \
X(file_03_limites_tex) \
X(file_04_derivation_tex) \
X(file_05_exponentielle_tex) \
X(file_06_logarithme_tex) \
X(file_07_trigo_tex) \
X(file_08_integration_tex) \
X(file_09_geometrie_tex) \
X(file_10_probabilites_tex) \
X(file_11_loisprobas_tex) \
X(file_12_stats_tex) \
X(file_13_arithmetique_tex) \
#define EXTERN_DECLARE(NAME) \
extern char const NAME[]; \
extern int const NAME ## _size;
ENUM_FILES(EXTERN_DECLARE)
struct source {
char const *str;
int len;
};
#define INITIALIZE(NAME) { NAME, (int)&NAME ## _size },
static struct source const files[] = { ENUM_FILES(INITIALIZE) };
#define COUNT(NAME) +1
static int file_count = 0 ENUM_FILES(COUNT);
#include "file.h"
static void render_document_tile(struct document const *doc, int where,
bool selected)
@ -130,12 +100,12 @@ int main(void)
TeX_intf_size(texsize);
TeX_intf_text(textext);
// TODO: Also load files from storage memory
struct document *documents[13] = { 0 };
int document_count = 13;
struct fileset *fs = fileset_new();
fileset_add_builtins(fs);
for(int i = 0; i < file_count; i++) {
documents[i] = document_parse(files[i].str, files[i].len);
for(int i = 0; i < fs->file_count; i++) {
struct file *f = &fs->files[i];
f->doc = document_parse(f->str, f->len);
}
/* Cursor for selecting stuff in the menu */
@ -145,7 +115,7 @@ int main(void)
/* Main menu loop */
while(1) {
struct document const *doc = documents[c_document];
struct document const *doc = fs->files[c_document].doc;
dclear(C_WHITE);
drect(4, 4, DWIDTH-1-4, 44, C_BLACK);
@ -155,16 +125,16 @@ int main(void)
/* Tiles */
for(int d = -2; d <= +2; d++) {
if(c_document + d < 0 || c_document + d >= document_count)
if(c_document + d < 0 || c_document + d >= fs->file_count)
continue;
render_document_tile(documents[c_document + d], d,
render_document_tile(fs->files[c_document + d].doc, d,
!d && c_mode == C_DOCUMENT);
}
if(c_document > 2) {
dtext_opt(20, 87, C_BLACK, C_NONE, DTEXT_CENTER, DTEXT_MIDDLE,
"<", -1);
}
if(c_document < document_count - 3) {
if(c_document < fs->file_count - 3) {
dtext_opt(DWIDTH-20, 87, C_BLACK, C_NONE, DTEXT_CENTER,
DTEXT_MIDDLE, ">", -1);
}
@ -209,7 +179,7 @@ int main(void)
c_page = 0;
}
if(key == KEY_RIGHT && c_mode == C_DOCUMENT
&& c_document+1 < document_count) {
&& c_document+1 < fs->file_count) {
c_document++;
c_page = 0;
}
@ -238,5 +208,6 @@ int main(void)
}
}
fileset_free(fs);
return 1;
}