]> mj.ucw.cz Git - libucw.git/blob - lib/lib.h
72337d93ea93b5118cd0aa180ce06ab174b1540d
[libucw.git] / lib / lib.h
1 /*
2  *      Sherlock Library -- Miscellaneous Functions
3  *
4  *      (c) 1997 Martin Mares, <mj@atrey.karlin.mff.cuni.cz>
5  */
6
7 #ifndef _SHERLOCK_LIB_H
8 #define _SHERLOCK_LIB_H
9
10 #include <lib/config.h>
11
12 /* Ugly structure handling macros */
13
14 #define OFFSETOF(s, i) ((unsigned int)&((s *)0)->i)
15 #define SKIP_BACK(s, i, p) ((s *)((char *)p - OFFSETOF(s, i)))
16 #define ALIGN(s, a) (((s)+a-1)&~(a-1))
17
18 /* Temporary Files */
19
20 #define TMP_DIR "tmp"
21 #define TMP_DIR_LEN 3
22
23 struct tempfile {
24   int fh;
25   byte name[32];
26 };
27
28 void open_temp(struct tempfile *, byte *);
29 void delete_temp(struct tempfile *);
30 u32 temprand(uns);
31
32 #define TF_GENERIC "t"
33 #define TF_QUEUE_CONTROL "c"
34 #define TF_QUEUE_DATA "d"
35 #define TF_DECODE "x"
36 #define TF_TRANSFORM "s"
37 #define TF_OBJECT "o"
38
39 /* Config Files */
40
41 struct cfitem {
42   byte *name;
43   int type;
44   void *var;
45 };
46
47 #define CI_STOP 0
48 #define CI_INT 1
49 #define CI_STRING 2
50 #define CI_FUNCTION 3
51
52 typedef byte *(*ci_func)(struct cfitem *, byte *);
53
54 void cf_read(byte *, struct cfitem *);
55 int cf_read_err(byte *, struct cfitem *); /* Read with possible error, 1 = succeeded */
56
57 /* Logging */
58
59 #define L_DEBUG "<0>"
60 #define L_INFO "<2>"
61 #define L_WARN "<4>"
62 #define L_ERROR "<6>"
63 #define L_FATAL "<9>"
64
65 void log(byte *, ...);
66 void die(byte *, ...) NONRET;
67 void initlog(byte *);
68 void open_log_file(byte *);
69
70 #ifdef DEBUG
71 #define ASSERT(x) do { if (!(x)) die("Assertion `%s' failed at %s:%d", #x, __FILE__, __LINE__); } while(0)
72 #else
73 #define ASSERT(x) do { } while(0)
74 #endif
75
76 /* Allocation */
77
78 void *xmalloc(uns);
79 void *xrealloc(void *, uns);
80 byte *stralloc(byte *);
81
82 /* Content-Type pattern matching and filters */
83
84 struct ct_filter;
85
86 int match_ct_patt(byte *, byte *);
87
88 struct ct_filter *new_ct_filter(void);
89 byte *add_ct_filter(struct ct_filter *, byte *);
90 int match_ct_filter(struct ct_filter *, byte *);
91
92 /* Binary log */
93
94 int log2(u32);
95
96 /* obj.c */
97
98 struct odes {                           /* Object description */
99   struct oattr *attrs;
100   struct mempool *pool;
101 };
102
103 struct oattr {                          /* Object attribute */
104   struct oattr *next, *same;
105   byte attr;
106   byte val[1];
107 };
108
109 void obj_dump(struct odes *);
110 struct odes *obj_fload(FILE *, byte *);
111 struct odes *obj_new(void);
112 struct odes *obj_load(byte *);
113 void obj_fwrite(FILE *, struct odes *);
114 void obj_write(byte *, struct odes *);
115 void obj_free(struct odes *);
116 struct oattr *find_attr(struct odes *, uns);
117 struct oattr *find_attr_last(struct odes *, uns);
118 uns del_attr(struct odes *, struct oattr *);
119 byte *find_aval(struct odes *, uns);
120 struct oattr *set_attr(struct odes *, uns, byte *);
121 struct oattr *set_attr_num(struct odes *, uns, uns);
122 struct oattr *add_attr(struct odes *, struct oattr *, uns, byte *);
123 struct oattr *prepend_attr(struct odes *, uns, byte *);
124
125 /* oname.c */
126
127 #define OID_MIN 0x10000         /* Values less than this have special meaning */
128
129 oid_t new_oid(uns);
130 void mk_obj_name(byte *, oid_t, byte *);
131 int dump_obj_to_file(byte *, oid_t, struct odes *, int);
132
133 /* wordsplit.c */
134
135 int wordsplit(byte *, byte **, uns);
136
137 /* pat(i)match.c */
138
139 int match_pattern(byte *, byte *);
140 int match_pattern_nocase(byte *, byte *);
141
142 /* md5hex.c */
143
144 void md5_to_hex(byte *, byte *);
145 void hex_to_md5(byte *, byte *);
146
147 #define MD5_SIZE 16
148 #define MD5_HEX_SIZE 33
149
150 /* prime.c */
151
152 int isprime(uns);
153 uns nextprime(uns);
154
155 /* timer.c */
156
157 void init_timer(void);
158 uns get_timer(void);
159
160 /* regex.c */
161
162 typedef struct regex regex;
163
164 regex *rx_compile(byte *r);
165 void rx_free(regex *r);
166 int rx_match(regex *r, byte *s);
167 int rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen);
168
169 /* objwalk.c */
170
171 void scan_obj_tree(byte *, void (*)(oid_t, byte *));
172
173 /* random.c */
174
175 uns random_max(uns);
176
177 /* mmap.c */
178
179 void *mmap_file(byte *name, unsigned *len);
180
181 #endif