summaryrefslogtreecommitdiff
path: root/src/fltk-environment.adb
blob: ae832c0463e5e69b60f47ae45ed8d56d0ef625fc (plain)
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561


with

    Interfaces.C.Strings,
    System;

use type

    Interfaces.C.int,
    Interfaces.C.Strings.chars_ptr,
    System.Address;


package body FLTK.Environment is


    function new_fl_preferences
           (P, V, A : in Interfaces.C.char_array)
        return System.Address;
    pragma Import (C, new_fl_preferences, "new_fl_preferences");
    pragma Inline (new_fl_preferences);

    procedure free_fl_preferences
           (E : in System.Address);
    pragma Import (C, free_fl_preferences, "free_fl_preferences");
    pragma Inline (free_fl_preferences);




    function fl_preferences_entries
           (E : in System.Address)
        return Interfaces.C.int;
    pragma Import (C, fl_preferences_entries, "fl_preferences_entries");
    pragma Inline (fl_preferences_entries);

    function fl_preferences_entry
           (E : in System.Address;
            I : in Interfaces.C.int)
        return Interfaces.C.Strings.chars_ptr;
    pragma Import (C, fl_preferences_entry, "fl_preferences_entry");
    pragma Inline (fl_preferences_entry);

    function fl_preferences_entryexists
           (E : in System.Address;
            K : in Interfaces.C.char_array)
        return Interfaces.C.int;
    pragma Import (C, fl_preferences_entryexists, "fl_preferences_entryexists");
    pragma Inline (fl_preferences_entryexists);

    function fl_preferences_size
           (E : in System.Address;
            K : in Interfaces.C.char_array)
        return Interfaces.C.int;
    pragma Import (C, fl_preferences_size, "fl_preferences_size");
    pragma Inline (fl_preferences_size);




    function fl_preferences_get_int
           (E : in     System.Address;
            K : in     Interfaces.C.char_array;
            V :    out Interfaces.C.int;
            D : in     Interfaces.C.int)
        return Interfaces.C.int;
    pragma Import (C, fl_preferences_get_int, "fl_preferences_get_int");
    pragma Inline (fl_preferences_get_int);

    function fl_preferences_get_float
           (E : in     System.Address;
            K : in     Interfaces.C.char_array;
            V :    out Interfaces.C.C_float;
            D : in     Interfaces.C.C_float)
        return Interfaces.C.int;
    pragma Import (C, fl_preferences_get_float, "fl_preferences_get_float");
    pragma Inline (fl_preferences_get_float);

    function fl_preferences_get_double
           (E : in     System.Address;
            K : in     Interfaces.C.char_array;
            V :    out Interfaces.C.double;
            D : in     Interfaces.C.double)
        return Interfaces.C.int;
    pragma Import (C, fl_preferences_get_double, "fl_preferences_get_double");
    pragma Inline (fl_preferences_get_double);

    function fl_preferences_get_str
           (E : in     System.Address;
            K : in     Interfaces.C.char_array;
            V :    out Interfaces.C.Strings.chars_ptr;
            D : in     Interfaces.C.char_array)
        return Interfaces.C.int;
    pragma Import (C, fl_preferences_get_str, "fl_preferences_get_str");
    pragma Inline (fl_preferences_get_str);




    function fl_preferences_set_int
           (E : in System.Address;
            K : in Interfaces.C.char_array;
            V : in Interfaces.C.int)
        return Interfaces.C.int;
    pragma Import (C, fl_preferences_set_int, "fl_preferences_set_int");
    pragma Inline (fl_preferences_set_int);

    function fl_preferences_set_float
           (E : in System.Address;
            K : in Interfaces.C.char_array;
            V : in Interfaces.C.C_float)
        return Interfaces.C.int;
    pragma Import (C, fl_preferences_set_float, "fl_preferences_set_float");
    pragma Inline (fl_preferences_set_float);

    function fl_preferences_set_float_prec
           (E : in System.Address;
            K : in Interfaces.C.char_array;
            V : in Interfaces.C.C_float;
            P : in Interfaces.C.int)
        return Interfaces.C.int;
    pragma Import (C, fl_preferences_set_float_prec, "fl_preferences_set_float_prec");
    pragma Inline (fl_preferences_set_float_prec);

    function fl_preferences_set_double
           (E : in System.Address;
            K : in Interfaces.C.char_array;
            V : in Interfaces.C.double)
        return Interfaces.C.int;
    pragma Import (C, fl_preferences_set_double, "fl_preferences_set_double");
    pragma Inline (fl_preferences_set_double);

    function fl_preferences_set_double_prec
           (E : in System.Address;
            K : in Interfaces.C.char_array;
            V : in Interfaces.C.double;
            P : in Interfaces.C.int)
        return Interfaces.C.int;
    pragma Import (C, fl_preferences_set_double_prec, "fl_preferences_set_double_prec");
    pragma Inline (fl_preferences_set_double_prec);

    function fl_preferences_set_str
           (E : in System.Address;
            K : in Interfaces.C.char_array;
            V : in Interfaces.C.char_array)
        return Interfaces.C.int;
    pragma Import (C, fl_preferences_set_str, "fl_preferences_set_str");
    pragma Inline (fl_preferences_set_str);




    function fl_preferences_deleteentry
           (E : in System.Address;
            K : in Interfaces.C.char_array)
        return Interfaces.C.int;
    pragma Import (C, fl_preferences_deleteentry, "fl_preferences_deleteentry");
    pragma Inline (fl_preferences_deleteentry);

    function fl_preferences_deleteallentries
           (E : in System.Address)
        return Interfaces.C.int;
    pragma Import (C, fl_preferences_deleteallentries, "fl_preferences_deleteallentries");
    pragma Inline (fl_preferences_deleteallentries);

    function fl_preferences_clear
           (E : in System.Address)
        return Interfaces.C.int;
    pragma Import (C, fl_preferences_clear, "fl_preferences_clear");
    pragma Inline (fl_preferences_clear);




    procedure fl_preferences_flush
           (E : in System.Address);
    pragma Import (C, fl_preferences_flush, "fl_preferences_flush");
    pragma Inline (fl_preferences_flush);




    procedure Finalize
           (This : in out Preferences) is
    begin
        if  This.Void_Ptr /= System.Null_Address and then
            This in Preferences'Class
        then
            free_fl_preferences (This.Void_Ptr);
            This.Void_Ptr := System.Null_Address;
        end if;
    end Finalize;




    package body Forge is

        function From_Filesystem
               (Path, Vendor, Application : in String)
            return Preferences is
        begin
            return This : Preferences do
                This.Void_Ptr := new_fl_preferences
                   (Interfaces.C.To_C (Path),
                    Interfaces.C.To_C (Vendor),
                    Interfaces.C.To_C (Application));
            end return;
        end From_Filesystem;

    end Forge;




    function Number_Of_Entries
           (This : in Preferences)
        return Natural is
    begin
        return Natural (fl_preferences_entries (This.Void_Ptr));
    end Number_Of_Entries;


    function Get_Key
           (This  : in Preferences;
            Index : in Natural)
        return String
    is
        Key : Interfaces.C.Strings.chars_ptr :=
            fl_preferences_entry (This.Void_Ptr, Interfaces.C.int (Index));
    begin
        --  no need for dealloc?
        if Key = Interfaces.C.Strings.Null_Ptr then
            raise Constraint_Error;
        else
            return Interfaces.C.Strings.Value (Key);
        end if;
    end Get_Key;


    function Entry_Exists
           (This : in Preferences;
            Key  : in String)
        return Boolean is
    begin
        return fl_preferences_entryexists (This.Void_Ptr, Interfaces.C.To_C (Key)) /= 0;
    end Entry_Exists;


    function Entry_Size
           (This : in Preferences;
            Key  : in String)
        return Natural is
    begin
        return Natural (fl_preferences_size (This.Void_Ptr, Interfaces.C.To_C (Key)));
    end Entry_Size;




    function Get
           (This : in Preferences;
            Key  : in String)
        return Integer
    is
        Value : Interfaces.C.int;
    begin
        if fl_preferences_get_int
           (This.Void_Ptr,
            Interfaces.C.To_C (Key),
            Value, 0) = 0
        then
            raise Preference_Error;
        end if;
        return Integer (Value);
    end Get;


    function Get
           (This : in Preferences;
            Key  : in String)
        return Float
    is
        Value : Interfaces.C.C_float;
    begin
        if fl_preferences_get_float
           (This.Void_Ptr,
            Interfaces.C.To_C (Key),
            Value, 0.0) = 0
        then
            raise Preference_Error;
        end if;
        return Float (Value);
    end Get;


    function Get
           (This : in Preferences;
            Key  : in String)
        return Long_Float
    is
        Value : Interfaces.C.double;
    begin
        if fl_preferences_get_double
           (This.Void_Ptr,
            Interfaces.C.To_C (Key),
            Value, 0.0) = 0
        then
            raise Preference_Error;
        end if;
        return Long_Float (Value);
    end Get;


    function Get
           (This : in Preferences;
            Key  : in String)
        return String
    is
        Value : Interfaces.C.Strings.chars_ptr;
        Check : Interfaces.C.int := fl_preferences_get_str
           (This.Void_Ptr,
            Interfaces.C.To_C (Key),
            Value,
            Interfaces.C.To_C ("default"));
    begin
        if Check = 0 then
            raise Preference_Error;
        end if;
        if Value = Interfaces.C.Strings.Null_Ptr then
            return "";
        else
            declare
                Str : String := Interfaces.C.Strings.Value (Value);
            begin
                Interfaces.C.Strings.Free (Value);
                return Str;
            end;
        end if;
    end Get;




    function Get
           (This    : in Preferences;
            Key     : in String;
            Default : in Integer)
        return Integer
    is
        Value, X : Interfaces.C.int;
    begin
        X := fl_preferences_get_int
           (This.Void_Ptr,
            Interfaces.C.To_C (Key),
            Value,
            Interfaces.C.int (Default));
        return Integer (Value);
    end Get;


    function Get
           (This    : in Preferences;
            Key     : in String;
            Default : in Float)
        return Float
    is
        Value : Interfaces.C.C_float;
        X : Interfaces.C.int;
    begin
        X := fl_preferences_get_float
           (This.Void_Ptr,
            Interfaces.C.To_C (Key),
            Value,
            Interfaces.C.C_float (Default));
        return Float (Value);
    end Get;


    function Get
           (This    : in Preferences;
            Key     : in String;
            Default : in Long_Float)
        return Long_Float
    is
        Value : Interfaces.C.double;
        X : Interfaces.C.int;
    begin
        X := fl_preferences_get_double
           (This.Void_Ptr,
            Interfaces.C.To_C (Key),
            Value,
            Interfaces.C.double (Default));
        return Long_Float (Value);
    end Get;


    function Get
           (This    : in Preferences;
            Key     : in String;
            Default : in String)
        return String
    is
        Value : Interfaces.C.Strings.chars_ptr;
        X : Interfaces.C.int := fl_preferences_get_str
           (This.Void_Ptr,
            Interfaces.C.To_C (Key),
            Value,
            Interfaces.C.To_C (Default));
    begin
        if Value = Interfaces.C.Strings.Null_Ptr then
            return "";
        else
            declare
                Str : String := Interfaces.C.Strings.Value (Value);
            begin
                Interfaces.C.Strings.Free (Value);
                return Str;
            end;
        end if;
    end Get;




    procedure Set
           (This  : in out Preferences;
            Key   : in     String;
            Value : in     Integer) is
    begin
        if fl_preferences_set_int
           (This.Void_Ptr,
            Interfaces.C.To_C (Key),
            Interfaces.C.int (Value)) = 0
        then
            raise Preference_Error;
        end if;
    end Set;


    procedure Set
           (This  : in out Preferences;
            Key   : in     String;
            Value : in     Float) is
    begin
        if fl_preferences_set_float
           (This.Void_Ptr,
            Interfaces.C.To_C (Key),
            Interfaces.C.C_float (Value)) = 0
        then
            raise Preference_Error;
        end if;
    end Set;


    procedure Set
           (This      : in out Preferences;
            Key       : in     String;
            Value     : in     Float;
            Precision : in     Natural) is
    begin
        if fl_preferences_set_float_prec
           (This.Void_Ptr,
            Interfaces.C.To_C (Key),
            Interfaces.C.C_float (Value),
            Interfaces.C.int (Precision)) = 0
        then
            raise Preference_Error;
        end if;
    end Set;


    procedure Set
           (This  : in out Preferences;
            Key   : in     String;
            Value : in     Long_Float) is
    begin
        if fl_preferences_set_double
           (This.Void_Ptr,
            Interfaces.C.To_C (Key),
            Interfaces.C.double (Value)) = 0
        then
            raise Preference_Error;
        end if;
    end Set;


    procedure Set
           (This      : in out Preferences;
            Key       : in     String;
            Value     : in     Long_Float;
            Precision : in     Natural) is
    begin
        if fl_preferences_set_double_prec
           (This.Void_Ptr,
            Interfaces.C.To_C (Key),
            Interfaces.C.double (Value),
            Interfaces.C.int (Precision)) = 0
        then
            raise Preference_Error;
        end if;
    end Set;


    procedure Set
           (This  : in out Preferences;
            Key   : in     String;
            Value : in     String) is
    begin
        if fl_preferences_set_str
           (This.Void_Ptr,
            Interfaces.C.To_C (Key),
            Interfaces.C.To_C (Value)) = 0
        then
            raise Preference_Error;
        end if;
    end Set;




    procedure Delete_Entry
           (This : in out Preferences;
            Key  : in     String) is
    begin
        if fl_preferences_deleteentry (This.Void_Ptr, Interfaces.C.To_C (Key)) = 0 then
            raise Preference_Error;
        end if;
    end Delete_Entry;


    procedure Delete_All_Entries
           (This : in out Preferences) is
    begin
        if fl_preferences_deleteallentries (This.Void_Ptr) = 0 then
            raise Preference_Error;
        end if;
    end Delete_All_Entries;


    procedure Clear
           (This : in out Preferences) is
    begin
        if fl_preferences_clear (This.Void_Ptr) = 0 then
            raise Preference_Error;
        end if;
    end Clear;




    procedure Flush
           (This : in Preferences) is
    begin
        fl_preferences_flush (This.Void_Ptr);
    end Flush;


end FLTK.Environment;