From 9f09ddb4a64d67579a389d42262ee50c2adbf427 Mon Sep 17 00:00:00 2001 From: scottmk Date: Sun, 6 Jun 2010 01:54:31 +0000 Subject: [PATCH 1/1] New function oilsUtilsIntervalToSeconds() to translate a string into a number representing the number of seconds in an interval of time. Simple integers are converted directly. Anything else is passed to PostgreSQL to be translated as an interval string. M Open-ILS/include/openils/oils_utils.h M Open-ILS/src/c-apps/oils_utils.c git-svn-id: svn://svn.open-ils.org/ILS/trunk@16602 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Open-ILS/include/openils/oils_utils.h | 2 + Open-ILS/src/c-apps/oils_utils.c | 81 +++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/Open-ILS/include/openils/oils_utils.h b/Open-ILS/include/openils/oils_utils.h index fae7e4ddfe..4357a40744 100644 --- a/Open-ILS/include/openils/oils_utils.h +++ b/Open-ILS/include/openils/oils_utils.h @@ -102,6 +102,8 @@ jsonObject* oilsUtilsFetchWorkstationByName( const char* name ); int oilsUtilsIsDBTrue( const char* val ); +long oilsUtilsIntervalToSeconds( const char* interval ); + #ifdef __cplusplus } #endif diff --git a/Open-ILS/src/c-apps/oils_utils.c b/Open-ILS/src/c-apps/oils_utils.c index 3739413784..22a2a57f88 100644 --- a/Open-ILS/src/c-apps/oils_utils.c +++ b/Open-ILS/src/c-apps/oils_utils.c @@ -1,3 +1,4 @@ +#include #include "openils/oils_utils.h" #include "openils/oils_idl.h" @@ -368,3 +369,83 @@ jsonObject* oilsUtilsFetchWorkstationByName( const char* name ) { jsonObjectFree(p); return r; } + +/** + @brief Convert a string to a number representing a time interval in seconds. + @param interval Pointer to string, e.g. "420" or "2 weeks". + @return If successful, the number of seconds that the string represents; otherwise -1. + + If the string is all digits (apart from any leading or trailing white space), convert + it directly. Otherwise pass it to PostgreSQL for translation. + + The result is the same as if we were to pass every string to PostgreSQL, except that, + depending on the value of LONG_MAX, we return values for some strings that represent + intervals too long for PostgreSQL to represent (i.e. more than 2147483647 seconds). + + WARNING: a valid interval of -1 second will be indistinguishable from an error. If + such an interval is a plausible possibility, don't use this function. +*/ +long oilsUtilsIntervalToSeconds( const char* s ) { + + if( !s ) { + osrfLogWarning( OSRF_LOG_MARK, "String to be converted is NULL" ); + return -1; + } + + // Skip leading white space + while( isspace( (unsigned char) *s )) + ++s; + + if( '\0' == *s ) { + osrfLogWarning( OSRF_LOG_MARK, "String to be converted is empty or all white space" ); + return -1; + } + + // See if the string is a raw number, i.e. all digits + // (apart from any leading or trailing white space) + + const char* p = s; // For traversing and examining the remaining string + if( isdigit( (unsigned char) *p )) { + // Looks like a number so far...skip over the digits + do { + ++p; + } while( isdigit( (unsigned char) *p )); + // Skip over any following white space + while( isspace( (unsigned char) *p )) + ++p; + if( '\0' == *p ) { + // This string is a raw number. Convert it directly. + long n = strtol( s, NULL, 10 ); + if( LONG_MAX == n ) { + // numeric overflow + osrfLogWarning( OSRF_LOG_MARK, + "String \"%s\"represents a number too big for a long", s ); + return -1; + } else + return n; + } + } + + // If we get to this point, the string is not all digits. Pass it to PostgreSQL. + + // Build the query + jsonObject* query_obj = jsonParseFmt( + "{\"from\":[\"config.interval_to_seconds\",\"%s\"]}", s ); + + // Execute the query + jsonObject* result = oilsUtilsCStoreReq( + "open-ils.cstore.json_query", query_obj ); + jsonObjectFree( query_obj ); + + // Get the results + jsonObject* seconds_obj = jsonObjectGetKey( result, "config.interval_to_seconds" ); + long seconds = -1; + if( seconds_obj && JSON_NUMBER == seconds_obj->type ) + seconds = (long) jsonObjectGetNumber( seconds_obj ); + else + osrfLogError( OSRF_LOG_MARK, + "Error calling json_query to convert \"%s\" to seconds", s ); + + jsonObjectFree( result ); + return seconds; +} -- 2.43.2