-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshd-lib.h
More file actions
94 lines (84 loc) · 2.7 KB
/
Copy pathshd-lib.h
File metadata and controls
94 lines (84 loc) · 2.7 KB
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
92
93
94
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
typedef struct {
size_t len;
char content[];
} page_t;
typedef struct {
size_t len;
page_t **pages;
} notebook_t;
#define MAX_PAGES 0x100
/**
* Create an empty notebook with space for a fixed number of pages.
* All pages are initially unallocated (invalid).
*
* @param len The number of pages in the notebook. Must be less than MAX_PAGES.
*
* @return A pointer to the newly created notebook if successful, or NULL.
*/
notebook_t *create_notebook(size_t len);
/**
* Free all memory associated with a notebook, including its pages.
*
* @param nb The notebook to destroy. Assume non-null.
*/
void destroy_notebook(notebook_t *nb);
/**
* Look up a page by index and return a pointer to its stored content.
*
* @param nb Notebook object. Assume non-null.
* @param page_nr The page # to read.
*
* @return A pointer to the page's content if the page exists, else NULL.
* A non-existent page is considered a failure case.
*/
char *read_page(notebook_t *nb, int page_nr);
/**
* Create or replace a page's content with the provided message.
*
* @param nb Notebook object. Assume non-null.
* @param page_nr The page # to write to.
* @param msg The message to write. Assume non-null.
*
* @return 0 on success, -1 otherwise.
* If a page_nr already exists, overwrite it with this new msg.
* If page_nr is in bounds and does not exist, create a new page with the provided msg.
*/
int write_page(notebook_t *nb, int page_nr, const char *msg);
/**
* Increase the allocated size of an existing page by a byte amount.
*
* @param nb Notebook object. Assume non-null.
* @param page_nr The page to grow.
* @param msg The amount to grow the page by in bytes.
*
* @return 0 on success, -1 otherwise
* A non-existent page is considered a failure case.
* A new size greater than the maximum unsigned 64-bit value is a failure case.
*/
int grow_page(notebook_t *nb, int page_nr, size_t amount);
/**
* Remove a page from the notebook and release its storage.
*
* @param nb Notebook object. Assume non-null.
* @param page_nr The page to delete.
*
* @return 0 on success, -1 otherwise.
* A non-existent page is considered a failure case.
*/
int delete_page(notebook_t *nb, int page_nr);
/**
* Append additional text to the end of an existing page's content.
*
* @param nb Notebook object. Assume non-null.
* @param page_nr The page to append to.
* @param msg The message to append. Assume non-null.
*
* @return 0 on success, -1 otherwise.
* A non-existent page is considered a failure case.
*/
int append_page(notebook_t *nb, int page_nr, const char *msg);