00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 static const char libbu_vls_RCSid[] = "@(#)$Header: /cvsroot/brlcad/brlcad/src/libbu/vls.c,v 14.20 2006/09/03 15:14:07 lbutler Exp $ (BRL)";
00044
00045 #include "common.h"
00046
00047 #include <stdlib.h>
00048 #include <stdio.h>
00049 #include <ctype.h>
00050 #ifdef HAVE_STRING_H
00051 # include <string.h>
00052 #else
00053 # include <strings.h>
00054 #endif
00055 #if defined(HAVE_STDARG_H)
00056
00057 # include <stdarg.h>
00058 #endif
00059 #if !defined(HAVE_STDARG_H) && defined(HAVE_VARARGS_H)
00060
00061 # include <varargs.h>
00062 #endif
00063 #ifdef HAVE_UNISTD_H
00064 # include <unistd.h>
00065 #endif
00066
00067 #include "machine.h"
00068 #include "bu.h"
00069
00070 #if defined(HAVE_VARARGS_H) || defined(HAVE_STDARG_H)
00071 BU_EXTERN(void bu_vls_vprintf, (struct bu_vls *vls, const char *fmt, va_list ap));
00072 #endif
00073
00074 const char bu_vls_message[] = "bu_vls_str";
00075 extern const char bu_strdup_message[];
00076
00077
00078
00079
00080
00081
00082
00083 void
00084 bu_vls_init(register struct bu_vls *vp)
00085 {
00086 if (vp == (struct bu_vls *)NULL)
00087 bu_bomb("bu_vls_init() passed NULL pointer");
00088
00089
00090
00091
00092 #if defined(DEBUG) && 0
00093 if (vp->vls_magic == BU_VLS_MAGIC) {
00094 if (vp->vls_str && vp->vls_len > 0 && vp->vls_max > 0) {
00095 bu_log("bu_vls_init potential leak [%s] (vls_len=%d)\n", vp->vls_str, vp->vls_len);
00096 }
00097 }
00098 #endif
00099 vp->vls_magic = BU_VLS_MAGIC;
00100 vp->vls_str = (char *)0;
00101 vp->vls_len = vp->vls_max = vp->vls_offset = 0;
00102 }
00103
00104
00105
00106
00107
00108
00109
00110
00111 void
00112 bu_vls_init_if_uninit(register struct bu_vls *vp)
00113 {
00114 if (vp == (struct bu_vls *)NULL)
00115 bu_bomb("bu_vls_init_if_uninit() passed NULL pointer");
00116
00117 if( vp->vls_magic == BU_VLS_MAGIC ) return;
00118 bu_vls_init( vp );
00119 }
00120
00121
00122
00123
00124
00125
00126
00127
00128 struct bu_vls *
00129 bu_vls_vlsinit(void)
00130 {
00131 register struct bu_vls *vp;
00132
00133 vp = (struct bu_vls *)bu_malloc(sizeof(struct bu_vls), "bu_vls_vlsinit struct");
00134 bu_vls_init(vp);
00135
00136 return vp;
00137 }
00138
00139
00140
00141
00142
00143
00144
00145 char *
00146 bu_vls_addr(register const struct bu_vls *vp)
00147 {
00148 static char nullbuf[4];
00149
00150 BU_CK_VLS(vp);
00151
00152 if( vp->vls_max == 0 || vp->vls_str == (char *)NULL ) {
00153
00154 nullbuf[0] = '\0';
00155 return(nullbuf);
00156 }
00157
00158
00159 if( vp->vls_max < 0 ||
00160 vp->vls_len < 0 ||
00161 vp->vls_offset < 0 ||
00162 vp->vls_str == (char *)NULL ||
00163 vp->vls_len + vp->vls_offset >= vp->vls_max ) {
00164 bu_log("bu_vls_addr: bad VLS. max=%d, len=%d, offset=%d\n",
00165 vp->vls_max, vp->vls_len, vp->vls_offset);
00166 bu_bomb("bu_vls_addr\n");
00167 }
00168
00169 return( vp->vls_str+vp->vls_offset );
00170 }
00171
00172
00173
00174
00175 void
00176 bu_vls_extend(register struct bu_vls *vp, unsigned int extra)
00177 {
00178 BU_CK_VLS(vp);
00179 if( extra < 40 ) extra = 40;
00180 if( vp->vls_max <= 0 || vp->vls_str == (char *)0 ) {
00181 vp->vls_max = extra;
00182 vp->vls_str = (char *)bu_malloc( vp->vls_max, bu_vls_message );
00183 vp->vls_len = 0;
00184 vp->vls_offset = 0;
00185 *vp->vls_str = '\0';
00186 return;
00187 }
00188 if( vp->vls_offset + vp->vls_len + extra >= vp->vls_max ) {
00189 vp->vls_max += extra;
00190 if( vp->vls_max < 120 ) vp->vls_max = 120;
00191 vp->vls_str = (char *)bu_realloc( vp->vls_str, vp->vls_max,
00192 bu_vls_message );
00193 }
00194 }
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 void
00207 bu_vls_setlen(struct bu_vls *vp, int newlen)
00208 {
00209 BU_CK_VLS(vp);
00210 if( vp->vls_len >= newlen ) return;
00211 bu_vls_extend( vp, newlen - vp->vls_len );
00212 vp->vls_len = newlen;
00213 }
00214
00215
00216
00217
00218
00219
00220 int
00221 bu_vls_strlen(register const struct bu_vls *vp)
00222 {
00223 BU_CK_VLS(vp);
00224 if( vp->vls_len <= 0 ) return 0;
00225 return vp->vls_len;
00226 }
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 void
00237 bu_vls_trunc(register struct bu_vls *vp, int len)
00238 {
00239 BU_CK_VLS(vp);
00240 if( len < 0 ) len = vp->vls_len + len;
00241 if( vp->vls_len <= len ) return;
00242 if( len == 0 ) vp->vls_offset = 0;
00243 vp->vls_str[len+vp->vls_offset] = '\0';
00244 vp->vls_len = len;
00245 }
00246
00247
00248
00249
00250
00251
00252
00253 void
00254 bu_vls_trunc2(register struct bu_vls *vp, int len)
00255 {
00256 BU_CK_VLS(vp);
00257 if( vp->vls_len <= len ) return;
00258 if( len < 0 ) len = 0;
00259 if( len == 0 ) vp->vls_offset = 0;
00260 vp->vls_str[len+vp->vls_offset] = '\0';
00261 vp->vls_len = len;
00262 }
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272 void
00273 bu_vls_nibble(register struct bu_vls *vp, int len)
00274 {
00275 BU_CK_VLS(vp);
00276 if( len < 0 && (-len) > vp->vls_offset ) len = -vp->vls_offset;
00277 if (len >= vp->vls_len) {
00278 bu_vls_trunc( vp, 0 );
00279 return;
00280 }
00281 vp->vls_len -= len;
00282 vp->vls_offset += len;
00283 }
00284
00285
00286
00287
00288
00289
00290 void
00291 bu_vls_free(register struct bu_vls *vp)
00292 {
00293 BU_CK_VLS(vp);
00294 if( vp->vls_str ) {
00295 vp->vls_str[0] = '?';
00296 bu_free( vp->vls_str, "bu_vls_free" );
00297 vp->vls_str = (char *)0;
00298 }
00299 vp->vls_offset = vp->vls_len = vp->vls_max = 0;
00300 }
00301
00302
00303
00304
00305
00306
00307
00308 void
00309 bu_vls_vlsfree(register struct bu_vls *vp)
00310 {
00311 if ( *(long *)vp != BU_VLS_MAGIC) return;
00312
00313 bu_vls_free( vp );
00314 bu_free( vp, "bu_vls_vlsfree" );
00315 }
00316
00317
00318
00319
00320
00321
00322
00323
00324 char *
00325 bu_vls_strdup(register const struct bu_vls *vp)
00326 {
00327 register char *str;
00328 register int len;
00329
00330 len = bu_vls_strlen(vp);
00331 str = bu_malloc(len+1, bu_strdup_message );
00332 strncpy(str, bu_vls_addr(vp), len);
00333 str[len] = '\0';
00334 return str;
00335 }
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347 char *
00348 bu_vls_strgrab(register struct bu_vls *vp)
00349 {
00350 register char *str;
00351
00352 BU_CK_VLS(vp);
00353 if( vp->vls_offset != 0 ) {
00354 str = bu_vls_strdup( vp );
00355 bu_vls_free( vp );
00356 return str;
00357 }
00358
00359 str = bu_vls_addr( vp );
00360 vp->vls_str = (char *)0;
00361 vp->vls_offset = vp->vls_len = vp->vls_max = 0;
00362 return str;
00363 }
00364
00365
00366
00367
00368
00369
00370 void
00371 bu_vls_strcpy(register struct bu_vls *vp, const char *s)
00372 {
00373 register int len;
00374
00375 BU_CK_VLS(vp);
00376 if( s == (const char *)NULL ) return;
00377 if( (len = strlen(s)) <= 0 ) {
00378 vp->vls_len = 0;
00379 vp->vls_offset = 0;
00380 if(vp->vls_max > 0)
00381 vp->vls_str[0] = '\0';
00382 return;
00383 }
00384 vp->vls_offset = 0;
00385 if( len+1 >= vp->vls_max ) bu_vls_extend( vp, len+1 );
00386 bcopy( s, vp->vls_str, len+1 );
00387 vp->vls_len = len;
00388 }
00389
00390
00391
00392
00393
00394
00395 void
00396 bu_vls_strncpy(register struct bu_vls *vp, const char *s, long int n)
00397 {
00398 register int len;
00399
00400 BU_CK_VLS(vp);
00401 if( s == (const char *)NULL ) return;
00402 len = strlen(s);
00403 if( len > n ) len = n;
00404 if( len <= 0 ) {
00405 vp->vls_len = 0;
00406 return;
00407 }
00408 vp->vls_offset = 0;
00409 if( len+1 >= vp->vls_max ) bu_vls_extend( vp, len+1 );
00410 bcopy( s, vp->vls_str, len );
00411 vp->vls_str[len] = '\0';
00412 vp->vls_len = len;
00413 }
00414
00415
00416
00417
00418
00419
00420 void
00421 bu_vls_strcat(register struct bu_vls *vp, const char *s)
00422 {
00423 register int len;
00424
00425 BU_CK_VLS(vp);
00426 if( s == (const char *)NULL ) return;
00427 if( (len = strlen(s)) <= 0 ) return;
00428 if( vp->vls_offset + vp->vls_len + len+1 >= vp->vls_max )
00429 bu_vls_extend( vp, len+1 );
00430 bcopy( s, vp->vls_str +vp->vls_offset + vp->vls_len, len+1 );
00431 vp->vls_len += len;
00432 }
00433
00434
00435
00436
00437
00438
00439 void
00440 bu_vls_strncat(register struct bu_vls *vp, const char *s, long int n)
00441 {
00442 register int len;
00443
00444 BU_CK_VLS(vp);
00445 if( s == (const char *)NULL ) return;
00446 len = strlen(s);
00447 if( len > n ) len = n;
00448 if( len <= 0 ) return;
00449 if( vp->vls_offset + vp->vls_len + len+1 >= vp->vls_max )
00450 bu_vls_extend( vp, len+1 );
00451 bcopy( s, vp->vls_str + vp->vls_offset + vp->vls_len, len );
00452 vp->vls_len += len;
00453 vp->vls_str[vp->vls_offset + vp->vls_len] = '\0';
00454 }
00455
00456
00457
00458
00459
00460
00461
00462 void
00463 bu_vls_vlscat(register struct bu_vls *dest, register const struct bu_vls *src)
00464 {
00465 BU_CK_VLS(src);
00466 BU_CK_VLS(dest);
00467 if( src->vls_len <= 0 ) return;
00468 if( dest->vls_offset + dest->vls_len + src->vls_len+1 >= dest->vls_max )
00469 bu_vls_extend( dest, src->vls_len+1 );
00470
00471 bcopy( src->vls_str+src->vls_offset,
00472 dest->vls_str +dest->vls_offset + dest->vls_len,
00473 src->vls_len+1 );
00474 dest->vls_len += src->vls_len;
00475 }
00476
00477
00478
00479
00480
00481
00482
00483 void
00484 bu_vls_vlscatzap(register struct bu_vls *dest, register struct bu_vls *src)
00485 {
00486 BU_CK_VLS(src);
00487 BU_CK_VLS(dest);
00488 if( src->vls_len <= 0 ) return;
00489 bu_vls_vlscat( dest, src );
00490 bu_vls_trunc( src, 0 );
00491 }
00492
00493
00494
00495
00496
00497
00498
00499 void
00500 bu_vls_from_argv(register struct bu_vls *vp, int argc, const char *argv[])
00501 {
00502 BU_CK_VLS(vp);
00503 for( ; argc > 0; argc--, argv++ ) {
00504 bu_vls_strcat( vp, *argv );
00505 if( argc > 1 ) bu_vls_strcat( vp, " " );
00506 }
00507 }
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527 int
00528 bu_argv_from_string(char **argv, int lim, register char *lp)
00529 {
00530 register int nwords;
00531 register char *lp1;
00532
00533 argv[0] = "_NIL_";
00534
00535 while( *lp != '\0' && isspace( *lp ) )
00536 lp++;
00537
00538 if( *lp == '\0' )
00539 return 0;
00540
00541
00542 nwords = 1;
00543 argv[0] = lp;
00544
00545 for( ; *lp != '\0'; lp++ ) {
00546 if( !isspace( *lp ) )
00547 continue;
00548
00549 *lp = '\0';
00550 lp1 = lp + 1;
00551 if( *lp1 != '\0' && !isspace( *lp1 ) ) {
00552
00553 if( nwords >= lim-1 )
00554 break;
00555
00556 argv[nwords++] = lp1;
00557 }
00558 }
00559 argv[nwords] = (char *)0;
00560 return nwords;
00561 }
00562
00563
00564
00565
00566 void
00567 bu_vls_fwrite(FILE *fp, const struct bu_vls *vp)
00568 {
00569 int status;
00570
00571 BU_CK_VLS(vp);
00572 if( vp->vls_len <= 0 ) return;
00573
00574 bu_semaphore_acquire(BU_SEM_SYSCALL);
00575 status = fwrite( vp->vls_str + vp->vls_offset, vp->vls_len, 1, fp );
00576 bu_semaphore_release(BU_SEM_SYSCALL);
00577
00578 if( status != 1 ) {
00579 perror("fwrite");
00580 bu_bomb("bu_vls_fwrite() write error\n");
00581 }
00582 }
00583
00584
00585
00586
00587 void
00588 bu_vls_write( int fd, const struct bu_vls *vp )
00589 {
00590
00591 BU_CK_VLS(vp);
00592 if( vp->vls_len <= 0 ) return;
00593
00594 #if !defined(HAVE_UNIX_IO)
00595 bu_bomb("bu_vls_write(): This isn't UNIX\n");
00596 #else
00597 {
00598 int status;
00599 bu_semaphore_acquire(BU_SEM_SYSCALL);
00600 status = write( fd, vp->vls_str + vp->vls_offset, vp->vls_len );
00601 bu_semaphore_release(BU_SEM_SYSCALL);
00602
00603 if( status != vp->vls_len ) {
00604 perror("write");
00605 bu_bomb("bu_vls_write() write error\n");
00606 }
00607 }
00608 #endif
00609 }
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621 int
00622 bu_vls_read( struct bu_vls *vp, int fd )
00623 {
00624 int ret = 0;
00625
00626 BU_CK_VLS(vp);
00627
00628 #if !defined(HAVE_UNIX_IO)
00629 bu_bomb("bu_vls_read(): This isn't UNIX\n");
00630 #else
00631 {
00632 int todo;
00633 int got;
00634 for(;;) {
00635 bu_vls_extend( vp, 4096 );
00636 todo = vp->vls_max - vp->vls_len - vp->vls_offset - 1;
00637
00638 bu_semaphore_acquire(BU_SEM_SYSCALL);
00639 got = read(fd, vp->vls_str+vp->vls_offset+vp->vls_len, todo );
00640 bu_semaphore_release(BU_SEM_SYSCALL);
00641
00642 if( got < 0 ) {
00643
00644 return -1;
00645 }
00646 if(got == 0) break;
00647 vp->vls_len += got;
00648 ret += got;
00649 }
00650
00651
00652 vp->vls_str[vp->vls_len+vp->vls_offset] = '\0';
00653 }
00654 #endif
00655 return ret;
00656 }
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672 int
00673 bu_vls_gets(register struct bu_vls *vp, register FILE *fp)
00674 {
00675 int startlen;
00676 int c;
00677
00678 BU_CK_VLS(vp);
00679
00680 startlen = bu_vls_strlen(vp);
00681 bu_vls_extend( vp, 80 );
00682 for( ;; ) {
00683
00684 bu_semaphore_acquire( BU_SEM_SYSCALL );
00685 c = getc(fp);
00686 bu_semaphore_release( BU_SEM_SYSCALL );
00687
00688
00689
00690 if( c == EOF || c == '\n' ) break;
00691 bu_vls_putc( vp, c );
00692 }
00693 if( c == EOF && bu_vls_strlen(vp) <= startlen ) return -1;
00694 vp->vls_str[vp->vls_offset + vp->vls_len] = '\0';
00695 return bu_vls_strlen(vp);
00696 }
00697
00698
00699
00700
00701
00702
00703 void
00704 bu_vls_putc(register struct bu_vls *vp, int c)
00705 {
00706 BU_CK_VLS(vp);
00707
00708 if( vp->vls_offset + vp->vls_len+1 >= vp->vls_max ) bu_vls_extend( vp, 80 );
00709 vp->vls_str[vp->vls_offset + vp->vls_len++] = (char)c;
00710 vp->vls_str[vp->vls_offset + vp->vls_len] = '\0';
00711 }
00712
00713
00714
00715
00716
00717
00718 void
00719 bu_vls_trimspace( struct bu_vls *vp )
00720 {
00721 BU_CK_VLS(vp);
00722
00723
00724 while( isspace( bu_vls_addr(vp)[bu_vls_strlen(vp)-1] ) )
00725 bu_vls_trunc( vp, -1 );
00726
00727
00728 while( isspace( *bu_vls_addr(vp) ) )
00729 bu_vls_nibble( vp, 1 );
00730 }
00731
00732 #if defined(HAVE_VARARGS_H) || defined(HAVE_STDARG_H)
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746 void
00747 bu_vls_vprintf(struct bu_vls *vls, const char *fmt, va_list ap)
00748 {
00749 register const char *sp;
00750 register const char *ep;
00751 register int len;
00752
00753 #define LONGINT 0x001
00754 #define FIELDLEN 0x002
00755 #define SHORTINT 0x003
00756
00757 int flags;
00758 int fieldlen=-1;
00759 char fbuf[64] = {0}, buf[1024] = {0};
00760
00761 BU_CK_VLS(vls);
00762 bu_vls_extend(vls, 96);
00763
00764 sp = fmt;
00765 while( *sp ) {
00766
00767 fmt = sp;
00768 while (*sp != '%' && *sp)
00769 sp++;
00770
00771 if (sp != fmt)
00772 bu_vls_strncat(vls, fmt, sp-fmt);
00773
00774 if (*sp == '\0')
00775 break;
00776
00777
00778
00779 flags = 0;
00780 ep = sp;
00781 while( *ep ) {
00782 ++ep;
00783 if (*ep == ' ' || *ep == '#' || *ep == '-' ||
00784 *ep == '+' || *ep == '.' || isdigit(*ep))
00785 continue;
00786 else if (*ep == 'l' || *ep == 'U' || *ep == 'O')
00787 flags |= LONGINT;
00788 else if (*ep == '*') {
00789 fieldlen = va_arg(ap, int);
00790 flags |= FIELDLEN;
00791 } else if (*ep == 'h') {
00792 flags |= SHORTINT;
00793 } else
00794
00795 break;
00796 }
00797
00798
00799 len = ep-sp+1;
00800 if (len > sizeof(fbuf)-1) len = sizeof(fbuf)-1;
00801 strncpy(fbuf, sp, len);
00802 fbuf[len] = '\0';
00803
00804
00805 switch( *ep ) {
00806 case 's':
00807 {
00808 register char *str;
00809
00810 str = va_arg(ap, char *);
00811 if (str) {
00812 if (flags & FIELDLEN) {
00813 int stringlen = strlen(str);
00814 int left_justify;
00815
00816 if ((left_justify = (fieldlen < 0)))
00817 fieldlen *= -1;
00818
00819 if (stringlen >= fieldlen)
00820 bu_vls_strncat(vls, str, fieldlen);
00821 else {
00822 struct bu_vls padded;
00823 int i;
00824
00825 bu_vls_init(&padded);
00826 if (left_justify)
00827 bu_vls_strcat(&padded, str);
00828 for (i = 0; i < fieldlen - stringlen; ++i)
00829 bu_vls_putc(&padded, ' ');
00830 if (!left_justify)
00831 bu_vls_strcat(&padded, str);
00832 bu_vls_vlscat(vls, &padded);
00833 }
00834 } else {
00835 bu_vls_strcat(vls, str);
00836 }
00837 } else {
00838 if (flags & FIELDLEN)
00839 bu_vls_strncat(vls, "(null)", fieldlen);
00840 else
00841 bu_vls_strcat(vls, "(null)");
00842 }
00843 }
00844 break;
00845 case 'S':
00846 {
00847 register struct bu_vls *vp;
00848
00849 vp = va_arg(ap, struct bu_vls *);
00850 if (vp) {
00851 BU_CK_VLS(vp);
00852 if (flags & FIELDLEN) {
00853 int stringlen = bu_vls_strlen(vp);
00854 int left_justify;
00855
00856 if ((left_justify = (fieldlen < 0)))
00857 fieldlen *= -1;
00858
00859 if (stringlen >= fieldlen)
00860 bu_vls_strncat(vls, bu_vls_addr(vp), fieldlen);
00861 else {
00862 struct bu_vls padded;
00863 int i;
00864
00865 bu_vls_init(&padded);
00866 if (left_justify)
00867 bu_vls_vlscat(&padded, vp);
00868 for (i = 0; i < fieldlen - stringlen; ++i)
00869 bu_vls_putc(&padded, ' ');
00870 if (!left_justify)
00871 bu_vls_vlscat(&padded, vp);
00872 bu_vls_vlscat(vls, &padded);
00873 }
00874 } else {
00875 bu_vls_vlscat(vls, vp);
00876 }
00877 } else {
00878 if (flags & FIELDLEN)
00879 bu_vls_strncat(vls, "(null)", fieldlen);
00880 else
00881 bu_vls_strcat(vls, "(null)");
00882 }
00883 }
00884 break;
00885 case 'e':
00886 case 'E':
00887 case 'f':
00888 case 'g':
00889 case 'G':
00890
00891 #if defined(LONGDBL)
00892 if (flags & LONGDBL) {
00893 register long double ld;
00894
00895 ld = va_arg(ap, long double);
00896 if (flags & FIELDLEN)
00897 sprintf(buf, fbuf, fieldlen, ld);
00898 else
00899 sprintf(buf, fbuf, ld);
00900 bu_vls_strcat(vls, buf);
00901 } else
00902 #endif
00903 {
00904 register double d;
00905
00906 d = va_arg(ap, double);
00907 if (flags & FIELDLEN)
00908 sprintf(buf, fbuf, fieldlen, d);
00909 else
00910 sprintf(buf, fbuf, d);
00911 bu_vls_strcat(vls, buf);
00912 }
00913 break;
00914 case 'd':
00915 case 'x':
00916 if (flags & LONGINT) {
00917
00918 register long ll;
00919
00920 ll = va_arg(ap, long);
00921 if (flags & FIELDLEN)
00922 sprintf(buf, fbuf, fieldlen, ll);
00923 else
00924 sprintf(buf, fbuf, ll);
00925 bu_vls_strcat(vls, buf);
00926 } else if (flags & SHORTINT) {
00927
00928 register short int sh;
00929 sh = (short int)va_arg(ap, int);
00930 if (flags & FIELDLEN)
00931 sprintf(buf, fbuf, fieldlen, sh);
00932 else
00933 sprintf(buf, fbuf, sh);
00934 bu_vls_strcat(vls, buf);
00935 } else {
00936
00937 register int j;
00938
00939 j = va_arg(ap, int);
00940 if (flags & FIELDLEN)
00941 sprintf(buf, fbuf, fieldlen, j);
00942 else
00943 sprintf(buf, fbuf, j);
00944 bu_vls_strcat(vls, buf);
00945 }
00946 break;
00947 case '%':
00948 bu_vls_putc(vls, '%');
00949 break;
00950 default:
00951 {
00952 register int j;
00953
00954
00955
00956
00957 j = va_arg(ap, int);
00958 if (flags & FIELDLEN)
00959 sprintf(buf, fbuf, fieldlen, j);
00960 else
00961 sprintf(buf, fbuf, j);
00962 bu_vls_strcat(vls, buf);
00963 break;
00964 }
00965 }
00966 sp = ep+1;
00967 }
00968
00969 va_end(ap);
00970 }
00971 #else
00972 # error "No implementation provided for bu_vls_vprintf()"
00973 #endif
00974
00975
00976 #if defined(HAVE_STDARG_H)
00977
00978
00979
00980
00981
00982 void
00983 bu_vls_printf(struct bu_vls *vls, char *fmt, ...)
00984 {
00985 va_list ap;
00986 va_start(ap, fmt);
00987 BU_CK_VLS(vls);
00988 bu_vls_vprintf(vls, fmt, ap);
00989 va_end(ap);
00990 }
00991
00992 #else
00993 # if defined(HAVE_VARARGS_H)
00994
00995 void
00996 bu_vls_printf(va_dcl va_alist)
00997 {
00998 va_list ap;
00999 struct bu_vls *vls;
01000 char *fmt;
01001
01002 va_start(ap);
01003 vls = va_arg(ap, struct bu_vls *);
01004 fmt = va_arg(ap, char *);
01005 BU_CK_VLS(vls);
01006 bu_vls_vprintf(vls, fmt, ap);
01007 va_end(ap);
01008 }
01009
01010 # else
01011
01012 void
01013 bu_vls_printf(struct bu_vls *vls, char *fmt, a,b,c,d,e,f,g,h,i,j)
01014 {
01015 char append_buf[65536] = {0};
01016
01017 BU_CK_VLS(vls);
01018 sprintf(append_buf, fmt, a,b,c,d,e,f,g,h,i,j);
01019 if (append_buf[sizeof(append_buf)-1] != '\0') {
01020
01021 append_buf[120] = '\0';
01022 bu_log("bu_vls_printf buffer overflow\nWhile building string '%s'...\n",
01023 append_buf);
01024 bu_bomb("bu_vls_printf buffer overflow\n");
01025 }
01026
01027 bu_vls_strcat(vls, append_buf);
01028 }
01029 # endif
01030 #endif
01031
01032
01033 #if defined(HAVE_STDARG_H)
01034
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046 void
01047 bu_vls_sprintf(struct bu_vls *vls, char *fmt, ...)
01048 {
01049 va_list ap;
01050 va_start(ap, fmt);
01051 BU_CK_VLS(vls);
01052 bu_vls_trunc(vls, 0);
01053 bu_vls_vprintf(vls, fmt, ap);
01054 va_end(ap);
01055 }
01056
01057 #else
01058 # if defined(HAVE_VARARGS_H)
01059
01060 void
01061 bu_vls_sprintf(va_dcl va_alist)
01062 {
01063 va_list ap;
01064 struct bu_vls *vls;
01065 char *fmt;
01066
01067 va_start(ap);
01068 vls = va_arg(ap, struct bu_vls *);
01069 fmt = va_arg(ap, char *);
01070 BU_CK_VLS(vls);
01071 bu_vls_trunc(vls, 0);
01072 bu_vls_vprintf(vls, fmt, ap);
01073 va_end(ap);
01074 }
01075
01076 # else
01077
01078 void
01079 bu_vls_sprintf(struct bu_vls *vls, char *fmt, a,b,c,d,e,f,g,h,i,j)
01080 {
01081 BU_CK_VLS(vls);
01082 bu_vls_trunc(vls, 0);
01083 bu_vls_printf(vls, fmt, a, b, c, d, e, f, g, h, i, j);
01084 }
01085 # endif
01086 #endif
01087
01088
01089
01090
01091
01092
01093 void
01094 bu_vls_spaces(register struct bu_vls *vp, int cnt)
01095 {
01096 BU_CK_VLS(vp);
01097 if( cnt <= 0 ) return;
01098 if( vp->vls_offset + vp->vls_len + cnt+1 >= vp->vls_max )
01099 bu_vls_extend( vp, cnt );
01100 memset( vp->vls_str + vp->vls_offset + vp->vls_len, ' ', cnt );
01101 vp->vls_len += cnt;
01102 }
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117 int
01118 bu_vls_print_positions_used(const struct bu_vls *vp)
01119 {
01120 char *start;
01121 int used;
01122
01123 BU_CK_VLS(vp);
01124
01125 if( (start = strrchr( bu_vls_addr(vp), '\n' )) == NULL )
01126 start = bu_vls_addr(vp);
01127 used = 0;
01128 while( *start != '\0' ) {
01129 if( *start == '\t' ) {
01130 used += 8 - (used % 8);
01131 } else {
01132 used++;
01133 }
01134 start++;
01135 }
01136 return used;
01137 }
01138
01139
01140
01141
01142
01143
01144
01145
01146 void
01147 bu_vls_detab(struct bu_vls *vp)
01148 {
01149 struct bu_vls src;
01150 register char *cp;
01151 int used;
01152
01153 BU_CK_VLS(vp);
01154 bu_vls_init( &src );
01155 bu_vls_vlscatzap( &src, vp );
01156 bu_vls_extend( vp, bu_vls_strlen(&src)+50 );
01157
01158 cp = bu_vls_addr( &src );
01159 used = 0;
01160 while( *cp != '\0' ) {
01161 if( *cp == '\t' ) {
01162 int todo;
01163 todo = 8 - (used % 8);
01164 bu_vls_spaces( vp, todo );
01165 used += todo;
01166 } else if( *cp == '\n' ) {
01167 bu_vls_putc( vp, '\n' );
01168 used = 0;
01169 } else {
01170 bu_vls_putc( vp, *cp );
01171 used++;
01172 }
01173 cp++;
01174 }
01175 bu_vls_free( &src );
01176 }
01177
01178
01179
01180
01181
01182
01183 void
01184 bu_vls_prepend(struct bu_vls *vp, char *str)
01185 {
01186 int len = strlen(str);
01187
01188 bu_vls_extend(vp, len);
01189
01190
01191 memmove( vp->vls_str+vp->vls_offset+len, vp->vls_str+vp->vls_offset, vp->vls_len );
01192
01193
01194 memcpy( vp->vls_str+vp->vls_offset, str, len);
01195 }
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206