00001
00002
00003
00004
00005
00006 #include <stdlib.h>
00007 #include <time.h>
00008 #include <errno.h>
00009 #include <stdio.h>
00010 #include <math.h>
00011
00012 char *cvsinfo_durcon = "cvsinfo: $Header: /home/cvsuser/cvsroot/JSOC/proj/globalhs/apps/durcon.c,v 1.2 2013/04/28 08:05:54 tplarson Exp $";
00013
00014 static int parse_duration(char **, double *);
00015 int drms_names_parseduration(char **, double *);
00016
00017 int main(int argc, char **argv)
00018 {
00019 int status=0;
00020 double dval;
00021 while (--argc > 0)
00022 {
00023 status+=drms_names_parseduration(++argv,&dval);
00024 printf("%.3f ",dval);
00025 }
00026 printf("\n");
00027 return status;
00028 }
00029
00030
00031 static inline int IsZero(double d)
00032 {
00033 return d == (double)0.0;
00034 }
00035
00036 static inline int IsPosHugeVal(double d)
00037 {
00038 return d == HUGE_VAL;
00039 }
00040
00041 static inline int IsNegHugeVal(double d)
00042 {
00043 return d == -HUGE_VAL;
00044 }
00045
00046
00047 static int parse_duration(char **in, double *duration)
00048 {
00049 char *end, *p = *in;
00050 double dval;
00051
00052 dval = strtod(p,&end);
00053 if ( (IsZero(dval) && end==p) ||
00054 ((IsPosHugeVal(dval) || IsNegHugeVal(dval)) && errno==ERANGE))
00055 {
00056 fprintf(stderr,"Syntax Error: Expected finite floating point value at "
00057 "beginning of time duration, found '%s'.\n", p);
00058 goto error;
00059 }
00060 else
00061 p = end;
00062 switch(*p++)
00063 {
00064 case 's':
00065 *duration = 1.0*dval;
00066 break;
00067 case 'm':
00068 *duration = 60.0*dval;
00069 break;
00070 case 'h':
00071 *duration = 3600.0*dval;
00072 break;
00073 case 'd':
00074 *duration = 86400.0*dval;
00075 break;
00076 case 'u':
00077
00078
00079 *duration = 1.0*dval;
00080 break;
00081 default:
00082 fprintf(stderr,"Syntax Error: Time duration unit must be one of 's', "
00083 "'m', 'h', 'd', 'u', found '%c', '%s'.\n", *(p-1), p);
00084 goto error;
00085 }
00086 *in = p;
00087 return 0;
00088 error:
00089 return 1;
00090 }
00091
00092 int drms_names_parseduration(char **in, double *duration)
00093 {
00094 char *tmp = strdup(*in);
00095 int ret = 1;
00096
00097 if (tmp)
00098 {
00099 char *tmp2 = tmp;
00100 ret = parse_duration(&tmp2, duration);
00101 free(tmp);
00102 }
00103
00104 return ret;
00105 }