-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutils.h
70 lines (56 loc) · 1.97 KB
/
utils.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
/*
* Audio Overload SDK
*
* Random utility functions
*
* Author: Nmlgc
*/
#ifndef UTILS_H
#define UTILS_H
/// Hash table
/// ----------
// This hash table maps variable-size keys to constant-size values. Before
// calling hashtable_get(), the hash table has to be initialized to the
// desired data size by calling hashtable_init().
typedef struct {
void *buf;
size_t len;
} blob_t;
typedef struct hashtable_bucket {
struct hashtable_bucket *next;
blob_t key;
uint8 data[0];
} hashtable_bucket_t;
typedef struct {
unsigned int i;
hashtable_bucket_t *bucket;
} hashtable_iterator_t;
typedef struct {
size_t data_size;
hashtable_bucket_t *buckets;
} hashtable_t;
// Flags used for hash table lookup
typedef enum {
HT_CREATE = 0x1, // create nonexisting entries
HT_CASE_INSENSITIVE = 0x2, // case-insensitive key lookup
} hashtable_flags_t;
// Initializes the hash table with the given data size.
ao_bool hashtable_init(hashtable_t *table, size_t data_size);
// Looks up the entry in [table] with the given [key] and the given [flags].
// Returns a writable pointer to the entry data, or NULL if the entry doesn't
// exist and the HT_CREATE flag was not given.
void* hashtable_get(hashtable_t *table, const blob_t *key, hashtable_flags_t flags);
// Iterates over all entries in [table], using [iter] to store the iteration
// state. [iter] should be cleared to 0 before the first call. Returns the
// current data entry as well as the [key] if that pointer is not NULL, or
// NULL if the end of [table] has been reached.
void* hashtable_iterate(blob_t **key, hashtable_t *table, hashtable_iterator_t *iter);
// Returns the number of entries in [table].
unsigned int hashtable_length(hashtable_t *table);
// Frees the hash table, assuming that any allocated blocks within the data
// already have been freed.
void hashtable_free(hashtable_t *table);
/// ----------
// Opens "[fn].[suffix]" for writing.
FILE* fopen_derivative(const char *fn, const char *suffix);
#endif /* UTILS_H */