]> Pileus Git - ~andy/lamechat/blob - util.h
Add util_exit function.
[~andy/lamechat] / util.h
1 /*
2  * Copyright (C) 2012-2013 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /* Macros */
19 #define ABS(a)   ((a) > 0 ? (a) : -(a))
20 #define MIN(a,b) ((a) < (b) ? (a) : (b))
21 #define MAX(a,b) ((a) > (b) ? (a) : (b))
22 #define CLAMP(x,l,h) MIN(MAX(x,l),h)
23 #define ROUND(x) ((int)((x)+0.5))
24 #define N_ELEMENTS(x) (sizeof(x)/sizeof((x)[0]))
25
26 #define new0(type) alloc0(sizeof(type))
27
28 #define MATCH_NONE  0b000
29 #define MATCH_START 0b001
30 #define MATCH_STOP  0b010
31 #define MATCH_BOTH  0b011
32 #define MATCH_HERE  0b100
33 #define MATCH_STR   " <>^-"
34
35 /* Types */
36 typedef void (*cb_t)(void *data);
37
38 typedef struct {
39         char *data;
40         int   len;
41         int   max;
42 } buf_t;
43
44 typedef struct {
45         int   fd;
46         cb_t  cb;
47         void *data;
48 } poll_t;
49
50 typedef struct {
51         poll_t    poll;
52         cb_t      timer;
53         void     *data;
54 } idle_t;
55
56 typedef struct {
57         char     *pattern; // regex string
58         void     *regex;   // compiled regex
59         int       count;   // number of matches
60         int       index;   // cached match position
61         int       size;    // max number of matches
62         int      *start;   // start pos of each match
63         int      *stop;    // end pos of each match
64 } reg_t;
65
66 /* Debug functions */
67 void util_init(void);
68 void util_exit(void);
69
70 /* String functions */
71 void strsub(char *str, char find, char repl);
72 char *strcopy(const char *str);
73 void strset(char **old, const char *str);
74 int compare(const char *a, const char *b);
75 int match(const char *a, const char *b);
76 int starts(const char *prefix, const char *str);
77 int prefix(const char *str, const char *prefix, const char **suffix);
78 int suffix(const char *str, const char *prefix, const char **suffix);
79 char *despace(char *text);
80 void escape(char *dst, const char *src, int len);
81
82 /* Memory functions */
83 void *alloc0(int size);
84 void free0(void *ptr);
85 void append(buf_t *buf, const char *data, int len);
86 void release(buf_t *buf);
87 const char *reset(buf_t *buf);
88
89 /* Data functions */
90 int base64(const void *in, int ilen, void *out, int olen);
91
92 /* File functions */
93 char *read_file(const char *path, int *len);
94
95 /* Polling functions */
96 void poll_add(poll_t *poll, int fd, cb_t cb, void *data);
97 void poll_ctl(poll_t *poll, int in, int out, int err);
98 void poll_del(poll_t *poll);
99 int poll_run(int timeout);
100 void poll_quit(void);
101
102 /* Idle functions */
103 void idle_add(idle_t *idle);
104 void idle_set(idle_t *idle, int first, int next);
105
106 /* Regex functions */
107 void reg_set(reg_t *reg, const char *pattern, int flags);
108 void reg_clr(reg_t *reg);
109 int reg_match(reg_t *reg, const char *text);
110 int reg_check(reg_t *reg, int pos);
111
112 /* Debug functions */
113 void notice(char *fmt, ...);
114 void debug(char *fmt, ...);
115 void error(char *fmt, ...);