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

File: [Development] / JSOC / proj / globalhs / apps / durcon.c (download)
Revision: 1.2, Sun Apr 28 07:05:54 2013 UTC (5 years, 5 months ago) by tplarson
Branch: MAIN
CVS Tags: globalhs_version_9, globalhs_version_8, globalhs_version_7, globalhs_version_6, globalhs_version_5, globalhs_version_4, globalhs_version_3, globalhs_version_2, globalhs_version_19, globalhs_version_18, globalhs_version_17, globalhs_version_16, globalhs_version_15, globalhs_version_14, globalhs_version_13, globalhs_version_12, globalhs_version_11, globalhs_version_10, globalhs_version_1, globalhs_version_0, Ver_LATEST, Ver_9-2, Ver_9-1, Ver_9-0, Ver_8-8, Ver_8-7, Ver_8-6, Ver_8-5, Ver_8-4, Ver_8-3, Ver_8-2, Ver_8-12, Ver_8-11, Ver_8-10, Ver_8-1, HEAD
Changes since 1.1: +2 -0 lines
added string to track cvs versions

/* self-contained source code for durcon,
   converts commandline arguments given as a duration to a number of seconds.
   compile it like this: 'cc -o durcon durcon.c' or similar
*/ 

#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <stdio.h>
#include <math.h>

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 $";

static int parse_duration(char **, double *);
int drms_names_parseduration(char **, double *);

int main(int argc, char **argv)
{
  int status=0;
  double dval;
  while (--argc > 0)
  {
    status+=drms_names_parseduration(++argv,&dval);
    printf("%.3f   ",dval);
  }
  printf("\n");
  return status;
}


static inline int IsZero(double d)
{
return d == (double)0.0;
}

static inline int IsPosHugeVal(double d)
{
return d == HUGE_VAL;
}

static inline int IsNegHugeVal(double d)
{
return d == -HUGE_VAL;
}

/* Parse time duration constant */
static int parse_duration(char **in, double *duration)
{
  char *end, *p = *in;
  double dval;

  dval = strtod(p,&end);
  if ( (IsZero(dval) && end==p)  || 
       ((IsPosHugeVal(dval) || IsNegHugeVal(dval)) && errno==ERANGE))
  {
    fprintf(stderr,"Syntax Error: Expected finite floating point value at "
	    "beginning of time duration, found '%s'.\n", p);    
    goto error;
  }
  else
    p = end;
  switch(*p++)
  {
  case 's':
    *duration = 1.0*dval;
    break;
  case 'm':
    *duration = 60.0*dval;
    break;
  case 'h':
    *duration = 3600.0*dval;
    break;
  case 'd':
    *duration = 86400.0*dval;
    break;
  case 'u':
    /* Means 'unit' - for SLOT type slotted keys, can have a query of the form
     * [392.3/100u].  This just means [392.3 - 492.3). */
    *duration = 1.0*dval;
    break;
  default:
    fprintf(stderr,"Syntax Error: Time duration unit must be one of 's', "
	    "'m', 'h', 'd', 'u', found '%c', '%s'.\n", *(p-1), p); 
    goto error;
  }
  *in = p;
  return 0;
 error:
  return 1;
}

int drms_names_parseduration(char **in, double *duration)
{
   char *tmp = strdup(*in);
   int ret = 1;

   if (tmp)
   {
      char *tmp2 = tmp;
      ret = parse_duration(&tmp2, duration);
      free(tmp);
   }

   return ret;
}

Karen Tian
Powered by
ViewCVS 0.9.4