-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicFPCTypes4Delphi.pas
283 lines (241 loc) · 8.11 KB
/
BasicFPCTypes4Delphi.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
Unit BasicFPCTypes4Delphi;
Interface
{$IFDEF MSWINDOWS}
Uses Windows;
{$ENDIF}
{$IF CompilerVersion > 20 }
{$LEGACYIFEND OFF}
{$DEFINE HASINLINE}
{$IFEND}
type QWord = {$IF CompilerVersion > 20 } uint64 {$ELSE} int64 {$IFEND};
PQWord = ^QWord;
ValReal = Extended;
DWord = {$IF CompilerVersion > 20 } uint32 {$ELSE} LongWord {$IFEND};
TWordArray = Array[0..(maxint - 1) div sizeOf(Word) - 1] of word;
PWordArray = ^TWordArray;
TLongWordArray = Array[0..(maxint - 1) div sizeOf(LongWord) - 1] of LongWord;
PLongWordArray = ^TLongWordArray;
{$IF CompilerVersion <= 20 }
SizeInt = Longint;
{$ELSE}
{$IFDEF CPU32BITS}
SizeInt = LongInt;
{$ENDIF}
{$IFDEF CPU64BITS}
SizeInt = Int64;
{$ENDIF}
{$IFEND}
const
FPC_FULLVERSION = 30399; // 30400 mit hat sich scheinbar einiges geändert
{$IFDEF MSWINDOWS}
AllFilesMask = '*.*';
{$ELSE}
AllFilesMask = '*';
{$ENDIF}
{$IFDEF MSWINDOWS}
MaxPathLen = windows.MAX_PATH;
{$ENDIF}
procedure FillWord(var data; wordCount: integer; fillValue: word);
function SetDirSeparators(const aPath: string): string;
{$IF CompilerVersion > 20}
function GetLocalTimeOffset: integer;
{$IFEND}
function RPos(aCharToFind: char; const aString: string): integer;
function SwapEndian(const AValue: SmallInt): SmallInt; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
function SwapEndian(const AValue: Word): Word; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
function SwapEndian(const AValue: LongInt): LongInt; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
function SwapEndian(const AValue: DWord): DWord; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
function SwapEndian(const AValue: Int64): Int64; overload;
{$IF CompilerVersion > 20}
function SwapEndian(const AValue: QWord): QWord; overload;
{$IFEND}
function BEtoN(const AValue: SmallInt): SmallInt; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
function BEtoN(const AValue: Word): Word; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
function BEtoN(const AValue: LongInt): LongInt; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
function BEtoN(const AValue: DWord): DWord; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
function BEtoN(const AValue: Int64): Int64; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
{$IF CompilerVersion > 20}
function BEtoN(const AValue: QWord): QWord; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
{$IFEND}
function NtoBE(const AValue: SmallInt): SmallInt; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
function NtoBE(const AValue: Word): Word; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
function NtoBE(const AValue: LongInt): LongInt; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
function NtoBE(const AValue: DWord): DWord; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
function NtoBE(const AValue: Int64): Int64; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
{$IF CompilerVersion > 20}
function NtoBE(const AValue: QWord): QWord; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
{$IFEND}
Implementation
uses DateUtils, SysUtils;
procedure FillWord(var data; wordCount: integer; fillValue: word);
var wordArray: array [0..0] of word absolute data;
i: integer;
begin
{$R-}
for i:= 0 to wordCount do
wordArray[i]:= fillValue;
end;
function SetDirSeparators(const aPath: string): string;
begin
result:= aPath; // Todo: check implementation
end;
{$IF CompilerVersion > 20}
function GetLocalTimeOffset: integer;
begin
result:= round(TTImeZone.Local.UtcOffset.TotalMinutes);
end;
{$IFEND}
function RPos(aCharToFind: char; const aString: string): integer;
begin
result:= LastDelimiter(aCharToFind, aString);
end;
function SwapEndian(const AValue: SmallInt): SmallInt; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
begin
{ the extra Word type cast is necessary because the "AValue shr 8" }
{ is turned into "longint(AValue) shr 8", so if AValue < 0 then }
{ the sign bits from the upper 16 bits are shifted in rather than }
{ zeroes. }
Result := SmallInt(((Word(AValue) shr 8) or (Word(AValue) shl 8)) and $ffff);
end;
function SwapEndian(const AValue: Word): Word; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
begin
Result := ((AValue shr 8) or (AValue shl 8)) and $ffff;
end;
function SwapEndian(const AValue: LongInt): LongInt; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
begin
Result := ((AValue shl 8) and $FF00FF00) or ((AValue shr 8) and $00FF00FF);
Result := (Result shl 16) or (Result shr 16);
end;
function SwapEndian(const AValue: DWord): DWord; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
begin
Result := ((AValue shl 8) and $FF00FF00) or ((AValue shr 8) and $00FF00FF);
Result := (Result shl 16) or (Result shr 16);
end;
function SwapEndian(const AValue: Int64): Int64; overload;
begin
Result := ((AValue shl 8) and $FF00FF00FF00FF00) or
((AValue shr 8) and $00FF00FF00FF00FF);
Result := ((Result shl 16) and $FFFF0000FFFF0000) or
((Result shr 16) and $0000FFFF0000FFFF);
Result := (Result shl 32) or ((Result shr 32));
end;
{$IF CompilerVersion > 20}
function SwapEndian(const AValue: QWord): QWord; overload;
begin
Result := ((AValue shl 8) and $FF00FF00FF00FF00) or
((AValue shr 8) and $00FF00FF00FF00FF);
Result := ((Result shl 16) and $FFFF0000FFFF0000) or
((Result shr 16) and $0000FFFF0000FFFF);
Result := (Result shl 32) or ((Result shr 32));
end;
{$IFEND}
{$IFDEF CPUARM}
{$undef ENDIAN_BIG}
{$ENDIF}
{$IFDEF CPUX86}
{$undef ENDIAN_BIG}
{$ENDIF}
{$IFDEF CPUX64}
{$undef ENDIAN_BIG}
{$ENDIF}
function BEtoN(const AValue: SmallInt): SmallInt; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
begin
{$IFDEF ENDIAN_BIG}
Result := AValue;
{$ELSE}
Result := SwapEndian(AValue);
{$ENDIF}
end;
function BEtoN(const AValue: Word): Word; {$IFDEF HASINLINE} inline; {$ENDIF}
begin
{$IFDEF ENDIAN_BIG}
Result := AValue;
{$ELSE}
Result := SwapEndian(AValue);
{$ENDIF}
end;
function BEtoN(const AValue: LongInt): LongInt; {$IFDEF HASINLINE} inline; {$ENDIF}
begin
{$IFDEF ENDIAN_BIG}
Result := AValue;
{$ELSE}
Result := SwapEndian(AValue);
{$ENDIF}
end;
function BEtoN(const AValue: DWord): DWord; {$IFDEF HASINLINE} inline; {$ENDIF}
begin
{$IFDEF ENDIAN_BIG}
Result := AValue;
{$ELSE}
Result := SwapEndian(AValue);
{$ENDIF}
end;
function BEtoN(const AValue: Int64): Int64; {$IFDEF HASINLINE} inline; {$ENDIF}
begin
{$IFDEF ENDIAN_BIG}
Result := AValue;
{$ELSE}
Result := SwapEndian(AValue);
{$ENDIF}
end;
{$IF CompilerVersion > 20}
function BEtoN(const AValue: QWord): QWord; inline;
begin
{$IFDEF ENDIAN_BIG}
Result := AValue;
{$ELSE}
Result := SwapEndian(AValue);
{$ENDIF}
end;
{$IFEND}
function NtoBE(const AValue: SmallInt): SmallInt; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
begin
{$IFDEF ENDIAN_BIG}
Result := AValue;
{$ELSE}
Result := SwapEndian(AValue);
{$ENDIF}
end;
function NtoBE(const AValue: Word): Word; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
begin
{$IFDEF ENDIAN_BIG}
Result := AValue;
{$ELSE}
Result := SwapEndian(AValue);
{$ENDIF}
end;
function NtoBE(const AValue: LongInt): LongInt; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
begin
{$IFDEF ENDIAN_BIG}
Result := AValue;
{$ELSE}
Result := SwapEndian(AValue);
{$ENDIF}
end;
function NtoBE(const AValue: DWord): DWord; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
begin
{$IFDEF ENDIAN_BIG}
Result := AValue;
{$ELSE}
Result := SwapEndian(AValue);
{$ENDIF}
end;
function NtoBE(const AValue: Int64): Int64; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
begin
{$IFDEF ENDIAN_BIG}
Result := AValue;
{$ELSE}
Result := SwapEndian(AValue);
{$ENDIF}
end;
{$IF CompilerVersion > 20}
function NtoBE(const AValue: QWord): QWord; {$IFDEF HASINLINE} inline; {$ENDIF} overload;
begin
{$IFDEF ENDIAN_BIG}
Result := AValue;
{$ELSE}
Result := SwapEndian(AValue);
{$ENDIF}
end;
{$IFEND}
end.