Debian Codegraph @

ext/cstring.h

raw symbols SCIP
1 /* two template types: STRING(t) which defines a pascal-style string
2 * of element (t) [STRING(char) is the closest to the pascal string],
3 * and ANCHOR(t) which defines a baseplate that a linked list can be
4 * built up from. [The linked list /must/ contain a ->next pointer
5 * for linking the list together with.]
6 */
7 #ifndef _CSTRING_D
8 #define _CSTRING_D
9
10 #include <string.h>
11 #include <stdlib.h>
12
13 #ifndef __WITHOUT_AMALLOC
14 # include "amalloc.h"
15 #endif
16
17 /* expandable Pascal-style string.
18 */
19 #define STRING(type) struct { type *text; int size, alloc; }
20
21 #define CREATE(x) ( (T(x) = (void*)0), (S(x) = (x).alloc = 0) )
22 #define EXPAND(x) (S(x)++)[(S(x) < (x).alloc) \
23 ? (T(x)) \
24 : (T(x) = T(x) ? realloc(T(x), sizeof T(x)[0] * ((x).alloc += 100)) \
25 : malloc(sizeof T(x)[0] * ((x).alloc += 100)) )]
26
27 #define DELETE(x) ALLOCATED(x) ? (free(T(x)), S(x) = (x).alloc = 0) \
28 : ( S(x) = 0 )
29 #define CLIP(t,i,sz) \
30 S(t) -= ( ((i) >= 0) && ((sz) > 0) && (((i)+(sz)) <= S(t)) ) ? \
31 (memmove(&T(t)[i], &T(t)[i+sz], (S(t)-(i+sz)+1)*sizeof(T(t)[0])), \
32 (sz)) : 0
33
34 #define RESERVE(x, sz) T(x) = ((x).alloc > S(x) + (sz) \
35 ? T(x) \
36 : T(x) \
37 ? realloc(T(x), sizeof T(x)[0] * ((x).alloc = 100+(sz)+S(x))) \
38 : malloc(sizeof T(x)[0] * ((x).alloc = 100+(sz)+S(x))))
39 #define SUFFIX(t,p,sz) \
40 memcpy(((S(t) += (sz)) - (sz)) + \
41 (T(t) = T(t) ? realloc(T(t), sizeof T(t)[0] * ((t).alloc += sz)) \
42 : malloc(sizeof T(t)[0] * ((t).alloc += sz))), \
43 (p), sizeof(T(t)[0])*(sz))
44
45 #define PREFIX(t,p,sz) \
46 RESERVE( (t), (sz) ); \
47 if ( S(t) ) { memmove(T(t)+(sz), T(t), S(t)); } \
48 memcpy( T(t), (p), (sz) ); \
49 S(t) += (sz)
50
51 /* reference-style links (and images) are stored in an array
52 */
53 #define T(x) (x).text
54 #define S(x) (x).size
55 #define ALLOCATED(x) (x).alloc
56
57 /* abstract anchor type that defines a list base
58 * with a function that attaches an element to
59 * the end of the list.
60 *
61 * the list base field is named .text so that the T()
62 * macro will work with it.
63 */
64 #define ANCHOR(t) struct { t *text, *end; }
65 #define E(t) ((t).end)
66
67 #define ATTACH(t, p) ( T(t) ? ( (E(t)->next = (p)), (E(t) = (p)) ) \
68 : ( (T(t) = E(t) = (p)) ) )
69
70 typedef STRING(char) Cstring;
71
72 extern void Csputc(int, Cstring *);
73 extern int Csprintf(Cstring *, char *, ...);
74 extern int Cswrite(Cstring *, char *, int);
75
76 #endif/*_CSTRING_D*/