00001
00002
00003 #include <stdio.h>
00004
00005 #include <soi_str.h>
00006 #include <stdlib.h>
00007
00008 typedef struct
00009 {
00010 char *nm;
00011 int code;
00012 } nametable;
00013
00014 nametable tmunit[] = {
00015 {"hz", 8},
00016 {"hertz", 8},
00017 {"rot", 7},
00018 {"rots", 7},
00019 {"rotation", 7},
00020 {"rotations", 7},
00021 {"deg", 6},
00022 {"degs", 6},
00023 {"degree", 6},
00024 {"degrees", 6},
00025 {"wk", 5},
00026 {"wks", 5},
00027 {"week", 5},
00028 {"weeks", 5},
00029 {"w", 5},
00030 {"d", 4},
00031 {"day", 4},
00032 {"days", 4},
00033 {"h", 3},
00034 {"hr", 3},
00035 {"hrs", 3},
00036 {"hour", 3},
00037 {"hours", 3},
00038 {"m", 2},
00039 {"min", 2},
00040 {"mins", 2},
00041 {"minute", 2},
00042 {"minutes", 2},
00043 {"secs", 1},
00044 {"second", 1},
00045 {"seconds", 1},
00046 {"s", 1},
00047 {"", 0},
00048 {0, 0},
00049 };
00050
00051 long secs[] = {
00052 1,
00053 5,
00054 10,
00055 15,
00056 30,
00057 1*60,
00058 5*60,
00059 6*60,
00060 10*60,
00061 12*60,
00062 15*60,
00063 20*60,
00064 30*60,
00065 1*3600,
00066 2*3600,
00067 3*3600,
00068 4*3600,
00069 6*3600,
00070 8*3600,
00071 12*3600,
00072 };
00073
00074 static int lookup(char *n, nametable *t)
00075 {
00076 while (Strcmp(n,t->nm) && t->code) ++t;
00077 return(t->code);
00078 }
00079
00080 TIME atoinc(char *str)
00081 {
00082 double num, mult;
00083 char *units, *base_units;
00084 TIME rv;
00085
00086 if (!str) return 0;
00087 num = strtod (str, &units);
00088 if (str == units) num = 1;
00089 if (*units == '_') units++;
00090
00091 base_units = mprefix(units, &mult);
00092 if (*base_units == '_') base_units++;
00093
00094 switch(lookup(base_units,tmunit))
00095 {
00096 case 1:
00097 rv = (mult*num);
00098 break;
00099 case 2:
00100 rv = (mult*num*60.0);
00101 break;
00102 case 3:
00103 rv = (mult*num*3600.0);
00104 break;
00105 case 4:
00106 rv = (mult*num*86400);
00107 break;
00108 case 5:
00109 rv = (num*604800);
00110 break;
00111 case 6:
00112 rv = (num);
00113 break;
00114 case 7:
00115 rv = (num*360);
00116 break;
00117 case 8:
00118 rv = (mult*num);
00119 break;
00120 default:
00121 rv = (0);
00122 break;
00123 }
00124
00125 return rv;
00126 }
00127
00128
00129 TIME atoinc2(char *str)
00130 {
00131 double num, mult;
00132 char *units, *base_units;
00133 TIME rv;
00134
00135 if (!str) return 0;
00136 num = strtod (str, &units);
00137 if (str == units) num = 1;
00138 if (*units == '_') units++;
00139
00140 base_units = mprefix(units, &mult);
00141 if (*base_units == '_') base_units++;
00142
00143 switch(lookup(base_units,tmunit))
00144 {
00145 case 1:
00146 rv = (mult*num);
00147 break;
00148 case 2:
00149 rv = (mult*num*60.0);
00150 break;
00151 case 3:
00152 rv = (mult*num*3600.0);
00153 break;
00154 case 4:
00155 rv = (mult*num*86400);
00156 break;
00157 case 5:
00158 rv = (num*604800);
00159 break;
00160 case 6:
00161 rv = (num);
00162 break;
00163 case 7:
00164 rv = (num*360);
00165 break;
00166 case 8:
00167 rv = (mult*num);
00168 break;
00169 default:
00170 rv = -1;
00171 break;
00172 }
00173
00174 return rv;
00175 }
00176
00177 #define integral(x) ((x)==(long)(x))
00178
00179 char *sprint_inc(char *str, TIME inc)
00180 {
00181 double ii;
00182
00183 if (integral(ii = inc/31556952)) sprintf(str,"%.0fyear",ii);
00184 else if (integral(ii = inc/604800)) sprintf(str,"%.0fweek",ii);
00185 else if (integral(ii = inc/86400)) sprintf(str,"%.0fday",ii);
00186 else if (integral(ii = inc/3600)) sprintf(str,"%.0fhour",ii);
00187 else if (integral(ii = inc/60)) sprintf(str,"%.0fminute",ii);
00188 else sprintf(str,"%.fsecond",ii=inc);
00189 if (ii>1 || ii<-1) strcat(str,"s");
00190 return(str);
00191 }
00192
00193 int fprint_inc(FILE *fp, TIME inc)
00194 {
00195 char str[64];
00196 return(fprintf(fp,"%s",sprint_inc(str,inc)));
00197 }
00198
00199 int print_inc(TIME inc)
00200 {
00201 return(fprint_inc(stdout,inc));
00202 }