feat(file): added empty file creation and slicing a file
This commit is contained in:
parent
4e4e7be60e
commit
e8338d011d
2 changed files with 24 additions and 0 deletions
|
|
@ -11,6 +11,8 @@ typedef struct {
|
||||||
uint64_t lines;
|
uint64_t lines;
|
||||||
} File;
|
} File;
|
||||||
|
|
||||||
|
File* new_empty_file();
|
||||||
File* new_file(const char*);
|
File* new_file(const char*);
|
||||||
|
File* slice_file(File*, uint64_t, uint64_t);
|
||||||
|
|
||||||
#endif // FILE_H
|
#endif // FILE_H
|
||||||
|
|
|
||||||
22
src/file.c
22
src/file.c
|
|
@ -1,5 +1,13 @@
|
||||||
#include "file.h"
|
#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) {
|
File* new_file(const char* path) {
|
||||||
if (!path) return NULL;
|
if (!path) return NULL;
|
||||||
FILE* f = fopen(path, "rb");
|
FILE* f = fopen(path, "rb");
|
||||||
|
|
@ -45,3 +53,17 @@ File* new_file(const char* path) {
|
||||||
|
|
||||||
return file;
|
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;
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue