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

  1. /*****************************************************************************
  2.    MODULE: rputt.c
  3.   PURPOSE: recio character 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.     rputtm(                  /* put time to record stream                   */
  30.         REC *rp,             /* record pointer                              */
  31.         struct tm t)         /* broken-down time                            */
  32. /****************************************************************************/
  33. {
  34.     size_t maxsize;          /* max size of str buffer */
  35.     size_t len;              /* actual length of str */
  36.     char *str=NULL;          /* storage for output string */
  37.     int errnum;              /* error number */
  38.  
  39.     if (_risready(rp, R_WRITE)) {
  40.         rfldno(rp)++;
  41.         rflags(rp) &= ~_R_TXT;
  42.  
  43.         /* if not first field, put field separator if not null */
  44.         if (rfldno(rp) > 1 && rfldch(rp)) {
  45.             if (_rputc(rp, rfldch(rp))) goto done;
  46.         }
  47.  
  48.         /* put time into formatted string */
  49.         maxsize = strlen(rtmfmt(rp));  /* arbitrary initial max size   */
  50.         do {                           /* loop until max size adequate */
  51.             maxsize += 16;             /* arbitrary max size increment */
  52.             do {                       /* loop until memory allocated  */
  53.                 str = (char *) realloc(str, maxsize+1);
  54.                 if (!str) {
  55.                     errnum = rseterr(rp, R_ENOMEM);
  56.                     if (errnum) goto done;
  57.                 }
  58.             } while (!str);
  59.             len = strftime(str, maxsize, rtmfmt(rp), &t);
  60.         } while (!len);
  61.  
  62.         /* put formatted string to output */
  63.         if (fputs(str, rfp(rp)) != EOF) { 
  64.             rcol(rp) += len;
  65.         } else {
  66.             rseterr(rp, R_ENOPUT);
  67.             goto done;
  68.         }
  69.     }
  70. done:
  71.     free(str);
  72.     return;
  73. }
  74.  
  75. /****************************************************************************/
  76. void                         /* returns nothing                             */
  77.     rputt(                   /* put time to record stream                   */
  78.         REC *rp,             /* record pointer                              */
  79.         time_t time)         /* time                                        */
  80. /****************************************************************************/
  81. {
  82.     rputtm(rp, timetotm(time));
  83. }
  84.