~rcr/rirc

ref: c677d85e88b1397d0eaa9b99ad3acf531f64aa3f rirc/src/utils/utils.h -rw-r--r-- 2.0 KiB
c677d85e — Richard Robbins add -m/--mode flag 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#ifndef RIRC_UTILS_UTILS_H
#define RIRC_UTILS_UTILS_H

#include <stdio.h>
#include <stdlib.h>

#define ARR_LEN(A) (sizeof((A)) / sizeof((A)[0]))

#define MAX(A, B) ((A) > (B) ? (A) : (B))
#define MIN(A, B) ((A) > (B) ? (B) : (A))

#define SEC_IN_MS(X) ((X) * 1000)
#define SEC_IN_US(X) ((X) * 1000 * 1000)

#define UNUSED(X) ((void)(X))

#define MESSAGE(TYPE, ...) \
	fprintf(stderr, "%s %s:%d:%s ", (TYPE), __FILE__, __LINE__, __func__); \
	fprintf(stderr, __VA_ARGS__); \
	fprintf(stderr, "\n"); \
	fflush(stderr);

#if !(defined NDEBUG) && !(defined TESTING)
#define debug(...) \
	do { MESSAGE("DEBUG", __VA_ARGS__); } while (0)
#define debug_send(L, M) \
	do { fprintf(stderr, "DEBUG (--> %3zu) %s\n", (L), (M)); fflush(stderr); } while (0)
#define debug_recv(L, M) \
	do { fprintf(stderr, "DEBUG (<-- %3zu) %s\n", (L), (M)); fflush(stderr); } while (0)
#else
#define debug(...) \
	do { ; } while (0)
#define debug_send(L, M) \
	do { ; } while (0)
#define debug_recv(L, M) \
	do { ; } while (0)
#endif

#ifndef fatal
#define fatal(...) \
	do { MESSAGE("FATAL", __VA_ARGS__); exit(EXIT_FAILURE); } while (0)
#define fatal_noexit(...) \
	do { MESSAGE("FATAL", __VA_ARGS__); } while (0)
#endif

enum casemapping
{
	CASEMAPPING_INVALID,
	CASEMAPPING_ASCII,
	CASEMAPPING_RFC1459,
	CASEMAPPING_STRICT_RFC1459
};

struct irc_message
{
	char *params;
	const char *command;
	const char *from;
	const char *host;
	size_t len_command;
	size_t len_from;
	size_t len_host;
	unsigned n_params;
	unsigned split : 1;
};

int irc_ischan(const char*);
int irc_isnick(const char*);
int irc_pinged(enum casemapping, const char*, const char*);
int irc_strcmp(enum casemapping, const char*, const char*);
int irc_strncmp(enum casemapping, const char*, const char*, size_t);
char* irc_strsep(char**);
char* irc_strtrim(char**);
char* irc_strwrap(unsigned, char**, char*);

int irc_message_param(struct irc_message*, char**);
int irc_message_parse(struct irc_message*, char*);
int irc_message_split(struct irc_message*, const char**, const char**);

#endif