home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_02 / allison / preprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-30  |  921 b   |  40 lines

  1. LISTING 10 - Functions to build strings backwards
  2. /* preprint.c:  Functions to prepend strings */
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdarg.h>
  7. #include <stdlib.h>
  8.  
  9. int prepend(char *buf, unsigned offset, char *new_str)
  10. {
  11.     int new_len = strlen(new_str);
  12.     int new_start = offset - new_len;
  13.  
  14.     /* Push a string onto the front of another */
  15.     if (new_start >= 0)
  16.         memcpy(buf+new_start,new_str,new_len);
  17.  
  18.     /* Return new start position (negative if underflowed) */
  19.     return new_start;
  20. }
  21.  
  22. int preprintf(char *buf, unsigned offset, char *format, ...)
  23. {
  24.     int pos = offset;
  25.     char *temp = malloc(BUFSIZ);
  26.  
  27.     /* Format, then push */
  28.     if (temp)
  29.     {
  30.         va_list args;
  31.  
  32.         va_start(args,format);
  33.         vsprintf(temp,format,args);
  34.         pos = prepend(buf,offset,temp);
  35.         va_end(args);
  36.         free(temp);
  37.     }
  38.     return pos;
  39. }
  40.