home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 565a.lha / WorldTime_v1.31 / main.c < prev    next >
C/C++ Source or Header  |  1991-11-06  |  9KB  |  368 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 (CheckIO((struct IORequest *)&timereq.tr_node)) {
  101.             WaitIO((struct IORequest *)&timereq.tr_node);
  102.             dotimes();
  103.             timereq.tr_time.tv_secs=1;        
  104.             timereq.tr_time.tv_micro=0;
  105.             SendIO((struct IORequest *)&timereq.tr_node);
  106.         }
  107.         while (Msg=(struct IntuiMessage *)GetMsg(Window->UserPort)) {
  108.             class=Msg->Class; code=Msg->Code;
  109.             x=Window->MouseX; y=Window->MouseY;
  110.             ReplyMsg((struct Message *)Msg);
  111.             if (class==CLOSEWINDOW) quit();
  112.             if (class==MOUSEBUTTONS && code==SELECTDOWN) {
  113.                 if (y>185 && y<195) {
  114.                     strcpy(otherplace,defotherplace);
  115.                     othertime=defothertime;
  116.                 }
  117.                 else {
  118.                     x-=8; y-=12;
  119.                     x/=156; y/=8;
  120.                     if (x>3 || x<0 || y>20 || y<0) continue;
  121.                     a=(x*21)+y;
  122.                     strcpy(otherplace,TimeZones[a].name);
  123.                     othertime=TimeZones[a].time;
  124.                 }
  125.                 iconify();
  126.                 drawtimes();
  127.                 lastmin=lastday=-1;
  128.                 dotimes();
  129.             }
  130.         }
  131.     }
  132. }
  133.  
  134. quit()
  135. {
  136.     if (Window) CloseWindow(Window);
  137.     if (timerport) {
  138.         AbortIO((struct IORequest *)&timereq);
  139.         WaitIO((struct IORequest *)&timereq);
  140.         CloseDevice((struct IORequest *)&timereq);
  141.         DeletePort(timerport);
  142.     }
  143.     CloseLibrary((struct Library *)IntuitionBase);
  144.     CloseLibrary((struct Library *)GfxBase);
  145.     exit(0);
  146. }
  147.  
  148. dotimes()
  149. {
  150.     int myhour,myminute,day,month,year,wday,mytot,x,y,gmttot,min,hour,a;
  151.     struct DateStamp now;
  152.     char buf[40],buf1[40],c,*ptr;
  153.  
  154.     DateStamp(&now);
  155.     myhour=now.ds_Minute/60; myminute=now.ds_Minute%60;
  156.  
  157.     if (now.ds_Minute!=lastmin) {
  158.         lastmin=now.ds_Minute;
  159.         gmttot=now.ds_Minute-(int)(yourtime*(float)60);
  160.         if (gmttot>1439) gmttot-=1440;
  161.         else if (gmttot<0) gmttot+=1440;
  162.  
  163.         x=112; y=18;
  164.         for (a=0;a<84;a++) {
  165.             mytot=gmttot+(int)(TimeZones[a].time*(float)60);
  166.             if (mytot>1439) mytot-=1440;
  167.             else if (mytot<0) mytot+=1440;
  168.             switch (yestom(lastmin,yourtime,TimeZones[a].time)) {
  169.                 case -1:
  170.                     c='-';
  171.                     SetAPen(rp,yesterdaycol);
  172.                     break;
  173.                 case 1:
  174.                     c='+';
  175.                     SetAPen(rp,tomorrowcol);
  176.                     break;
  177.                 case 0:
  178.                     c=' ';
  179.                     SetAPen(rp,todaycol);
  180.                     break;
  181.             }
  182.             hour=mytot/60; min=mytot%60;
  183.             sprintf(buf,"%c%2d:%02d",c,hour,min);
  184.             Move(rp,x,y);
  185.             Text(rp,buf,6);
  186.             y+=8;
  187.             if (y>=184) {
  188.                 x+=158; y=18;
  189.             }
  190.         }
  191.     }
  192.     sprintf(buf,"%2d:%02d:%02d, ",myhour,myminute,now.ds_Tick/TICKS_PER_SECOND);
  193.     SetAPen(rp,clockcol);
  194.     Move(rp,timeoff,192);
  195.     Text(rp,buf,10);
  196.     day=now.ds_Days;
  197.     if (day==lastday) return(0);
  198.     lastday=day;
  199.     CalcDate(&day,&month,&year);
  200.     wday=now.ds_Days%7;
  201.     if (day==1 || day==21 || day==31) ptr="st";
  202.     else if (day==2 || day==22) ptr="nd";
  203.     else if (day==3 || day==23) ptr="rd";
  204.     else ptr="th";
  205.     sprintf(buf,"%s, the %d%s of %s, 19%d",days[wday],day,ptr,months[month],year);
  206.     sprintf(buf1,"%-40s",buf);
  207.     Text(rp,buf1,40);
  208. }
  209.  
  210. CalcDate(day,month,year2)
  211. int *day,*month,*year2;
  212. {
  213.     int i,mdays,year;
  214.  
  215.     year=78;
  216.     year+=((*day)/1461)*4;
  217.     (*day)%=1461;
  218.     while ((*day)) {
  219.         mdays=365;
  220.         if ((year&3)==0) mdays++;
  221.         if ((*day)<mdays) break;
  222.         (*day)-=mdays;
  223.         year++;
  224.     }
  225.     for (i=0,(*day)++;i<12;i++) {
  226.         mdays=wdays[i];
  227.         if (i==1&&(year&3)==0) mdays++;
  228.         if ((*day)<=mdays) break;
  229.         (*day)-=mdays;
  230.     }
  231.     (*month)=i;
  232.     (*year2)=year;
  233. }
  234.  
  235. iconify()
  236. {
  237.     char buf[60];
  238.     int day,month,year,hour,min,sec,x,y,gmttot,dayoff,len=-1,xp=-1,h,w,v2=0;
  239.     struct DateStamp now;
  240.     ULONG class;
  241.     USHORT code;
  242.  
  243.     if (GfxBase->LibNode.lib_Version>=36) v2=1;
  244.  
  245.     if (Window) CloseWindow(Window);
  246.     if (otherplace[0]==0) iconwin.Height=10;
  247.     else iconwin.Height=21;
  248.     h=IntuitionBase->ActiveScreen->Height;
  249.     w=IntuitionBase->ActiveScreen->Width;
  250.     if (iconwin.Height+iconwin.TopEdge>h) iconwin.TopEdge=h-iconwin.Height;
  251.     if (iconwin.LeftEdge+388>w) iconwin.LeftEdge=w-388;
  252.  
  253.     if (!(Window=OpenWindow(&iconwin))) quit();
  254.     rp=Window->RPort;
  255.  
  256.     FOREVER {
  257.         Wait(1<<Window->UserPort->mp_SigBit|1<<timerport->mp_SigBit);
  258.         if (CheckIO((struct IORequest *)&timereq.tr_node)) {
  259.             WaitIO((struct IORequest *)&timereq.tr_node);
  260.             DateStamp(&now);
  261.             hour=now.ds_Minute/60; min=now.ds_Minute%60;
  262.             sec=now.ds_Tick/TICKS_PER_SECOND;
  263.             day=now.ds_Days;
  264.             CalcDate(&day,&month,&year);
  265.             sprintf(buf,"CHIP:%4d FAST:%4d %02d-%3s-%2d %2d:%02d:%02d",
  266.                 AvailMem(MEMF_CHIP)>>10,AvailMem(MEMF_FAST)>>10,
  267.                 day,shortmonths[month],year,hour,min,sec);
  268.             if (v2) {
  269.                 if (IntuitionBase->ActiveWindow==Window) {
  270.                     SetAPen(rp,2); SetBPen(rp,3);
  271.                 }
  272.                 else {
  273.                     SetAPen(rp,1); SetBPen(rp,0);
  274.                 }
  275.             }
  276.             else {
  277.                 SetAPen(rp,2); SetBPen(rp,1);
  278.             }
  279.             Move(rp,30,7); Text(rp,buf,38);
  280.             gmttot=now.ds_Minute-(int)(yourtime*(float)60);
  281.             if (gmttot>1439) gmttot-=1440;
  282.             else if (gmttot<0) gmttot+=1440;
  283.             gmttot+=(int)(othertime*(float)60);
  284.             if (gmttot>1439) gmttot-=1440;
  285.             else if (gmttot<0) gmttot+=1440;
  286.             dayoff=yestom(now.ds_Minute,yourtime,othertime);
  287.             hour=gmttot/60; min=gmttot%60;
  288.             day=now.ds_Days+dayoff; x=day;
  289.             CalcDate(&day,&month,&year);
  290.             sprintf(buf,"%s : %9s, %02d-%3s-%2d, %2d:%02d:%02d",
  291.                 otherplace,days[x%7],day,shortmonths[month],year,hour,min,sec);
  292.             if (len==-1) {
  293.                 len=strlen(buf);
  294.                 xp=(388-(len*8))/2;
  295.                 if (xp<0) xp=0;
  296.             }
  297.             SetAPen(rp,1); SetBPen(rp,0);
  298.             Move(rp,xp,17); Text(rp,buf,len);
  299.             timereq.tr_time.tv_secs=1;        
  300.             timereq.tr_time.tv_micro=0;
  301.             SendIO((struct IORequest *)&timereq.tr_node);
  302.             continue;
  303.         }
  304.         while (Msg=(struct IntuiMessage *)GetMsg(Window->UserPort)) {
  305.             class=Msg->Class; code=Msg->Code;
  306.             x=Window->MouseX; y=Window->MouseY;
  307.             ReplyMsg((struct Message *)Msg);
  308.             if (class==CLOSEWINDOW) quit();
  309.             if (class==MOUSEBUTTONS && code==MENUDOWN) {
  310.                 CloseWindow(Window);
  311.                 if (!(Window=OpenWindow(&win))) quit();
  312.                 rp=Window->RPort;
  313.                 return(0);
  314.             }
  315.         }
  316.     }
  317. }
  318.  
  319. fixcr(buf)
  320. char *buf;
  321. {
  322.     while (*buf && *buf!='\n') ++buf;
  323.     *buf=0;
  324. }
  325.  
  326. yestom(here,heretime,theretime)
  327. int here;
  328. float heretime,theretime;
  329. {
  330.     float diff;
  331.     int mind;
  332.  
  333.     diff=(heretime+12)-(theretime+12);
  334.     mind=(int)(diff*(float)60);
  335.     if (mind<0) {
  336.         if ((here-mind)>1439) return(1);
  337.     }
  338.     else if (mind>0) {
  339.         if ((here-mind)<0) return(-1);
  340.     }
  341.     return(0);
  342. }
  343.  
  344. drawtimes()
  345. {
  346.     int a,x,y,b;
  347.  
  348.     x=8; y=18;
  349.     SetAPen(rp,citycol);
  350.     SetBPen(rp,0);
  351.     SetDrMd(rp,JAM2);
  352.     for (a=0;a<84;a++) {
  353.         Move(rp,x,y);
  354.         Text(rp,TimeZones[a].name,strlen(TimeZones[a].name));
  355.         y+=8;
  356.         if (y>=184) {
  357.             x+=158; y=18;
  358.         }
  359.     }
  360.  
  361.     SetAPen(rp,clockcol);
  362.     Move(rp,8,192);
  363.     b=strlen(yourplace);
  364.     Text(rp,yourplace,b);
  365.     Text(rp,",",1);
  366.     timeoff=24+(b*8);
  367. }
  368.