home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / me34src.zip / me3 / util / spoof.c < prev    next >
C/C++ Source or Header  |  1995-01-14  |  3KB  |  115 lines

  1. /* 
  2.  * spoof.c : kinda like sprintf
  3.  *    Craig Durland 8/87 modified from spewf    Public Domain
  4.  *    3/92 added stdarg support
  5.  */
  6.  
  7. #ifdef __STDC__
  8.  
  9. #include <stdarg.h>
  10. #define VA_START va_start
  11.  
  12. #else    /* __STDC__ */
  13.  
  14. #include <varargs.h>
  15. #define VA_START(a,b) va_start(a)
  16.  
  17. #endif
  18.  
  19. #include "const.h"
  20.  
  21. #define BLANKS \
  22.  "                                                                                "
  23. /*12345678901234567890123456789012345678901234567890123456789012345678901234567890*/
  24.  
  25. static void fillit(buf,string,width,left_just)
  26.   char *buf, *string; int width, left_just;
  27. {
  28.   int len = strlen(string);
  29.  
  30.   if (width == 0 || width > 80 || len == width) strcat(buf,string);
  31.   else
  32.     if (width < len) strncat(buf,string,width);
  33.     else
  34.       if (left_just) { strcat(buf,string); strncat(buf,BLANKS,width-len); }
  35.       else { strncat(buf,BLANKS,width-len); strcat(buf,string); }
  36. }
  37.  
  38. #ifdef __STDC__
  39. char *spoof(char *buf, char *format, ...)
  40. #else
  41. char *spoof(buf,format,va_alist) char *buf, *format; va_dcl
  42. #endif
  43. {
  44.   extern char *i_to_a(), *l_to_a(), *tobase();
  45.  
  46.   char c[2];
  47.   int left_just, width;
  48.   long x;
  49.  
  50.   va_list varptr;
  51.  
  52.   VA_START(varptr, format);
  53.  
  54.   *buf = c[1] = '\0';    /* for %c */
  55.   while (*format)
  56.   {
  57.     switch (*format)
  58.     {
  59.       case '%': left_just = FALSE; width = 0;
  60.     more:    /* process some more of '%' */
  61.         switch (*++format)
  62.         {
  63.       case '%': strcat(buf,"%"); break;
  64.       case '-': left_just = TRUE; goto more;
  65.       case 'c': c[0]=va_arg(varptr,int);
  66.         fillit(buf,c,width,left_just); break;
  67.       case 'd': fillit(buf,i_to_a(va_arg(varptr,int)),width,left_just);
  68.         break;
  69.       case 'l':    /* 'd' only */
  70.         fillit(buf,l_to_a(va_arg(varptr,long)),width,left_just);
  71.         format++; break;
  72.       case 's': fillit(buf,va_arg(varptr,char *),width,left_just);
  73.          break;
  74.       case 'u':
  75.         x = (long)va_arg(varptr,unsigned int);
  76.         fillit(buf,l_to_a(x),width,left_just);
  77.         break;
  78.       case 'x':
  79.         fillit(buf,tobase((long)va_arg(varptr,unsigned int),16),
  80.             width,left_just);
  81.         break;
  82.       case '0': case '1': case '2': case '3': case '4': case '5':
  83.       case '6': case '7': case '8': case '9':
  84.         width = width*10 +*format -'0'; goto more;
  85.       case '\0': format--; break;    /* format not finished => ignore */
  86.       default:     /* copy %<char not matched> */
  87.         strcat(buf,"%"); *c = *(format-1); strcat(buf,c);
  88.         }
  89.     break;
  90.       default: *c = *format; strcat(buf,c);
  91.     }
  92.     format++;
  93.   }
  94.  
  95.   va_end(varptr);
  96.   return buf;
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. /* ****************  TEST ********************* */
  105. #ifdef TEST
  106.  
  107. main()
  108. {
  109.   char buf[200];
  110.  
  111.   puts(spoof(buf, "This is a %s %d", "test", 123));
  112. }
  113.  
  114. #endif
  115.