-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPhoneNumbers.pas
More file actions
135 lines (113 loc) · 3.16 KB
/
PhoneNumbers.pas
File metadata and controls
135 lines (113 loc) · 3.16 KB
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
unit PhoneNumbers;
interface
uses System.Classes,System.SysUtils,System.StrUtils
,WinApi.Windows,Vcl.Dialogs,System.UITypes;
type
TLibPhoneNumber = class
private type
TParseFunction = function (phonenumber : WideString; country : WideString; out formatetNumber : WideString) : Boolean; stdcall;
TIsValidNumberFunction = function (phonenumber : WideString; country : WideString) : Boolean; stdcall;
private
dll : HMODULE;
parseFunction : TParseFunction;
isValidNumberFunction : TIsValidNumberFunction;
private const
DLLNAME = 'PhoneNumbersUnmanaged.dll';
public
constructor Create;
destructor Destroy; override;
class function Parse(const phonenumber, country : String; E164notation : Boolean = true) : String;
class function IsValidNumber(const phonenumber, country : String) : Boolean;
end;
var
PhoneNumbersDllOverrideDir: String = '';
implementation
uses
system.ioUtils;
var
instance : TLibPhoneNumber;
{ TLibPhoneNumber }
constructor TLibPhoneNumber.Create;
var
lDllFn, lDllDir: String;
begin
dll := 0;
parseFunction := nil;
if PhoneNumbersDllOverrideDir = '' then
lDllDir:= ExtractFilePath(ParamStr(0))
else
lDllDir:= PhoneNumbersDllOverrideDir;
lDllFn:= TPath.Combine(lDllDir, DLLNAME);
if not FileExists(lDllFn) then
begin
MessageDlg(Format('"%s" not found.',[DLLNAME])+#10+ExtractFilePath(ParamStr(0)), mtError, [mbOK], 0);
exit;
end;
dll := LoadLibrary(PChar(lDllFn));
if dll <> 0 then
begin
parseFunction := GetProcAddress(dll, 'parse');
isValidNumberFunction := GetProcAddress(dll, 'isValidNumber');
end else
MessageDlg(Format('error loading "%s".',[DLLNAME])+#10+ExtractFilePath(ParamStr(0)), mtError, [mbOK], 0);
end;
destructor TLibPhoneNumber.Destroy;
begin
if dll <> 0 then
FreeLibrary(dll);
dll := 0;
inherited;
end;
class function TLibPhoneNumber.IsValidNumber(const phonenumber,
country: String): Boolean;
begin
Result := false;
if instance = nil then
instance := TLibPhoneNumber.Create;
if not Assigned(instance.isValidNumberFunction) then
exit;
Result := instance.isValidNumberFunction(phoneNumber,country);
end;
class function TLibPhoneNumber.Parse(const phonenumber,
country: String; E164notation : Boolean = true): String;
var
hstr : WideString;
splitted: TArray<String>;
i : Integer;
begin
Result := '';
if instance = nil then
instance := TLibPhoneNumber.Create;
if not Assigned(instance.parseFunction) then
begin
Result := phonenumber;
exit;
end;
if instance.parseFunction(PhoneNumber,country,hstr) then
Result := hstr;
if Result = '' then
exit;
if E164notation then
begin
Result := ReplaceText(Result,' ','');
exit;
end;
splitted := Result.Split([' ']);
if Length(splitted) < 2 then
exit;
for i := Low(splitted) to high(splitted) do
begin
if i = Low(splitted) then
Result := splitted[i]
else
if i = Low(splitted)+1 then
Result := Result + ' ('+splitted[i]+')'
else
Result := Result + ' '+splitted[i];
end;
end;
initialization
instance := nil;
finalization
if Assigned(instance) then begin instance.Free; instance := nil; end;
end.