(file) Return to durcon.c CVS log (file) (dir) Up to [Development] / JSOC / proj / globalhs / apps

  1 tplarson 1.1 /* self-contained source code for durcon,
  2                 converts commandline arguments given as a duration to a number of seconds.
  3                 compile it like this: 'cc -o durcon durcon.c' or similar
  4              */ 
  5              
  6              #include <stdlib.h>
  7              #include <time.h>
  8              #include <errno.h>
  9              #include <stdio.h>
 10              #include <math.h>
 11              
 12 tplarson 1.2 char *cvsinfo_durcon = "cvsinfo: $Header: durcon.c,v tplarson Exp $";
 13              
 14 tplarson 1.1 static int parse_duration(char **, double *);
 15              int drms_names_parseduration(char **, double *);
 16              
 17              int main(int argc, char **argv)
 18              {
 19                int status=0;
 20                double dval;
 21                while (--argc > 0)
 22                {
 23                  status+=drms_names_parseduration(++argv,&dval);
 24                  printf("%.3f   ",dval);
 25                }
 26                printf("\n");
 27                return status;
 28              }
 29              
 30              
 31              static inline int IsZero(double d)
 32              {
 33              return d == (double)0.0;
 34              }
 35 tplarson 1.1 
 36              static inline int IsPosHugeVal(double d)
 37              {
 38              return d == HUGE_VAL;
 39              }
 40              
 41              static inline int IsNegHugeVal(double d)
 42              {
 43              return d == -HUGE_VAL;
 44              }
 45              
 46              /* Parse time duration constant */
 47              static int parse_duration(char **in, double *duration)
 48              {
 49                char *end, *p = *in;
 50                double dval;
 51              
 52                dval = strtod(p,&end);
 53                if ( (IsZero(dval) && end==p)  || 
 54                     ((IsPosHugeVal(dval) || IsNegHugeVal(dval)) && errno==ERANGE))
 55                {
 56 tplarson 1.1     fprintf(stderr,"Syntax Error: Expected finite floating point value at "
 57              	    "beginning of time duration, found '%s'.\n", p);    
 58                  goto error;
 59                }
 60                else
 61                  p = end;
 62                switch(*p++)
 63                {
 64                case 's':
 65                  *duration = 1.0*dval;
 66                  break;
 67                case 'm':
 68                  *duration = 60.0*dval;
 69                  break;
 70                case 'h':
 71                  *duration = 3600.0*dval;
 72                  break;
 73                case 'd':
 74                  *duration = 86400.0*dval;
 75                  break;
 76                case 'u':
 77 tplarson 1.1     /* Means 'unit' - for SLOT type slotted keys, can have a query of the form
 78                   * [392.3/100u].  This just means [392.3 - 492.3). */
 79                  *duration = 1.0*dval;
 80                  break;
 81                default:
 82                  fprintf(stderr,"Syntax Error: Time duration unit must be one of 's', "
 83              	    "'m', 'h', 'd', 'u', found '%c', '%s'.\n", *(p-1), p); 
 84                  goto error;
 85                }
 86                *in = p;
 87                return 0;
 88               error:
 89                return 1;
 90              }
 91              
 92              int drms_names_parseduration(char **in, double *duration)
 93              {
 94                 char *tmp = strdup(*in);
 95                 int ret = 1;
 96              
 97                 if (tmp)
 98 tplarson 1.1    {
 99                    char *tmp2 = tmp;
100                    ret = parse_duration(&tmp2, duration);
101                    free(tmp);
102                 }
103              
104                 return ret;
105              }

Karen Tian
Powered by
ViewCVS 0.9.4