runic.h

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/****
* runic.h - API for .runic filetype
*                        covering opening, closing, and
*                        modifying source code files
*                        directly on disk using a tree
*                        format.
****/

// dependencies
#include <stdio.h> // perror, rename, remove
#include <stdlib.h> // exit
#include <stdbool.h> // bool
#include <stddef.h> // size_t
#include <stdint.h> // uintXX_t
#include <string.h> // memcmp, memcpy, strlen
#include <fcntl.h> // open flags
#include <unistd.h> // close, sysconf
#include <sys/stat.h> // struct stat, open, fstat
#include <sys/mman.h> // mmap, munmap, map flags
// see documentation for other necessary dependencies

// preprocessor statements
#ifndef RUNIC_H
#define RUNIC_H
#ifdef __cplusplus
        extern "C" {
#endif

// constants
#define DEFAULT_ROOT 0x15

// enums
enum runic_file_modes {
        READONLY, READWRITE, CREATEWRITE
};

typedef enum runic_obj_ty {
        NODE, ATOM
} runic_obj_ty_t;

// structs
typedef struct runic {
        const char* path;
        int fd;
        struct stat sb;
        int mode;
        uint8_t* base;
} runic_t;

typedef struct runic_obj {
        uint8_t* base;
        uint64_t offset;
} runic_obj_t;

// accessors
//// file
runic_t runic_open(const char* path, int mode);
bool runic_close(runic_t r);
runic_obj_t runic_root(runic_t r);
uint64_t runic_free(runic_t r);
uint64_t runic_remaining(runic_t r, bool silent);

//// node
runic_obj_ty_t runic_obj_ty(runic_obj_t ro);
runic_obj_t runic_node_left(runic_obj_t ro);
runic_obj_t runic_node_right(runic_obj_t ro);

//// atom
size_t runic_atom_size(runic_obj_t ro);
bool runic_atom_read(runic_obj_t ro, char* c);

// mutators
//// file
bool runic_set_root(runic_t* r, runic_obj_t ro);
runic_t runic_shrink(runic_t* r);
runic_obj_t runic_alloc_node(runic_t* r);
runic_obj_t runic_alloc_atom(runic_t* r, size_t sz);
runic_obj_t runic_alloc_atom_str(runic_t* r, const char* value);

//// node
bool runic_node_set_left(runic_obj_t* parent, runic_obj_t child);
bool runic_node_set_right(runic_obj_t* parent, runic_obj_t child);

//// atom
bool runic_atom_write(runic_obj_t* ro, const char* val);

// closing statements
#ifdef __cplusplus
        }
#endif
#endif /* runic.h */