forked from baresip/baresip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdial_number.c
60 lines (55 loc) · 1018 Bytes
/
dial_number.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* @file dial_number.c Dialing numbers helpers
*
* Copyright (C) 2010 Alfred E. Heggestad
*/
#include <re.h>
#include <baresip.h>
#include "core.h"
#include <ctype.h>
int clean_number(char* str)
{
int i = 0, k = 0;
/* only clean numeric numbers
* In other cases trust the user input
*/
while (str[i]) {
if (isalpha(str[i]) != 0)
return -1;
else if (str[i] == '@')
return -1;
++i;
}
i = 0;
/* remove (0) which is in some mal-formated numbers
* but only if trailed by another character
*/
if (str[0] == '+' || (str[0] == '0' && str[1] == '0'))
while (str[i]) {
if (str[i] == '('
&& str[i + 1] == '0'
&& str[i + 2] == ')'
&& (str[i + 3] == ' '
|| (str[i + 3] >= '0'
&& str[i + 3] <= '9')
)
) {
str[i + 1] = ' ';
break;
}
++i;
}
i = 0;
while (str[i]) {
/* keep only '+' as first digit.
*/
if ((str[i] == '+' && k == 0)
|| isdigit(str[i]) > 0
)
str[k++] = str[i++];
else
++i;
}
str[k] = '\0';
return k;
}