home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 530b.lha / WorldTime_v1.1 / main.c < prev    next >
C/C++ Source or Header  |  1991-07-03  |  9KB  |  355 lines

  1. #include "wtime.h"
  2.  
  3. main(argc,argv)
  4. int argc;
  5. char *argv[];
  6. {
  7.     int a,x,y,starticon=0;
  8.     ULONG class;
  9.     USHORT code;
  10.     FILE *in=NULL;
  11.     char buf[80],datfile[80];
  12.  
  13.     defotherplace[0]=datfile[0]=0;
  14.     todaycol=2; yesterdaycol=tomorrowcol=clockcol=3; citycol=1;
  15.  
  16.     if (argc>1) in=fopen(argv[1],"r");
  17.     if (!in) {
  18.         if (!(in=fopen("s:worldtime.cfg","r")))
  19.             if (!(in=fopen("worldtime.cfg","r"))) exit(0);
  20.     }
  21.     while (!(feof(in))) {
  22.         buf[0]=0;
  23.         fgets(buf,80,in);
  24.         if (strncmp(buf,"-lp",3)==0) {
  25.             strncpy(yourplace,&buf[3],19);
  26.             yourplace[19]=0;
  27.             fixcr(yourplace);
  28.         }
  29.         else if (strncmp(buf,"-lt",3)==0) yourtime=atof(&buf[3]);
  30.         else if (strncmp(buf,"-op",3)==0) {
  31.             strncpy(defotherplace,&buf[3],19);
  32.             defotherplace[19]=0;
  33.             fixcr(defotherplace);
  34.         }
  35.         else if (strncmp(buf,"-tc",3)==0) todaycol=atoi(&buf[3]);
  36.         else if (strncmp(buf,"-yc",3)==0) yesterdaycol=atoi(&buf[3]);
  37.         else if (strncmp(buf,"-mc",3)==0) tomorrowcol=atoi(&buf[3]);
  38.         else if (strncmp(buf,"-cc",3)==0) clockcol=atoi(&buf[3]);
  39.         else if (strncmp(buf,"-ic",3)==0) citycol=atoi(&buf[3]);
  40.         else if (strncmp(buf,"-ot",3)==0) defothertime=atof(&buf[3]);
  41.         else if (strncmp(buf,"-x",2)==0) iconwin.LeftEdge=atoi(&buf[2]);
  42.         else if (strncmp(buf,"-y",2)==0) iconwin.TopEdge=atoi(&buf[2]);
  43.         else if (strncmp(buf,"-i",2)==0) starticon=1;
  44.         else if (strncmp(buf,"-d",2)==0) {
  45.             strncpy(datfile,&buf[3],79);
  46.             datfile[79]=0;
  47.             fixcr(datfile);
  48.         }
  49.     }
  50.     fclose(in);
  51.     in=NULL;
  52.     if (datfile[0]) in=fopen(datfile,"r");
  53.     if (!in) {
  54.         if (!(in=fopen("s:worldtime.dat","r")))
  55.             if (!(in=fopen("worldtime.dat","r"))) exit(0);
  56.     }
  57.     for (a=0;a<84;a++) {
  58.         buf[0]=0;
  59.         fgets(buf,80,in);
  60.         if (feof(in)) break;
  61.         fixcr(buf);
  62.         for (x=0;x<80;x++) {
  63.             if (buf[x]==',' || buf[x]==0) break;
  64.         }
  65.         strncpy(datfile,buf,x);
  66.         if (x>13) x=13; datfile[x]=0;
  67.         strcpy(TimeZones[a].name,datfile); TimeZones[a].time=atof(&buf[x+1]);
  68.     }
  69.     fclose(in);
  70.     timerport=Window=NULL;
  71.  
  72.     IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",0);
  73.     GfxBase=(struct GfxBase *)OpenLibrary("graphics.library",0);
  74.     if (!(timerport=CreatePort(0,0))) quit();
  75.     OpenDevice(TIMERNAME,UNIT_VBLANK,(struct IORequest *)&timereq,0);
  76.  
  77.     timereq.tr_node.io_Message.mn_ReplyPort=timerport;
  78.     timereq.tr_node.io_Command=TR_ADDREQUEST;
  79.     timereq.tr_node.io_Flags=0;
  80.     timereq.tr_time.tv_secs=1;
  81.     timereq.tr_time.tv_micro=0;
  82.     SendIO((struct IORequest *)&timereq.tr_node);
  83.  
  84.     if (starticon) {
  85.         strcpy(otherplace,defotherplace);
  86.         othertime=defothertime;
  87.         iconify();
  88.     }
  89.     else {
  90.         if (!(Window=OpenWindow(&win))) quit();
  91.         rp=Window->RPort;
  92.     }
  93.  
  94.     drawtimes();
  95.     lastmin=lastday=-1;
  96.     dotimes();
  97.  
  98.     FOREVER {
  99.         Wait(1<<Window->UserPort->mp_SigBit|1<<timerport->mp_SigBit);
  100.         if (GetMsg(timerport)) {
  101.             dotimes();
  102.             timereq.tr_time.tv_secs=1;        
  103.             timereq.tr_time.tv_micro=0;
  104.             SendIO((struct IORequest *)&timereq.tr_node);
  105.         }
  106.         while (Msg=(struct IntuiMessage *)GetMsg(Window->UserPort)) {
  107.             class=Msg->Class; code=Msg->Code;
  108.             x=Window->MouseX; y=Window->MouseY;
  109.             ReplyMsg((struct Message *)Msg);
  110.             if (class==CLOSEWINDOW) quit();
  111.             if (class==MOUSEBUTTONS && code==SELECTDOWN) {
  112.                 if (y>185 && y<195) {
  113.                     strcpy(otherplace,defotherplace);
  114.                     othertime=defothertime;
  115.                 }
  116.                 else {
  117.                     x-=8; y-=12;
  118.                     x/=156; y/=8;
  119.                     if (x>3 || x<0 || y>20 || y<0) continue;
  120.                     a=(x*21)+y;
  121.                     strcpy(otherplace,TimeZones[a].name);
  122.                     othertime=TimeZones[a].time;
  123.                 }
  124.                 iconify();
  125.                 drawtimes();
  126.                 lastmin=lastday=-1;
  127.                 dotimes();
  128.             }
  129.         }
  130.     }
  131. }
  132.  
  133. quit()
  134. {
  135.     if (Window) CloseWindow(Window);
  136.     if (timerport) {
  137.         AbortIO((struct IORequest *)&timereq);
  138.         CloseDevice((struct IORequest *)&timereq);
  139.         DeletePort(timerport);
  140.     }
  141.     CloseLibrary((struct Library *)IntuitionBase);
  142.     CloseLibrary((struct Library *)GfxBase);
  143.     exit(0);
  144. }
  145.  
  146. dotimes()
  147. {
  148.     int myhour,myminute,day,month,year,wday,mytot,x,y,gmttot,min,hour,a;
  149.     struct DateStamp now;
  150.     char buf[40],buf1[40],c,*ptr;
  151.  
  152.     DateStamp(&now);
  153.     myhour=now.ds_Minute/60; myminute=now.ds_Minute%60;
  154.  
  155.     if (now.ds_Minute!=lastmin) {
  156.         lastmin=now.ds_Minute;
  157.         gmttot=now.ds_Minute-(int)(yourtime*(float)60);
  158.         if (gmttot>1439) gmttot-=1440;
  159.         else if (gmttot<0) gmttot+=1440;
  160.  
  161.         x=112; y=18;
  162.         for (a=0;a<84;a++) {
  163.             mytot=gmttot+(int)(TimeZones[a].time*(float)60);
  164.             if (mytot>1439) mytot-=1440;
  165.             else if (mytot<0) mytot+=1440;
  166.             switch (yestom(lastmin,yourtime,TimeZones[a].time)) {
  167.                 case -1:
  168.                     c='-';
  169.                     SetAPen(rp,yesterdaycol);
  170.                     break;
  171.                 case 1:
  172.                     c='+';
  173.                     SetAPen(rp,tomorrowcol);
  174.                     break;
  175.                 case 0:
  176.                     c=' ';
  177.                     SetAPen(rp,todaycol);
  178.                     break;
  179.             }
  180.             hour=mytot/60; min=mytot%60;
  181.             sprintf(buf,"%c%2d:%02d",c,hour,min);
  182.             Move(rp,x,y);
  183.             Text(rp,buf,6);
  184.             y+=8;
  185.             if (y>=184) {
  186.                 x+=158; y=18;
  187.             }
  188.         }
  189.     }
  190.     sprintf(buf,"%2d:%02d:%02d, ",myhour,myminute,now.ds_Tick/TICKS_PER_SECOND);
  191.     SetAPen(rp,clockcol);
  192.     Move(rp,timeoff,192);
  193.     Text(rp,buf,10);
  194.     day=now.ds_Days;
  195.     if (day==lastday) return(0);
  196.     lastday=day;
  197.     CalcDate(&day,&month,&year);
  198.     wday=now.ds_Days%7;
  199.     if (day==1 || day==21 || day==31) ptr="st";
  200.     else if (day==2 || day==22) ptr="nd";
  201.     else if (day==3 || day==23) ptr="rd";
  202.     else ptr="th";
  203.     sprintf(buf,"%s, the %d%s of %s, 19%d",days[wday],day,ptr,months[month],year);
  204.     sprintf(buf1,"%-40s",buf);
  205.     Text(rp,buf1,40);
  206. }
  207.  
  208. CalcDate(day,month,year2)
  209. int *day,*month,*year2;
  210. {
  211.     int i,mdays,year;
  212.  
  213.     year=78;
  214.     year+=((*day)/1461)*4;
  215.     (*day)%=1461;
  216.     while ((*day)) {
  217.         mdays=365;
  218.         if ((year&3)==0) mdays++;
  219.         if ((*day)<mdays) break;
  220.         (*day)-=mdays;
  221.         year++;
  222.     }
  223.     for (i=0,(*day)++;i<12;i++) {
  224.         mdays=wdays[i];
  225.         if (i==1&&(year&3)==0) mdays++;
  226.         if ((*day)<=mdays) break;
  227.         (*day)-=mdays;
  228.     }
  229.     (*month)=i;
  230.     (*year2)=year;
  231. }
  232.  
  233. iconify()
  234. {
  235.     char buf[60];
  236.     int day,month,year,hour,min,sec,x,y,gmttot,dayoff,len=-1,xp=-1,h,w;
  237.     struct DateStamp now;
  238.     ULONG class;
  239.     USHORT code;
  240.  
  241.     if (Window) CloseWindow(Window);
  242.     if (otherplace[0]==0) iconwin.Height=10;
  243.     else iconwin.Height=21;
  244.     h=IntuitionBase->ActiveScreen->Height;
  245.     w=IntuitionBase->ActiveScreen->Width;
  246.     if (iconwin.Height+iconwin.TopEdge>h) iconwin.TopEdge=h-iconwin.Height;
  247.     if (iconwin.LeftEdge+388>w) iconwin.LeftEdge=w-388;
  248.  
  249.     if (!(Window=OpenWindow(&iconwin))) quit();
  250.     rp=Window->RPort;
  251.  
  252.     FOREVER {
  253.         Wait(1<<Window->UserPort->mp_SigBit|1<<timerport->mp_SigBit);
  254.         if (GetMsg(timerport)) {
  255.             DateStamp(&now);
  256.             hour=now.ds_Minute/60; min=now.ds_Minute%60;
  257.             sec=now.ds_Tick/TICKS_PER_SECOND;
  258.             day=now.ds_Days;
  259.             CalcDate(&day,&month,&year);
  260.             sprintf(buf,"CHIP:%4d FAST:%4d %02d-%3s-%2d %2d:%02d:%02d",
  261.                 AvailMem(MEMF_CHIP)>>10,AvailMem(MEMF_FAST)>>10,
  262.                 day,shortmonths[month],year,hour,min,sec);
  263.             SetAPen(rp,2); SetBPen(rp,1);
  264.             Move(rp,30,7); Text(rp,buf,38);
  265.             gmttot=now.ds_Minute-(int)(yourtime*(float)60);
  266.             if (gmttot>1439) gmttot-=1440;
  267.             else if (gmttot<0) gmttot+=1440;
  268.             gmttot+=(int)(othertime*(float)60);
  269.             if (gmttot>1439) gmttot-=1440;
  270.             else if (gmttot<0) gmttot+=1440;
  271.             dayoff=yestom(now.ds_Minute,yourtime,othertime);
  272.             hour=gmttot/60; min=gmttot%60;
  273.             day=now.ds_Days+dayoff; x=day;
  274.             CalcDate(&day,&month,&year);
  275.             sprintf(buf,"%s : %9s, %02d-%3s-%2d, %2d:%02d:%02d",
  276.                 otherplace,days[x%7],day,shortmonths[month],year,hour,min,sec);
  277.             if (len==-1) {
  278.                 len=strlen(buf);
  279.                 xp=(388-(len*8))/2;
  280.                 if (xp<0) xp=0;
  281.             }
  282.             SetAPen(rp,1); SetBPen(rp,0);
  283.             Move(rp,xp,17); Text(rp,buf,len);
  284.             timereq.tr_time.tv_secs=1;        
  285.             timereq.tr_time.tv_micro=0;
  286.             SendIO((struct IORequest *)&timereq.tr_node);
  287.             continue;
  288.         }
  289.         while (Msg=(struct IntuiMessage *)GetMsg(Window->UserPort)) {
  290.             class=Msg->Class; code=Msg->Code;
  291.             x=Window->MouseX; y=Window->MouseY;
  292.             ReplyMsg((struct Message *)Msg);
  293.             if (class==CLOSEWINDOW) quit();
  294.             if (class==MOUSEBUTTONS && code==MENUDOWN) {
  295.                 CloseWindow(Window);
  296.                 if (!(Window=OpenWindow(&win))) quit();
  297.                 rp=Window->RPort;
  298.                 return(0);
  299.             }
  300.         }
  301.     }
  302. }
  303.  
  304. fixcr(buf)
  305. char *buf;
  306. {
  307.     while (*buf) {
  308.         if (*buf=='\n') *buf=0;
  309.         ++buf;
  310.     }
  311. }
  312.  
  313. yestom(here,heretime,theretime)
  314. int here;
  315. float heretime,theretime;
  316. {
  317.     float diff;
  318.     int mind;
  319.  
  320.     diff=(heretime+12)-(theretime+12);
  321.     mind=(int)(diff*(float)60);
  322.     if (mind<0) {
  323.         if ((here-mind)>1439) return(1);
  324.     }
  325.     else if (mind>0) {
  326.         if ((here-mind)<0) return(-1);
  327.     }
  328.     return(0);
  329. }
  330.  
  331. drawtimes()
  332. {
  333.     int a,x,y,b;
  334.  
  335.     x=8; y=18;
  336.     SetAPen(rp,citycol);
  337.     SetBPen(rp,0);
  338.     SetDrMd(rp,JAM2);
  339.     for (a=0;a<84;a++) {
  340.         Move(rp,x,y);
  341.         Text(rp,TimeZones[a].name,strlen(TimeZones[a].name));
  342.         y+=8;
  343.         if (y>=184) {
  344.             x+=158; y=18;
  345.         }
  346.     }
  347.  
  348.     SetAPen(rp,clockcol);
  349.     Move(rp,8,192);
  350.     b=strlen(yourplace);
  351.     Text(rp,yourplace,b);
  352.     Text(rp,",",1);
  353.     timeoff=24+(b*8);
  354. }
  355.