-
Notifications
You must be signed in to change notification settings - Fork 1
/
player-aux.adb
111 lines (88 loc) · 2.22 KB
/
player-aux.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
with Player.Exceptions;
with Ada.Exceptions;
with Ada.Unchecked_Deallocation;
with Interfaces;
use Interfaces;
package body Player.Aux is
use type C.Int;
type Node;
type Node_Access is access Node;
type Node is record
Next : Node_Access;
Str : C.Strings.Chars_Ptr;
end record;
-- This keeps copies of Strings C-ified, so they're foreignly accessible.
-- You should free them at appropriate moments.
protected Copies is
procedure Add (S : in String; Ptr : out C.Strings.Chars_Ptr);
procedure Free;
private
First : Node_Access;
end Copies;
protected body Copies is
---------
-- Add --
---------
procedure Add (S : in String; Ptr : out C.Strings.Chars_Ptr) is
begin
Ptr := C.Strings.New_String (S);
declare
New_Node : constant Node_Access := new Node'(First, Ptr);
begin
First := New_Node;
end;
end Add;
----------
-- Free --
----------
procedure Free is
------------
-- Delete --
------------
procedure Delete is new Ada.Unchecked_Deallocation (Node, Node_Access);
Pos, Next : Node_Access := First;
begin
while Pos /= null loop
Next := Pos.Next;
C.Strings.Free (Pos.Str);
Delete (Pos);
Pos := Next;
end loop;
First := null;
end Free;
end Copies;
-----------
-- Check --
-----------
procedure Check (I : in C.Int) is
begin
if I = -1 then
Ada.Exceptions.Raise_Exception
(Player.Exceptions.Player_Error'Identity,
Player.Exceptions.Error_Str);
end if;
end Check;
----------------
-- Check_Fail --
----------------
procedure Check_Fail is
begin
Check (-1);
end Check_Fail;
------------------
-- Free_Strings --
------------------
procedure Free_Strings is
begin
Copies.Free;
end Free_Strings;
----------
-- To_C --
----------
function To_C (S : in String) return Interfaces.C.Strings.Chars_Ptr is
Ptr : C.Strings.Chars_Ptr;
begin
Copies.Add (S, Ptr);
return Ptr;
end To_C;
end Player.Aux;