home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 423_01 / recio200 / rcgets.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-15  |  3.5 KB  |  99 lines

  1. /*****************************************************************************
  2.    MODULE: rcgets.c
  3.   PURPOSE: recio column delimited string and char input functions
  4. COPYRIGHT: (C) 1994 William Pierpoint
  5.  COMPILER: Borland C Version 3.1
  6.        OS: MSDOS Version 6.2
  7.   VERSION: 2.00
  8.   RELEASE: April 15, 1994
  9. *****************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15. #include "recio.h"
  16.  
  17. extern int _rstatus(REC *rp, int mode);
  18. extern char *_rfldstr(REC *rp, size_t len);
  19. extern char *_rerrs(REC *rp, int errnum);
  20.  
  21. #define rcol(rp)         ((rp)->r_colno)
  22.  
  23. /****************************************************************************/
  24. int                          /* return character; EOF on error              */
  25.     rcgetc(                  /* get character at column position            */
  26.         REC *rp,             /* record pointer                              */
  27.         size_t col)          /* column position                             */
  28. /****************************************************************************/
  29. {
  30.     int ch=EOF;              /* character */
  31.     char *fldp;              /* field pointer */
  32.     
  33.     if (!_rstatus(rp, R_READ)) {
  34.         if (col >= rbegcolno(rp)) {
  35.             rcol(rp) = col - rbegcolno(rp);
  36.             fldp = _rfldstr(rp, 1);
  37.             if (fldp) {
  38.                 /* loop while field empty or error cleared */
  39.                 for (;;) {
  40.                     /* if field not empty, success */
  41.                     if (*fldp != '\0') {
  42.                         ch = *fldp;
  43.                         goto done;
  44.                     }
  45.                     
  46.                     /* field empty; see if errfn() replaces it */
  47.                     fldp = _rerrs(rp, R_EMISDAT);
  48.                     if (fldp) { 
  49.                         continue; 
  50.                     } else { 
  51.                         goto done;
  52.                     }
  53.                 }
  54.             }
  55.         }
  56.         rseterr(rp, R_EINVAL);
  57.     }
  58. done:
  59.     return (ch);
  60. }
  61.  
  62. /****************************************************************************/
  63. char *                       /* return pointer to string; "" on error       */
  64.     rcgets(                  /* get string between columns (inclusive)      */
  65.         REC *rp,             /* record pointer                              */
  66.         size_t begcol,       /* beginning column                            */
  67.         size_t endcol)       /* ending column                               */
  68. /****************************************************************************/
  69. {
  70.     static char empty[]="";  /* empty string */
  71.     char *retp=empty;        /* return pointer */
  72.     char *fldp;              /* pointer to field buffer */
  73.     
  74.     if (!_rstatus(rp, R_READ)) {
  75.         if (endcol >= begcol && begcol >= rbegcolno(rp)) {
  76.             rcol(rp) = begcol - rbegcolno(rp);
  77.             fldp = _rfldstr(rp, endcol-begcol+1);
  78.             if (fldp) {
  79.                     
  80.                 /* if field not empty, success */
  81.                 if (*fldp) {
  82.                     retp = fldp;
  83.                     goto done;
  84.                     
  85.                 /* field empty (beyond end of record) */
  86.                 } else {
  87.                     rsetfldstr(rp, "");
  88.                     rsetwarn(rp, R_WEMPSTR);
  89.                     retp = rflds(rp);
  90.                     goto done;
  91.                 }
  92.             }
  93.         }
  94.         rseterr(rp, R_EINVAL);
  95.     }
  96. done:
  97.     return (retp);
  98. }
  99.