[asterisk-commits] trunk r10105 - in /trunk: ./ apps/ channels/
funcs/ include/asterisk/ res/
asterisk-commits at lists.digium.com
asterisk-commits at lists.digium.com
Tue Feb 14 17:24:25 MST 2006
Author: kpfleming
Date: Tue Feb 14 18:24:24 2006
New Revision: 10105
URL: http://svn.digium.com/view/asterisk?rev=10105&view=rev
Log:
add API function for parsing strings to time_t (issue #6320, with mods)
Modified:
trunk/apps/app_sayunixtime.c
trunk/apps/app_voicemail.c
trunk/channels/chan_iax2.c
trunk/channels/chan_sip.c
trunk/funcs/func_strings.c
trunk/include/asterisk/strings.h
trunk/res/res_agi.c
trunk/utils.c
Modified: trunk/apps/app_sayunixtime.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_sayunixtime.c?rev=10105&r1=10104&r2=10105&view=diff
==============================================================================
--- trunk/apps/app_sayunixtime.c (original)
+++ trunk/apps/app_sayunixtime.c Tue Feb 14 18:24:24 2006
@@ -76,45 +76,26 @@
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;
+ format = "c"; /* default datetime */
- 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";
- }
+ 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 (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;
- }
- }
- }
- }
-
- 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);
Modified: trunk/apps/app_voicemail.c
URL: http://svn.digium.com/view/asterisk/trunk/apps/app_voicemail.c?rev=10105&r1=10104&r2=10105&view=diff
==============================================================================
--- trunk/apps/app_voicemail.c (original)
+++ trunk/apps/app_voicemail.c Tue Feb 14 18:24:24 2006
@@ -3608,25 +3608,21 @@
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;
}
}
Modified: trunk/channels/chan_iax2.c
URL: http://svn.digium.com/view/asterisk/trunk/channels/chan_iax2.c?rev=10105&r1=10104&r2=10105&view=diff
==============================================================================
--- trunk/channels/chan_iax2.c (original)
+++ trunk/channels/chan_iax2.c Tue Feb 14 18:24:24 2006
@@ -2562,7 +2562,7 @@
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 @@
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")) {
Modified: trunk/channels/chan_sip.c
URL: http://svn.digium.com/view/asterisk/trunk/channels/chan_sip.c?rev=10105&r1=10104&r2=10105&view=diff
==============================================================================
--- trunk/channels/chan_sip.c (original)
+++ trunk/channels/chan_sip.c Tue Feb 14 18:24:24 2006
@@ -12152,15 +12152,12 @@
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 @@
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;
Modified: trunk/funcs/func_strings.c
URL: http://svn.digium.com/view/asterisk/trunk/funcs/func_strings.c?rev=10105&r1=10104&r2=10105&view=diff
==============================================================================
--- trunk/funcs/func_strings.c (original)
+++ trunk/funcs/func_strings.c Tue Feb 14 18:24:24 2006
@@ -232,8 +232,8 @@
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 @@
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_localtime(&epochi, &time, args.timezone);
-
- if (!strftime(buf, len, args.format ? args.format : "%c", &time))
+ ast_get_time_t(args.epoch, &epochi, time(NULL));
+ ast_localtime(&epochi, &tm, args.timezone);
+
+ if (!args.format)
+ args.format = "%c";
+
+ if (!strftime(buf, len, args.format, &tm))
ast_log(LOG_WARNING, "C function strftime() output nothing?!!\n");
buf[len - 1] = '\0';
Modified: trunk/include/asterisk/strings.h
URL: http://svn.digium.com/view/asterisk/trunk/include/asterisk/strings.h?rev=10105&r1=10104&r2=10105&view=diff
==============================================================================
--- trunk/include/asterisk/strings.h (original)
+++ trunk/include/asterisk/strings.h Tue Feb 14 18:24:24 2006
@@ -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 at digium.com>
*
@@ -223,6 +223,15 @@
*/
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 {
Modified: trunk/res/res_agi.c
URL: http://svn.digium.com/view/asterisk/trunk/res/res_agi.c?rev=10105&r1=10104&r2=10105&view=diff
==============================================================================
--- trunk/res/res_agi.c (original)
+++ trunk/res/res_agi.c Tue Feb 14 18:24:24 2006
@@ -742,16 +742,13 @@
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 @@
if (argc > 5 && !ast_strlen_zero(argv[5]))
zone = argv[5];
- if (sscanf(argv[2], "%ld", &unixtime) != 1)
- return RESULT_SHOWUSAGE;
-
- res = ast_say_date_with_format(chan, (time_t) unixtime, argv[3], chan->language, format, zone);
+ if (ast_get_time_t(argv[2], &unixtime, 0))
+ return RESULT_SHOWUSAGE;
+
+ 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[])
Modified: trunk/utils.c
URL: http://svn.digium.com/view/asterisk/trunk/utils.c?rev=10105&r1=10104&r2=10105&view=diff
==============================================================================
--- trunk/utils.c (original)
+++ trunk/utils.c Tue Feb 14 18:24:24 2006
@@ -1046,3 +1046,27 @@
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;
+}
More information about the asterisk-commits
mailing list