]> pd.if.org Git - lice/blob - util.h
autocommit for files dated 2014-11-17 20:15:26
[lice] / util.h
1 #ifndef GMCC_UTIL_HDR
2 #define GMCC_UTIL_HDR
3 #include <stdbool.h>
4 #include <stdio.h>
5
6 #include "list.h"
7
8 /*
9  * Type: string_t
10  *  A type capable of representing a self-resizing string with
11  *  O(1) strlen.
12  */
13 typedef struct string_s string_t;
14
15 /*
16  * Function: string_create
17  *  Create a string object
18  */
19 string_t *string_create(void);
20
21 /*
22  * Function: string_buffer
23  *  Return the raw buffer of a string object
24  */
25 char *string_buffer(string_t *string);
26
27 /*
28  * Function: string_cat
29  *  Append a character to a string object
30  */
31 void string_cat(string_t *string, char ch);
32
33 /*
34  * Function: string_catf
35  *  Append a formatted string to a string object
36  */
37 void string_catf(string_t *string, const char *fmt, ...);
38
39 /*
40  * Function: string_quote
41  *  Escape a string's quotes
42  */
43 char *string_quote(char *p);
44
45 /*
46  * Function: string_length
47  *  Get the length of the given string
48  */
49 size_t string_length(string_t *string);
50
51 /*
52  * Type: table_t
53  *  A key value associative table
54  */
55 typedef struct table_s table_t;
56
57 struct table_s {
58     list_t  *list;
59     table_t *parent;
60 };
61
62 /*
63  * Function: table_create
64  *  Creates a table_t object
65  */
66 void *table_create(void *parent);
67
68 /*
69  * Funciton: table_find
70  *  Searches for a given value in the table based on the
71  *  key associated with it.
72  */
73 void *table_find(table_t *table, const char *key);
74
75 /*
76  * Function: table_insert
77  *  Inserts a value for the given key as an entry in the
78  *  table.
79  */
80 void  table_insert(table_t *table, char *key, void *value);
81
82 /*
83  * Function: table_parent
84  *  Returns the parent opaque object for the given table to
85  *  be used as the argument to a new table.
86  */
87 void *table_parent(table_t *table);
88
89 /*
90  * Function: table_values
91  *  Generates a list of all the values in the table, useful for
92  *  iterating over the values.
93  */
94 list_t *table_values(table_t *table);
95
96 /*
97  * Function: table_keys
98  *  Generate a list of all the keys in the table, useful for
99  *  iteration over the keys.
100  */
101 list_t *table_keys(table_t *table);
102
103 /*
104  * Macro: SENTINEL_TABLE
105  *  Initialize an empty table in place
106  */
107 #define SENTINEL_TABLE ((table_t) { \
108     .list   = &SENTINEL_LIST,       \
109     .parent = NULL                  \
110 })
111
112
113 #define MIN(A, B) (((A) < (B)) ? (A) : (B))
114 #define MAX(A, B) (((A) > (B)) ? (A) : (B))
115
116 /*
117  * Function: memory_allocate
118  * Allocate some memory
119  */
120 void *memory_allocate(size_t bytes);
121
122 /*
123  * Structure: pair_t
124  *  A class container describing a pair
125  */
126 typedef struct {
127     /* Variable: first */
128     void *first;
129     /* Variable: second */
130     void *second;
131 } pair_t;
132
133 /*
134  * Function: pair_create
135  *  Used to create a <pair_t>.
136  *
137  * Parameters:
138  *  first  - Pointer to first object
139  *  second - Pointer to second object
140  *
141  * Returns:
142  *  A pointer to a constructed <pair_t> containing first and last
143  *  pointers which point to the same address as the ones supplied
144  *  as parameters to this function.
145  */
146 pair_t *pair_create(void *first, void *last);
147
148 /* String utilities */
149 int strcasecmp(const char *s1, const char *s2);
150 int strncasecmp(const char *s1, const char *s2, size_t n);
151 size_t getline(char **line, size_t *n, FILE *stream);
152
153 /*
154  * Macro: bool_stringa
155  *  Returns a "true" or "false" for an expression that evaluates to a
156  *  boolean representation enforced with cast-to-bool `!!`
157  */
158 #define bool_string(BOOL) \
159     ((!!(BOOL)) ? "true" : "false")
160
161 #endif