home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / lout2.lzh / LOUT2 / z35.c < prev    next >
Text File  |  1994-01-23  |  8KB  |  152 lines

  1. /*@z35.c:Time Keeper: MomentSym(), TimeString()@******************************/
  2. /*                                                                           */
  3. /*  LOUT: A HIGH-LEVEL LANGUAGE FOR DOCUMENT FORMATTING (VERSION 2.05)       */
  4. /*  COPYRIGHT (C) 1993 Jeffrey H. Kingston                                   */
  5. /*                                                                           */
  6. /*  Jeffrey H. Kingston (jeff@cs.su.oz.au)                                   */
  7. /*  Basser Department of Computer Science                                    */
  8. /*  The University of Sydney 2006                                            */
  9. /*  AUSTRALIA                                                                */
  10. /*                                                                           */
  11. /*  This program is free software; you can redistribute it and/or modify     */
  12. /*  it under the terms of the GNU General Public License as published by     */
  13. /*  the Free Software Foundation; either version 1, or (at your option)      */
  14. /*  any later version.                                                       */
  15. /*                                                                           */
  16. /*  This program is distributed in the hope that it will be useful,          */
  17. /*  but WITHOUT ANY WARRANTY; without even the implied warranty of           */
  18. /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            */
  19. /*  GNU General Public License for more details.                             */
  20. /*                                                                           */
  21. /*  You should have received a copy of the GNU General Public License        */
  22. /*  along with this program; if not, write to the Free Software              */
  23. /*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
  24. /*                                                                           */
  25. /*  FILE:         z35.c                                                      */
  26. /*  MODULE:       Time Keeper                                                */
  27. /*  EXTERNS:      MomentSym, InitTime(), StartMoment(), TimeString()         */
  28. /*                                                                           */
  29. /*****************************************************************************/
  30. #include <time.h>
  31. #include "externs"
  32.  
  33. #define load(str, typ, encl)                        \
  34.   sym = InsertSym(str, typ, no_fpos, DEFAULT_PREC,              \
  35.   FALSE, FALSE, 0, encl, MakeWord(WORD, STR_EMPTY, no_fpos));        \
  36.   if( typ == NPAR )  visible(sym) = TRUE
  37.  
  38. #define add_par(format, val, sym)                    \
  39.   sprintf( (char *) buff, format, val);                    \
  40.   par = New(PAR);  actual(par) = sym;                    \
  41.   Link(current_moment, par);                        \
  42.   tmp = MakeWord(WORD, buff, no_fpos);                    \
  43.   Link(par, tmp);
  44.  
  45. static OBJECT current_moment = nil;
  46. static FULL_CHAR time_string[30] = { '\0' };
  47.  
  48.  
  49. /*****************************************************************************/
  50. /*                                                                           */
  51. /*  OBJECT MomentSym;                                                        */
  52. /*                                                                           */
  53. /*  The symbol table entry for the @Moment symbol.                           */
  54. /*                                                                           */
  55. /*****************************************************************************/
  56.  
  57. OBJECT MomentSym = nil;
  58.  
  59.  
  60. /*****************************************************************************/
  61. /*                                                                           */
  62. /*  FULL_CHAR *TimeString()                                                  */
  63. /*                                                                           */
  64. /*  Returns a pointer to a string containing the current time.               */
  65. /*                                                                           */
  66. /*****************************************************************************/
  67.  
  68. FULL_CHAR *TimeString()
  69. { return time_string;
  70. } /* end TimeString */
  71.  
  72.  
  73. /*@::InitTime(), StartMoment()@***********************************************/
  74. /*                                                                           */
  75. /*  InitTime()                                                               */
  76. /*                                                                           */
  77. /*  Place a declaration of the @Moment symbol into the symbol table, and     */
  78. /*  initialize the value of the object StartMoment.                          */
  79. /*                                                                           */
  80. /*****************************************************************************/
  81.  
  82. InitTime()
  83. { long raw_time; struct tm *now;
  84.   FULL_CHAR buff[20]; OBJECT par, tmp, sym, env;
  85.   OBJECT tag, second, minute, hour, weekday,
  86.     monthday, yearday, month, year, century, dst;
  87.   debug0(DTK, D, "InitTime()");
  88.  
  89.   /* define @Moment symbol with its host of named parameters */
  90.   MomentSym = load(KW_MOMENT,         LOCAL, StartSym);
  91.   tag       = load(KW_TAG,            NPAR,  MomentSym);
  92.   second    = load(KW_SECOND,         NPAR,  MomentSym);
  93.   minute    = load(KW_MINUTE,         NPAR,  MomentSym);
  94.   hour      = load(KW_HOUR,           NPAR,  MomentSym);
  95.   monthday  = load(KW_DAY,            NPAR,  MomentSym);
  96.   month     = load(KW_MONTH,          NPAR,  MomentSym);
  97.   year      = load(KW_YEAR,           NPAR,  MomentSym);
  98.   century   = load(KW_CENTURY,        NPAR,  MomentSym);
  99.   weekday   = load(KW_WEEKDAY,        NPAR,  MomentSym);
  100.   yearday   = load(KW_YEARDAY,        NPAR,  MomentSym);
  101.   dst       = load(KW_DAYLIGHTSAVING, NPAR,  MomentSym);
  102.  
  103.   /* get current time and convert to ASCII */
  104.   if( time(&raw_time) == -1 )
  105.     Error(WARN, no_fpos, "unable to obtain the current time");
  106.   now = localtime(&raw_time);
  107.   StringCopy(time_string, AsciiToFull(asctime(now)));
  108.  
  109.   /* start of current_moment */
  110.   current_moment = New(CLOSURE);
  111.   actual(current_moment) = MomentSym;
  112.  
  113.   /* attach its many parameters */
  114.   add_par("%s",   KW_NOW,                      tag);
  115.   add_par("%d",   now->tm_sec,                 second);
  116.   add_par("%d",   now->tm_min,                 minute);
  117.   add_par("%d",   now->tm_hour,                hour);
  118.   add_par("%d",   now->tm_mday,                monthday);
  119.   add_par("%d",   now->tm_mon + 1,             month);
  120.   add_par("%.2d", now->tm_year % 100,          year);
  121.   add_par("%d",   (now->tm_year+1900) / 100,   century);
  122.   add_par("%d",   now->tm_wday + 1,            weekday);
  123.   add_par("%d",   now->tm_yday,                yearday);
  124.   add_par("%d",   now->tm_isdst,               dst);
  125.  
  126.   /* add a null environment */
  127.   env = New(ENV);
  128.   AttachEnv(env, current_moment);
  129.   debug0(DTK, D, "InitTime() returning.");
  130.   debug0(DTK, DD, "current_moment =");
  131.   ifdebug(DTK, DD, DebugObject(current_moment));
  132. } /* end InitTime */
  133.  
  134.  
  135. /*****************************************************************************/
  136. /*                                                                           */
  137. /*  OBJECT StartMoment()                                                     */
  138. /*                                                                           */
  139. /*  Returns a copy of the initial time.                                      */
  140. /*                                                                           */
  141. /*****************************************************************************/
  142.  
  143. OBJECT StartMoment()
  144. { OBJECT res;
  145.   debug0(DTK, D, "StartMoment()");
  146.   assert(current_moment != nil, "StartMoment: current_moment == nil!");
  147.   res = CopyObject(current_moment, no_fpos);
  148.   debug0(DTK, D, "StartMoment returning");
  149.   ifdebug(DTK, D, DebugObject(res));
  150.   return res;
  151. }
  152.