]> Pileus Git - ~andy/lamechat/blob - util.h
XMPP Connect
[~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 /* Types */
29 typedef void (*cb_t)(void *data, int fd);
30
31 typedef struct {
32         void *data;
33         int   len;
34         int   max;
35 } buf_t;
36
37 typedef struct {
38         int   fd;
39         cb_t  cb;
40         void *data;
41 } poll_t;
42
43 /* Debug functions */
44 void util_init(void);
45
46 /* Stirng functions */
47 void strsub(char *str, char find, char repl);
48 char *strcopy(const char *str);
49 int match(const char *a, const char *b);
50
51 /* Memory functions */
52 void *alloc0(int size);
53 void append(buf_t *buf, const void *data, int len);
54 void release(buf_t *buf);
55
56 /* File functions */
57 char *read_file(const char *path, int *len);
58
59 /* Polling functions */
60 int poll_add(poll_t *poll, int fd, cb_t cb, void *data);
61 int poll_ctl(poll_t *poll, int in, int out, int err);
62 int poll_del(poll_t *poll);
63 int poll_run(int timeout);
64 void poll_quit(void);
65
66 /* Debug functions */
67 void debug(char *fmt, ...);
68 void error(char *fmt, ...);