First steps toward a C library for Preserves

This commit is contained in:
Tony Garnock-Jones 2022-01-14 00:27:18 +01:00
parent 15765126e2
commit 9a718548c0
4 changed files with 1158 additions and 0 deletions

2
implementations/c/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
m.output.txt
m

View File

@ -0,0 +1,8 @@
m: main.c preserves.h
gcc -Wall -Wextra -Werror -g3 -o $@ main.c
go: m
cat ../../tests/samples.bin | ./m | tee m.output.txt
clean:
rm -f m

63
implementations/c/main.c Normal file
View File

@ -0,0 +1,63 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#define PRESERVES_IMPLEMENTATION
#include "preserves.h"
int main(__attribute__ ((unused)) int argc,
__attribute__ ((unused)) char const * const argv[])
{
preserves_bytes_t input = preserves_create_bytes();
{
preserves_bytes_t chunk = preserves_create_bytes();
if (preserves_resize_bytes(&chunk, 131072) == -1) {
perror("allocating chunk");
return EXIT_FAILURE;
}
while (true) {
size_t count = fread(chunk.ptr, 1, chunk.len, stdin);
if (count == 0) {
if (ferror(stdin)) {
perror("reading");
return EXIT_FAILURE;
}
break;
}
if (preserves_extend_bytes(&input, preserves_bytes_subsequence(&chunk, 0, count)) == -1) {
perror("appending");
return EXIT_FAILURE;
}
}
preserves_free_bytes(&chunk);
}
{
preserves_reader_t reader = preserves_create_reader();
preserves_reader_result_t result = preserves_read_binary(&reader, &input, 1);
if (result.index == NULL) {
perror("parsing");
return EXIT_FAILURE;
}
printf("Size of index: %lu bytes; %lu entries\n",
reader.index_pos * sizeof(preserves_index_entry_t),
reader.index_pos);
if (true) {
for (preserves_index_entry_t *i = result.index; i != result.end_marker; i++) {
preserves_dump_index_entry(stdout, &reader.input, i, true);
}
preserves_dump_index_entry(stdout, &reader.input, result.end_marker, true);
}
preserves_free_reader(&reader);
}
preserves_free_bytes(&input);
return EXIT_SUCCESS;
}

File diff suppressed because it is too large Load Diff