Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

time_util.c

Go to the documentation of this file.
00001 /*
00002  * time_util.c
00003  *
00004  * Copyright (c) 2003 The University of Utah and the Flux Group.
00005  * All rights reserved.
00006  *
00007  * This file is licensed under the terms of the GNU Public License.  
00008  * See the file "license.terms" for restrictions on redistribution 
00009  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
00010  */
00011 
00012 /**
00013  * @file time_util.c
00014  *
00015  * Implementations of the functions in time_util.c.
00016  */
00017 
00018 #include "config.h"
00019 
00020 #include <stdio.h>
00021 #include <string.h>
00022 
00023 #include <sys/types.h>
00024 #include <sys/time.h>
00025 
00026 #include "assert_pp.h"
00027 #include "time_util.h"
00028 
00029 int string_to_microsec(unsigned long long *us_out, const char *str)
00030 {
00031     char dead, units[3] = { 'u', 's', '\0' };
00032     int retval = 0;
00033 
00034     require(us_out != NULL);
00035     require(str != NULL);
00036 
00037     /* Clear the output value */
00038     *us_out = 0;
00039 
00040     /*
00041      * Scan the string for the number and an optional unit string.  We also
00042      * check for any extra characters that should not be in the input.  In
00043      * other words, if sscanf matches the extra character, we know there is
00044      * garbage at the end of the input.
00045      */
00046     switch( sscanf(str, "%qu%c%c%c", us_out, &units[0], &units[1], &dead) )
00047     {
00048     case 1: /* Only the number matched, use the default units. */
00049     case 3:
00050         if( strcmp(units, "us") == 0 )
00051         {
00052             retval = 1;
00053         }
00054         else if( strcmp(units, "ms") == 0 )
00055         {
00056             *us_out *= 1000;
00057             retval = 1;
00058         }
00059         break;
00060     case 2:
00061         units[1] = '\0';
00062         if( strcmp(units, "s") == 0 )
00063         {
00064             *us_out *= 1000000;
00065             retval = 1;
00066         }
00067         else if( strcmp(units, "m") == 0 )
00068         {
00069             *us_out *= 1000000 * 60;
00070             retval = 1;
00071         }
00072         break;
00073     case 0:
00074         /* Nothing matched, bail out. */
00075         break;
00076     case 4:
00077         /* There is garbage at the end of the input. */
00078         break;
00079     case EOF:
00080         /* The string is empty. */
00081         break;
00082     default:
00083         ensure(0);
00084         break;
00085     }
00086     return( retval );
00087 }

Generated on Mon Dec 1 16:29:07 2003 for CPU Broker by doxygen 1.3.4