-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseful.pas
185 lines (168 loc) · 4.33 KB
/
useful.pas
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
unit useful;
{$mode objfpc}{$H+}
interface
uses
SysUtils, LCLIntf, LCLType, Classes, Graphics, Controls,
Forms, Dialogs, Menus, StdCtrls, ExtCtrls, DB;
procedure DegToDMS(x: double; var Degrees, Minutes, Seconds: integer);
procedure NaturalSort(aList: TStrings);
procedure ParseName(const taxonomic_name: string; var genus: string;
var nomen: string; var species: string; var author1: string;
var ssp: string; var infraname: string; var author2: string);
function Extenso(Data: string): string;
function ExtractText(const Str: string; const Delim1, Delim2: char): string;
function GetFileNameWithoutExt(Filenametouse: string): string;
function GetUnit(const s: string): string;
function IsOnline(reliableserver: string = 'http://www.google.com'): boolean;
function Roman(Data: string): string;
implementation
uses StrUtils, fphttpclient, naturalsortunit;
const
mesext: array[1..12] of string =
('I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X', 'XI', 'XII');
procedure DegToDMS(x: double; var Degrees, Minutes, Seconds: integer);
begin
Degrees := Trunc(x);
x := (x - Degrees) * 60;
Minutes := Trunc(x);
x := (x - Minutes) * 60;
Seconds := Round(x);
end;
procedure NaturalSort(aList: TStrings);
var
L: TStringList;
begin
L := TStringList.Create;
try
L.Assign(aList);
L.CustomSort(@UTF8NaturalCompareList);
aList.Assign(L);
finally
L.Free;
end;
end;
procedure ParseName(const taxonomic_name: string; var genus: string;
var nomen: string; var species: string; var author1: string;
var ssp: string; var infraname: string; var author2: string);
var
taxonomy: TStringList;
begin
if taxonomic_name = '' then
Exit;
genus := '';
nomen := '';
species := '';
author1 := '';
ssp := '';
infraname := '';
author2 := '';
taxonomy := TStringList.Create;
taxonomy.Delimiter := ' ';
taxonomy.StrictDelimiter := True;
taxonomy.DelimitedText := taxonomic_name;
genus := taxonomy[0];
taxonomy.Delete(0);
if taxonomy.Count > 0 then
begin
if taxonomy[0].StartsWith('cf.') or taxonomy[0].StartsWith('aff.') then
begin
nomen := taxonomy[0];
taxonomy.Delete(0);
end;
end;
if taxonomy.Count > 0 then
begin
species := taxonomy[0];
taxonomy.Delete(0);
end;
if taxonomy.Count > 0 then
begin
author1 := taxonomy[0];
taxonomy.Delete(0);
end;
if taxonomy.Count > 0 then
begin
if taxonomy[0].StartsWith('var.') or taxonomy[0].StartsWith('subsp.') then
ssp := taxonomy[0];
taxonomy.Delete(0);
end;
if taxonomy.Count > 0 then
begin
infraname := taxonomy[0];
taxonomy.Delete(0);
end;
if taxonomy.Count > 0 then
begin
author2 := taxonomy[0];
taxonomy.Delete(0);
end;
taxonomy.Free;
end;
function Extenso(Data: string): string;
var
dia, mes, ano: string;
begin
if Data = '' then
Result := '';
dia := Data.Split('-')[0];
mes := Data.Split('-')[1];
ano := Data.Split('-')[1];
Result := dia + ' de ' + mesext[StrToIntDef(mes, 1)] + ' de ' + ano;
end;
function ExtractText(const Str: string; const Delim1, Delim2: char): string;
var
pos1, pos2: integer;
begin
Result := '';
pos1 := Pos(Delim1, Str);
pos2 := Pos(Delim2, Str);
if (pos1 > 0) and (pos2 > pos1) then
Result := Copy(Str, pos1 + 1, pos2 - pos1 - 1);
end;
function GetFileNameWithoutExt(Filenametouse: string): string;
begin
Result := ExtractFilename(Copy(Filenametouse, 1,
RPos(ExtractFileExt(Filenametouse), Filenametouse) - 1));
end;
function GetUnit(const s: string): string;
begin
if (Pos('(', s) > 0) and (Pos(')', s) > 0) then
Result := ExtractText(s, '(', ')')
else
Result := '';
end;
function IsOnline(reliableserver: string = 'http://www.google.com'): boolean;
var
http: tfphttpclient;
httpstate: integer;
begin
Result := False;
try
http := tfphttpclient.Create(nil);
try
http.Get(reliableserver);
httpstate := http.ResponseStatusCode;
if httpstate = 200 then
Result := True
else
Result := False;
except
on E: Exception do
Result := False;
end;
finally
http.Free;
end;
end;
function Roman(Data: string): string;
var
dia, mes, ano: string;
begin
if Data = '' then
Result := '';
dia := Data.Split('-')[0];
mes := Data.Split('-')[1];
ano := Data.Split('-')[1];
Result := dia + '-' + mesext[StrToIntDef(mes, 1)] + '-' + ano;
end;
end.