2013-10-04 14:53:11 -04:00
|
|
|
// a mini library of useful types and functions
|
|
|
|
|
|
|
|
#ifndef _INCLUDED_MINILIB_H
|
|
|
|
#define _INCLUDED_MINILIB_H
|
|
|
|
|
|
|
|
/** types *******************************************************/
|
|
|
|
|
2014-01-06 16:51:53 -05:00
|
|
|
#include <stdbool.h>
|
2013-10-04 14:53:11 -04:00
|
|
|
|
|
|
|
typedef unsigned char byte;
|
|
|
|
typedef unsigned int uint;
|
|
|
|
|
2014-02-04 17:44:55 -05:00
|
|
|
/** generic ops *************************************************/
|
|
|
|
|
2014-02-05 18:57:48 -05:00
|
|
|
#ifndef MIN
|
2014-02-04 17:44:55 -05:00
|
|
|
#define MIN(x, y) ((x) < (y) ? (x) : (y))
|
2014-02-05 18:57:48 -05:00
|
|
|
#endif
|
|
|
|
#ifndef MAX
|
2014-02-04 17:44:55 -05:00
|
|
|
#define MAX(x, y) ((x) > (y) ? (x) : (y))
|
2014-02-05 18:57:48 -05:00
|
|
|
#endif
|
2014-02-04 17:44:55 -05:00
|
|
|
|
2013-10-04 14:53:11 -04:00
|
|
|
/** memomry allocation ******************************************/
|
|
|
|
|
2013-12-29 14:33:23 -05:00
|
|
|
// TODO make a lazy m_renew that can increase by a smaller amount than requested (but by at least 1 more element)
|
|
|
|
|
2013-10-04 14:53:11 -04:00
|
|
|
#define m_new(type, num) ((type*)(m_malloc(sizeof(type) * (num))))
|
|
|
|
#define m_new0(type, num) ((type*)(m_malloc0(sizeof(type) * (num))))
|
2013-12-21 13:17:45 -05:00
|
|
|
#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)))
|
2013-12-29 14:33:23 -05:00
|
|
|
#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))
|
2014-01-12 21:31:00 -05:00
|
|
|
#define m_del_var(obj_type, var_type, var_num, ptr) (m_free(ptr, sizeof(obj_type) + sizeof(var_type) * (var_num)))
|
2013-10-04 14:53:11 -04:00
|
|
|
|
|
|
|
void *m_malloc(int num_bytes);
|
|
|
|
void *m_malloc0(int num_bytes);
|
2013-12-29 14:33:23 -05:00
|
|
|
void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes);
|
|
|
|
void m_free(void *ptr, int num_bytes);
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2013-10-23 15:20:17 -04:00
|
|
|
int m_get_total_bytes_allocated(void);
|
2014-01-01 16:15:47 -05:00
|
|
|
int m_get_current_bytes_allocated(void);
|
2014-01-01 16:42:21 -05:00
|
|
|
int m_get_peak_bytes_allocated(void);
|
2013-10-04 14:53:11 -04:00
|
|
|
|
|
|
|
/** unichar / UTF-8 *********************************************/
|
|
|
|
|
|
|
|
typedef int unichar; // TODO
|
|
|
|
|
2013-12-30 13:23:50 -05:00
|
|
|
unichar utf8_get_char(const char *s);
|
|
|
|
char *utf8_next_char(const char *s);
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2014-01-06 16:51:53 -05:00
|
|
|
bool unichar_isspace(unichar c);
|
|
|
|
bool unichar_isalpha(unichar c);
|
|
|
|
bool unichar_isprint(unichar c);
|
|
|
|
bool unichar_isdigit(unichar c);
|
2014-01-22 15:40:02 -05:00
|
|
|
bool unichar_isxdigit(unichar c);
|
2013-10-04 14:53:11 -04:00
|
|
|
|
|
|
|
/** string ******************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
#define streq(s1, s2) (strcmp((s1), (s2)) == 0)
|
|
|
|
*/
|
|
|
|
|
2014-01-15 17:58:39 -05:00
|
|
|
long strtonum(const char *restrict s, int base);
|
|
|
|
|
2013-10-04 14:53:11 -04:00
|
|
|
/** variable string *********************************************/
|
|
|
|
|
2013-10-20 09:39:58 -04:00
|
|
|
typedef struct _vstr_t {
|
|
|
|
int alloc;
|
|
|
|
int len;
|
|
|
|
char *buf;
|
2014-02-06 16:11:19 -05:00
|
|
|
struct {
|
|
|
|
bool had_error : 1;
|
|
|
|
bool fixed_buf : 1;
|
|
|
|
};
|
2013-10-20 09:39:58 -04:00
|
|
|
} vstr_t;
|
|
|
|
|
2014-02-06 16:11:19 -05:00
|
|
|
// convenience macro to declare a vstr with a fixed size buffer on the stack
|
|
|
|
#define VSTR_FIXED(vstr, alloc) vstr_t vstr; char vstr##_buf[(alloc)]; vstr_init_fixed_buf(&vstr, (alloc), vstr##_buf);
|
|
|
|
|
2014-01-13 16:15:23 -05:00
|
|
|
void vstr_init(vstr_t *vstr, int alloc);
|
2014-02-06 16:11:19 -05:00
|
|
|
void vstr_init_fixed_buf(vstr_t *vstr, int alloc, char *buf);
|
2013-10-20 09:39:58 -04:00
|
|
|
void vstr_clear(vstr_t *vstr);
|
2013-10-23 15:20:17 -04:00
|
|
|
vstr_t *vstr_new(void);
|
2014-01-13 16:15:23 -05:00
|
|
|
vstr_t *vstr_new_size(int alloc);
|
2013-10-04 14:53:11 -04:00
|
|
|
void vstr_free(vstr_t *vstr);
|
|
|
|
void vstr_reset(vstr_t *vstr);
|
2014-01-06 16:51:53 -05:00
|
|
|
bool vstr_had_error(vstr_t *vstr);
|
2013-10-04 14:53:11 -04:00
|
|
|
char *vstr_str(vstr_t *vstr);
|
|
|
|
int vstr_len(vstr_t *vstr);
|
|
|
|
void vstr_hint_size(vstr_t *vstr, int size);
|
2014-02-06 16:11:19 -05:00
|
|
|
char *vstr_extend(vstr_t *vstr, int size);
|
2014-01-13 16:15:23 -05:00
|
|
|
bool vstr_set_size(vstr_t *vstr, int size);
|
|
|
|
bool vstr_shrink(vstr_t *vstr);
|
2013-10-04 14:53:11 -04:00
|
|
|
char *vstr_add_len(vstr_t *vstr, int len);
|
2013-10-20 09:39:58 -04:00
|
|
|
void vstr_add_byte(vstr_t *vstr, byte v);
|
|
|
|
void vstr_add_char(vstr_t *vstr, unichar chr);
|
2013-10-04 14:53:11 -04:00
|
|
|
void vstr_add_str(vstr_t *vstr, const char *str);
|
|
|
|
void vstr_add_strn(vstr_t *vstr, const char *str, int len);
|
2013-10-20 09:39:58 -04:00
|
|
|
//void vstr_add_le16(vstr_t *vstr, unsigned short v);
|
|
|
|
//void vstr_add_le32(vstr_t *vstr, unsigned int v);
|
2013-10-04 14:53:11 -04:00
|
|
|
void vstr_cut_tail(vstr_t *vstr, int len);
|
2013-11-03 13:20:56 -05:00
|
|
|
void vstr_printf(vstr_t *vstr, const char *fmt, ...);
|
2013-11-03 13:30:10 -05:00
|
|
|
|
2014-02-04 17:47:06 -05:00
|
|
|
/** non-dynamic size-bounded variable buffer/string *************/
|
|
|
|
|
|
|
|
#define CHECKBUF(buf, max_size) char buf[max_size + 1]; uint buf##_len = max_size; char *buf##_p = buf;
|
2014-02-05 18:57:48 -05:00
|
|
|
#define CHECKBUF_RESET(buf, max_size) buf##_len = max_size; buf##_p = buf;
|
2014-02-04 17:47:06 -05:00
|
|
|
#define CHECKBUF_APPEND(buf, src, src_len) \
|
|
|
|
{ int l = MIN(src_len, buf##_len); \
|
|
|
|
memcpy(buf##_p, src, l); \
|
|
|
|
buf##_len -= l; \
|
|
|
|
buf##_p += l; }
|
|
|
|
#define CHECKBUF_APPEND_0(buf) { *buf##_p = 0; }
|
|
|
|
#define CHECKBUF_LEN(buf) (buf##_p - buf)
|
|
|
|
|
2013-11-03 13:30:10 -05:00
|
|
|
#ifdef va_start
|
2013-11-03 13:20:56 -05:00
|
|
|
void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap);
|
2013-11-03 13:30:10 -05:00
|
|
|
#endif
|
2013-10-04 14:53:11 -04:00
|
|
|
|
2014-02-16 11:11:42 -05:00
|
|
|
// Debugging helpers
|
|
|
|
int DEBUG_printf(const char *fmt, ...);
|
|
|
|
|
2013-10-04 14:53:11 -04:00
|
|
|
#endif // _INCLUDED_MINILIB_H
|