#include "config.h" /* Copyright (C) 2002 Brad Jorsch This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include #include "wmweather+.h" #include "forecast.h" #include "getLine.h" #include "convert.h" #include "download.h" #include "diff.h" #include "die.h" #include "subst.h" /* Important variables */ #define MRF_MAX 7 static time_t mrf_time=0; static char *mrf_file=NULL; static char *mrf_newfile=NULL; static char *mrf_req[2]={ NULL, NULL }; static struct forecast forecasts[MRF_MAX]; /********* init functions ************/ static int parse_mrf(char *file); static void reset_mrf(void){ int i; for(i=0; i=0){ if(S_ISREG(statbuf.st_mode) && statbuf.st_size!=0 && diff(mrf_newfile, mrf_file) && parse_mrf(mrf_newfile)){ mrf_time=find_next_time(mrf_newfile, "MOS GUIDANCE", 1440); rename(mrf_newfile, mrf_file); } else { unlink(mrf_newfile); if(!parse_mrf(mrf_file)) reset_mrf(); } } } void mrf_cleanup(void){ if(mrf_file==NULL) return; unlink(mrf_newfile); unlink(mrf_file); } void update_mrf(int force){ time_t t; if(mrf_file==NULL) return; t=time(NULL)/60; if(!force && mrf_time>t) return; mrf_time=find_next_time(mrf_file, "MOS GUIDANCE", 15); download_file(mrf_newfile, mrf_req[0], mrf_req[1], 0, mrf_callback, NULL); } #define NEXT(s) free(s); \ len=getLine(&s, fp); \ if(strstr(s, "")!=NULL) len=0; #define DIE() return (free(s), fclose(fp), 0) #define INT(c) (tmp[0]=*c, tmp[1]=*(c+1), tmp[2]=*(c+2), tmp[3]=0, atoi(tmp)) static int parse_mrf(char *file){ FILE *fp; char *s, *c; int len; int mon, day; int i, j, m, x, y; char tmp[4]={0, 0, 0, 0}; reset_mrf(); if((fp=fopen(file, "r"))==NULL) return 0; /* Look for something like an MRF coded forecast */ c=NULL; while(!feof(fp)){ len=getLine(&s, fp); if((c=strstr(s, "MOS GUIDANCE"))!=NULL) break; free(s); } if(c==NULL) return (fclose(fp), 0); c=strchr(c, '/'); if(c==NULL || !isdigit(*(c-1)) || !isdigit(*(c+1))) DIE(); mon=atoi(c-2); x=atoi(c+1); if(mon<1 || mon>12 || x<1 || x>31) DIE(); c=strchr(c+1, '/'); if(c==NULL || !isdigit(*(c-1)) || !isdigit(*(c+1))) DIE(); y=atoi(c+1)-1900; NEXT(s); if(len<10) DIE(); if(strncmp(s, "FHR", 3)) DIE(); NEXT(s); if(len<10) DIE(); for(i=0; i<7; i++){ if(!strncmp(s, wdaynames[i], 3)) break; } if(i>=7) DIE(); day=atoi(s+4); if(x>25 && day<5) mon++; if(x<5 && day>25) mon--; for(m=0; m