blob: 0fad9459f15241c0a351c15f614b6b920265e463 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001// a mini library of useful types and functions
2
3#ifndef _INCLUDED_MINILIB_H
4#define _INCLUDED_MINILIB_H
5
6/** types *******************************************************/
7
ian-v5fd8fd22014-01-06 13:51:53 -08008#include <stdbool.h>
Damien429d7192013-10-04 19:53:11 +01009
10typedef unsigned char byte;
11typedef unsigned int uint;
12
Paul Sokolovsky6e6b8882014-02-05 00:44:55 +020013/** generic ops *************************************************/
14
15#define MIN(x, y) ((x) < (y) ? (x) : (y))
16#define MAX(x, y) ((x) > (y) ? (x) : (y))
17
Damien429d7192013-10-04 19:53:11 +010018/** memomry allocation ******************************************/
19
Damien732407f2013-12-29 19:33:23 +000020// TODO make a lazy m_renew that can increase by a smaller amount than requested (but by at least 1 more element)
21
Damien429d7192013-10-04 19:53:11 +010022#define m_new(type, num) ((type*)(m_malloc(sizeof(type) * (num))))
23#define m_new0(type, num) ((type*)(m_malloc0(sizeof(type) * (num))))
Damiend99b0522013-12-21 18:17:45 +000024#define m_new_obj(type) (m_new(type, 1))
25#define m_new_obj_var(obj_type, var_type, var_num) ((obj_type*)m_malloc(sizeof(obj_type) + sizeof(var_type) * (var_num)))
Damien732407f2013-12-29 19:33:23 +000026#define m_renew(type, ptr, old_num, new_num) ((type*)(m_realloc((ptr), sizeof(type) * (old_num), sizeof(type) * (new_num))))
27#define m_del(type, ptr, num) m_free(ptr, sizeof(type) * (num))
28#define m_del_obj(type, ptr) (m_del(type, ptr, 1))
John R. Lenton07205ec2014-01-13 02:31:00 +000029#define m_del_var(obj_type, var_type, var_num, ptr) (m_free(ptr, sizeof(obj_type) + sizeof(var_type) * (var_num)))
Damien429d7192013-10-04 19:53:11 +010030
Damien429d7192013-10-04 19:53:11 +010031void *m_malloc(int num_bytes);
32void *m_malloc0(int num_bytes);
Damien732407f2013-12-29 19:33:23 +000033void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes);
34void m_free(void *ptr, int num_bytes);
Damien429d7192013-10-04 19:53:11 +010035
Damien8b3a7c22013-10-23 20:20:17 +010036int m_get_total_bytes_allocated(void);
Paul Sokolovsky02de0c52014-01-01 23:15:47 +020037int m_get_current_bytes_allocated(void);
Paul Sokolovsky780f5552014-01-01 23:42:21 +020038int m_get_peak_bytes_allocated(void);
Damien429d7192013-10-04 19:53:11 +010039
40/** unichar / UTF-8 *********************************************/
41
42typedef int unichar; // TODO
43
Damien George8cc96a32013-12-30 18:23:50 +000044unichar utf8_get_char(const char *s);
45char *utf8_next_char(const char *s);
Damien429d7192013-10-04 19:53:11 +010046
ian-v5fd8fd22014-01-06 13:51:53 -080047bool unichar_isspace(unichar c);
48bool unichar_isalpha(unichar c);
49bool unichar_isprint(unichar c);
50bool unichar_isdigit(unichar c);
Paul Sokolovsky0b7184d2014-01-22 22:40:02 +020051bool unichar_isxdigit(unichar c);
Damien429d7192013-10-04 19:53:11 +010052
53/** string ******************************************************/
54
55/*
56#define streq(s1, s2) (strcmp((s1), (s2)) == 0)
57*/
58
Damien George5573f9f2014-01-15 22:58:39 +000059long strtonum(const char *restrict s, int base);
60
Damien429d7192013-10-04 19:53:11 +010061/** variable string *********************************************/
62
Damien68f59a92013-10-20 14:39:58 +010063typedef struct _vstr_t {
64 int alloc;
65 int len;
66 char *buf;
ian-v5fd8fd22014-01-06 13:51:53 -080067 bool had_error;
Damien68f59a92013-10-20 14:39:58 +010068} vstr_t;
Damien429d7192013-10-04 19:53:11 +010069
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020070void vstr_init(vstr_t *vstr, int alloc);
Damien68f59a92013-10-20 14:39:58 +010071void vstr_clear(vstr_t *vstr);
Damien8b3a7c22013-10-23 20:20:17 +010072vstr_t *vstr_new(void);
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020073vstr_t *vstr_new_size(int alloc);
Damien429d7192013-10-04 19:53:11 +010074void vstr_free(vstr_t *vstr);
75void vstr_reset(vstr_t *vstr);
ian-v5fd8fd22014-01-06 13:51:53 -080076bool vstr_had_error(vstr_t *vstr);
Damien429d7192013-10-04 19:53:11 +010077char *vstr_str(vstr_t *vstr);
78int vstr_len(vstr_t *vstr);
79void vstr_hint_size(vstr_t *vstr, int size);
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020080char *vstr_extend(vstr_t *vstr, int size);
81bool vstr_set_size(vstr_t *vstr, int size);
82bool vstr_shrink(vstr_t *vstr);
Damien429d7192013-10-04 19:53:11 +010083char *vstr_add_len(vstr_t *vstr, int len);
Damien68f59a92013-10-20 14:39:58 +010084void vstr_add_byte(vstr_t *vstr, byte v);
85void vstr_add_char(vstr_t *vstr, unichar chr);
Damien429d7192013-10-04 19:53:11 +010086void vstr_add_str(vstr_t *vstr, const char *str);
87void vstr_add_strn(vstr_t *vstr, const char *str, int len);
Damien68f59a92013-10-20 14:39:58 +010088//void vstr_add_le16(vstr_t *vstr, unsigned short v);
89//void vstr_add_le32(vstr_t *vstr, unsigned int v);
Damien429d7192013-10-04 19:53:11 +010090void vstr_cut_tail(vstr_t *vstr, int len);
Damien2f06c572013-11-03 18:20:56 +000091void vstr_printf(vstr_t *vstr, const char *fmt, ...);
Damien96a0add2013-11-03 18:30:10 +000092
Paul Sokolovskye11b17c2014-02-05 00:47:06 +020093/** non-dynamic size-bounded variable buffer/string *************/
94
95#define CHECKBUF(buf, max_size) char buf[max_size + 1]; uint buf##_len = max_size; char *buf##_p = buf;
96#define CHECKBUF_APPEND(buf, src, src_len) \
97 { int l = MIN(src_len, buf##_len); \
98 memcpy(buf##_p, src, l); \
99 buf##_len -= l; \
100 buf##_p += l; }
101#define CHECKBUF_APPEND_0(buf) { *buf##_p = 0; }
102#define CHECKBUF_LEN(buf) (buf##_p - buf)
103
Damien96a0add2013-11-03 18:30:10 +0000104#ifdef va_start
Damien2f06c572013-11-03 18:20:56 +0000105void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap);
Damien96a0add2013-11-03 18:30:10 +0000106#endif
Damien429d7192013-10-04 19:53:11 +0100107
Damien429d7192013-10-04 19:53:11 +0100108#endif // _INCLUDED_MINILIB_H