]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
Use OBUCK_INCOMPLETE_MAGIC for incomplete entries.
[libucw.git] / lib / lib.h
1 /*
2  *      Sherlock Library -- Miscellaneous Functions
3  *
4  *      (c) 1997--2000 Martin Mares <mj@ucw.cz>
5  */
6
7 /*
8  *  This file should be included as the very first include in all
9  *  source files, especially before all OS includes since it sets
10  *  up libc feature macros.
11  */
12
13 #ifndef _SHERLOCK_LIB_H
14 #define _SHERLOCK_LIB_H
15
16 #include "lib/config.h"
17
18 /* Tell libc we're going to use all extensions available */
19
20 #define _GNU_SOURCE
21
22 #ifndef NULL
23 #define NULL ((void *)0)
24 #endif
25
26 /* Ugly structure handling macros */
27
28 #define OFFSETOF(s, i) ((unsigned int)&((s *)0)->i)
29 #define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
30 #define ALIGN(s, a) (((s)+a-1)&~(a-1))
31
32 /* Temporary Files */
33
34 #define TMP_DIR "tmp"
35 #define TMP_DIR_LEN 3
36
37 struct tempfile {
38   int fh;
39   byte name[32];
40 };
41
42 void open_temp(struct tempfile *, byte *);
43 void delete_temp(struct tempfile *);
44 u32 temprand(uns);
45
46 /* FIXME: Remove? */
47 #define TF_GENERIC "t"
48 #define TF_QUEUE_CONTROL "c"
49 #define TF_QUEUE_DATA "d"
50 #define TF_DECODE "x"
51 #define TF_TRANSFORM "s"
52 #define TF_OBJECT "o"
53
54 /* Logging */
55
56 /* FIXME: Define new logging mechanism? */
57
58 #define L_DEBUG "<0>"
59 #define L_INFO "<2>"
60 #define L_WARN "<4>"
61 #define L_ERROR "<6>"
62 #define L_FATAL "<9>"
63
64 void log(byte *, ...);
65 void die(byte *, ...) NONRET;
66 void initlog(byte *);
67 void open_log_file(byte *);
68
69 #ifdef DEBUG
70 #define ASSERT(x) do { if (!(x)) die("Assertion `%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
71 #else
72 #define ASSERT(x) do { } while(0)
73 #endif
74
75 /* Allocation */
76
77 #ifdef DMALLOC
78 /*
79  * The standard dmalloc macros tend to produce lots of namespace
80  * conflicts and we use only xmalloc and xfree, so we can define
81  * the stubs ourselves.
82  */
83 #define DMALLOC_DISABLE
84 #include <dmalloc.h>
85 #define xmalloc(size) _xmalloc_leap(__FILE__, __LINE__, size)
86 #define xrealloc(ptr,size) _xrealloc_leap(__FILE__, __LINE__, ptr, size)
87 #define xfree(ptr) _xfree_leap(__FILE__, __LINE__, ptr)
88 #else
89 /*
90  * Unfortunately, several libraries we might want to link to define
91  * their own xmalloc and we don't want to interfere with them, hence
92  * the renaming.
93  */
94 #define xmalloc bird_xmalloc
95 void *xmalloc(unsigned);
96 void *xrealloc(void *, unsigned);
97 #define xfree(x) free(x)
98 #endif
99
100 byte *stralloc(byte *);
101
102 /* Content-Type pattern matching and filters */
103
104 int match_ct_patt(byte *, byte *);
105
106 /* Binary log */
107
108 int log2(u32);
109
110 /* wordsplit.c */
111
112 int wordsplit(byte *, byte **, uns);
113
114 /* pat(i)match.c */
115
116 int match_pattern(byte *, byte *);
117 int match_pattern_nocase(byte *, byte *);
118
119 /* md5hex.c */
120
121 void md5_to_hex(byte *, byte *);
122 void hex_to_md5(byte *, byte *);
123
124 #define MD5_SIZE 16
125 #define MD5_HEX_SIZE 33
126
127 /* prime.c */
128
129 int isprime(uns);
130 uns nextprime(uns);
131
132 /* timer.c */
133
134 void init_timer(void);
135 uns get_timer(void);
136
137 /* regex.c */
138
139 typedef struct regex regex;
140
141 regex *rx_compile(byte *r);
142 void rx_free(regex *r);
143 int rx_match(regex *r, byte *s);
144 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
145
146 /* random.c */
147
148 uns random_max(uns);
149
150 /* mmap.c */
151
152 void *mmap_file(byte *name, unsigned *len);
153
154 #endif