home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / recio213.zip / rcputs.c < prev    next >
C/C++ Source or Header  |  1995-09-05  |  4KB  |  99 lines

  1. /*****************************************************************************
  2.    MODULE: rcputs.c
  3.   PURPOSE: recio column delimited string and char output functions
  4. COPYRIGHT: (C) 1994-1995, William Pierpoint
  5.  COMPILER: Borland C Version 3.1
  6.        OS: MSDOS Version 6.2
  7.   VERSION: 2.13
  8.   RELEASE: September 4, 1995
  9. *****************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15. #include "recio.h"
  16.  
  17. #define rcol(rp)         ((rp)->r_colno)
  18. #define rfp(rp)          ((rp)->r_fp)
  19. #define rflags(rp)       ((rp)->r_flags)
  20. #define rfldch(rp)       ((rp)->r_fldch)
  21. #define rtxtch(rp)       ((rp)->r_txtch)
  22. #define rflags(rp)       ((rp)->r_flags)
  23.  
  24. extern int _risready(REC *rp, int mode);
  25. extern int _rputc(REC *rp, int ch);
  26.  
  27. /****************************************************************************/
  28. void                         /* returns nothing                             */
  29.     rcputc(                  /* put character to record stream              */
  30.         REC   *rp,           /* record pointer                              */
  31.         size_t col,          /* column position                             */
  32.         int    ch)           /* character to put to record stream           */
  33. /****************************************************************************/
  34. {
  35.     if (_risready(rp, R_WRITE)) {
  36.         if (col >= rcolno(rp)) {
  37.             rfldno(rp)++;
  38.             rflags(rp) &= ~_R_TXT;
  39.             
  40.             /* if colno < col, pad with spaces */
  41.             while (rcolno(rp) < col) {
  42.                 if (_rputc(rp, ' ')) goto done;
  43.             }
  44.             
  45.             /* put character */
  46.             _rputc(rp, ch);
  47.         } else {
  48.             rseterr(rp, R_EINVAL); \
  49.         }
  50.     }
  51. done:
  52.     return;
  53. }
  54.  
  55. /****************************************************************************/
  56. void                         /* returns nothing                             */
  57.     rcputs(                  /* put string to record stream                 */
  58.         REC  *rp,            /* record pointer                              */
  59.         size_t begcol,       /* beginning column                            */
  60.         size_t endcol,       /* ending column                               */
  61.   const char *str)           /* pointer to string                           */
  62. /****************************************************************************/
  63. /* note: assumes str does not contain a newline character */
  64. {
  65.     size_t sl;               /* string length */
  66.  
  67.     if (_risready(rp, R_WRITE)) {
  68.         if (endcol >= begcol && begcol >= rcolno(rp)) {
  69.             rfldno(rp)++;
  70.             rflags(rp) &= ~_R_TXT;
  71.  
  72.             /* if colno < begcol, pad with spaces */
  73.             while (rcolno(rp) < begcol) {
  74.                 if (_rputc(rp, ' ')) goto done;
  75.             }
  76.  
  77.             sl = strlen(str);
  78.  
  79.             /* if str fits within the space, output it */
  80.             if (sl <= (endcol-begcol+1)) {
  81.                 if (fputs(str, rfp(rp)) != EOF) { 
  82.                     rcol(rp) += sl;
  83.                 } else {
  84.                     rseterr(rp, R_ENOPUT);
  85.                 }
  86.             /* else put as much of the string as possible */
  87.             } else {
  88.                 rsetwarn(rp, R_WWIDTH);
  89.                 while (rcolno(rp) <= endcol) {
  90.                     if (_rputc(rp, *str++)) goto done;
  91.                 }
  92.             }
  93.         } else {
  94.             rseterr(rp, R_EINVAL); \
  95.         }
  96.     }
  97. done:
  98. }
  99.