home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Exec 3 / CD_Magazyn_EXEC_nr_3.iso / Internet / Strony_WWW / SDK / www.redloop.com / downloads / ddate.c < prev   
C/C++ Source or Header  |  2000-06-29  |  10KB  |  370 lines

  1. /* $ DVCS ID: $jer|,523/lhos,KYTP!41023161\b"?" <<= DO NOT DELETE! */
  2.  
  3. /* ddate.c .. converts boring normal dates to fun Discordian Date -><-
  4.    written  the 65th day of The Aftermath in the Year of Our Lady of 
  5.    Discord 3157 by Druel the Chaotic aka Jeremy Johnson aka
  6.    mpython@gnu.ai.mit.edu  
  7.       28 Sever St Apt #3
  8.       Worcester MA 01609
  9.  
  10.    and I'm not responsible if this program messes anything up (except your 
  11.    mind, I'm responsible for that)
  12.  
  13.    (k) YOLD 3161 and all time before and after.
  14.    Reprint, reuse, and recycle what you wish.
  15.    This program is in the public domain.  Distribute freely.  Or not.
  16.  
  17.    Majorly hacked, extended and bogotified/debogotified on 
  18.    Sweetmorn, Bureaucracy 42, 3161 YOLD, by Lee H:. O:. Smith, KYTP, 
  19.    aka Andrew Bulhak, aka acb@dev.null.org
  20.  
  21.    and I'm not responsible if this program messes anything up (except your 
  22.    mind, I'm responsible for that) (and that goes for me as well --lhos)
  23.  
  24.    Version history:
  25.    Bureflux 3161:      First release of enhanced ddate with format strings
  26.    59 Bcy, 3161:       PRAISE_BOB and KILL_BOB options split, other minor
  27.                        changes.
  28.  
  29.    1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
  30.    - added Native Language Support
  31.  
  32.    2000-03-17 Burt Holzman <bnh@iname.com>
  33.    - added range checks for dates
  34. */
  35. /* #include "../defines.h"        /* for util-linux-version */
  36.  
  37.  
  38. /* configuration options  VVVVV   READ THIS!!! */
  39.  
  40. /* If you wish ddate(1) to print the date in the same format as Druel's 
  41.  * original ddate when called in immediate mode, define OLD_IMMEDIATE_FMT 
  42.  */
  43.  
  44. #define OLD_IMMEDIATE_FMT
  45.  
  46. /* If you wish to use the US format for aneristic dates (m-d-y), as opposed to
  47.  * the Commonwealth format, define US_FORMAT.
  48.  */
  49.  
  50. /* #define US_FORMAT */
  51.  
  52. /* If you are ideologically, theologically or otherwise opposed to the 
  53.  * Church of the SubGenius and do not wish your copy of ddate(1) to contain
  54.  * code for counting down to X-Day, undefine KILL_BOB */
  55.  
  56. #define KILL_BOB 13013
  57.  
  58. /* If you wish ddate(1) to contain SubGenius slogans, define PRAISE_BOB */
  59.  
  60. /*#define PRAISE_BOB 13013*/
  61.  
  62. #include <stdlib.h>
  63. #include <string.h>
  64. #include <time.h>
  65. #include <stdio.h>
  66.  
  67. #ifndef __GNUC__
  68. #define inline /* foo */
  69. #endif
  70.  
  71. #define util_linux_version "Amiga SDK"
  72.  
  73. #ifdef KILL_BOB
  74. int xday_countdown(int yday, int year);
  75. #endif
  76.  
  77.  
  78. /* string constants */
  79.  
  80. char *day_long[5] = { 
  81.     "Sweetmorn", "Boomtime", "Pungenday", "Prickle-Prickle", "Setting Orange"
  82. };
  83.  
  84. char *day_short[5] = {"SM","BT","PD","PP","SO"};
  85.  
  86. char *season_long[5] = { 
  87.     "Chaos", "Discord", "Confusion", "Bureaucracy", "The Aftermath"
  88. };
  89.  
  90. char *season_short[5] = {"Chs", "Dsc", "Cfn", "Bcy", "Afm"};
  91.  
  92. char *holyday[5][2] = { 
  93.     { "Mungday", "Chaoflux" },
  94.     { "Mojoday", "Discoflux" },
  95.     { "Syaday",  "Confuflux" },
  96.     { "Zaraday", "Bureflux" },
  97.     { "Maladay", "Afflux" }
  98. };
  99.  
  100. struct disc_time {
  101.     int season; /* 0-4 */
  102.     int day; /* 0-72 */
  103.     int yday; /* 0-365 */
  104.     int year; /* 3066- */
  105. };
  106.  
  107. char *excl[] = {
  108.     "Hail Eris!", "All Hail Discordia!", "Kallisti!", "Fnord.", "Or not.",
  109.     "Wibble.", "Pzat!", "P'tang!", "Frink!", 
  110. #ifdef PRAISE_BOB
  111.     "Slack!", "Praise \"Bob\"!", "Or kill me.",
  112. #endif /* PRAISE_BOB */
  113.     /* randomness, from the Net and other places. Feel free to add (after
  114.        checking with the relevant authorities, of course). */
  115.     "Grudnuk demand sustenance!", "Keep the Lasagna flying!", 
  116.     "Umlaut Zebra über alles!", "You are what you see.",
  117.     "Or is it?", "This statement is false.",
  118. #if defined(linux) || defined (__linux__) || defined (__linux)
  119.     "Hail Eris, Hack Linux!",
  120. #endif
  121.     ""
  122. };
  123.  
  124. char default_fmt[] = "%{%A, %B %d%}, %Y YOLD";
  125. char *default_immediate_fmt=
  126. #ifdef OLD_IMMEDIATE_FMT
  127. "Today is %{%A, the %e day of %B%} in the YOLD %Y%N%nCelebrate %H"
  128. #else
  129. default_fmt
  130. #endif
  131. ;
  132.  
  133. #define DY(y) (y+1166)
  134.  
  135. static inline char *ending(int i) {
  136.     return (i%10==1)?"st":(i%10==2?"nd":(i%10==3?"rd":"th"));
  137. }
  138.  
  139. static inline int leapp(int i) {
  140.     return (!(DY(i)%4))&&((DY(i)%100)||(!(DY(i)%400)));
  141. }
  142.  
  143. /* select a random string */
  144. static inline char *sel(char **strings, int num) {
  145.     return(strings[num]);
  146. }
  147.  
  148. void print(struct disc_time,char **); /* old */
  149. void format(char *buf, const char* fmt, struct disc_time dt);
  150. /* read a fortune file */
  151. int load_fortunes(char *fn, char *delim, char** result);
  152.  
  153. struct disc_time convert(int,int);
  154. struct disc_time makeday(int,int,int);
  155.  
  156. int
  157. main (int argc, char *argv[]) 
  158. {
  159.     long t;
  160.     struct tm *eris;
  161.     int bob,raw;
  162.     struct disc_time hastur;
  163.     char schwa[23*17], *fnord=0;
  164.     int pi;
  165.     char *progname, *p;
  166.  
  167.     progname = argv[0];
  168.     if ((p = strrchr(progname, '/')) != NULL)
  169.     progname = p+1;
  170.  
  171.  
  172.     /* srandom(time(NULL)); */
  173.     /* do args here */
  174.     for(pi=1; pi<argc; pi++) {
  175.     switch(argv[pi][0]) {
  176.     case '+': fnord=argv[pi]+1; break;
  177.     case '-': 
  178.         switch(argv[pi][1]) {
  179.         case 'V':
  180.         printf("%s from %s\n", progname, util_linux_version);
  181.         default: goto usage;
  182.         }
  183.     default: goto thud;
  184.     }
  185.     }
  186.  
  187.   thud:
  188.     if (argc-pi==3){ 
  189.     int moe=atoi(argv[pi]), larry=atoi(argv[pi+1]), curly=atoi(argv[pi+2]);
  190.     hastur=makeday(
  191. #ifdef US_FORMAT
  192.         moe,larry,
  193. #else
  194.         larry,moe,
  195. #endif
  196.         curly);
  197.     if (hastur.season == -1) {
  198.         printf("Invalid date -- out of range\n");
  199.         return -1;
  200.     }
  201.     fnord=fnord?fnord:default_fmt;
  202.     } else if (argc!=pi) { 
  203.       usage:
  204.     fprintf(stderr,"usage: %s [+format] [day month year]\n", argv[0]);
  205.     exit(1);
  206.     } else {
  207.     t= time(NULL);
  208.     eris=localtime(&t);
  209.     bob=eris->tm_yday; /* days since Jan 1. */
  210.     raw=eris->tm_year; /* years since 1980 */
  211.     hastur=convert(bob,raw);
  212.     fnord=fnord?fnord:default_immediate_fmt;
  213.     }
  214.     format(schwa, fnord, hastur);
  215.     printf("%s\n", schwa);
  216.    
  217.     return 0;
  218. }
  219.  
  220. void format(char *buf, const char* fmt, struct disc_time dt)
  221. {
  222.     int tib_start=-1, tib_end=0;
  223.     int i, fmtlen=strlen(fmt);
  224.     char *bufptr=buf;
  225.  
  226. /*    fprintf(stderr, "format(%p, \"%s\", dt)\n", buf, fmt);*/
  227.  
  228.     /* first, find extents of St. Tib's Day area, if defined */
  229.     for(i=0; i<fmtlen; i++) {
  230.     if(fmt[i]=='%') {
  231.         switch(fmt[i+1]) {
  232.         case 'A':
  233.         case 'a':
  234.         case 'd':
  235.         case 'e':
  236.         if(tib_start>0)        tib_end=i+1;
  237.         else            tib_start=i;
  238.         break;
  239.         case '{': tib_start=i; break;
  240.         case '}': tib_end=i+1; break;
  241.         }
  242.     }
  243.     }
  244.  
  245.     /* now do the formatting */
  246.     buf[0]=0;
  247.  
  248.     for(i=0; i<fmtlen; i++) {
  249.     if((i==tib_start) && (dt.day==-1)) {
  250.         /* handle St. Tib's Day */
  251.         strcpy(bufptr, "St. Tib's Day"); bufptr += 13;
  252.         i=tib_end;
  253.     } else {
  254.         if(fmt[i]=='%') {
  255.         char *wibble=0, snarf[23];
  256.         switch(fmt[++i]) {
  257.         case 'A': wibble=day_long[dt.yday%5]; break;
  258.         case 'a': wibble=day_short[dt.yday%5]; break;
  259.         case 'B': wibble=season_long[dt.season]; break;
  260.         case 'b': wibble=season_short[dt.season]; break;
  261.         case 'd': sprintf(snarf, "%d", dt.day+1); wibble=snarf; break;
  262.         case 'e': sprintf(snarf, "%d%s", dt.day+1, ending(dt.day+1)); 
  263.             wibble=snarf; break;
  264.         case 'H': if(dt.day==4||dt.day==49)
  265.             wibble=holyday[dt.season][dt.day==49]; break;
  266.         case 'N': if(dt.day!=4&&dt.day!=49) goto eschaton; break;
  267.         case 'n': *(bufptr++)='\n'; break;
  268.         case 't': *(bufptr++)='\t'; break;
  269.             
  270.         case 'Y': sprintf(snarf, "%d", dt.year); wibble=snarf; break;
  271.         case '.': wibble=sel(excl, sizeof(excl)/sizeof(excl[0]));
  272.             break;
  273. #ifdef KILL_BOB
  274.         case 'X': sprintf(snarf, "%d", 
  275.                   xday_countdown(dt.yday, dt.year));
  276.                   wibble = snarf; break;
  277. #endif /* KILL_BOB */
  278.         }
  279.         if(wibble) {
  280. /*            fprintf(stderr, "wibble = (%s)\n", wibble);*/
  281.             strcpy(bufptr, wibble); bufptr+=strlen(wibble);
  282.         }
  283.         } else {
  284.         *(bufptr++) = fmt[i];
  285.         }
  286.     }
  287.     }
  288.   eschaton:
  289.     *(bufptr)=0;
  290. }
  291.  
  292. struct disc_time makeday(int imonth,int iday,int iyear) /*i for input */
  293.     struct disc_time funkychickens;
  294.     
  295.     int cal[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
  296.     int dayspast=0;
  297.  
  298.     /* basic range checks */
  299.     if (imonth < 1 || imonth > 12) {
  300.         funkychickens.season = -1;
  301.         return funkychickens;
  302.     }
  303.     if (iday < 1 || iday > cal[imonth-1]) {
  304.         if (!(imonth == 2 && iday == 29 && iyear%4 == 0 &&
  305.           (iyear%100 != 0 || iyear%400 == 0))) {
  306.             funkychickens.season = -1;
  307.             return funkychickens;
  308.         }
  309.     }
  310.     
  311.     imonth--;
  312.     funkychickens.year= iyear+1166;
  313.     while(imonth>0) { dayspast+=cal[--imonth]; }
  314.     funkychickens.day=dayspast+iday-1;
  315.     funkychickens.season=0;
  316.     if((funkychickens.year%4)==2) {
  317.     if (funkychickens.day==59 && iday==29)  funkychickens.day=-1;
  318.     }
  319.     funkychickens.yday=funkychickens.day;
  320. /*               note: EQUAL SIGN...hopefully that fixes it */
  321.     while(funkychickens.day>=73) {
  322.     funkychickens.season++;
  323.     funkychickens.day-=73;
  324.     }
  325.     return funkychickens;
  326. }
  327.  
  328. struct disc_time convert(int nday, int nyear)
  329. {  struct disc_time funkychickens;
  330.    
  331.    funkychickens.year = nyear+3066;
  332.    funkychickens.day=nday;
  333.    funkychickens.season=0;
  334.    if ((funkychickens.year%4)==2)
  335.      {if (funkychickens.day==59)
  336.     funkychickens.day=-1;
  337.      else if (funkychickens.day >59)
  338.        funkychickens.day-=1;
  339.     }
  340.    funkychickens.yday=funkychickens.day;
  341.    while (funkychickens.day>=73)
  342.      { funkychickens.season++;
  343.        funkychickens.day-=73;
  344.      }
  345.    return funkychickens;
  346.   
  347.  }
  348.  
  349. #ifdef KILL_BOB
  350.  
  351. /* Code for counting down to X-Day, X-Day being Cfn 40, 3164 
  352.  *
  353.  * After `X-Day' passed without incident, the CoSG declared that it had 
  354.  * got the year upside down --- X-Day is actually in 8661 AD rather than 
  355.  * 1998 AD.
  356.  *
  357.  * Thus, the True X-Day is Cfn 40, 9827.
  358.  *
  359.  */
  360.  
  361. int xday_countdown(int yday, int year) {
  362.     int r=(185-yday)+(((yday<59)&&(leapp(year)))?1:0);
  363.     while(year<9827) r+=(leapp(++year)?366:365);
  364.     while(year>9827) r-=(leapp(year--)?366:365);
  365.     return r;
  366. }
  367.  
  368. #endif
  369.