annotate src/vbuf.c @ 69:63c02a242ca9

minor code cleanup
author Carl Byington <carl@five-ten-sg.com>
date Sat, 10 May 2008 12:45:23 -0700
parents f6db1f060a95
children b12f4e50e2e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
43
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
1 // {{{ includes
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
2
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
3 #include <ctype.h>
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
4 //#include "defines.h"
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
5 #include <errno.h>
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
6 #include <iconv.h>
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
7 #include <malloc.h>
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
8 #include <signal.h>
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
9 #include <stdarg.h>
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
10 #include <stdio.h>
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
11 #include <stdlib.h>
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
12 #include <string.h>
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
13 #include "vbuf.h"
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
14 #include "generic.h"
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
15
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
16 #ifdef WITH_DMALLOC
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
17 #include <dmalloc.h>
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
18 #endif
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
19
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
20 // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
21
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
22 int skip_nl( char *s ) // {{{ returns the width of the newline at s[0]
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
23 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
24 if( s[0] == '\n' ) return 1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
25 if( s[0] == '\r' && s[1] == '\n' ) return 2;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
26 if( s[0] == '\0' ) return 0;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
27 return -1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
28 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
29 int find_nl( vstr *vs ) // {{{ find newline of type type in b
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
30 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
31 char *nextr, *nextn;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
32
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
33 nextr = memchr( vs->b, '\r', vs->dlen );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
34 nextn = memchr( vs->b, '\n', vs->dlen );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
35
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
36 //case 1: UNIX, we find \n first
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
37 if( nextn && (nextr == NULL || nextr > nextn ) ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
38 return nextn - vs->b;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
39 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
40
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
41 //case 2: DOS, we find \r\n
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
42 if( NULL != nextr && NULL != nextn && 1 == (char*)nextn - (char*)nextr ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
43 return nextr - vs->b;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
44 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
45
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
46 //case 3: we find nothing
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
47
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
48 return -1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
49 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
50
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
51 // {{{ UTF8 <-> UTF16 <-> ISO8859 Character set conversion functions and (ack) their globals
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
52
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
53 //TODO: the following should not be
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
54 char *wwbuf=NULL;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
55 size_t nwwbuf=0;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
56 static int unicode_up=0;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
57 iconv_t i16to8, i8to16, i8859_1to8, i8toi8859_1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
58
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
59 void unicode_init() // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
60 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
61 char *wipe = "";
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
62 char dump[4];
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
63
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
64 if( unicode_up ) unicode_close();
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
65
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
66 if( (iconv_t)-1 == (i16to8 = iconv_open( "UTF-8", "UTF-16" ) ) ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
67 fprintf(stderr, "doexport(): Couldn't open iconv descriptor for UTF-16 to UTF-8.\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
68 exit( 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
69 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
70
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
71 if( (iconv_t)-1 == (i8to16 = iconv_open( "UTF-16", "UTF-8" ) ) ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
72 fprintf(stderr, "doexport(): Couldn't open iconv descriptor for UTF-8 to UTF-16.\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
73 exit( 2 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
74 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
75
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
76 //iconv will prefix output with an FF FE (utf-16 start seq), the following dumps that.
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
77 memset( dump, 'x', 4 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
78 ASSERT( 0 == utf8to16( wipe, 1, dump, 4 ), "unicode_init(): attempt to dump FF FE failed." );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
79
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
80 if( (iconv_t)-1 == (i8859_1to8 = iconv_open( "UTF-8", "ISO_8859-1" ) ) ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
81 fprintf(stderr, "doexport(): Couldn't open iconv descriptor for ASCII to UTF-8.\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
82 exit( 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
83 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
84
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
85
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
86 if( (iconv_t)-1 == (i8toi8859_1 = iconv_open( "ISO_8859-1", "UTF-8" ) ) ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
87 fprintf(stderr, "doexport(): Couldn't open iconv descriptor for UTF-8 to ASCII.\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
88 exit( 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
89 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
90
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
91 unicode_up = 1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
92 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
93 // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
94 void unicode_close() // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
95 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
96 unicode_up = 0;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
97 iconv_close( i8to16 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
98 iconv_close( i16to8 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
99 iconv_close( i8859_1to8 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
100 iconv_close( i8toi8859_1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
101 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
102 // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
103
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
104 //int utf16_write( FILE* stream, const void *buf, size_t count ) // {{{ write utf-8 or iso_8869-1 to stream after converting it to utf-16
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
105 //{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
106 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
107 // //TODO: if anything big comes through here we are sunk, should do it
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
108 // //bit-by-bit, not one-big-gulp
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
109 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
110 // size_t inbytesleft, outbytesleft;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
111 // char *inbuf, *outbuf;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
112 // size_t icresult;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
113 // size_t rl;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
114 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
115 // //do we have enough buffer space?
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
116 // if( !wwbuf || nwwbuf < (count * 2 + 2) ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
117 // wwbuf = F_REALLOC( wwbuf, count * 2 +2 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
118 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
119 // nwwbuf = count * 2 + 2;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
120 // }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
121 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
122 // inbytesleft = count; outbytesleft = nwwbuf;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
123 // inbuf = (char*)buf; outbuf = wwbuf;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
124 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
125 //// fprintf(stderr, "X%s, %dX", (char*)buf, strlen( buf ));
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
126 //// fflush(stderr);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
127 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
128 // if( (rl = strlen( buf ) + 1) != count ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
129 // fprintf(stderr, "utf16_write(): reported buffer size (%d) does not match string length (%d)\n",
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
130 // count,
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
131 // rl);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
132 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
133 // //hexdump( (char*)buf, 0, count, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
134 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
135 // raise( SIGSEGV );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
136 // inbytesleft = rl;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
137 // }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
138 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
139 //// fprintf(stderr, " attempting to convert:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
140 //// hexdump( (char*)inbuf, 0, count, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
141 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
142 // icresult = iconv( i8to16, &inbuf, &inbytesleft, &outbuf, &outbytesleft );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
143 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
144 //// fprintf(stderr, " converted:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
145 //// hexdump( (char*)buf, 0, count, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
146 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
147 //// fprintf(stderr, " to:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
148 //// hexdump( (char*)wwbuf, 0, nwwbuf, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
149 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
150 // if( (size_t)-1 == icresult ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
151 // fprintf(stderr, "utf16_write(): iconv failure(%d): %s\n", errno, strerror( errno ) );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
152 // fprintf(stderr, " attempted to convert:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
153 // hexdump( (char*)inbuf, 0, count, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
154 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
155 // fprintf(stderr, " result:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
156 // hexdump( (char*)outbuf, 0, count, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
157 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
158 // fprintf(stderr, "I'm going to segfault now.\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
159 // raise( SIGSEGV );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
160 // exit(1);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
161 // }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
162 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
163 // if( inbytesleft > 0 ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
164 // fprintf(stderr, "utf16_write(): iconv returned a short count.\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
165 // exit(1);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
166 // }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
167 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
168 // return fwrite( wwbuf, nwwbuf - outbytesleft - 2, 1, stream );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
169 //}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
170 // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
171
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
172 //char *utf16buf = NULL;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
173 //int utf16buf_len = 0;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
174 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
175 //int utf16_fprintf( FILE* stream, const char *fmt, ... ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
176 //{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
177 // int result=0;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
178 // va_list ap;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
179 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
180 // if( utf16buf == NULL ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
181 // utf16buf = (char*)F_MALLOC( SZ_MAX + 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
182 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
183 // utf16buf_len = SZ_MAX + 1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
184 // }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
185 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
186 // va_start( ap, fmt );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
187 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
188 // result = vsnprintf( utf16buf, utf16buf_len, fmt, ap );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
189 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
190 // if( result + 1 > utf16buf_len ) { //didn't have space, realloc() and try again
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
191 // fprintf(stderr, "utf16_fprintf(): buffer too small (%d), F_MALLOC(%d)\n", utf16buf_len, result);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
192 // free( utf16buf );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
193 // utf16buf_len = result + 1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
194 // utf16buf = (char*)F_MALLOC( utf16buf_len );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
195 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
196 // result = vsnprintf( utf16buf, utf16buf_len, fmt, ap );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
197 // }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
198 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
199 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
200 // //didn't have space...again...something weird is going on...
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
201 // ASSERT( result + 1 <= utf16buf_len, "utf16_fprintf(): Unpossible error!\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
202 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
203 // if( 1 != utf16_write( stream, utf16buf, result + 1 ) )
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
204 // DIE( "Write error? -> %s or %s\n", strerror( errno ), uerr_str( uerr_get() ) );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
205 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
206 // return result;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
207 //}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
208 //// }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
209 //int utf16to8( char *inbuf_o, char *outbuf_o, int length ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
210 //{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
211 // int inbytesleft = length;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
212 // int outbytesleft = length;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
213 // char *inbuf = inbuf_o;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
214 // char *outbuf = outbuf_o;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
215 // int rlen = -1, tlen;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
216 // int icresult = -1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
217 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
218 // int i, strlen=-1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
219 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
220 // DEBUG(
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
221 // fprintf(stderr, " utf16to8(): attempting to convert:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
222 // //hexdump( (char*)inbuf_o, 0, length, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
223 // fflush(stderr);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
224 // );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
225 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
226 // for( i=0; i<length ; i+=2 ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
227 // if( inbuf_o[i] == 0 && inbuf_o[i + 1] == 0 ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
228 // //fprintf(stderr, "End of string found at: %d\n", i );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
229 // strlen = i;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
230 // }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
231 // }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
232 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
233 // //hexdump( (char*)inbuf_o, 0, strlen, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
234 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
235 // if( -1 == strlen ) WARN("String is not zero-terminated.");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
236 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
237 // //iconv does not like it when the inbytesleft > actual string length
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
238 // //enum: zero terminated, length valid
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
239 // // zero terminated, length short //we won't go beyond length ever, so this is same as NZT case
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
240 // // zero terminated, length long
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
241 // // not zero terminated
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
242 // // TODO: MEMORY BUG HERE!
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
243 // for( tlen = 0; tlen <= inbytesleft - 2; tlen+=2 ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
244 // if( inbuf_o[tlen] == 0 && inbuf_o[tlen+1] == 0 ){
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
245 // rlen = tlen + 2;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
246 // tlen = rlen;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
247 // break;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
248 // }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
249 // if( tlen == inbytesleft )fprintf(stderr, "Space allocated for string > actual string length. Go windows!\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
250 // }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
251 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
252 // if( rlen >= 0 )
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
253 // icresult = iconv( i16to8, &inbuf, &rlen, &outbuf, &outbytesleft );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
254 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
255 // if( icresult == (size_t)-1 ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
256 // fprintf(stderr, "utf16to8(): iconv failure(%d): %s\n", errno, strerror( errno ) );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
257 // fprintf(stderr, " attempted to convert:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
258 // hexdump( (char*)inbuf_o, 0, length, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
259 // fprintf(stderr, " result:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
260 // hexdump( (char*)outbuf_o, 0, length, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
261 // fprintf(stderr, " MyDirtyOut:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
262 // for( i=0; i<length; i++) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
263 // if( inbuf_o[i] != '\0' ) fprintf(stderr, "%c", inbuf_o[i] );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
264 // }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
265 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
266 // fprintf( stderr, "\n" );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
267 // raise( SIGSEGV );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
268 // exit(1);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
269 // }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
270 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
271 // DEBUG(
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
272 // fprintf(stderr, " result:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
273 // hexdump( (char*)outbuf_o, 0, length, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
274 // )
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
275 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
276 // //fprintf(stderr, "utf16to8() returning %s\n", outbuf );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
277 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
278 // return icresult;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
279 //}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
280 //// }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
281 int utf16_is_terminated( char *str, int length ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
282 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
283 VSTR_STATIC( errbuf, 100 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
284 int len = -1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
285 int i;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
286 for( i=0; i<length ; i+=2 ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
287 if( str[i] == 0 && str[i + 1] == 0 ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
288 //fprintf(stderr, "End of string found at: %d\n", i );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
289 len = i;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
290 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
291 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
292
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
293 //hexdump( (char*)inbuf_o, 0, len, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
294
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
295 if( -1 == len ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
296 vshexdump( errbuf, str, 0, length, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
297 WARN("String is not zero terminated (probably broken data from registry) %s.", errbuf->b);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
298 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
299
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
300 return (-1 == len )?0:1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
301 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
302 int vb_utf16to8( vbuf *dest, char *buf, int len ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
303 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
304 int inbytesleft = len;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
305 char *inbuf = buf;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
306 //int rlen = -1, tlen;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
307 int icresult = -1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
308 VBUF_STATIC( dumpster, 100 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
309
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
310 //int i; //, strlen=-1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
311 int outbytesleft;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
312 char *outbuf;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
313
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
314 ASSERT( unicode_up, "vb_utf16to8() called before unicode started." );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
315
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
316 if( 2 > dest->blen ) vbresize( dest, 2 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
317 dest->dlen = 0;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
318
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
319 //Bad Things can happen if a non-zero-terminated utf16 string comes through here
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
320 if( !utf16_is_terminated( buf, len ) ) return -1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
321
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
322 do {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
323 outbytesleft = dest->blen - dest->dlen;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
324 outbuf = dest->b + dest->dlen;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
325 icresult = iconv( i16to8, &inbuf, &inbytesleft, &outbuf, &outbytesleft );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
326 dest->dlen = outbuf - dest->b;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
327 vbgrow( dest, inbytesleft);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
328 } while( (size_t)-1 == icresult && E2BIG == errno );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
329
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
330 if( 0 != vb_utf8to16T( dumpster, dest->b, dest->dlen ) )
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
331 DIE("Reverse conversion failed.");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
332
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
333 if( icresult == (size_t)-1 ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
334 //TODO: error
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
335 //ERR_UNIX( errno, "vb_utf16to8():iconv failure: %s", strerror( errno ) );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
336 unicode_init();
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
337 return -1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
338 /*
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
339 fprintf(stderr, " attempted to convert:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
340 hexdump( (char*)cin, 0, inlen, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
341 fprintf(stderr, " result:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
342 hexdump( (char*)bout->b, 0, bout->dlen, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
343 fprintf(stderr, " MyDirtyOut:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
344 for( i=0; i<inlen; i++) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
345 if( inbuf[i] != '\0' ) fprintf(stderr, "%c", inbuf[i] );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
346 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
347
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
348 fprintf( stderr, "\n" );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
349 raise( SIGSEGV );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
350 exit(1);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
351 */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
352 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
353
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
354 if( icresult ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
355 //ERR_UNIX( EILSEQ, "Uhhhh...vb_utf16to8() returning icresult == %d", icresult );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
356 return -1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
357 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
358 return icresult;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
359 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
360 // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
361
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
362 int utf8to16( char *inbuf_o, int iblen, char *outbuf_o, int oblen) // {{{ iblen, oblen: bytes including \0
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
363 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
364 //TODO: this is *only* used to dump the utf16 preamble now...
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
365 //TODO: This (and 8to16) are the most horrible things I have ever seen...
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
366 int inbytesleft;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
367 int outbytesleft = oblen;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
368 char *inbuf = inbuf_o;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
369 char *outbuf = outbuf_o;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
370 //int rlen = -1, tlen;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
371 int icresult = -1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
372
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
373 char *stend;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
374
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
375 //int i; //, strlen=-1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
376
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
377 DEBUG(
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
378 fprintf(stderr, " utf8to16(): attempting to convert:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
379 //hexdump( (char*)inbuf_o, 0, length, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
380 fflush(stderr);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
381 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
382
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
383 stend = memchr( inbuf_o, '\0', iblen );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
384 ASSERT( NULL != stend, "utf8to16(): in string not zero terminated." );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
385
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
386 inbytesleft = ( stend - inbuf_o + 1 < iblen )? stend - inbuf_o + 1: iblen;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
387
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
388 //iconv does not like it when the inbytesleft > actual string length
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
389 //enum: zero terminated, length valid
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
390 // zero terminated, length short //we won't go beyond length ever, so this is same as NZT case
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
391 // zero terminated, length long
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
392 // not zero terminated
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
393 // TODO: MEMORY BUG HERE!
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
394 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
395 /*
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
396 for( tlen = 0; tlen <= inbytesleft - 2; tlen+=2 ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
397 if( inbuf_o[tlen] == 0 && inbuf_o[tlen+1] == 0 ){
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
398 rlen = tlen + 2;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
399 tlen = rlen;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
400 break;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
401 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
402 if( tlen == inbytesleft )fprintf(stderr, "Space allocated for string > actual string length. Go windows!\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
403 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
404 */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
405
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
406 //if( rlen >= 0 )
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
407 icresult = iconv( i8to16, &inbuf, &inbytesleft, &outbuf, &outbytesleft );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
408
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
409 if( icresult == (size_t)-1 ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
410 DIE("iconv failure(%d): %s\n", errno, strerror( errno ) );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
411 //fprintf(stderr, " attempted to convert:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
412 //hexdump( (char*)inbuf_o, 0, iblen, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
413 //fprintf(stderr, " result:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
414 //hexdump( (char*)outbuf_o, 0, oblen, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
415 //fprintf(stderr, " MyDirtyOut:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
416 // for( i=0; i<iblen; i++) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
417 // if( inbuf_o[i] != '\0' ) fprintf(stderr, "%c", inbuf_o[i] );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
418 // }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
419 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
420 // fprintf( stderr, "\n" );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
421 // raise( SIGSEGV );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
422 // exit(1);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
423 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
424
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
425 // DEBUG(
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
426 // fprintf(stderr, " result:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
427 // hexdump( (char*)outbuf_o, 0, oblen, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
428 // )
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
429
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
430 //fprintf(stderr, "utf8to16() returning %s\n", outbuf );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
431
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
432 //TODO: error
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
433 if( icresult ) printf("Uhhhh...utf8to16() returning icresult == %d\n", icresult );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
434 return icresult;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
435 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
436 // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
437
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
438 int vb_utf8to16T( vbuf *bout, char *cin, int inlen ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
439 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
440 //TODO: This (and 8to16) are the most horrible things I have ever seen...
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
441 int inbytesleft = inlen;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
442 char *inbuf = cin;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
443 //int rlen = -1, tlen;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
444 int icresult = -1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
445
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
446 //int i; //, strlen=-1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
447
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
448 //if( rlen >= 0 )
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
449 int outbytesleft;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
450 char *outbuf;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
451 if( 2 > bout->blen ) vbresize( bout, 2 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
452 bout->dlen = 0;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
453
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
454 do {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
455 outbytesleft = bout->blen - bout->dlen;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
456 outbuf = bout->b + bout->dlen;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
457 icresult = iconv( i8to16, &inbuf, &inbytesleft, &outbuf, &outbytesleft );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
458 bout->dlen = outbuf - bout->b;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
459 vbgrow( bout, 20 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
460 } while( (size_t)-1 == icresult && E2BIG == errno );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
461
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
462 if( icresult == (size_t)-1 ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
463 WARN("iconv failure: %s", strerror( errno ) );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
464 //ERR_UNIX( errno, "vb_utf8to16():iconv failure: %s", strerror( errno ) );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
465 unicode_init();
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
466 return -1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
467 /*
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
468 fprintf(stderr, "vb_utf8to16(): iconv failure(%d == %d?): %s\n", errno, E2BIG, strerror( errno ) );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
469 fprintf(stderr, " attempted to convert:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
470 hexdump( (char*)cin, 0, inlen, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
471 fprintf(stderr, " result:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
472 hexdump( (char*)bout->b, 0, bout->dlen, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
473 fprintf(stderr, " MyDirtyOut:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
474 for( i=0; i<inlen; i++) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
475 if( inbuf[i] != '\0' ) fprintf(stderr, "%c", inbuf[i] );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
476 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
477
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
478 fprintf( stderr, "\n" );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
479 raise( SIGSEGV );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
480 exit(1);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
481 */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
482 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
483
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
484 //TODO: error
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
485 if( icresult ) printf("Uhhhh...vb_utf8to16() returning icresult == %d\n", icresult );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
486 return icresult;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
487 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
488 // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
489 #if 1
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
490 void cheap_uni2ascii(char *src, char *dest, int l) /* {{{ Quick and dirty UNICODE to std. ascii */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
491 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
492
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
493 for (; l > 0; l -=2) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
494 *dest = *src;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
495 dest++; src +=2;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
496 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
497 *dest = 0;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
498 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
499 // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
500 #endif
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
501
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
502 void cheap_ascii2uni(char *src, char *dest, int l) /* {{{ Quick and dirty ascii to unicode */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
503 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
504 for (; l > 0; l--) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
505 *dest++ = *src++;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
506 *dest++ = 0;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
507
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
508 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
509 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
510 // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
511
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
512 // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
513 // {{{ VARBUF Functions
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
514 vbuf *vballoc( size_t len ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
515 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
516 struct varbuf *result;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
517
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
518 result = F_MALLOC( sizeof( struct varbuf ) );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
519
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
520 result->dlen = 0;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
521 result->blen = 0;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
522 result->buf = NULL;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
523
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
524 vbresize( result, len );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
525
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
526 return result;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
527
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
528 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
529 void vbcheck( vbuf *vb ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
530 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
531 ASSERT( vb->b - vb->buf <= vb->blen, "vbcheck(): vb->b outside of buffer range.");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
532 ASSERT( vb->dlen <= vb->blen, "vbcheck(): data length > buffer length.");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
533
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
534 ASSERT( vb->blen < 1024*1024, "vbcheck(): blen is a bit large...hmmm.");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
535 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
536 void vbfree( vbuf *vb ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
537 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
538 free( vb->buf );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
539 free( vb );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
540 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
541 void vbclear( struct varbuf *vb ) // {{{ditch the data, keep the buffer
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
542 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
543 vbresize( vb, 0 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
544 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
545 void vbresize( struct varbuf *vb, size_t len ) // {{{ DESTRUCTIVELY grow or shrink buffer
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
546 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
547 vb->dlen = 0;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
548
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
549 if( vb->blen >= len ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
550 vb->b = vb->buf;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
551 return;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
552 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
553
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
554 vb->buf = F_REALLOC( vb->buf, len );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
555 vb->b = vb->buf;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
556 vb->blen = len;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
557 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
558 int vbavail( vbuf *vb ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
559 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
560 return vb->blen - ((char*)vb->b - (char*)vb->buf + vb->dlen);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
561 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
562 //void vbdump( vbuf *vb ) // {{{ TODO: to stdout? Yuck
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
563 //{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
564 // printf("vb dump-------------\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
565 // printf("dlen: %d\n", vb->dlen );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
566 // printf("blen: %d\n", vb->blen );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
567 // printf("b - buf: %d\n", vb->b - vb->buf );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
568 // printf("buf:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
569 // hexdump( vb->buf, 0, vb->blen, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
570 // printf("b:\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
571 // hexdump( vb->b, 0, vb->dlen, 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
572 // printf("^^^^^^^^^^^^^^^^^^^^\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
573 //} // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
574 void vbgrow( struct varbuf *vb, size_t len ) // {{{ out: vbavail(vb) >= len, data are preserved
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
575 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
576 if( 0 == len ) return;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
577
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
578 if( 0 == vb->blen ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
579 vbresize( vb, len );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
580 return;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
581 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
582
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
583 if( vb->dlen + len > vb->blen ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
584 if( vb->dlen + len < vb->blen * 1.5 ) len = vb->blen * 1.5;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
585 char *nb = F_MALLOC( vb->blen + len );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
586 //printf("vbgrow() got %p back from malloc(%d)\n", nb, vb->blen + len);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
587 vb->blen = vb->blen + len;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
588 memcpy( nb, vb->b, vb->dlen );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
589
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
590 //printf("vbgrow() I am going to free %p\n", vb->buf );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
591 free( vb->buf );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
592 vb->buf = nb;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
593 vb->b = vb->buf;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
594 } else {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
595 if( vb->b != vb->buf )
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
596 memcpy( vb->buf, vb->b, vb->dlen );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
597 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
598
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
599 vb->b = vb->buf;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
600
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
601 ASSERT( vbavail( vb ) >= len, "vbgrow(): I have failed in my mission." );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
602 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
603 void vbset( vbuf *vb, void *b, size_t len ) // {{{ set vbuf b size=len, resize if necessary, relen = how much to over-allocate
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
604 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
605 vbresize( vb, len );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
606
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
607 memcpy( vb->b, b, len );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
608 vb->dlen = len;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
609 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
610 void vsskipws( vstr *vs ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
611 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
612 char *p = vs->b;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
613 while( p - vs->b < vs->dlen && isspace( p[0] ) ) p++;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
614
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
615 vbskip( (vbuf*)vs, p - vs->b );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
616 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
617 void vbappend( struct varbuf *vb, void *b, size_t len ) // {{{ append len bytes of b to vbuf, resize if necessary
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
618 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
619 if( 0 == vb->dlen ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
620 vbset( vb, b, len );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
621 return;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
622 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
623
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
624 vbgrow( vb, len );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
625
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
626 memcpy( vb->b + vb->dlen, b, len );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
627 vb->dlen += len;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
628
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
629 //printf("vbappend() end: >%s/%d<\n", vbuf->b, vbuf->dlen );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
630 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
631 void vbskip( struct varbuf *vb, size_t skip ) // {{{ dumps the first skip bytes from vbuf
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
632 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
633 ASSERT( skip <= vb->dlen, "vbskip(): Attempt to seek past end of buffer." );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
634 //memmove( vbuf->b, vbuf->b + skip, vbuf->dlen - skip );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
635 vb->b += skip;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
636 vb->dlen -= skip;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
637 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
638 void vboverwrite( struct varbuf *vbdest, struct varbuf *vbsrc ) // {{{ overwrite vbdest with vbsrc
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
639 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
640 vbresize( vbdest, vbsrc->blen );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
641 memcpy( vbdest->b, vbsrc->b, vbsrc->dlen );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
642 vbdest->blen = vbsrc->blen;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
643 vbdest->dlen = vbsrc->dlen;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
644 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
645 // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
646 // {{{ VARSTR Functions
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
647 vstr *vsalloc( size_t len ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
648 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
649 vstr *result = (vstr*)vballoc( len + 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
650 vsset( result, "" );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
651 return result;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
652 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
653 char *vsstr( vstr *vs ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
654 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
655 return vs->b;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
656 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
657 size_t vslen( vstr *vs ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
658 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
659 return strlen( vsstr( vs ));
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
660 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
661 void vsfree( vstr *vs ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
662 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
663 vbfree( (vbuf*)vs );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
664 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
665 void vscharcat( vstr *vb, int ch ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
666 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
667 vbgrow( (vbuf*)vb, 1);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
668 vb->b[vb->dlen-1] = ch;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
669 vb->b[vb->dlen] = '\0';
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
670 vb->dlen++;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
671 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
672 void vsnprepend( vstr *vb, char *str, size_t len ) // {{{ prependappend string str to vbuf, vbuf must already contain a valid string
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
673 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
674 ASSERT( vb->b[vb->dlen-1] == '\0', "vsncat(): attempt to append string to non-string.");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
675 int sl = strlen( str );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
676 int n = (sl<len)?sl:len;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
677 //string append
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
678 vbgrow( (vbuf*)vb, n + 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
679 memmove( vb->b + n, vb->b, vb->dlen - 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
680 memcpy( vb->b, str, n );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
681 //strncat( vb->b, str, n );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
682
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
683 vb->dlen += n;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
684 vb->b[ vb->dlen - 1 ] = '\0';
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
685 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
686 void vsskip( vstr *vs, size_t len ) // {{{ len < dlen-1 -> skip len chars, else DIE
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
687 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
688 ASSERT( len < vs->dlen - 1, "Attempt to skip past end of string" );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
689 vbskip( (vbuf*)vs, len );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
690 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
691 int vsskipline( vstr *vs ) // {{{ in: vb->b == "stuff\nmore_stuff"; out: vb->b == "more_stuff"
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
692 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
693 int nloff = find_nl( vs );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
694 int nll = skip_nl( vs->b + nloff );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
695
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
696 if( nloff < 0 ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
697 //TODO: error
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
698 printf("vb_skipline(): there seems to be no newline here.\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
699 return -1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
700 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
701 if( skip_nl < 0 ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
702 //TODO: error
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
703 printf("vb_skipline(): there seems to be no newline here...except there should be. :P\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
704 return -1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
705 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
706
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
707 memmove( vs->b, vs->b + nloff + nll, vs->dlen - nloff - nll );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
708
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
709 vs->dlen -= nloff + nll;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
710
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
711 return 0;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
712 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
713 int vscatprintf( vstr *vs, char *fmt, ... ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
714 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
715 int size;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
716 va_list ap;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
717
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
718 /* Guess we need no more than 100 bytes. */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
719 //vsresize( vb, 100 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
720 if(!vs->b || vs->dlen == 0) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
721 vsset( vs, "" );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
722 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
723
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
724 while (1) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
725 /* Try to print in the allocated space. */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
726 va_start(ap, fmt);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
727 size = vsnprintf (vs->b + vs->dlen - 1, vs->blen - vs->dlen, fmt, ap);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
728 va_end(ap);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
729
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
730 /* If that worked, return the string. */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
731 if (size > -1 && size < vs->blen - vs->dlen ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
732 vs->dlen += size;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
733 return size;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
734 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
735 /* Else try again with more space. */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
736 if ( size >= 0 ) /* glibc 2.1 */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
737 vbgrow( (vbuf*)vs, size+1 ); /* precisely what is needed */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
738 else /* glibc 2.0 */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
739 vbgrow( (vbuf*)vs, vs->blen);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
740 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
741 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
742 int vslast( vstr *vs ) // {{{ returns the last character stored in a vstr
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
743 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
744 if( vs->dlen < 1 ) return -1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
745 if( vs->b[vs->dlen-1] != '\0' ) return -1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
746 if( vs->dlen == 1 ) return '\0';
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
747 return vs->b[vs->dlen-2];
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
748 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
749 void vs_printf( vstr *vs, char *fmt, ... ) // {{{ print over vb
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
750 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
751 int size;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
752 va_list ap;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
753
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
754 /* Guess we need no more than 100 bytes. */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
755 vbresize( (vbuf*)vs, 100 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
756
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
757 while (1) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
758 /* Try to print in the allocated space. */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
759 va_start(ap, fmt);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
760 size = vsnprintf (vs->b, vs->blen, fmt, ap);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
761 va_end(ap);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
762
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
763 /* If that worked, return the string. */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
764 if (size > -1 && size < vs->blen) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
765 vs->dlen = size + 1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
766 return;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
767 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
768 /* Else try again with more space. */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
769 if ( size >= 0 ) /* glibc 2.1 */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
770 vbresize( (vbuf*)vs, size+1 ); /* precisely what is needed */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
771 else /* glibc 2.0 */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
772 vbresize( (vbuf*)vs, vs->blen*2);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
773 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
774 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
775 void vs_printfa( vstr *vs, char *fmt, ... ) // {{{ printf append to vs
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
776 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
777 int size;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
778 va_list ap;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
779
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
780 if( vs->blen - vs->dlen < 50 )
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
781 vbgrow( (vbuf*)vs, 100 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
782
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
783 while (1) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
784 /* Try to print in the allocated space. */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
785 va_start(ap, fmt);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
786 size = vsnprintf (vs->b + vs->dlen - 1, vs->blen - vs->dlen + 1, fmt, ap);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
787 va_end(ap);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
788
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
789 /* If that worked, return the string. */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
790 if (size > -1 && size < vs->blen) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
791 vs->dlen += size;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
792 return;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
793 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
794 /* Else try again with more space. */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
795 if ( size >= 0 ) /* glibc 2.1 */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
796 vbgrow( (vbuf*)vs, size+1 - vs->dlen ); /* precisely what is needed */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
797 else /* glibc 2.0 */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
798 vbgrow( (vbuf*)vs, size );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
799 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
800 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
801 void vshexdump( vstr *vs, char *b, size_t start, size_t stop, int ascii ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
802 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
803 char c;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
804 int diff,i;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
805
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
806 while (start < stop ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
807 diff = stop - start;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
808 if (diff > 16) diff = 16;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
809
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
810 vs_printfa(vs, ":%08X ",start);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
811
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
812 for (i = 0; i < diff; i++) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
813 if( 8 == i ) vs_printfa( vs, " " );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
814 vs_printfa(vs, "%02X ",(unsigned char)*(b+start+i));
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
815 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
816 if (ascii) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
817 for (i = diff; i < 16; i++) vs_printfa(vs, " ");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
818 for (i = 0; i < diff; i++) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
819 c = *(b+start+i);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
820 vs_printfa(vs, "%c", isprint(c) ? c : '.');
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
821 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
822 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
823 vs_printfa(vs, "\n");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
824 start += 16;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
825 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
826 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
827 void vsset( vstr *vs, char *s ) // {{{ Store string s in vs
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
828 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
829 vsnset( vs, s, strlen( s ) );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
830 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
831 void vsnset( vstr *vs, char *s, size_t n ) // {{{ Store string s in vs
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
832 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
833 vbresize( (vbuf*)vs, n + 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
834 memcpy( vs->b, s, n);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
835 vs->b[n] = '\0';
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
836 vs->dlen = n+1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
837 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
838 void vsgrow( vstr *vs, size_t len ) // {{{ grow buffer by len bytes, data are preserved
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
839 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
840 vbgrow( (vbuf*)vs, len );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
841 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
842 size_t vsavail( vstr *vs ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
843 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
844 return vbavail( (vbuf*)vs );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
845 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
846 void vsnset16( vstr *vs, char *s, size_t len ) // {{{ Like vbstrnset, but for UTF16
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
847 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
848 vbresize( (vbuf*)vs, len+1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
849 memcpy( vs->b, s, len );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
850
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
851 vs->b[len] = '\0';
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
852 vs->dlen = len+1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
853 vs->b[len] = '\0';
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
854 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
855 void vscat( vstr *vs, char *str ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
856 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
857 vsncat( vs, str, strlen(str ) );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
858 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
859 int vscmp( vstr *vs, char *str ) // {{{
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
860 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
861 return strcmp( vs->b, str );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
862 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
863 void vsncat( vstr *vs, char *str, size_t len ) // {{{ append string str to vstr, vstr must already contain a valid string
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
864 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
865 ASSERT( vs->b[vs->dlen-1] == '\0', "vsncat(): attempt to append string to non-string.");
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
866 int sl = strlen( str );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
867 int n = (sl<len)?sl:len;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
868 //string append
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
869 vbgrow( (vbuf*)vs, n + 1 );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
870 memcpy( vs->b + vs->dlen - 1, str, n );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
871 //strncat( vs->b, str, n );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
872
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
873 vs->dlen += n;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
874 vs->b[ vs->dlen - 1 ] = '\0';
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
875 } // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
876 void vstrunc( vstr *v, int off ) // {{{ Drop chars [off..dlen]
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
877 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
878 if( off >= v->dlen - 1 ) return; //nothing to do
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
879 v->b[off] = '\0';
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
880 v->dlen = off + 1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
881 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
882 // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
883 // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
884 // {{{ User input
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
885 // TODO: not sure how useful this stuff is here
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
886 int fmyinput(char *prmpt, char *ibuf, int maxlen) /* {{{ get user input */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
887 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
888 printf("%s",prmpt);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
889
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
890 fgets(ibuf,maxlen+1,stdin);
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
891
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
892 ibuf[strlen(ibuf)-1] = 0;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
893
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
894 return(strlen(ibuf));
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
895 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
896 // }}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
897 //}}}
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
898 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
899 //
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
900 //{{{ String formatting and output to FILE *stream or just stdout, etc
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
901 // TODO: a lot of old, unused stuff in here
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
902 void vswinhex8(vstr *vs, unsigned char *hbuf, int start, int stop, int loff ) // {{{ Produce regedit-style hex output */
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
903 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
904 int i;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
905 int lineflag=0;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
906
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
907 for( i=start; i<stop; i++)
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
908 {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
909 loff += vscatprintf( vs, "%02x", hbuf[i] );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
910 if( i < stop - 1 ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
911 loff+=vscatprintf( vs, "," );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
912 switch( lineflag ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
913 case 0:
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
914 if( loff >= 77) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
915 lineflag=1;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
916 loff=0;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
917 vscatprintf( vs, "\\%s ", STUPID_CR );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
918 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
919 break;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
920 case 1:
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
921 if( loff >= 75 ) {
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
922 loff=0;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
923 vscatprintf( vs, "\\%s ", STUPID_CR );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
924 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
925 break;
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
926 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
927 // if( 24 < i || 0 == (i - 17) % 25 ) fprintf( stream, "\\\n " );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
928 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
929 }
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
930
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
931 // fprintf( stream, "\n" );
f6db1f060a95 start on outlook 2003 64 bit format
carl
parents:
diff changeset
932 } // }}}