feat(file): added empty file creation and slicing a file

This commit is contained in:
lisk77 2025-08-17 17:07:16 +02:00
parent 4e4e7be60e
commit e8338d011d
2 changed files with 24 additions and 0 deletions

View file

@ -1,5 +1,13 @@
#include "file.h"
File* new_empty_file() {
File* file = calloc(1, sizeof(File));
file->content = NULL;
file->lines = 0;
return file;
}
File* new_file(const char* path) {
if (!path) return NULL;
FILE* f = fopen(path, "rb");
@ -45,3 +53,17 @@ File* new_file(const char* path) {
return file;
}
File* slice_file(File* original, uint64_t start, uint64_t end) {
if (!original || (end < start) || (end > original->lines)) return NULL;
File* slice = calloc(1, sizeof(File));
if (!slice) return NULL;
uint64_t lines = end-start;
if (!original->content || lines == 0) return NULL;
slice->content = original->content + start;
slice->lines = lines;
return slice;
}