43
|
1 /* {{{ vbuf.h - variable length buffer functions
|
|
2 *
|
|
3 * Functions that try to make dealing with buffers easier.
|
|
4 *
|
|
5 * vbuf
|
|
6 *
|
|
7 * vstr
|
|
8 * - should always contain a valid string
|
|
9 *
|
|
10 * }}} */
|
|
11
|
|
12 #ifndef VBUF_H
|
|
13 #define VBUF_H
|
|
14 #define SZ_MAX 4096
|
|
15 #include <stdlib.h>
|
|
16 #include <stdio.h>
|
|
17 #include <stdarg.h>
|
|
18 /***************************************************/
|
|
19
|
|
20 // {{{ Tokenizer const TOK_EMPTY, TOK_ELEMENT, DELIM
|
|
21 #define DELIM '\\'
|
|
22
|
|
23 #define TOK_EMPTY 0
|
|
24 #define TOK_DELIM 1
|
|
25 #define TOK_PARENT 2
|
|
26 #define TOK_CURRENT 3
|
|
27 #define TOK_ELEMENT 4
|
|
28
|
|
29 #define TOK_ERROR 10
|
|
30 #define TOK_BUF_SMALL 11
|
|
31 // }}}
|
|
32
|
|
33
|
|
34 // Variable-length buffers
|
|
35 struct varbuf { // {{{
|
|
36 size_t dlen; //length of data stored in buffer
|
|
37 size_t blen; //length of buffer
|
|
38 char *buf; //buffer
|
|
39 char *b; //start of stored data
|
|
40 }; // }}}
|
|
41
|
|
42
|
|
43 // The exact same thing as a varbuf but should always contain at least '\0'
|
|
44 struct varstr { // {{{
|
|
45 size_t dlen; //length of data stored in buffer
|
|
46 size_t blen; //length of buffer
|
|
47 char *buf; //buffer
|
|
48 char *b; //start of stored data
|
|
49 }; // }}}
|
|
50
|
|
51
|
|
52 typedef struct varbuf vbuf;
|
|
53 typedef struct varstr vstr;
|
|
54
|
|
55 #define VBUF_STATIC(x,y) static vbuf *x = NULL; if(!x) x = vballoc(y);
|
|
56 #define VSTR_STATIC(x,y) static vstr *x = NULL; if(!x) x = vsalloc(y);
|
|
57
|
|
58 // vbuf functions
|
|
59 struct varbuf *vballoc( size_t len );
|
|
60 void vbfree( vbuf *vb );
|
|
61 void vbclear( vbuf *vb ); //ditch the data, keep the buffer
|
|
62 void vbresize( vbuf *vb, size_t len );
|
|
63 int vbavail( vbuf *vb );
|
|
64 void vbdump( vbuf *vb );
|
|
65 void vbgrow( vbuf *vb, size_t len ); // grow buffer by len bytes, data are preserved
|
|
66 void vbset( vbuf *vb, void *data, size_t len );
|
|
67 void vbskipws( vbuf *vb );
|
|
68 void vbappend( vbuf *vb, void *data, size_t length );
|
|
69 void vbskip( vbuf *vb, size_t skip );
|
|
70 void vboverwrite( vbuf *vbdest, vbuf *vbsrc );
|
|
71
|
|
72 // vstr functions
|
|
73 vstr *vsalloc( size_t len );
|
|
74 char *vsb( vstr *vs );
|
|
75 size_t vslen( vstr *vs ); //strlen
|
|
76 void vsfree( vstr *vs );
|
|
77 void vsset( vstr *vs, char *s ); // Store string s in vb
|
|
78 void vsnset( vstr *vs, char *s, size_t n ); // Store string s in vb
|
|
79 void vsgrow( vstr *vs, size_t len ); // grow buffer by len bytes, data are preserved
|
|
80 size_t vsavail( vstr *vs );
|
|
81 void vscat( vstr *vs, char *str );
|
|
82 void vsncat( vstr *vs, char *str, size_t len );
|
|
83 void vsnprepend( vstr *vs, char *str, size_t len ) ;
|
|
84 void vsskip( vstr *vs, size_t len );
|
|
85 int vscmp( vstr *vs, char *str );
|
|
86 void vsskipws( vstr *vs );
|
|
87 void vs_printf( vstr *vs, char *fmt, ... );
|
|
88 void vs_printfa( vstr *vs, char *fmt, ... );
|
|
89 void vshexdump( vstr *vs, char *b, size_t start, size_t stop, int ascii );
|
|
90 int vscatprintf( vstr *vs, char *fmt, ... );
|
|
91 void vsvprintf( vstr *vs, char *fmt, va_list ap );
|
|
92 void vstrunc( vstr *vs, int off ); // Drop chars [off..dlen]
|
|
93 int vslast( vstr *vs ); // returns the last character stored in a vstr string
|
|
94 void vscharcat( vstr *vs, int ch );
|
|
95 int vsutf16( vstr *vs, vbuf *in ); //in: in=zero-terminated utf16; out: vs=utf8; returns: 0 on success, else on fail
|
|
96
|
|
97 int vs_parse_escaped_string( vstr *vs, char *str, size_t len );
|
|
98
|
|
99
|
|
100 /*
|
|
101 * Windows unicode output trash - this stuff sucks
|
|
102 * TODO: most of this should not be here
|
|
103 */
|
|
104
|
|
105 void unicode_init();
|
|
106 void unicode_close();
|
|
107 int utf16_write( FILE* stream, const void *buf, size_t count );
|
|
108 int utf16_fprintf( FILE* stream, const char *fmt, ... );
|
|
109 int utf16to8( char *inbuf_o, char *outbuf_o, int length );
|
|
110 int utf8to16( char *inbuf_o, int iblen, char *outbuf_o, int oblen);
|
|
111 int vb_utf8to16T( vbuf *bout, char *cin, int inlen );
|
|
112 int vb_utf16to8( vbuf *dest, char *buf, int len );
|
|
113 int iso8859_1to8( char *inbuf_o, char *outbuf_o, int length );
|
|
114 int utf8toascii( const char *inbuf_o, char *outbuf_o, int length );
|
|
115
|
|
116 /* dump ascii hex in windoze format */
|
|
117 void winhex(FILE* stream, unsigned char *hbuf, int start, int stop, int loff);
|
|
118 void winhex8(FILE *stream, unsigned char *hbuf, int start, int stop, int loff );
|
|
119
|
|
120 void vbwinhex8(vbuf *vb, unsigned char *hbuf, int start, int stop, int loff );
|
|
121
|
|
122 /* general search routine, find something in something else */
|
|
123 int find_in_buf(char *buf, char *what, int sz, int len, int start);
|
|
124
|
|
125 /* Get INTEGER from memory. This is probably low-endian specific? */
|
|
126 int get_int( char *array );
|
|
127
|
|
128 int find_nl( vstr *vs ); // find newline of type type in b
|
|
129 int skip_nl( char *s ); // returns the width of the newline at s[0]
|
|
130 //int vb_readline( struct varbuf *vb, int *ctype, FILE *in ); // read *AT LEAST* one full line of data from in
|
|
131 int vb_skipline( struct varbuf *vb ); // in: vb->b == "stuff\nmore_stuff"; out: vb->b == "more_stuff"
|
|
132 /* Get a string of HEX bytes (space separated),
|
|
133 * or if first char is ' get an ASCII string instead. */
|
|
134 int gethexorstr(char **c, char *wb);
|
|
135 char *esc_index( char *s, int c ); // just like index(3), but works on strings with escape sequences
|
|
136 char *esc_rindex( char *s, int c ); // just like rindex(3), but works on strings with escape sequences
|
|
137
|
|
138 char *tok_esc_char( char *s, int *is_esc, int *c );
|
|
139 int vb_path_token( vbuf *tok, char **path ); // returns things like TOK_EMPTY, TOK_ERROR, complete list at top
|
|
140
|
|
141 int gettoken( char *tok, int len, char **path, char delim ); // Path tokenizer: increments path, dumps token in tok
|
|
142 #endif
|