comparison src/vbuf.c @ 176:ac6e22c8a9cf

build separate libpst, libpst-libs, libpst-devel rpms. remove many functions from the interface by making them static.
author Carl Byington <carl@five-ten-sg.com>
date Wed, 08 Apr 2009 14:13:06 -0700
parents 6954d315aaa8
children b65e8d0a088a
comparison
equal deleted inserted replaced
175:03dbd449290f 176:ac6e22c8a9cf
1 1
2 #include "define.h" 2 #include "define.h"
3 3
4 static int pst_skip_nl(char *s); // returns the width of the newline at s[0]
5 static int pst_find_nl(vstr *vs); // find newline of type type in b
6
7 // vbuf functions
8 static void pst_vbfree(vbuf *vb);
9 static void pst_vbclear(vbuf *vb); // ditch the data, keep the buffer
10 static void pst_vbresize(vbuf *vb, size_t len);
11 static size_t pst_vbavail(vbuf *vb);
12 static void pst_vbdump(vbuf *vb);
13 static void pst_vbskipws(vbuf *vb);
14 static void pst_vbskip(vbuf *vb, size_t skip);
15 static void pst_vboverwrite(vbuf *vbdest, vbuf *vbsrc);
16
17 // vstr functions
18 static vstr *pst_vsalloc(size_t len);
19 static char *pst_vsb(vstr *vs);
20 static size_t pst_vslen(vstr *vs); //strlen
21 static void pst_vsfree(vstr *vs);
22 static void pst_vsset(vstr *vs, char *s); // Store string s in vb
23 static void pst_vsnset(vstr *vs, char *s, size_t n); // Store string s in vb
24 static void pst_vsgrow(vstr *vs, size_t len); // grow buffer by len bytes, data are preserved
25 static size_t pst_vsavail(vstr *vs);
26 static void pst_vscat(vstr *vs, char *str);
27 static void pst_vsncat(vstr *vs, char *str, size_t len);
28 static void pst_vsnprepend(vstr *vs, char *str, size_t len) ;
29 static void pst_vsskip(vstr *vs, size_t len);
30 static int pst_vscmp(vstr *vs, char *str);
31 static void pst_vsskipws(vstr *vs);
32 static void pst_vs_printf(vstr *vs, char *fmt, ...);
33 static void pst_vs_printfa(vstr *vs, char *fmt, ...);
34 static void pst_vshexdump(vstr *vs, const char *b, size_t start, size_t stop, int ascii);
35 static int pst_vscatprintf(vstr *vs, char *fmt, ...);
36 static void pst_vsvprintf(vstr *vs, char *fmt, va_list ap);
37 static void pst_vstrunc(vstr *vs, size_t off); // Drop chars [off..dlen]
38 static int pst_vslast(vstr *vs); // returns the last character stored in a vstr string
39 static void pst_vscharcat(vstr *vs, int ch);
40
41 static void pst_unicode_close();
42
43 static int pst_vb_skipline(vbuf *vb); // in: vb->b == "stuff\nmore_stuff"; out: vb->b == "more_stuff"
44
45
46
4 47
5 #define ASSERT(x,...) { if( !(x) ) DIE(( __VA_ARGS__)); } 48 #define ASSERT(x,...) { if( !(x) ) DIE(( __VA_ARGS__)); }
6 49
7 50
8 int pst_skip_nl(char *s) 51 static int pst_skip_nl(char *s)
9 { 52 {
10 if (s[0] == '\n') 53 if (s[0] == '\n')
11 return 1; 54 return 1;
12 if (s[0] == '\r' && s[1] == '\n') 55 if (s[0] == '\r' && s[1] == '\n')
13 return 2; 56 return 2;
15 return 0; 58 return 0;
16 return -1; 59 return -1;
17 } 60 }
18 61
19 62
20 int pst_find_nl(vstr * vs) 63 static int pst_find_nl(vstr * vs)
21 { 64 {
22 char *nextr, *nextn; 65 char *nextr, *nextn;
23 66
24 nextr = memchr(vs->b, '\r', vs->dlen); 67 nextr = memchr(vs->b, '\r', vs->dlen);
25 nextn = memchr(vs->b, '\n', vs->dlen); 68 nextn = memchr(vs->b, '\n', vs->dlen);
59 } 102 }
60 unicode_up = 1; 103 unicode_up = 1;
61 } 104 }
62 105
63 106
64 void pst_unicode_close() 107 static void pst_unicode_close()
65 { 108 {
66 iconv_close(i16to8); 109 iconv_close(i16to8);
67 if (target_open_from) iconv_close(i8totarget); 110 if (target_open_from) iconv_close(i8totarget);
68 if (target_open_to) iconv_close(target2i8); 111 if (target_open_to) iconv_close(target2i8);
69 if (target_charset) free((char *)target_charset); 112 if (target_charset) free((char *)target_charset);
197 } 240 }
198 241
199 242
200 vbuf *pst_vballoc(size_t len) 243 vbuf *pst_vballoc(size_t len)
201 { 244 {
202 struct varbuf *result = malloc(sizeof(struct varbuf)); 245 vbuf *result = malloc(sizeof(vbuf));
203 if (result) { 246 if (result) {
204 result->dlen = 0; 247 result->dlen = 0;
205 result->blen = 0; 248 result->blen = 0;
206 result->buf = NULL; 249 result->buf = NULL;
207 pst_vbresize(result, len); 250 pst_vbresize(result, len);
209 else DIE(("malloc() failure")); 252 else DIE(("malloc() failure"));
210 return result; 253 return result;
211 } 254 }
212 255
213 256
214 void pst_vbcheck(vbuf * vb) 257 static void pst_vbcheck(vbuf * vb)
215 { 258 {
216 ASSERT(vb->b >= vb->buf, "vbcheck(): data not inside buffer"); 259 ASSERT(vb->b >= vb->buf, "vbcheck(): data not inside buffer");
217 ASSERT((size_t)(vb->b - vb->buf) <= vb->blen, "vbcheck(): vb->b outside of buffer range."); 260 ASSERT((size_t)(vb->b - vb->buf) <= vb->blen, "vbcheck(): vb->b outside of buffer range.");
218 ASSERT(vb->dlen <= vb->blen, "vbcheck(): data length > buffer length."); 261 ASSERT(vb->dlen <= vb->blen, "vbcheck(): data length > buffer length.");
219 ASSERT(vb->blen < 1024 * 1024, "vbcheck(): blen is a bit large...hmmm."); 262 ASSERT(vb->blen < 1024 * 1024, "vbcheck(): blen is a bit large...hmmm.");
220 } 263 }
221 264
222 265
223 void pst_vbfree(vbuf * vb) 266 static void pst_vbfree(vbuf * vb)
224 { 267 {
225 free(vb->buf); 268 free(vb->buf);
226 free(vb); 269 free(vb);
227 } 270 }
228 271
229 272
230 void pst_vbclear(struct varbuf *vb) // ditch the data, keep the buffer 273 static void pst_vbclear(vbuf *vb) // ditch the data, keep the buffer
231 { 274 {
232 pst_vbresize(vb, 0); 275 pst_vbresize(vb, 0);
233 } 276 }
234 277
235 278
236 void pst_vbresize(struct varbuf *vb, size_t len) // DESTRUCTIVELY grow or shrink buffer 279 static void pst_vbresize(vbuf *vb, size_t len) // DESTRUCTIVELY grow or shrink buffer
237 { 280 {
238 vb->dlen = 0; 281 vb->dlen = 0;
239 282
240 if (vb->blen >= len) { 283 if (vb->blen >= len) {
241 vb->b = vb->buf; 284 vb->b = vb->buf;
246 vb->b = vb->buf; 289 vb->b = vb->buf;
247 vb->blen = len; 290 vb->blen = len;
248 } 291 }
249 292
250 293
251 size_t pst_vbavail(vbuf * vb) 294 static size_t pst_vbavail(vbuf * vb)
252 { 295 {
253 return vb->blen - vb->dlen - (size_t)(vb->b - vb->buf); 296 return vb->blen - vb->dlen - (size_t)(vb->b - vb->buf);
254 } 297 }
255 298
256 299
257 void pst_vbgrow(struct varbuf *vb, size_t len) // out: vbavail(vb) >= len, data are preserved 300 void pst_vbgrow(vbuf *vb, size_t len) // out: vbavail(vb) >= len, data are preserved
258 { 301 {
259 if (0 == len) 302 if (0 == len)
260 return; 303 return;
261 304
262 if (0 == vb->blen) { 305 if (0 == vb->blen) {
292 memcpy(vb->b, b, len); 335 memcpy(vb->b, b, len);
293 vb->dlen = len; 336 vb->dlen = len;
294 } 337 }
295 338
296 339
297 void pst_vsskipws(vstr * vs) 340 static void pst_vsskipws(vstr * vs)
298 { 341 {
299 char *p = vs->b; 342 char *p = vs->b;
300 while ((size_t)(p - vs->b) < vs->dlen && isspace(p[0])) 343 while ((size_t)(p - vs->b) < vs->dlen && isspace(p[0]))
301 p++; 344 p++;
302 345
303 pst_vbskip((vbuf *) vs, p - vs->b); 346 pst_vbskip((vbuf *) vs, p - vs->b);
304 } 347 }
305 348
306 349
307 // append len bytes of b to vbuf, resize if necessary 350 // append len bytes of b to vbuf, resize if necessary
308 void pst_vbappend(struct varbuf *vb, void *b, size_t len) 351 void pst_vbappend(vbuf *vb, void *b, size_t len)
309 { 352 {
310 if (0 == vb->dlen) { 353 if (0 == vb->dlen) {
311 pst_vbset(vb, b, len); 354 pst_vbset(vb, b, len);
312 return; 355 return;
313 } 356 }
316 vb->dlen += len; 359 vb->dlen += len;
317 } 360 }
318 361
319 362
320 // dumps the first skip bytes from vbuf 363 // dumps the first skip bytes from vbuf
321 void pst_vbskip(struct varbuf *vb, size_t skip) 364 static void pst_vbskip(vbuf *vb, size_t skip)
322 { 365 {
323 ASSERT(skip <= vb->dlen, "vbskip(): Attempt to seek past end of buffer."); 366 ASSERT(skip <= vb->dlen, "vbskip(): Attempt to seek past end of buffer.");
324 vb->b += skip; 367 vb->b += skip;
325 vb->dlen -= skip; 368 vb->dlen -= skip;
326 } 369 }
327 370
328 371
329 // overwrite vbdest with vbsrc 372 // overwrite vbdest with vbsrc
330 void pst_vboverwrite(struct varbuf *vbdest, struct varbuf *vbsrc) 373 static void pst_vboverwrite(vbuf *vbdest, vbuf *vbsrc)
331 { 374 {
332 pst_vbresize(vbdest, vbsrc->blen); 375 pst_vbresize(vbdest, vbsrc->blen);
333 memcpy(vbdest->b, vbsrc->b, vbsrc->dlen); 376 memcpy(vbdest->b, vbsrc->b, vbsrc->dlen);
334 vbdest->blen = vbsrc->blen; 377 vbdest->blen = vbsrc->blen;
335 vbdest->dlen = vbsrc->dlen; 378 vbdest->dlen = vbsrc->dlen;
336 } 379 }
337 380
338 381
339 vstr *pst_vsalloc(size_t len) 382 static vstr *pst_vsalloc(size_t len)
340 { 383 {
341 vstr *result = (vstr *) pst_vballoc(len + 1); 384 vstr *result = (vstr *) pst_vballoc(len + 1);
342 pst_vsset(result, ""); 385 pst_vsset(result, "");
343 return result; 386 return result;
344 } 387 }
349 { 392 {
350 return vs->b; 393 return vs->b;
351 } 394 }
352 395
353 396
354 size_t pst_vslen(vstr * vs) 397 static size_t pst_vslen(vstr * vs)
355 { 398 {
356 return strlen(pst_vsstr(vs)); 399 return strlen(pst_vsstr(vs));
357 } 400 }
358 401
359 402
360 void pst_vsfree(vstr * vs) 403 static void pst_vsfree(vstr * vs)
361 { 404 {
362 pst_vbfree((vbuf *) vs); 405 pst_vbfree((vbuf *) vs);
363 } 406 }
364 407
365 408
366 void pst_vscharcat(vstr * vb, int ch) 409 static void pst_vscharcat(vstr * vb, int ch)
367 { 410 {
368 pst_vbgrow((vbuf *) vb, 1); 411 pst_vbgrow((vbuf *) vb, 1);
369 vb->b[vb->dlen - 1] = ch; 412 vb->b[vb->dlen - 1] = ch;
370 vb->b[vb->dlen] = '\0'; 413 vb->b[vb->dlen] = '\0';
371 vb->dlen++; 414 vb->dlen++;
372 } 415 }
373 416
374 417
375 // prependappend string str to vbuf, vbuf must already contain a valid string 418 // prependappend string str to vbuf, vbuf must already contain a valid string
376 void pst_vsnprepend(vstr * vb, char *str, size_t len) 419 static void pst_vsnprepend(vstr * vb, char *str, size_t len)
377 { 420 {
378 ASSERT(vb->b[vb->dlen - 1] == '\0', "vsncat(): attempt to append string to non-string."); 421 ASSERT(vb->b[vb->dlen - 1] == '\0', "vsncat(): attempt to append string to non-string.");
379 size_t sl = strlen(str); 422 size_t sl = strlen(str);
380 size_t n = (sl < len) ? sl : len; 423 size_t n = (sl < len) ? sl : len;
381 pst_vbgrow((vbuf *) vb, n + 1); 424 pst_vbgrow((vbuf *) vb, n + 1);
385 vb->b[vb->dlen - 1] = '\0'; 428 vb->b[vb->dlen - 1] = '\0';
386 } 429 }
387 430
388 431
389 // len < dlen-1 -> skip len chars, else DIE 432 // len < dlen-1 -> skip len chars, else DIE
390 void pst_vsskip(vstr * vs, size_t len) 433 static void pst_vsskip(vstr * vs, size_t len)
391 { 434 {
392 ASSERT(len < vs->dlen - 1, "Attempt to skip past end of string"); 435 ASSERT(len < vs->dlen - 1, "Attempt to skip past end of string");
393 pst_vbskip((vbuf *) vs, len); 436 pst_vbskip((vbuf *) vs, len);
394 } 437 }
395 438
396 439
397 // in: vb->b == "stuff\nmore_stuff"; out: vb->b == "more_stuff" 440 // in: vb->b == "stuff\nmore_stuff"; out: vb->b == "more_stuff"
398 int pst_vsskipline(vstr * vs) 441 static int pst_vsskipline(vstr * vs)
399 { 442 {
400 int nloff = pst_find_nl(vs); 443 int nloff = pst_find_nl(vs);
401 int nll = pst_skip_nl(vs->b + nloff); 444 int nll = pst_skip_nl(vs->b + nloff);
402 445
403 if (nloff < 0) { 446 if (nloff < 0) {
417 460
418 return 0; 461 return 0;
419 } 462 }
420 463
421 464
422 int pst_vscatprintf(vstr * vs, char *fmt, ...) 465 static int pst_vscatprintf(vstr * vs, char *fmt, ...)
423 { 466 {
424 int size; 467 int size;
425 va_list ap; 468 va_list ap;
426 469
427 /* Guess we need no more than 100 bytes. */ 470 /* Guess we need no more than 100 bytes. */
449 } 492 }
450 } 493 }
451 494
452 495
453 // returns the last character stored in a vstr 496 // returns the last character stored in a vstr
454 int pst_vslast(vstr * vs) 497 static int pst_vslast(vstr * vs)
455 { 498 {
456 if (vs->dlen < 1) 499 if (vs->dlen < 1)
457 return -1; 500 return -1;
458 if (vs->b[vs->dlen - 1] != '\0') 501 if (vs->b[vs->dlen - 1] != '\0')
459 return -1; 502 return -1;
462 return vs->b[vs->dlen - 2]; 505 return vs->b[vs->dlen - 2];
463 } 506 }
464 507
465 508
466 // print over vb 509 // print over vb
467 void pst_vs_printf(vstr * vs, char *fmt, ...) 510 static void pst_vs_printf(vstr * vs, char *fmt, ...)
468 { 511 {
469 int size; 512 int size;
470 va_list ap; 513 va_list ap;
471 514
472 /* Guess we need no more than 100 bytes. */ 515 /* Guess we need no more than 100 bytes. */
491 } 534 }
492 } 535 }
493 536
494 537
495 // printf append to vs 538 // printf append to vs
496 void pst_vs_printfa(vstr * vs, char *fmt, ...) 539 static void pst_vs_printfa(vstr * vs, char *fmt, ...)
497 { 540 {
498 int size; 541 int size;
499 va_list ap; 542 va_list ap;
500 543
501 if (vs->blen - vs->dlen < 50) 544 if (vs->blen - vs->dlen < 50)
519 pst_vbgrow((vbuf *) vs, size); 562 pst_vbgrow((vbuf *) vs, size);
520 } 563 }
521 } 564 }
522 565
523 566
524 void pst_vshexdump(vstr * vs, const char *b, size_t start, size_t stop, int ascii) 567 static void pst_vshexdump(vstr * vs, const char *b, size_t start, size_t stop, int ascii)
525 { 568 {
526 char c; 569 char c;
527 int diff, i; 570 int diff, i;
528 571
529 while (start < stop) { 572 while (start < stop) {
550 start += 16; 593 start += 16;
551 } 594 }
552 } 595 }
553 596
554 597
555 void pst_vsset(vstr * vs, char *s) // Store string s in vs 598 static void pst_vsset(vstr * vs, char *s) // Store string s in vs
556 { 599 {
557 pst_vsnset(vs, s, strlen(s)); 600 pst_vsnset(vs, s, strlen(s));
558 } 601 }
559 602
560 603
561 void pst_vsnset(vstr * vs, char *s, size_t n) // Store string s in vs 604 static void pst_vsnset(vstr * vs, char *s, size_t n) // Store string s in vs
562 { 605 {
563 pst_vbresize((vbuf *) vs, n + 1); 606 pst_vbresize((vbuf *) vs, n + 1);
564 memcpy(vs->b, s, n); 607 memcpy(vs->b, s, n);
565 vs->b[n] = '\0'; 608 vs->b[n] = '\0';
566 vs->dlen = n + 1; 609 vs->dlen = n + 1;
567 } 610 }
568 611
569 612
570 void pst_vsgrow(vstr * vs, size_t len) // grow buffer by len bytes, data are preserved 613 static void pst_vsgrow(vstr * vs, size_t len) // grow buffer by len bytes, data are preserved
571 { 614 {
572 pst_vbgrow((vbuf *) vs, len); 615 pst_vbgrow((vbuf *) vs, len);
573 } 616 }
574 617
575 618
576 size_t pst_vsavail(vstr * vs) 619 static size_t pst_vsavail(vstr * vs)
577 { 620 {
578 return pst_vbavail((vbuf *) vs); 621 return pst_vbavail((vbuf *) vs);
579 } 622 }
580 623
581 624
582 void pst_vsnset16(vstr * vs, char *s, size_t len) // Like vbstrnset, but for UTF16 625 static void pst_vsnset16(vstr * vs, char *s, size_t len) // Like vbstrnset, but for UTF16
583 { 626 {
584 pst_vbresize((vbuf *) vs, len + 1); 627 pst_vbresize((vbuf *) vs, len + 1);
585 memcpy(vs->b, s, len); 628 memcpy(vs->b, s, len);
586 629
587 vs->b[len] = '\0'; 630 vs->b[len] = '\0';
588 vs->dlen = len + 1; 631 vs->dlen = len + 1;
589 vs->b[len] = '\0'; 632 vs->b[len] = '\0';
590 } 633 }
591 634
592 635
593 void pst_vscat(vstr * vs, char *str) 636 static void pst_vscat(vstr * vs, char *str)
594 { 637 {
595 pst_vsncat(vs, str, strlen(str)); 638 pst_vsncat(vs, str, strlen(str));
596 } 639 }
597 640
598 641
599 int pst_vscmp(vstr * vs, char *str) 642 static int pst_vscmp(vstr * vs, char *str)
600 { 643 {
601 return strcmp(vs->b, str); 644 return strcmp(vs->b, str);
602 } 645 }
603 646
604 647
605 void pst_vsncat(vstr * vs, char *str, size_t len) // append string str to vstr, vstr must already contain a valid string 648 static void pst_vsncat(vstr * vs, char *str, size_t len) // append string str to vstr, vstr must already contain a valid string
606 { 649 {
607 ASSERT(vs->b[vs->dlen - 1] == '\0', "vsncat(): attempt to append string to non-string."); 650 ASSERT(vs->b[vs->dlen - 1] == '\0', "vsncat(): attempt to append string to non-string.");
608 size_t sl = strlen(str); 651 size_t sl = strlen(str);
609 size_t n = (sl < len) ? sl : len; 652 size_t n = (sl < len) ? sl : len;
610 //string append 653 //string append
613 vs->dlen += n; 656 vs->dlen += n;
614 vs->b[vs->dlen - 1] = '\0'; 657 vs->b[vs->dlen - 1] = '\0';
615 } 658 }
616 659
617 660
618 void pst_vstrunc(vstr * v, size_t off) // Drop chars [off..dlen] 661 static void pst_vstrunc(vstr * v, size_t off) // Drop chars [off..dlen]
619 { 662 {
620 if (off >= v->dlen - 1) 663 if (off >= v->dlen - 1)
621 return; //nothing to do 664 return; //nothing to do
622 v->b[off] = '\0'; 665 v->b[off] = '\0';
623 v->dlen = off + 1; 666 v->dlen = off + 1;