/
cphonenumber.cpp
200 lines (188 loc) · 5.7 KB
/
cphonenumber.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
*
* Copyright (C) 2017 Daniel-Constantin Mierla (asipto.com)
*
* This file is part of Kamailio, a free SIP server.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
*
* This file is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <phonenumbers/geocoding/phonenumber_offline_geocoder.h>
#include <phonenumbers/phonenumberutil.h>
#include <unicode/locid.h>
#include <string.h>
#include <cstring>
#include <string>
#include "cphonenumber.h"
using namespace i18n::phonenumbers;
using icu::Locale;
using std::string;
const PhoneNumberUtil &_phoneUtil(*PhoneNumberUtil::GetInstance());
static PhoneNumberOfflineGeocoder *_phoneGeoCoder =
new PhoneNumberOfflineGeocoder();
const char *telnum_linetype(PhoneNumberUtil::PhoneNumberType ltype)
{
switch(ltype) {
case PhoneNumberUtil::FIXED_LINE:
return "fixed-line";
case PhoneNumberUtil::MOBILE:
return "mobile";
// In some regions (e.g. the USA), it is impossible to distinguish between
// fixed-line and mobile numbers by looking at the phone number itself.
case PhoneNumberUtil::FIXED_LINE_OR_MOBILE:
return "fixed-line-or-mobile";
// Freephone lines
case PhoneNumberUtil::TOLL_FREE:
return "toll-free";
case PhoneNumberUtil::PREMIUM_RATE:
return "premium-rate";
// The cost of this call is shared between the caller and the recipient, and
// is hence typically less than PREMIUM_RATE calls. See
// http://en.wikipedia.org/wiki/Shared_Cost_Service for more information.
case PhoneNumberUtil::SHARED_COST:
return "shared-cost";
// Voice over IP numbers. This includes TSoIP (Telephony Service over IP).
case PhoneNumberUtil::VOIP:
return "voip";
// A personal number is associated with a particular person, and may be
// routed to either a MOBILE or FIXED_LINE number. Some more information can
// be found here: http://en.wikipedia.org/wiki/Personal_Numbers
case PhoneNumberUtil::PERSONAL_NUMBER:
return "personal-number";
case PhoneNumberUtil::PAGER:
return "pager";
// Used for "Universal Access Numbers" or "Company Numbers". They may be
// further routed to specific offices, but allow one number to be used for a
// company.
case PhoneNumberUtil::UAN:
return "uan";
// Used for "Voice Mail Access Numbers".
case PhoneNumberUtil::VOICEMAIL:
return "voicemail";
// A phone number is of type UNKNOWN when it does not fit any of the known
// patterns for a specific region.
case PhoneNumberUtil::UNKNOWN:
return "unknown";
}
return "unknown";
}
int telnum_possible(char *number, char *region)
{
string numStr(number);
string regionStr(region);
bool isPossible = _phoneUtil.IsPossibleNumberForString(numStr, regionStr);
return (isPossible ? 1 : 0);
}
char *telnum_cc(char *number)
{
string numStr(number);
string defaultRegion("ZZ");
PhoneNumber parsedNumber;
PhoneNumberUtil::ErrorType error =
_phoneUtil.Parse(numStr, defaultRegion, &parsedNumber);
if(error != PhoneNumberUtil::NO_PARSING_ERROR) {
return NULL;
}
string regionCode;
_phoneUtil.GetRegionCodeForNumber(parsedNumber, ®ionCode);
return strdup(regionCode.c_str());
}
telnum_t *telnum_parse(char *number, char *region)
{
string numStr(number);
string regionStr(region);
PhoneNumber parsedNumber;
PhoneNumberUtil::ErrorType error =
_phoneUtil.Parse(numStr, regionStr, &parsedNumber);
telnum_t *res = telnum_new(number);
if(res == NULL) {
return NULL;
}
if(error != PhoneNumberUtil::NO_PARSING_ERROR) {
string error = "Parsing number failed";
res->error = strdup(error.c_str());
return res;
}
if(!_phoneUtil.IsValidNumber(parsedNumber)) {
string error = "Invalid number";
res->error = strdup(error.c_str());
return res;
}
res->valid = 1;
string formattedNumberE164, formattedNumberNational;
_phoneUtil.Format(
parsedNumber, PhoneNumberUtil::E164, &formattedNumberE164);
_phoneUtil.Format(
parsedNumber, PhoneNumberUtil::NATIONAL, &formattedNumberNational);
res->normalized = strdup(formattedNumberE164.c_str());
res->natnum = strdup(formattedNumberNational.c_str());
string descNumber =
_phoneGeoCoder->GetDescriptionForNumber(parsedNumber, Locale("en"));
res->ndesc = strdup(descNumber.c_str());
res->ltype =
strdup(telnum_linetype(_phoneUtil.GetNumberType(parsedNumber)));
// res->cctel = _phoneUtil.GetCountryCodeForRegion(regionStr);
string regionCode;
_phoneUtil.GetRegionCodeForNumber(parsedNumber, ®ionCode);
res->cctel = _phoneUtil.GetCountryCodeForRegion(regionCode);
res->ccname = strdup(regionCode.c_str());
return res;
}
telnum_t *telnum_new(char *number)
{
telnum_t *tn = (telnum_t *)malloc(sizeof(telnum_t));
if(tn == NULL) {
return NULL;
}
tn->valid = 0;
tn->cctel = 0;
tn->number = strdup(number);
tn->normalized = NULL;
tn->ltype = NULL;
tn->ndesc = NULL;
tn->ccname = NULL;
tn->natnum = NULL;
tn->error = NULL;
return tn;
}
void telnum_free(telnum_t *tn)
{
if(tn == NULL) {
return;
}
if(tn->number) {
free(tn->number);
}
if(tn->normalized) {
free(tn->normalized);
}
if(tn->error) {
free(tn->error);
}
if(tn->ltype) {
free(tn->ltype);
}
if(tn->ndesc) {
free(tn->ndesc);
}
if(tn->ccname) {
free(tn->ccname);
}
if(tn->natnum) {
free(tn->natnum);
}
free(tn);
}