Version 0.1.3 from FTP
[asterisk/asterisk.git] / say.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Say numbers and dates (maybe words one day too)
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <asterisk/file.h>
15 #include <asterisk/channel.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/say.h>
18 #include <stdio.h>
19
20 int ast_say_digit_str(struct ast_channel *chan, char *fn2)
21 {
22         char fn[256] = "";
23         int num = 0;
24         int res = 0;
25         while(fn2[num] && !res) {
26                 snprintf(fn, sizeof(fn), "digits/%c", fn2[num]);
27                 res = ast_streamfile(chan, fn);
28                 if (!res) 
29                         res = ast_waitstream(chan, AST_DIGIT_ANY);
30                 ast_stopstream(chan);
31                 num++;
32         }
33         return res;
34 }
35
36 int ast_say_digits(struct ast_channel *chan, int num)
37 {
38         char fn2[256];
39         snprintf(fn2, sizeof(fn2), "%d", num);
40         return ast_say_digit_str(chan, fn2);
41 }
42 int ast_say_number(struct ast_channel *chan, int num)
43 {
44         int res = 0;
45         int playh = 0;
46         char fn[256] = "";
47         while(num && !res) {
48                 if (playh) {
49                         snprintf(fn, sizeof(fn), "digits/hundred");
50                         playh = 0;
51                 } else
52                 if (num < 20) {
53                         snprintf(fn, sizeof(fn), "digits/%d", num);
54                         num = 0;
55                 } else
56                 if (num < 100) {
57                         snprintf(fn, sizeof(fn), "digits/%d", (num /10) * 10);
58                         num -= ((num / 10) * 10);
59                 } else {
60                         if (num < 1000){
61                                 snprintf(fn, sizeof(fn), "digits/%d", (num/100));
62                                 playh++;
63                         } else {
64                                 ast_log(LOG_DEBUG, "Number '%d' is too big for me\n", num);
65                                 res = -1;
66                         }
67                 }
68                 if (!res) {
69                         res = ast_streamfile(chan, fn);
70                         if (!res) 
71                                 res = ast_waitstream(chan, AST_DIGIT_ANY);
72                         ast_stopstream(chan);
73                 }
74                 
75         }
76         return res;
77 }