aboutsummaryrefslogtreecommitdiffstats
path: root/main
diff options
context:
space:
mode:
authortilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b>2009-10-15 22:33:30 +0000
committertilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b>2009-10-15 22:33:30 +0000
commit0c997b3fd177c4da568347d1dd6056e88cec08c5 (patch)
treea02c02a5a661dba55796ba78a3c160128a2cf51b /main
parent3531d342bf3c976a443c3696088880419d2e05c5 (diff)
Create an API for adding an optional time unit onto the ends of time periods.
Two examples of its use are included, and the usage could be expanded in some cases into certain configuration options where time periods are specified. git-svn-id: http://svn.digium.com/svn/asterisk/trunk@224225 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'main')
-rw-r--r--main/app.c56
-rw-r--r--main/pbx.c14
2 files changed, 62 insertions, 8 deletions
diff --git a/main/app.c b/main/app.c
index 99dcfa677..580e91b76 100644
--- a/main/app.c
+++ b/main/app.c
@@ -2082,3 +2082,59 @@ void ast_safe_fork_cleanup(void)
ast_unreplace_sigchld();
}
+int ast_app_parse_timelen(const char *timestr, int *result, ast_timelen unit)
+{
+ int res;
+ char u[10];
+#ifdef HAVE_LONG_DOUBLE_WIDER
+ long double amount;
+ #define FMT "%30Lf%9s"
+#else
+ double amount;
+ #define FMT "%30lf%9s"
+#endif
+ if (!timestr) {
+ return -1;
+ }
+
+ if ((res = sscanf(timestr, FMT, &amount, u)) == 0) {
+#undef FMT
+ return -1;
+ } else if (res == 2) {
+ switch (u[0]) {
+ case 'h':
+ case 'H':
+ unit = TIMELEN_HOURS;
+ break;
+ case 's':
+ case 'S':
+ unit = TIMELEN_SECONDS;
+ break;
+ case 'm':
+ case 'M':
+ if (toupper(u[1]) == 'S') {
+ unit = TIMELEN_MILLISECONDS;
+ } else if (u[1] == '\0') {
+ unit = TIMELEN_MINUTES;
+ }
+ break;
+ }
+ }
+
+ switch (unit) {
+ case TIMELEN_HOURS:
+ amount *= 60;
+ /* fall-through */
+ case TIMELEN_MINUTES:
+ amount *= 60;
+ /* fall-through */
+ case TIMELEN_SECONDS:
+ amount *= 1000;
+ /* fall-through */
+ case TIMELEN_MILLISECONDS:
+ ;
+ }
+ *result = amount > INT_MAX ? INT_MAX : (int) amount;
+ return 0;
+}
+
diff --git a/main/pbx.c b/main/pbx.c
index dd2016ed2..58e689e54 100644
--- a/main/pbx.c
+++ b/main/pbx.c
@@ -9001,12 +9001,10 @@ static int pbx_builtin_execiftime(struct ast_channel *chan, const char *data)
*/
static int pbx_builtin_wait(struct ast_channel *chan, const char *data)
{
- double s;
int ms;
/* Wait for "n" seconds */
- if (data && (s = atof(data)) > 0.0) {
- ms = s * 1000.0;
+ if (!ast_app_parse_timelen(data, &ms, TIMELEN_SECONDS) && ms > 0) {
return ast_safe_sleep(chan, ms);
}
return 0;
@@ -9018,7 +9016,6 @@ static int pbx_builtin_wait(struct ast_channel *chan, const char *data)
static int pbx_builtin_waitexten(struct ast_channel *chan, const char *data)
{
int ms, res;
- double s;
struct ast_flags flags = {0};
char *opts[1] = { NULL };
char *parse;
@@ -9050,12 +9047,13 @@ static int pbx_builtin_waitexten(struct ast_channel *chan, const char *data)
}
}
/* Wait for "n" seconds */
- if (args.timeout && (s = atof(args.timeout)) > 0)
- ms = s * 1000.0;
- else if (chan->pbx)
+ if (!ast_app_parse_timelen(args.timeout, &ms, TIMELEN_SECONDS) && ms > 0) {
+ /* Yay! */
+ } else if (chan->pbx) {
ms = chan->pbx->rtimeoutms;
- else
+ } else {
ms = 10000;
+ }
res = ast_waitfordigit(chan, ms);
if (!res) {