-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneo.adb
291 lines (257 loc) · 12.5 KB
/
neo.adb
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
283
284
285
286
287
288
289
290
-- --
-- N E O E N G I N E --
-- --
-- Copyright (C) 202X Justin Squirek --
-- --
-- Neo 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 3 of the License, or (at your option) any later version. --
-- --
-- Neo 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. --
-- --
package body Neo is
---------------
-- Debugging --
---------------
Last_Error : Ptr_Last_Error := null;
procedure Set_Last_Error (Val : Ptr_Last_Error) is begin Last_Error := Val; end;
function Get_Last_Error return Int is (if Last_Error /= null then Last_Error.all else 0);
procedure Debug_Assert (Val : Int_Unsigned_C) is begin if Is_Debugging then Assert (Val /= 0); end if; end;
procedure Debug_Assert (Val : Int_16_Unsigned_C) is begin if Is_Debugging then Assert (Val /= 0); end if; end;
procedure Debug_Assert (Val : Int_C) is begin if Is_Debugging then Assert (Val /= 0); end if; end;
procedure Debug_Assert (Val : Int_Ptr) is begin if Is_Debugging then Assert (Val /= 0); end if; end;
procedure Debug_Assert (Val : Ptr) is begin if Is_Debugging then Assert (Val /= NULL_PTR); end if; end;
procedure Debug_Assert (Val : Bool) is begin if Is_Debugging then Assert (Val); end if; end;
procedure Assert (Val : Int_16_Unsigned_C) is begin Assert (Val /= 0); end;
procedure Assert (Val : Int_Unsigned_C) is begin Assert (Val /= 0); end;
procedure Assert (Val : Int_C) is begin Assert (Val /= 0); end;
procedure Assert (Val : Int_Ptr) is begin Assert (Val /= 0); end;
procedure Assert (Val : Ptr) is begin Assert (Val /= NULL_PTR); end;
procedure Assert (Val : Bool) is
begin
if not Val then
-- Get the call stack at the point of failure so we can return it through an exception message
declare
Traces : Tracebacks_Array (1..128);
Result : Str_8_Unbound := NULL_STR_8_UNBOUND;
Head_Index : Int := 0;
Chain_Length : Natural := 0;
Hit_At, In_At : Boolean := False;
begin
Call_Chain (Traces, Chain_Length);
-- Change line endings and ignore subprogram names due to dated exception message length restrictions - see RM 11.4.1(18)
for Item of Symbolic_Traceback (Traces) loop
if Item = ASCII.LF then
Append (Result, EOL_8);
Hit_At := False;
elsif Hit_At then Append (Result, Item);
elsif Item = ' ' then
Hit_At := In_At;
In_At := not In_At;
end if;
end loop;
-- Trim irrelevant bits
Tail (Result, Length (Result) - Index (Result, "neo.adb:"));
Tail (Result, Length (Result) - Index (Result, EOL_8) - 1);
Head_Index := Index (Result, "b__main");
if Head_Index = 0 then Head_Index := Index (Result, "at ???"); end if;
Head (Result, Head_Index - EOL_8'Length);
-- Propagate the exception with the trimmed stack trace as its message
raise Program_Error with "OS Error: " & Get_Last_Error'Image & EOL_8 & To_Str_8 (Result);
end;
end if;
end;
------------
-- Status --
------------
protected body Safe_Status is
procedure Occupied (Val : Bool) is begin Status := Val; end;
function Occupied return Bool is begin return Status; end;
end;
-------------
-- Counter --
-------------
protected body Safe_Counter is
function Get return Int is (Count);
procedure Set (Val : Int) is begin Count := Val; end;
procedure Increment (Amount : Int) is begin Count := Count + Amount; end;
procedure Decrement (Amount : Int) is begin Count := Count - Amount; end;
procedure Increment is begin Count := Count + 1; end;
procedure Decrement is begin Count := Count - 1; end;
end;
----------
-- Safe --
----------
package body Safe is
protected body T is
function Get return Safe_T is (Data);
procedure Set (Val : Safe_T) is begin Data := Val; end;
end;
end;
package body Safe_Discrete is
protected body T is
function Get return Safe_T is (Data);
procedure Set (Val : Safe_T) is begin Data := Val; end;
end;
end;
package body Safe_Digits is
protected body T is
function Get return Safe_T is (Data);
procedure Set (Val : Safe_T) is begin Data := Val; end;
end;
end;
------------
-- Timing --
------------
-- Fetch the time when the application was started
START_TIME : Time := Clock;
function Get_Start_Time return Time is (START_TIME);
-- Get the duration of the current timer
function Get_Duration (Timer : Timer_State) return Duration is (if Timer.Is_Stopped then Timer.Last else Clock - Timer.Start);
-- Start and start the timer and raise an error if it not being used properly
procedure Start (Timer : in out Timer_State) is
begin
pragma Assert (not Timer.Is_Stopped); Timer := (Is_Stopped => False, Start => Clock, others => <>);
end;
procedure Stop (Timer : in out Timer_State) is
begin
pragma Assert (Timer.Is_Stopped); Timer := (Is_Stopped => True, Last => Timer.Start - Clock, others => <>);
end;
-------------
-- Strings --
-------------
-- UTF routines
function To_UTF_16 (Item : Str_32) return Str_16 is ("");
function To_UTF_8 (Item : Str_32) return Str_8 is ("");
function UTF_To_Char_32 (Item : Str_8) return Char_32 is (NULL_CHAR_32);
-- Character conversions
function To_Char_8 (Item : Char_16) return Char_8 is (if Char_16'Pos (Item) > Char_8'Pos (Char_8'Last) then TOFU_8 else Char_8'Val (Char_16'Pos (Item)));
function To_Char_8 (Item : Char_32) return Char_8 is (if Char_32'Pos (Item) > Char_8'Pos (Char_8'Last) then TOFU_8 else Char_8'Val (Char_32'Pos (Item)));
function To_Char_16 (Item : Char_32) return Char_16 is (if Char_32'Pos (Item) > Char_16'Pos (Char_16'Last) then TOFU else Char_16'Val (Char_32'Pos (Item)));
-- String conversions
function To_Str_8_Path (Item : Str_16) return Str_8 is
Result : Str_8 (Item'First..Item'Last * 2);
for Result'Address use Item'Address;
begin
return To_Str_8 (Item);
end;
function To_Str_8 (Item : Str_32) return Str_8 is
Result : Str_8 (Item'Range);
begin
for I in Item'Range loop Result (I) := To_Char_8 (Item (I)); end loop;
return Result;
end;
function To_Str_16 (Item : Str_32) return Str_16 is
Result : Str_16 (Item'Range);
begin
for I in Item'Range loop Result (I) := To_Char_16 (Item (I)); end loop;
return Result;
end;
function To_Str_16_C (Item : Str_8_C) return Str_16_C is
Result : Str_16_C (Item'Range);
begin
for I in Item'Range loop Result (I) := To_Char_16_C (Item (I)); end loop;
return Result;
end;
function To_Str_32 (Item : Str_16) return Str_32 is
Result : Str_32 (Item'Range);
begin
for I in Item'Range loop Result (I) := To_Char_32 (Item (I)); end loop;
return Result;
end;
function To_Str_32 (Item : Str_8) return Str_32 is
Result : Str_32 (Item'Range);
begin
for I in Item'Range loop Result (I) := To_Char_32 (Item (I)); end loop;
return Result;
end;
function To_Str_16 (Item : Str_8) return Str is
Result : Str (Item'Range);
begin
for I in Item'Range loop Result (I) := To_Char_16 (Item (I)); end loop;
return Result;
end;
function To_Str_16 (Item : Str_16_C) return Str is
Last : Int_Size_C := Item'First;
Buffer : Str_Unbound := NULL_STR_UNBOUND;
begin
for I in Item'Range loop
exit when Item (I) = NULL_CHAR_16_C;
Buffer := Buffer & To_Char_16 (Item (Int_Size_C (I)));
end loop;
return S (Buffer);
end;
function To_Str_16 (Item : Ptr_Const_Char_16_C) return Str is
Length : Int := 0;
Buffer : Str_Unbound := NULL_STR_UNBOUND;
Temp : Ptr_Const_Char_16_C := Item;
begin
while Temp.all /= NULL_CHAR_16_C loop
Length := Length + 1;
Buffer := Buffer & Char_16 (Temp.all);
Temp := To_Ptr_Const_Char_16_C (To_Ptr (To_Int_Ptr (Temp) + Char_16_C'Size / Byte'Size));
end loop;
return S (Buffer);
end;
function To_Str_8 (Item : Ptr_Char_8_C) return Str_8 is
Length : Int := 0;
Buffer : Str_8_Unbound := NULL_STR_8_UNBOUND;
Temp : Ptr_Char_8_C := Item;
begin
while Temp.all /= NULL_CHAR_8_C loop
Length := Length + 1;
Buffer := Buffer & Char_8 (Temp.all);
Temp := To_Ptr_Char_8_C (To_Ptr (To_Int_Ptr (Temp) + Char_8_C'Size / Byte'Size));
end loop;
return To_Str_8 (Buffer);
end;
-- URL/web encoding
function Percent_Encode (Item : Str) return Str is
Result : Str_Unbound := NULL_STR_UNBOUND;
begin
for C of Item loop
Result := Result & U ((if C = ' ' then "+" elsif not Is_Letter (C) then "%" & Wide_Image (Char_16'Pos (C)) else C & ""));
end loop;
return S (Result);
end;
-- Integer to string with a changable base (e.g. decimal to binary or hex)
function Image_128 (Item : Int_128_Unsigned; Base : Int_Number_Base := 16) return Str_8 is ("");
-- Real to string
function Image_128 (Item : Real_128) return Str_8 is ("");
-- Integer to string with a changable base (e.g. decimal to binary or hex)
function Generic_To_Str_Int (Item : Num_T; Base : Positive; Do_Pad_Zeros : Bool := True) return Str is
package Num_T_Text_IO is new Ada_IO.Modular_IO (Num_T);
Buffer : Str_Unbound := NULL_STR_UNBOUND;
Input : Str (1..4096) := (others => NULL_CHAR);
begin
Num_T_Text_IO.Put (Input, Item, Ada_IO.Number_Base (Base));
if Base = 10 then return Trim (Input, Both); end if;
Buffer := To_Str_Unbound (Trim (Input, Both));
Delete (Buffer, 1, Trim (Base'Img, Both)'Length + 1);
Delete (Buffer, Length (Buffer), Length (Buffer));
if Item /= Num_T'Last and Do_Pad_Zeros then
for I in 1..Generic_To_Str_Int (Num_T'Last, Base)'Length - Length (Buffer) loop Insert (Buffer, 1, "0"); end loop;
end if;
return S (Buffer);
end;
-- Real to string
function Generic_To_Str_Real (Item : Num_T) return Str is
package Var_T_IO is new Ada.Text_IO.Float_IO (Num_T);
Result : Str_8 (1..Num_T'Digits * 2) := (others => ' ');
begin
-- Obtain Val's image and decide whether to use exponent notation if we encounter something ridiculous
if Item > 100_000_000_000_000.0 or Item < -0.000000000001 then
Var_T_IO.Put (Result, Item);
else
Var_T_IO.Put (Result, Item, Exp => 0);
end if;
-- Trim zeros
for I in reverse Result'Range loop
if Result (I) = '.' then Result (I) := ' '; exit; end if;
exit when Result (I) /= '0';
Result (I) := ' ';
end loop;
return To_Str (Trim (Result, Both));
end;
-- Tell the OS we mean business
begin Set_Priority (Priority'Last); end;