Version 0.1.10 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, char *ints, char *lang)
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, lang);
28                 if (!res) 
29                         res = ast_waitstream(chan, ints);
30                 ast_stopstream(chan);
31                 num++;
32         }
33         return res;
34 }
35
36 int ast_say_digits(struct ast_channel *chan, int num, char *ints, char *lang)
37 {
38         char fn2[256];
39         snprintf(fn2, sizeof(fn2), "%d", num);
40         return ast_say_digit_str(chan, fn2, ints, lang);
41 }
42 int ast_say_number(struct ast_channel *chan, int num, char *ints, char *language)
43 {
44         int res = 0;
45         int playh = 0;
46         char fn[256] = "";
47         if (!num) 
48                 return ast_say_digits(chan, 0,ints, language);
49         if (0) {
50         /* XXX Only works for english XXX */
51         } else {
52                 /* Use english numbers */
53                 language = "en";
54                 while(num && !res) {
55                         if (playh) {
56                                 snprintf(fn, sizeof(fn), "digits/hundred");
57                                 playh = 0;
58                         } else
59                         if (num < 20) {
60                                 snprintf(fn, sizeof(fn), "digits/%d", num);
61                                 num = 0;
62                         } else
63                         if (num < 100) {
64                                 snprintf(fn, sizeof(fn), "digits/%d", (num /10) * 10);
65                                 num -= ((num / 10) * 10);
66                         } else {
67                                 if (num < 1000){
68                                         snprintf(fn, sizeof(fn), "digits/%d", (num/100));
69                                         playh++;
70                                         num -= ((num / 100) * 100);
71                                 } else {
72                                         if (num < 1000000) {
73                                                 res = ast_say_number(chan, num / 1000, ints, language);
74                                                 if (res)
75                                                         return res;
76                                                 num = num % 1000;
77                                                 snprintf(fn, sizeof(fn), "digits/thousand");
78                                         } else {
79                                                 if (num < 1000000000) {
80                                                         res = ast_say_number(chan, num / 1000000, ints, language);
81                                                         if (res)
82                                                                 return res;
83                                                         num = num % 1000000;
84                                                         snprintf(fn, sizeof(fn), "digits/million");
85                                                 } else {
86                                                         ast_log(LOG_DEBUG, "Number '%d' is too big for me\n", num);
87                                                         res = -1;
88                                                 }
89                                         }
90                                 }
91                         }
92                         if (!res) {
93                                 res = ast_streamfile(chan, fn, language);
94                                 if (!res) 
95                                         res = ast_waitstream(chan, ints);
96                                 ast_stopstream(chan);
97                         }
98                         
99                 }
100         }
101         return res;
102 }