diff options
author | kpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b> | 2006-02-15 00:24:24 +0000 |
---|---|---|
committer | kpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b> | 2006-02-15 00:24:24 +0000 |
commit | 15db46a445a4d6d71993475369b4a4906861f362 (patch) | |
tree | 6572dc8ac1336a5e0c0859186138a5313e88bd85 | |
parent | 69f7e69869d726709046bffd71fd109b7dc4560d (diff) |
add API function for parsing strings to time_t (issue #6320, with mods)
git-svn-id: http://svn.digium.com/svn/asterisk/trunk@10105 f38db490-d61c-443f-a65b-d21fe96a405b
-rw-r--r-- | apps/app_sayunixtime.c | 47 | ||||
-rw-r--r-- | apps/app_voicemail.c | 8 | ||||
-rw-r--r-- | channels/chan_iax2.c | 5 | ||||
-rw-r--r-- | channels/chan_sip.c | 13 | ||||
-rw-r--r-- | funcs/func_strings.c | 15 | ||||
-rw-r--r-- | include/asterisk/strings.h | 11 | ||||
-rw-r--r-- | res/res_agi.c | 17 | ||||
-rw-r--r-- | utils.c | 24 |
8 files changed, 67 insertions, 73 deletions
diff --git a/apps/app_sayunixtime.c b/apps/app_sayunixtime.c index c6db4f140..ad96c6d6f 100644 --- a/apps/app_sayunixtime.c +++ b/apps/app_sayunixtime.c @@ -76,45 +76,26 @@ static int sayunixtime_exec(struct ast_channel *chan, void *data) struct localuser *u; char *s,*zone=NULL,*timec,*format; time_t unixtime; - struct timeval tv; + s = ast_strdupa(data); + if (!s) + return data ? -1 : 0; LOCAL_USER_ADD(u); - tv = ast_tvnow(); - unixtime = (time_t)tv.tv_sec; - - if( !strcasecmp(chan->language, "da" ) ) { - format = "A dBY HMS"; - } else if ( !strcasecmp(chan->language, "de" ) ) { - format = "A dBY HMS"; - } else { - format = "ABdY 'digits/at' IMp"; - } - - if (data) { - s = data; - if ((s = ast_strdupa(s))) { - timec = strsep(&s,"|"); - if ((timec) && (*timec != '\0')) { - long timein; - if (sscanf(timec,"%ld",&timein) == 1) { - unixtime = (time_t)timein; - } - } - if (s) { - zone = strsep(&s,"|"); - if (zone && (*zone == '\0')) - zone = NULL; - if (s) { - format = s; - } - } - } + format = "c"; /* default datetime */ + + timec = strsep(&s,"|"); + ast_get_time_t(timec, &unixtime, time(NULL)); + if (s) { + zone = strsep(&s,"|"); + if (ast_strlen_zero(zone)) + zone = NULL; } + if (s) /* override format */ + format = s; - if (chan->_state != AST_STATE_UP) { + if (chan->_state != AST_STATE_UP) res = ast_answer(chan); - } if (!res) res = ast_say_date_with_format(chan, unixtime, AST_DIGIT_ANY, chan->language, format, zone); diff --git a/apps/app_voicemail.c b/apps/app_voicemail.c index da3f90c0e..13ad2f786 100644 --- a/apps/app_voicemail.c +++ b/apps/app_voicemail.c @@ -3608,25 +3608,21 @@ static int play_message_datetime(struct ast_channel *chan, struct ast_vm_user *v int res = 0; struct vm_zone *the_zone = NULL; time_t t; - long tin; - if (sscanf(origtime,"%ld",&tin) < 1) { + if (ast_get_time_t(origtime, &t, 0)) { ast_log(LOG_WARNING, "Couldn't find origtime in %s\n", filename); return 0; } - t = tin; /* Does this user have a timezone specified? */ if (!ast_strlen_zero(vmu->zonetag)) { /* Find the zone in the list */ struct vm_zone *z; - z = zones; - while (z) { + for (z = zones; z; z = z->next) { if (!strcmp(z->name, vmu->zonetag)) { the_zone = z; break; } - z = z->next; } } diff --git a/channels/chan_iax2.c b/channels/chan_iax2.c index b54f17d21..094a6ab63 100644 --- a/channels/chan_iax2.c +++ b/channels/chan_iax2.c @@ -2562,7 +2562,7 @@ static struct iax2_peer *realtime_peer(const char *peername, struct sockaddr_in struct ast_variable *var; struct ast_variable *tmp; struct iax2_peer *peer=NULL; - time_t regseconds, nowtime; + time_t regseconds = 0, nowtime; int dynamic=0; if (peername) @@ -2603,8 +2603,7 @@ static struct iax2_peer *realtime_peer(const char *peername, struct sockaddr_in break; } } else if (!strcasecmp(tmp->name, "regseconds")) { - if (sscanf(tmp->value, "%ld", (time_t *)®seconds) != 1) - regseconds = 0; + ast_get_time_t(tmp->value, ®seconds, 0); } else if (!strcasecmp(tmp->name, "ipaddr")) { inet_aton(tmp->value, &(peer->addr.sin_addr)); } else if (!strcasecmp(tmp->name, "port")) { diff --git a/channels/chan_sip.c b/channels/chan_sip.c index 468514a96..db3d3f1c1 100644 --- a/channels/chan_sip.c +++ b/channels/chan_sip.c @@ -12152,15 +12152,12 @@ static struct sip_peer *build_peer(const char *name, struct ast_variable *v, int ast_variables_destroy(peer->chanvars); peer->chanvars = NULL; } - while(v) { - if (handle_common_options(&peerflags, &mask, v)) { - v = v->next; + for (; v; v = v->next) { + if (handle_common_options(&peerflags, &mask, v)) continue; - } if (realtime && !strcasecmp(v->name, "regseconds")) { - if (sscanf(v->value, "%ld", (time_t *)®seconds) != 1) - regseconds = 0; + ast_get_time_t(v->value, ®seconds, 0); } else if (realtime && !strcasecmp(v->name, "ipaddr") && !ast_strlen_zero(v->value) ) { inet_aton(v->value, &(peer->addr.sin_addr)); } else if (realtime && !strcasecmp(v->name, "name")) @@ -12309,10 +12306,6 @@ static struct sip_peer *build_peer(const char *name, struct ast_variable *v, int peer->maxms = 0; } } - /* else if (strcasecmp(v->name,"type")) - * ast_log(LOG_WARNING, "Ignoring %s\n", v->name); - */ - v=v->next; } if (!ast_test_flag((&global_flags_page2), SIP_PAGE2_IGNOREREGEXPIRE) && ast_test_flag((&peer->flags_page2), SIP_PAGE2_DYNAMIC) && realtime) { time_t nowtime; diff --git a/funcs/func_strings.c b/funcs/func_strings.c index 5dafb1f33..4d992b1b5 100644 --- a/funcs/func_strings.c +++ b/funcs/func_strings.c @@ -232,8 +232,8 @@ static int acf_strftime(struct ast_channel *chan, char *cmd, char *parse, AST_APP_ARG(timezone); AST_APP_ARG(format); ); - long epochi; - struct tm time; + time_t epochi; + struct tm tm; buf[0] = '\0'; @@ -245,14 +245,13 @@ static int acf_strftime(struct ast_channel *chan, char *cmd, char *parse, AST_STANDARD_APP_ARGS(args, parse); - if (ast_strlen_zero(args.epoch) || !sscanf(args.epoch, "%ld", &epochi)) { - struct timeval tv = ast_tvnow(); - epochi = tv.tv_sec; - } + ast_get_time_t(args.epoch, &epochi, time(NULL)); + ast_localtime(&epochi, &tm, args.timezone); - ast_localtime(&epochi, &time, args.timezone); + if (!args.format) + args.format = "%c"; - if (!strftime(buf, len, args.format ? args.format : "%c", &time)) + if (!strftime(buf, len, args.format, &tm)) ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n"); buf[len - 1] = '\0'; diff --git a/include/asterisk/strings.h b/include/asterisk/strings.h index 5399c32da..a48520ad6 100644 --- a/include/asterisk/strings.h +++ b/include/asterisk/strings.h @@ -1,7 +1,7 @@ /* * Asterisk -- An open source telephony toolkit. * - * Copyright (C) 1999 - 2005, Digium, Inc. + * Copyright (C) 1999 - 2006, Digium, Inc. * * Mark Spencer <markster@digium.com> * @@ -223,6 +223,15 @@ int ast_false(const char *val); */ void ast_join(char *s, size_t len, char * const w[]); +/* + \brief Parse a time (integer) string. + \param src String to parse + \param dst Destination + \param _default Value to use if the string does not contain a valid time + \return zero on success, non-zero on failure +*/ +int ast_get_time_t(const char *src, time_t *dst, time_t _default); + /* The realloca lets us ast_restrdupa(), but you can't mix any other ast_strdup calls! */ struct ast_realloca { diff --git a/res/res_agi.c b/res/res_agi.c index 85436a672..ef7576fcd 100644 --- a/res/res_agi.c +++ b/res/res_agi.c @@ -742,16 +742,13 @@ static int handle_saytime(struct ast_channel *chan, AGI *agi, int argc, char *ar if (res == 1) return RESULT_SUCCESS; fdprintf(agi->fd, "200 result=%d\n", res); - if (res >= 0) - return RESULT_SUCCESS; - else - return RESULT_FAILURE; + return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; } static int handle_saydatetime(struct ast_channel *chan, AGI *agi, int argc, char *argv[]) { int res=0; - long unixtime; + time_t unixtime; char *format, *zone=NULL; if (argc < 4) @@ -770,19 +767,15 @@ static int handle_saydatetime(struct ast_channel *chan, AGI *agi, int argc, char if (argc > 5 && !ast_strlen_zero(argv[5])) zone = argv[5]; - if (sscanf(argv[2], "%ld", &unixtime) != 1) + if (ast_get_time_t(argv[2], &unixtime, 0)) return RESULT_SHOWUSAGE; - res = ast_say_date_with_format(chan, (time_t) unixtime, argv[3], chan->language, format, zone); + res = ast_say_date_with_format(chan, unixtime, argv[3], chan->language, format, zone); if (res == 1) return RESULT_SUCCESS; fdprintf(agi->fd, "200 result=%d\n", res); - - if (res >= 0) - return RESULT_SUCCESS; - else - return RESULT_FAILURE; + return (res >= 0) ? RESULT_SUCCESS : RESULT_FAILURE; } static int handle_sayphonetic(struct ast_channel *chan, AGI *agi, int argc, char *argv[]) @@ -1046,3 +1046,27 @@ void __ast_string_field_index_build(struct ast_string_field_mgr *mgr, va_end(ap2); } + + +/* + * get values from config variables. + */ +int ast_get_time_t(const char *src, time_t *dst, time_t _default) +{ + long t; + + if (dst == NULL) + return -1; + + *dst = _default; + + if (ast_strlen_zero(src)) + return -1; + + /* only integer at the moment, but one day we could accept more formats */ + if (sscanf(src, "%ld", &t) == 1) { + *dst = t; + return 0; + } else + return -1; +} |