Main Page | Namespace List | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

vbuf.h

Go to the documentation of this file.
00001 /* vbuf.h - variable length buffer functions
00002  *
00003  * Functions that try to make dealing with buffers easier.
00004  *
00005  * vbuf
00006  *
00007  * vstr
00008  * - should always contain a valid string
00009  *
00010  */
00011 
00012 #ifndef VBUF_H
00013 #define VBUF_H
00014 #define SZ_MAX     4096
00015 #include <stdlib.h>
00016 #include <stdio.h>
00017 #include <stdarg.h>
00018 /***************************************************/
00019 
00020 // Tokenizer const TOK_EMPTY, TOK_ELEMENT, DELIM
00021 #define DELIM '\\'
00022 
00023 #define TOK_EMPTY   0
00024 #define TOK_DELIM   1
00025 #define TOK_PARENT  2
00026 #define TOK_CURRENT 3
00027 #define TOK_ELEMENT 4
00028 
00029 #define TOK_ERROR   10
00030 #define TOK_BUF_SMALL   11
00031 
00032 
00033 
00034 // Variable-length buffers
00035 struct varbuf {
00036     size_t dlen;    //length of data stored in buffer
00037     size_t blen;    //length of buffer
00038     char *buf;      //buffer
00039     char *b;        //start of stored data
00040 };
00041 
00042 
00043 // The exact same thing as a varbuf but should always contain at least '\0'
00044 struct varstr {
00045     size_t dlen;    //length of data stored in buffer
00046     size_t blen;    //length of buffer
00047     char *buf;      //buffer
00048     char *b;        //start of stored data
00049 };
00050 
00051 
00052 typedef struct varbuf vbuf;
00053 typedef struct varstr vstr;
00054 
00055 #define VBUF_STATIC(x,y) static vbuf *x = NULL; if(!x) x = vballoc(y);
00056 #define VSTR_STATIC(x,y) static vstr *x = NULL; if(!x) x = vsalloc(y);
00057 
00058 // vbuf functions
00059 struct varbuf *vballoc( size_t len );
00060 void vbfree(      vbuf *vb );
00061 void vbclear(     vbuf *vb ); //ditch the data, keep the buffer
00062 void vbresize(    vbuf *vb, size_t len );
00063 size_t vbavail(   vbuf *vb );
00064 void vbdump(      vbuf *vb );
00065 void vbgrow(      vbuf *vb, size_t len ); // grow buffer by len bytes, data are preserved
00066 void vbset(       vbuf *vb, void *data, size_t len );
00067 void vbskipws(    vbuf *vb );
00068 void vbappend(    vbuf *vb, void *data, size_t length );
00069 void vbskip(      vbuf *vb, size_t skip );
00070 void vboverwrite( vbuf *vbdest, vbuf *vbsrc );
00071 
00072 // vstr functions
00073 vstr *vsalloc( size_t len );
00074 char *vsb(      vstr *vs );
00075 size_t vslen(     vstr *vs ); //strlen
00076 void vsfree(      vstr *vs );
00077 void vsset(       vstr *vs, char *s ); // Store string s in vb
00078 void vsnset(      vstr *vs, char *s, size_t n ); // Store string s in vb
00079 void vsgrow(      vstr *vs, size_t len ); // grow buffer by len bytes, data are preserved
00080 size_t vsavail(   vstr *vs );
00081 void vscat(       vstr *vs, char *str );
00082 void vsncat(      vstr *vs, char *str, size_t len );
00083 void vsnprepend(  vstr *vs, char *str, size_t len ) ;
00084 void vsskip(      vstr *vs, size_t len );
00085 int  vscmp(       vstr *vs, char *str );
00086 void vsskipws(    vstr *vs );
00087 void vs_printf(   vstr *vs, char *fmt, ... );
00088 void vs_printfa(  vstr *vs, char *fmt, ... );
00089 void vshexdump(   vstr *vs, char *b, size_t start, size_t stop, int ascii );
00090 int  vscatprintf( vstr *vs, char *fmt, ... );
00091 void vsvprintf(   vstr *vs, char *fmt, va_list ap );
00092 void vstrunc(     vstr *vs, size_t off ); // Drop chars [off..dlen]
00093 int  vslast(      vstr *vs ); // returns the last character stored in a vstr string
00094 void vscharcat(   vstr *vs, int ch );
00095 int  vsutf16(     vstr *vs, vbuf *in ); //in: in=zero-terminated utf16; out: vs=utf8; returns: 0 on success, else on fail
00096 
00097 int vs_parse_escaped_string( vstr *vs, char *str, size_t len );
00098 
00099 
00100 /*
00101  * Windows unicode output trash - this stuff sucks
00102  * TODO: most of this should not be here
00103  */
00104 
00105 void unicode_init();
00106 void unicode_close();
00107 int utf16_write( FILE* stream, const void *buf, size_t count );
00108 int utf16_fprintf( FILE* stream, const char *fmt, ... );
00109 int utf16to8( char *inbuf_o, char *outbuf_o, int length );
00110 int utf8to16( char *inbuf_o, int iblen, char *outbuf_o, int oblen);
00111 int vb_utf8to16T( vbuf *bout, char *cin, int inlen );
00112 int vb_utf16to8( vbuf *dest, char *buf, int len );
00113 int iso8859_1to8( char *inbuf_o, char *outbuf_o, int length );
00114 int utf8toascii( const char *inbuf_o, char *outbuf_o, int length );
00115 
00116 /* dump ascii hex in windoze format */
00117 void winhex(FILE* stream, unsigned char *hbuf, int start, int stop, int loff);
00118 void winhex8(FILE *stream, unsigned char *hbuf, int start, int stop, int loff );
00119 
00120 void vbwinhex8(vbuf *vb, unsigned char *hbuf, int start, int stop, int loff );
00121 
00122 /* general search routine, find something in something else */
00123 int find_in_buf(char *buf, char *what, int sz, int len, int start);
00124 
00125 /* Get INTEGER from memory. This is probably low-endian specific? */
00126 int get_int( char *array );
00127 
00128 int find_nl( vstr *vs ); // find newline of type type in b
00129 int skip_nl( char *s ); // returns the width of the newline at s[0]
00130 //int vb_readline( struct varbuf *vb, int *ctype, FILE *in ); // read *AT LEAST* one full line of data from in
00131 int vb_skipline( struct varbuf *vb ); // in: vb->b == "stuff\nmore_stuff"; out: vb->b == "more_stuff"
00132 /* Get a string of HEX bytes (space separated),
00133  * or if first char is ' get an ASCII string instead.  */
00134 int gethexorstr(char **c, char *wb);
00135 char *esc_index( char *s, int c ); // just like index(3), but works on strings with escape sequences
00136 char *esc_rindex( char *s, int c ); // just like rindex(3), but works on strings with escape sequences
00137 
00138 char *tok_esc_char( char *s, int *is_esc, int *c );
00139 int vb_path_token( vbuf *tok, char **path ); // returns things like TOK_EMPTY, TOK_ERROR, complete list at top
00140 
00141 int gettoken( char *tok, int len, char **path, char delim ); // Path tokenizer: increments path, dumps token in tok
00142 #endif

Generated on Thu Dec 11 11:58:49 2008 for 'LibPst' by  doxygen 1.3.9.1