comparison src/vbuf.h @ 70:b12f4e50e2e8

Patch from Joachim Metz <joachim.metz@gmail.com> for 64 bit compile. Signed/unsigned cleanup from 'CFLAGS=-Wextra ./configure' Reindent vbuf.c to make it readable.
author Carl Byington <carl@five-ten-sg.com>
date Sun, 11 May 2008 10:04:33 -0700
parents f6db1f060a95
children ed2a260bbb98
comparison
equal deleted inserted replaced
69:63c02a242ca9 70:b12f4e50e2e8
1 /* {{{ vbuf.h - variable length buffer functions 1 /* vbuf.h - variable length buffer functions
2 * 2 *
3 * Functions that try to make dealing with buffers easier. 3 * Functions that try to make dealing with buffers easier.
4 * 4 *
5 * vbuf 5 * vbuf
6 * 6 *
7 * vstr 7 * vstr
8 * - should always contain a valid string 8 * - should always contain a valid string
9 * 9 *
10 * }}} */ 10 */
11 11
12 #ifndef VBUF_H 12 #ifndef VBUF_H
13 #define VBUF_H 13 #define VBUF_H
14 #define SZ_MAX 4096 14 #define SZ_MAX 4096
15 #include <stdlib.h> 15 #include <stdlib.h>
16 #include <stdio.h> 16 #include <stdio.h>
17 #include <stdarg.h> 17 #include <stdarg.h>
18 /***************************************************/ 18 /***************************************************/
19 19
20 // {{{ Tokenizer const TOK_EMPTY, TOK_ELEMENT, DELIM 20 // Tokenizer const TOK_EMPTY, TOK_ELEMENT, DELIM
21 #define DELIM '\\' 21 #define DELIM '\\'
22 22
23 #define TOK_EMPTY 0 23 #define TOK_EMPTY 0
24 #define TOK_DELIM 1 24 #define TOK_DELIM 1
25 #define TOK_PARENT 2 25 #define TOK_PARENT 2
26 #define TOK_CURRENT 3 26 #define TOK_CURRENT 3
27 #define TOK_ELEMENT 4 27 #define TOK_ELEMENT 4
28 28
29 #define TOK_ERROR 10 29 #define TOK_ERROR 10
30 #define TOK_BUF_SMALL 11 30 #define TOK_BUF_SMALL 11
31 // }}}
32 31
33 32
34 // Variable-length buffers 33
35 struct varbuf { // {{{ 34 // Variable-length buffers
35 struct varbuf {
36 size_t dlen; //length of data stored in buffer 36 size_t dlen; //length of data stored in buffer
37 size_t blen; //length of buffer 37 size_t blen; //length of buffer
38 char *buf; //buffer 38 char *buf; //buffer
39 char *b; //start of stored data 39 char *b; //start of stored data
40 }; // }}} 40 };
41 41
42 42
43 // The exact same thing as a varbuf but should always contain at least '\0' 43 // The exact same thing as a varbuf but should always contain at least '\0'
44 struct varstr { // {{{ 44 struct varstr {
45 size_t dlen; //length of data stored in buffer 45 size_t dlen; //length of data stored in buffer
46 size_t blen; //length of buffer 46 size_t blen; //length of buffer
47 char *buf; //buffer 47 char *buf; //buffer
48 char *b; //start of stored data 48 char *b; //start of stored data
49 }; // }}} 49 };
50 50
51 51
52 typedef struct varbuf vbuf; 52 typedef struct varbuf vbuf;
53 typedef struct varstr vstr; 53 typedef struct varstr vstr;
54 54
58 // vbuf functions 58 // vbuf functions
59 struct varbuf *vballoc( size_t len ); 59 struct varbuf *vballoc( size_t len );
60 void vbfree( vbuf *vb ); 60 void vbfree( vbuf *vb );
61 void vbclear( vbuf *vb ); //ditch the data, keep the buffer 61 void vbclear( vbuf *vb ); //ditch the data, keep the buffer
62 void vbresize( vbuf *vb, size_t len ); 62 void vbresize( vbuf *vb, size_t len );
63 int vbavail( vbuf *vb ); 63 size_t vbavail( vbuf *vb );
64 void vbdump( vbuf *vb ); 64 void vbdump( vbuf *vb );
65 void vbgrow( vbuf *vb, size_t len ); // grow buffer by len bytes, data are preserved 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 ); 66 void vbset( vbuf *vb, void *data, size_t len );
67 void vbskipws( vbuf *vb ); 67 void vbskipws( vbuf *vb );
68 void vbappend( vbuf *vb, void *data, size_t length ); 68 void vbappend( vbuf *vb, void *data, size_t length );
69 void vbskip( vbuf *vb, size_t skip ); 69 void vbskip( vbuf *vb, size_t skip );
70 void vboverwrite( vbuf *vbdest, vbuf *vbsrc ); 70 void vboverwrite( vbuf *vbdest, vbuf *vbsrc );
71 71
72 // vstr functions 72 // vstr functions
73 vstr *vsalloc( size_t len ); 73 vstr *vsalloc( size_t len );
74 char *vsb( vstr *vs ); 74 char *vsb( vstr *vs );
87 void vs_printf( vstr *vs, char *fmt, ... ); 87 void vs_printf( vstr *vs, char *fmt, ... );
88 void vs_printfa( 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 ); 89 void vshexdump( vstr *vs, char *b, size_t start, size_t stop, int ascii );
90 int vscatprintf( vstr *vs, char *fmt, ... ); 90 int vscatprintf( vstr *vs, char *fmt, ... );
91 void vsvprintf( vstr *vs, char *fmt, va_list ap ); 91 void vsvprintf( vstr *vs, char *fmt, va_list ap );
92 void vstrunc( vstr *vs, int off ); // Drop chars [off..dlen] 92 void vstrunc( vstr *vs, size_t off ); // Drop chars [off..dlen]
93 int vslast( vstr *vs ); // returns the last character stored in a vstr string 93 int vslast( vstr *vs ); // returns the last character stored in a vstr string
94 void vscharcat( vstr *vs, int ch ); 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 95 int vsutf16( vstr *vs, vbuf *in ); //in: in=zero-terminated utf16; out: vs=utf8; returns: 0 on success, else on fail
96 96
97 int vs_parse_escaped_string( vstr *vs, char *str, size_t len ); 97 int vs_parse_escaped_string( vstr *vs, char *str, size_t len );