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

  1. /*****************************************************************************
  2.    MODULE: rcputt.c
  3.   PURPOSE: recio column delimited time 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. #include <time.h>
  15.  
  16. #include "recio.h"
  17.  
  18. extern int _risready(REC *rp, int mode);
  19. extern int _rputc(REC *rp, int ch);
  20.  
  21. #define rflags(rp)       ((rp)->r_flags)
  22. #define rfp(rp)          ((rp)->r_fp)
  23. #define rcol(rp)         ((rp)->r_colno)
  24. #define rfldch(rp)       ((rp)->r_fldch)
  25. #define rtmfmt(rp)       ((rp)->r_tmfmt)
  26.  
  27. /****************************************************************************/
  28. void                         /* returns nothing                             */
  29.     rcputtm(                 /* put time to record stream                   */
  30.         REC *rp,             /* record pointer                              */
  31.         size_t begcol,       /* field inclusive beginning column            */
  32.         size_t endcol,       /* field inclusive ending column               */
  33.         struct tm t)         /* broken-down time                            */
  34. /****************************************************************************/
  35. {
  36.     size_t maxsize;          /* max size of str buffer */
  37.     size_t len;              /* actual length of str */
  38.     char *str=NULL;          /* storage for output string */
  39.     int errnum;              /* error number */
  40.  
  41.     if (_risready(rp, R_WRITE)) {
  42.         if (endcol >= begcol && begcol >= rcolno(rp)) {
  43.             rfldno(rp)++;
  44.             rflags(rp) &= ~_R_TXT;
  45.  
  46.             /* if colno < begcol, pad with spaces */
  47.             while (rcolno(rp) < begcol) {
  48.                 if (_rputc(rp, ' ')) goto done;
  49.             }
  50.  
  51.             /* put time into formatted string */
  52.             maxsize = strlen(rtmfmt(rp));  /* arbitrary initial max size   */
  53.             do {                           /* loop until max size adequate */
  54.                 maxsize += 16;             /* arbitrary max size increment */
  55.                 do {                       /* loop until memory allocated  */
  56.                     str = (char *) realloc(str, maxsize+1);
  57.                     if (!str) {
  58.                         errnum = rseterr(rp, R_ENOMEM);
  59.                         if (errnum) goto done;
  60.                     }
  61.                 } while (!str);
  62.                 len = strftime(str, maxsize, rtmfmt(rp), &t);
  63.             } while (!len);
  64.     
  65.             /* if str fits within the space, output it */
  66.             if (len <= (endcol-begcol+1)) {
  67.                 if (fputs(str, rfp(rp)) != EOF) { 
  68.                     rcol(rp) += len;
  69.                 } else {
  70.                     rseterr(rp, R_ENOPUT);
  71.                 }
  72.             /* else converted string too long for space */
  73.             } else {
  74.                 rsetwarn(rp, R_WWIDTH);
  75.                 while (rcolno(rp) <= endcol) {
  76.                     if (_rputc(rp, '*')) goto done;
  77.                 }
  78.             }
  79.         } else {
  80.             rseterr(rp, R_EINVAL); \
  81.         }
  82.     }
  83. done:
  84.     free(str);
  85.     return;
  86. }
  87.  
  88. /****************************************************************************/
  89. void                         /* returns nothing                             */
  90.     rcputt(                  /* put time to record stream                   */
  91.         REC *rp,             /* record pointer                              */
  92.         size_t begcol,       /* field inclusive beginning column            */
  93.         size_t endcol,       /* field inclusive ending column               */
  94.         time_t time)         /* time                                        */
  95. /****************************************************************************/
  96. {
  97.     rcputtm(rp, begcol, endcol, timetotm(time));
  98. }
  99.