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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
|
-- Programmed by Jedidiah Barber
-- Released into the public domain
with
Ada.Assertions,
Interfaces.C.Strings;
use type
Interfaces.C.int,
Interfaces.C.Strings.chars_ptr;
package body FLTK.Filenames is
package Chk renames Ada.Assertions;
------------------------
-- Constants From C --
------------------------
error_bsize : constant Interfaces.C.int;
pragma Import (C, error_bsize, "error_bsize");
------------------------
-- Functions From C --
------------------------
procedure free_filename_file_list
(L : in Storage.Integer_Address;
N : in Interfaces.C.int);
pragma Import (C, free_filename_file_list, "free_filename_file_list");
pragma Inline (free_filename_file_list);
function filename_dname
(L : in Storage.Integer_Address;
N : in Interfaces.C.int)
return Interfaces.C.Strings.chars_ptr;
pragma Import (C, filename_dname, "filename_dname");
pragma Inline (filename_dname);
procedure filename_decode_uri
(URI : in Interfaces.C.char_array);
pragma Import (C, filename_decode_uri, "filename_decode_uri");
pragma Inline (filename_decode_uri);
function filename_absolute
(To : in Interfaces.C.char_array;
Len : in Interfaces.C.int;
From : in Interfaces.C.char_array)
return Interfaces.C.int;
pragma Import (C, filename_absolute, "filename_absolute");
pragma Inline (filename_absolute);
function filename_expand
(To : in Interfaces.C.char_array;
Len : in Interfaces.C.int;
From : in Interfaces.C.char_array)
return Interfaces.C.int;
pragma Import (C, filename_expand, "filename_expand");
pragma Inline (filename_expand);
function filename_ext
(Buf : in Interfaces.C.char_array)
return Interfaces.C.Strings.chars_ptr;
pragma Import (C, filename_ext, "filename_ext");
pragma Inline (filename_ext);
function filename_isdir
(Name : in Interfaces.C.char_array)
return Interfaces.C.int;
pragma Import (C, filename_isdir, "filename_isdir");
pragma Inline (filename_isdir);
function filename_list
(D : in Interfaces.C.char_array;
L : out Storage.Integer_Address;
F : in Storage.Integer_Address)
return Interfaces.C.int;
pragma Import (C, filename_list, "filename_list");
pragma Inline (filename_list);
function filename_match
(Name, Pattern : in Interfaces.C.char_array)
return Interfaces.C.int;
pragma Import (C, filename_match, "filename_match");
pragma Inline (filename_match);
function filename_name
(Name : in Interfaces.C.char_array)
return Interfaces.C.Strings.chars_ptr;
pragma Import (C, filename_name, "filename_name");
pragma Inline (filename_name);
function filename_relative
(To : in Interfaces.C.char_array;
Len : in Interfaces.C.int;
From : in Interfaces.C.char_array)
return Interfaces.C.int;
pragma Import (C, filename_relative, "filename_relative");
pragma Inline (filename_relative);
function filename_setext
(To : in Interfaces.C.char_array;
Len : in Interfaces.C.int;
Ext : in Interfaces.C.char_array)
return Interfaces.C.Strings.chars_ptr;
pragma Import (C, filename_setext, "filename_setext");
pragma Inline (filename_setext);
function filename_open_uri
(U, M : in Interfaces.C.char_array;
Len : in Interfaces.C.int)
return Interfaces.C.int;
pragma Import (C, filename_open_uri, "filename_open_uri");
pragma Inline (filename_open_uri);
function filename_alphasort
(A, B : in Interfaces.C.char_array)
return Interfaces.C.int;
pragma Import (C, filename_alphasort, "filename_alphasort");
function filename_casealphasort
(A, B : in Interfaces.C.char_array)
return Interfaces.C.int;
pragma Import (C, filename_casealphasort, "filename_casealphasort");
function filename_numericsort
(A, B : in Interfaces.C.char_array)
return Interfaces.C.int;
pragma Import (C, filename_numericsort, "filename_numericsort");
function filename_casenumericsort
(A, B : in Interfaces.C.char_array)
return Interfaces.C.int;
pragma Import (C, filename_casenumericsort, "filename_casenumericsort");
------------------------------
-- Comparison Subprograms --
------------------------------
function Alpha_Sort
(A, B : in String)
return Comparison
is
Result : Interfaces.C.int :=
filename_alphasort (Interfaces.C.To_C (A), Interfaces.C.To_C (B));
begin
pragma Assert
(Result in Comparison'Pos (Comparison'First) .. Comparison'Pos (Comparison'Last));
return Comparison'Val (Result);
exception
when Chk.Assertion_Error => raise Internal_FLTK_Error;
end Alpha_Sort;
function Case_Alpha_Sort
(A, B : in String)
return Comparison
is
Result : Interfaces.C.int :=
filename_casealphasort (Interfaces.C.To_C (A), Interfaces.C.To_C (B));
begin
pragma Assert
(Result in Comparison'Pos (Comparison'First) .. Comparison'Pos (Comparison'Last));
return Comparison'Val (Result);
exception
when Chk.Assertion_Error => raise Internal_FLTK_Error;
end Case_Alpha_Sort;
function Numeric_Sort
(A, B : in String)
return Comparison
is
Result : Interfaces.C.int :=
filename_numericsort (Interfaces.C.To_C (A), Interfaces.C.To_C (B));
begin
pragma Assert
(Result in Comparison'Pos (Comparison'First) .. Comparison'Pos (Comparison'Last));
return Comparison'Val (Result);
exception
when Chk.Assertion_Error => raise Internal_FLTK_Error;
end Numeric_Sort;
function Case_Numeric_Sort
(A, B : in String)
return Comparison
is
Result : Interfaces.C.int :=
filename_casenumericsort (Interfaces.C.To_C (A), Interfaces.C.To_C (B));
begin
pragma Assert
(Result in Comparison'Pos (Comparison'First) .. Comparison'Pos (Comparison'Last));
return Comparison'Val (Result);
exception
when Chk.Assertion_Error => raise Internal_FLTK_Error;
end Case_Numeric_Sort;
---------------------------
-- Listing Subprograms --
---------------------------
procedure Finalize
(This : in out File_List) is
begin
if This.Void_Ptr /= Null_Pointer and This.Needs_Dealloc then
free_filename_file_list (This.Void_Ptr, This.Entries);
This.Void_Ptr := Null_Pointer;
end if;
end Finalize;
function Length
(This : in File_List)
return Natural is
begin
return Natural (This.Entries);
end Length;
function Item
(This : in File_List;
Index : in Positive)
return Path_String is
begin
return Interfaces.C.Strings.Value
(filename_dname (This.Void_Ptr, Interfaces.C.int (Index) - 1));
end Item;
--------------------
-- Filename API --
--------------------
function Decode_URI
(URI : in Path_String)
return Path_String
is
C_Ptr : Interfaces.C.char_array := Interfaces.C.To_C (URI);
begin
filename_decode_uri (C_Ptr);
return Interfaces.C.To_Ada (C_Ptr);
end Decode_URI;
procedure Open_URI
(URI : in Path_String)
is
Message : Interfaces.C.char_array (1 .. Interfaces.C.size_t (error_bsize)) :=
(others => Interfaces.C.char'Val (0));
Result : Interfaces.C.int := filename_open_uri
(Interfaces.C.To_C (URI),
Message,
error_bsize);
begin
if Result = 0 then
raise Open_URI_Error with "Error: " & Interfaces.C.To_Ada (Message);
else
pragma Assert (Result = 1);
end if;
exception
when Chk.Assertion_Error => raise Internal_FLTK_Error;
end Open_URI;
function Absolute
(Name : in Path_String)
return Path_String
is
Result : Interfaces.C.char_array (1 .. Interfaces.C.size_t (Max_Path_Length)) :=
(others => Interfaces.C.char'Val (0));
Code : Interfaces.C.int := filename_absolute
(Result,
Interfaces.C.int (Max_Path_Length),
Interfaces.C.To_C (Name));
begin
return Interfaces.C.To_Ada (Result);
end Absolute;
function Absolute
(Name : in Path_String;
Changed : out Boolean)
return Path_String
is
Result : Interfaces.C.char_array (1 .. Interfaces.C.size_t (Max_Path_Length)) :=
(others => Interfaces.C.char'Val (0));
Code : Interfaces.C.int := filename_absolute
(Result,
Interfaces.C.int (Max_Path_Length),
Interfaces.C.To_C (Name));
begin
Changed := Code /= 0;
return Interfaces.C.To_Ada (Result);
end Absolute;
function Relative
(Name : in Path_String)
return Path_String
is
Result : Interfaces.C.char_array (1 .. Interfaces.C.size_t (Max_Path_Length)) :=
(others => Interfaces.C.char'Val (0));
Code : Interfaces.C.int := filename_relative
(Result,
Interfaces.C.int (Max_Path_Length),
Interfaces.C.To_C (Name));
begin
return Interfaces.C.To_Ada (Result);
end Relative;
function Relative
(Name : in Path_String;
Changed : out Boolean)
return Path_String
is
Result : Interfaces.C.char_array (1 .. Interfaces.C.size_t (Max_Path_Length)) :=
(others => Interfaces.C.char'Val (0));
Code : Interfaces.C.int := filename_relative
(Result,
Interfaces.C.int (Max_Path_Length),
Interfaces.C.To_C (Name));
begin
Changed := Code /= 0;
return Interfaces.C.To_Ada (Result);
end Relative;
function Expand
(Name : in Path_String)
return Path_String
is
Result : Interfaces.C.char_array (1 .. Interfaces.C.size_t (Max_Path_Length)) :=
(others => Interfaces.C.char'Val (0));
Code : Interfaces.C.int := filename_expand
(Result,
Interfaces.C.int (Max_Path_Length),
Interfaces.C.To_C (Name));
begin
return Interfaces.C.To_Ada (Result);
end Expand;
function Expand
(Name : in Path_String;
Changed : out Boolean)
return Path_String
is
Result : Interfaces.C.char_array (1 .. Interfaces.C.size_t (Max_Path_Length)) :=
(others => Interfaces.C.char'Val (0));
Code : Interfaces.C.int := filename_expand
(Result,
Interfaces.C.int (Max_Path_Length),
Interfaces.C.To_C (Name));
begin
Changed := Code /= 0;
return Interfaces.C.To_Ada (Result);
end Expand;
function Base_Name
(Name : in Path_String)
return Path_String
is
Data : Interfaces.C.char_array := Interfaces.C.To_C (Name);
begin
return Interfaces.C.Strings.Value (filename_name (Data));
end Base_Name;
function Extension
(Name : in Path_String)
return Path_String
is
Data : Interfaces.C.char_array := Interfaces.C.To_C (Name);
Result : Interfaces.C.Strings.chars_ptr := filename_ext (Data);
begin
if Result = Interfaces.C.Strings.Null_Ptr then
return "";
else
return Interfaces.C.Strings.Value (Result);
end if;
end Extension;
function Set_Extension
(Name : in Path_String;
Suffix : in String)
return Path_String
is
Data : Interfaces.C.char_array (1 .. Interfaces.C.size_t (Max_Path_Length)) :=
(others => Interfaces.C.char'Val (0));
Result : Interfaces.C.Strings.chars_ptr;
begin
Data (1 .. Name'Length) := Interfaces.C.To_C (Name);
Result := filename_setext
(Data,
Data'Length,
Interfaces.C.To_C (Suffix));
return Interfaces.C.Strings.Value (Result);
end Set_Extension;
function Is_Directory
(Name : in Path_String)
return Boolean is
begin
return filename_isdir (Interfaces.C.To_C (Name)) /= 0;
end Is_Directory;
Current_Sort : Compare_Function;
function Compare_Hook
(DA, DB : in Storage.Integer_Address)
return Interfaces.C.int;
pragma Convention (C, Compare_Hook);
function Compare_Hook
(DA, DB : in Storage.Integer_Address)
return Interfaces.C.int
is
Result : Comparison := Current_Sort
(Interfaces.C.Strings.Value (filename_dname (DA, 0)),
Interfaces.C.Strings.Value (filename_dname (DB, 0)));
begin
return Comparison'Pos (Result) - 1;
end Compare_Hook;
function Get_Listing
(Name : in Path_String;
Sort : in not null Compare_Function := Numeric_Sort'Access)
return File_List is
begin
Current_Sort := Sort;
return This : File_List do
This.Entries := filename_list
(Interfaces.C.To_C (Name),
This.Void_Ptr,
Storage.To_Integer (Compare_Hook'Address));
end return;
end Get_Listing;
function Match
(Input, Pattern : in String)
return Boolean is
begin
return filename_match (Interfaces.C.To_C (Input), Interfaces.C.To_C (Pattern)) /= 0;
end Match;
end FLTK.Filenames;
|