home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 344_02 / print_ta.c < prev    next >
Text File  |  1990-05-20  |  4KB  |  116 lines

  1. /*
  2. HEADER:         ;
  3. TITLE:          print at tabs function
  4. VERSION:        1.0;
  5.  
  6. DESCRIPTION:    provides a print at a given tab position function.
  7.                 This function was designed to mimic the tab() function
  8.                 of BASIC including its idiosyncrisies. This is hady
  9.                 If you have to maintain reports written both in BASIC
  10.                 and C, as I have had to do over the years.
  11.           syntax: print_tab(file_pointer,tab_position,car_ret_flag,args...)
  12.                 Note: tab position 1 is the first char of the line
  13.                 examples:
  14.                     print_tab(fp_report,10,1,"hello") -- this prints
  15.                         to fp_report at tab position 10, the string "hello"
  16.                         and DOES A CARRIAGE RETURN (flag = 1)
  17.               print_tab(fp_report,40,0,"sum = %lf, avg = %lf",sum,avg) --
  18.                     prints to fp_report at tab 40, two values plus
  19.                     message string, and does not do a carriage returns
  20.  
  21.               RULES TO KEEP IN MIND:
  22.                     print_tab() must keep track of where the current
  23.                     imaginary print head is. It assumes that it is
  24.                     all the way on the left margin on the initial call.
  25.                     Either use the print_tab() exclusively to print
  26.                     everything to a file, or make sure the last call
  27.                     to print_tab() requested a carriage return, and
  28.                     Any printing before the next call to print_tabn()
  29.                     ended in a carriage return.
  30.  
  31. KEYWORDS:       tab,BASIC;
  32. SYSTEM:         Xenix 3.4b, MSDOS;
  33. FILENAME:       print_tab.c
  34. WARNINGS:       My compiler did not have stdarg.h so I used varargs.h,
  35.                 you may have to modify some if you are using stdarg.h
  36.                 I'm not sure.
  37.                 compile with -dNO_PROTOTYPE if your system does not
  38.                 support prototyping, with -dFOR_MSDOS if you are compiling
  39.                 for MSDOS with an ANSI standard compiler.
  40.                 Defaults assume compiling with prototypes for
  41.                 Xenix 3.4b on Altos 2086 computer.
  42.  
  43. SEE-ALSO:       demo.c
  44. AUTHORS:        Vern Martin, 449 W. Harrison, Alliance, Ohio 44601;
  45. COMPILERS:      ECOSOFT ECO-C88, XENIX 3.4B STANDARD COMPILER;
  46. */
  47.  
  48.  
  49. #include <stdio.h>
  50. #include <varargs.h>
  51. #ifdef FOR_MSDOS
  52. #   include <stdlib.h>
  53. #endif
  54.  
  55. #define done(x) { va_end(args); return(x); }
  56.  
  57. int print_tab(fp,tab,crflag,va_alist)
  58. FILE *fp;   /* file to print to */
  59. int tab;    /* tab position */
  60. int crflag; /* if == 1, then carriage return at end of print */
  61. va_dcl      /* whatever other arguments passed to sprintf */
  62. {
  63. /* local va_list */
  64.     va_list args;   /* args for sprintf */
  65. /* local char */
  66. #define PRBUF_MAX 254
  67.     static char prbuf[ PRBUF_MAX + 1 ]; /* print buffer */
  68.             /* static to avoid setting up every time */
  69.     char *fmt;  /* format for vsprintf() */
  70.     char *crstring = (crflag == 1) ? "\n" : "";
  71. /* local int */
  72.     int cct;    /* char count, no. of chars printed to prbuf */
  73.     static int tabpos = 1;/* absolute tab position, this is where the
  74.             print head is */
  75.     int spaces; /* number of spaces to add in order to achiev tab */
  76.     int output = 0; /* number of characters in output to file */
  77.     int add = 0;    /* output + add == total chars to file */
  78.  
  79. /* initialize args */
  80.     va_start(args);
  81.  
  82.     fmt = va_arg(args,char *);
  83.  
  84.     if ( (cct = vsprintf(prbuf,fmt,args)) > PRBUF_MAX) done(-10);
  85.  
  86.     if (tab == 0) spaces = tab; else spaces = tab - tabpos;
  87.  
  88. /* if current print head postion exceeds position desired, advance to begin
  89.     of next line, and tab desired spaces */
  90.     if (spaces < 0) {
  91.         if (fprintf(fp,"\n") < 0) done(-15);
  92.         tab--;  /* this is necessary to match what tab() in BASIC
  93.             does */
  94.         spaces = tab;
  95.         tabpos = 1;
  96.         add = 2; /* count \n + null at end of string */
  97.     }
  98.  
  99. /* add one more to the tabbing, to agree with BASIC tab()  */
  100.  
  101.     if ( (output = fprintf(fp,"%*.s%s%s",spaces," ",prbuf,crstring))
  102.         < 0) done(-20);
  103.  
  104.     if (crflag == 1) {
  105.         tabpos = 1;
  106.     }
  107.     else {
  108.  
  109.         if (tab == 0) tabpos += cct; else tabpos += cct + tab - tabpos;
  110.  
  111.     }
  112.  
  113. /* return number of characters placed in file */
  114.     done(output + add);
  115. }
  116.