-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
93 lines (73 loc) · 3.04 KB
/
utils.h
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
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* utils.h: Random useful things.
*
* Copyright © 2012 Zachary Catlin. See LICENSE for terms.
*/
#ifndef ML_UTILS_H
#define ML_UTILS_H
#ifndef ML_STDLIB_H
#define ML_STDLIB_H
#include <stdlib.h>
#endif
#ifndef ML_STDIO_H
#define ML_STDIO_H
#include <stdio.h>
#endif
#ifndef ML_LIMITS_H
#define ML_LIMITS_H
#include <limits.h>
#endif
/* Either successfully allocates memory or quits the program */
void * malloc_or_die_impl(size_t len, const char *type,
const char *fname, int line);
#define malloc_or_die(n, type) \
((type *) malloc_or_die_impl(n * sizeof(type), #type, __FILE__, __LINE__))
#define mod_2(n, type, fname, line) \
((type *) malloc_or_die_impl(n * sizeof(type), #type, fname, line))
/* A length-prefixed, 8-bit clean string */
typedef struct {
size_t len;
char s[1];
} len_string;
/* A list of len_strings */
struct lstring_list {
len_string *s;
struct lstring_list *next;
};
typedef struct lstring_list lstr_list_t;
/* The len_string * returned by the following functions is allocated from
* the heap and can be deallocated by a simple free() of the len_string *. */
/* Returns a pointer to a len_string of length len, initialized to all-nulls */
len_string * mk_blank_lstring_impl(size_t len, const char *fname, int line);
/* Returns a pointer to a len_string of length len, initialized to the len
* bytes starting with buf */
len_string * lstring_dupbuf_impl(size_t len, const char *buf,
const char *fname, int line);
/* The following are like a combination of strdup and strcat: */
len_string * lstrcat_impl(const len_string *a, const len_string *b,
const char *fname, int line);
len_string * lstrcat_s_impl(const len_string *a, const char *str,
const char *fname, int line);
len_string * lstrcat_buf_impl(const len_string *a, size_t len, const char *buf,
const char *fname, int line);
/* Generally, one uses the following macros for ease of debugging: */
#define mk_blank_lstring(len) mk_blank_lstring_impl((len), __FILE__, __LINE__)
#define lstring_dupbuf(len, buf) lstring_dupbuf_impl((len), (buf), \
__FILE__, __LINE__)
#define lstrcat(a, b) lstrcat_impl((a), (b), __FILE__, __LINE__)
#define lstrcat_s(a, str) lstrcat_s_impl((a), (str), __FILE__, __LINE__)
#define lstrcat_buf(a, len, buf) lstrcat_buf_impl((a), (len), (buf), \
__FILE__, __LINE__)
/* Returns 1 if the contents of a and b are equal, 0 otherwise: */
int lstr_eq(const len_string *a, const len_string *b);
/* Writes the contents of *lstr to stream f */
void lstr_fwrite(const len_string *lstr, FILE *f);
/* Returns whether or not s is contained in the list starting with l */
int lstr_in_list(const len_string *s, const lstr_list_t *l);
/* A character-to-hexadecimal digit table */
extern const char hex_digits[256];
/* It's useful to know the least number of unsigned ints that can contain
* 256 bits: */
#define ML_UINT_BIT (sizeof(unsigned int) * CHAR_BIT)
#define CLASS_SZ ((256 + ML_UINT_BIT - 1) / ML_UINT_BIT)
#endif