misc.h 3.1 KB
Newer Older
Damien's avatar
Damien committed
1
2
3
4
5
6
7
// a mini library of useful types and functions

#ifndef _INCLUDED_MINILIB_H
#define _INCLUDED_MINILIB_H

/** types *******************************************************/

8
#include <stdbool.h>
Damien's avatar
Damien committed
9
10
11
12
13
14

typedef unsigned char byte;
typedef unsigned int uint;

/** memomry allocation ******************************************/

15
16
// TODO make a lazy m_renew that can increase by a smaller amount than requested (but by at least 1 more element)

Damien's avatar
Damien committed
17
18
#define m_new(type, num) ((type*)(m_malloc(sizeof(type) * (num))))
#define m_new0(type, num) ((type*)(m_malloc0(sizeof(type) * (num))))
19
20
#define m_new_obj(type) (m_new(type, 1))
#define m_new_obj_var(obj_type, var_type, var_num) ((obj_type*)m_malloc(sizeof(obj_type) + sizeof(var_type) * (var_num)))
21
22
23
#define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
#define m_del(type, ptr, num) m_free(ptr, sizeof(type) * (num))
#define m_del_obj(type, ptr) (m_del(type, ptr, 1))
Damien's avatar
Damien committed
24
25
26

void *m_malloc(int num_bytes);
void *m_malloc0(int num_bytes);
27
28
void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes);
void m_free(void *ptr, int num_bytes);
Damien's avatar
Damien committed
29

30
int m_get_total_bytes_allocated(void);
31
int m_get_current_bytes_allocated(void);
32
int m_get_peak_bytes_allocated(void);
Damien's avatar
Damien committed
33
34
35
36
37

/** unichar / UTF-8 *********************************************/

typedef int unichar; // TODO

38
39
unichar utf8_get_char(const char *s);
char *utf8_next_char(const char *s);
Damien's avatar
Damien committed
40

41
42
43
44
bool unichar_isspace(unichar c);
bool unichar_isalpha(unichar c);
bool unichar_isprint(unichar c);
bool unichar_isdigit(unichar c);
Damien's avatar
Damien committed
45
46
47
48
49
50
51
52
53

/** string ******************************************************/

/*
#define streq(s1, s2) (strcmp((s1), (s2)) == 0)
*/

/** variable string *********************************************/

Damien's avatar
Damien committed
54
55
56
57
typedef struct _vstr_t {
    int alloc;
    int len;
    char *buf;
58
    bool had_error;
Damien's avatar
Damien committed
59
60
} vstr_t;

61
void vstr_init(vstr_t *vstr, int alloc);
Damien's avatar
Damien committed
62
void vstr_clear(vstr_t *vstr);
63
vstr_t *vstr_new(void);
64
vstr_t *vstr_new_size(int alloc);
Damien's avatar
Damien committed
65
66
void vstr_free(vstr_t *vstr);
void vstr_reset(vstr_t *vstr);
67
bool vstr_had_error(vstr_t *vstr);
Damien's avatar
Damien committed
68
69
70
char *vstr_str(vstr_t *vstr);
int vstr_len(vstr_t *vstr);
void vstr_hint_size(vstr_t *vstr, int size);
71
72
73
char  *vstr_extend(vstr_t *vstr, int size);
bool vstr_set_size(vstr_t *vstr, int size);
bool vstr_shrink(vstr_t *vstr);
Damien's avatar
Damien committed
74
char *vstr_add_len(vstr_t *vstr, int len);
Damien's avatar
Damien committed
75
76
void vstr_add_byte(vstr_t *vstr, byte v);
void vstr_add_char(vstr_t *vstr, unichar chr);
Damien's avatar
Damien committed
77
78
void vstr_add_str(vstr_t *vstr, const char *str);
void vstr_add_strn(vstr_t *vstr, const char *str, int len);
Damien's avatar
Damien committed
79
80
//void vstr_add_le16(vstr_t *vstr, unsigned short v);
//void vstr_add_le32(vstr_t *vstr, unsigned int v);
Damien's avatar
Damien committed
81
void vstr_cut_tail(vstr_t *vstr, int len);
82
void vstr_printf(vstr_t *vstr, const char *fmt, ...);
83
84

#ifdef va_start
85
void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap);
86
#endif
Damien's avatar
Damien committed
87
88
89
90
91

/** unique string ***********************************************/

typedef unsigned int qstr;

92
void qstr_init(void);
Damien's avatar
Damien committed
93
qstr qstr_from_str_static(const char *str);
94
qstr qstr_from_str_take(char *str, int alloc_len);
Damien's avatar
Damien committed
95
96
97
98
qstr qstr_from_strn_copy(const char *str, int len);
const char* qstr_str(qstr qstr);

#endif // _INCLUDED_MINILIB_H