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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825 | ------------------------------------------------------------------
-- Tokeneer ID Station Core Software
--
-- Copyright (2003) United States Government, as represented
-- by the Director, National Security Agency. All rights reserved.
--
-- This material was originally developed by Praxis High Integrity
-- Systems Ltd. under contract to the National Security Agency.
------------------------------------------------------------------
------------------------------------------------------------------
-- File
--
-- Implementation Notes:
-- This is based on Stream_IO so cannot be Examined by SPARK.
--
------------------------------------------------------------------
with Ada.Streams.Stream_IO;
use type Ada.Streams.Stream_IO.File_Mode;
with Ada.Characters.Latin_1;
with Ada.Characters.Handling;
with Interfaces.C;
use type Interfaces.C.Size_T;
with Text_IO;
with Ada.Exceptions;
package body File
with SPARK_Mode => Off -- exception handlers
is
------------------------------------------------------------------
-- IsOpen
--
-- Description:
-- Returns True if the file is open.
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
function IsOpen (File : T) return Boolean is
(File.Handle /= null
and then Ada.Streams.Stream_IO.Is_Open (File.Handle.all));
------------------------------------------------------------------
-- IsIn
--
-- Description:
-- Returns True if the file is open in a readable mode.
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
function IsIn (File : T) return Boolean is
(IsOpen (File)
and then Ada.Streams.Stream_IO.Mode(File.Handle.all) =
Ada.Streams.Stream_IO.In_File);
------------------------------------------------------------------
-- IsOut
--
-- Description:
-- Returns True if the file is open in a writable mode.
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
function IsOut (File : T) return Boolean is
(IsOpen (File)
and then (Ada.Streams.Stream_IO.Mode(File.Handle.all) =
Ada.Streams.Stream_IO.Out_File or
Ada.Streams.Stream_IO.Mode(File.Handle.all) =
Ada.Streams.Stream_IO.Append_File));
------------------------------------------------------------------
-- Construct
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
function Construct (TheName : CommonTypes.StringF1) return T
is
NewName : NameT;
begin
if TheName'Last > MaxNameLength then
NewName.Length := MaxNameLength;
NewName.Text := TheName(1..MaxNameLength);
else
NewName.Length := TheName'Last;
NewName.Text(1..TheName'Last) := TheName;
end if;
return T'(NewName, null);
end Construct;
------------------------------------------------------------------
-- SetName
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
procedure SetName (TheName : in CommonTypes.StringF1;
TheFile : in out T)
is
begin
if TheName'Last > MaxNameLength then
TheFile.Name.Length := MaxNameLength;
TheFile.Name.Text := TheName(1..MaxNameLength);
else
TheFile.Name.Length := TheName'Last;
TheFile.Name.Text(1..TheName'Last) := TheName;
end if;
end SetName;
------------------------------------------------------------------
-- GetName
--
-- Description:
-- Gets the name of a file.
--
-- Traceunit : C.File.GetName
-- Traceto :
------------------------------------------------------------------
procedure GetName (TheFile : in T;
TheName : out String;
NameLength : out Natural)
is
begin
TheName := (others => ' ');
if TheName'Length >= TheFile.Name.Length then
TheName(TheName'First .. TheName'First + TheFile.Name.Length - 1)
:= TheFile.Name.Text(1..TheFile.Name.Length);
else
TheName(TheName'First .. TheName'Last)
:= TheFile.Name.Text(1 .. TheName'Length);
end if;
NameLength := TheFile.Name.Length;
end GetName;
------------------------------------------------------------------
-- OpenAppend
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
procedure OpenAppend (TheFile : in out T;
Success : out Boolean)
is
begin
TheFile.Handle := new Ada.Streams.Stream_IO.File_Type;
Ada.Streams.Stream_IO.Open
(File => TheFile.Handle.all,
Mode => Ada.Streams.Stream_IO.Append_File,
Name => TheFile.Name.Text(1..TheFile.Name.Length),
Form => "");
Success := True;
exception
when others =>
Success := False;
end OpenAppend;
------------------------------------------------------------------
-- OpenWrite
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
procedure OpenWrite (TheFile : in out T;
Success : out Boolean)
is
begin
TheFile.Handle := new Ada.Streams.Stream_IO.File_Type;
Ada.Streams.Stream_IO.Open
(File => TheFile.Handle.all,
Mode => Ada.Streams.Stream_IO.Out_File,
Name => TheFile.Name.Text(1..TheFile.Name.Length),
Form => "");
Success := True;
exception
when others =>
Success := False;
end OpenWrite;
------------------------------------------------------------------
-- OpenRead
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
procedure OpenRead ( TheFile : in out T ;
Success : out Boolean )
is
begin
TheFile.Handle := new Ada.Streams.Stream_IO.File_Type;
Ada.Streams.Stream_IO.Open
(File => TheFile.Handle.all,
Mode => Ada.Streams.Stream_IO.In_File,
Name => TheFile.Name.Text(1..TheFile.Name.Length),
Form => "");
Success := True;
exception
when others =>
Success := False;
end OpenRead;
------------------------------------------------------------------
-- PutChar
--
-- Description:
-- Writes a character to file.
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
procedure PutChar (TheFile : in out T;
Item : in Character)
is
Strm : Ada.Streams.Stream_IO.Stream_Access;
begin
if IsOut(TheFile) then
Strm := Ada.Streams.Stream_IO.Stream(TheFile.Handle.all);
Character'Write(Strm, Item);
end if;
exception
when others => null;
end PutChar;
------------------------------------------------------------------
-- Close
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
procedure Close (TheFile : in out T;
Success : out Boolean)
is
begin
Ada.Streams.Stream_IO.Close
(File => TheFile.Handle.all);
Success := True;
exception
when others =>
Success := False;
end Close;
------------------------------------------------------------------
-- Exists
--
-- Implementation Notes:
-- A file exists if it is open or can be opened.
--
------------------------------------------------------------------
function Exists (TheFile : T) return Boolean
is
CloseOK : Boolean;
Result : Boolean;
TempF : T := TheFile;
begin
if IsOpen(TheFile) then
Result := True;
else
OpenRead (TheFile => TempF,
Success => Result);
if Result then
Close (TheFile => TempF,
Success => CloseOK);
end if;
end if;
return Result;
end Exists;
------------------------------------------------------------------
-- PutString
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
procedure PutString (TheFile : in out T;
Text : in String;
Stop : in Natural)
is
Strm : Ada.Streams.Stream_IO.Stream_Access;
FinalStr : Positive;
Final : Positive;
begin
if IsOut(TheFile) then
if Stop = 0 then
Final := Text'Last;
FinalStr := Text'Last;
else
Final := Stop + Text'First - 1;
if Stop <= Text'Length then
FinalStr := Stop + Text'First - 1;
else
FinalStr := Text'Last;
end if;
end if;
Strm := Ada.Streams.Stream_IO.Stream(TheFile.Handle.all);
for I in Positive range Text'First .. FinalStr loop
Character'Write(Strm, Text(I));
end loop;
for I in Positive range FinalStr + 1 .. Final loop
Character'Write(Strm, ' ');
end loop;
end if;
exception
when others => null;
end PutString;
------------------------------------------------------------------
-- GetString
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
procedure GetString (TheFile : in out T;
Text : out String;
Stop : out Natural)
is
Strm : Ada.Streams.Stream_IO.Stream_Access;
begin
Stop := Text'First - 1;
if IsIn (TheFile) then
Strm := Ada.Streams.Stream_IO.Stream(TheFile.Handle.all);
loop
exit when EndOfFile(TheFile);
Stop := Stop + 1;
Character'Read(Strm, Text(Stop));
exit when Stop = Text'Last;
end loop;
end if;
exception
when others => null;
end GetString;
------------------------------------------------------------------
-- GetChar
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
procedure GetChar (TheFile : in out T;
Item : out Character)
is
Strm : Ada.Streams.Stream_IO.Stream_Access;
begin
if IsIn (TheFile) then
Strm := Ada.Streams.Stream_IO.Stream(TheFile.Handle.all);
Character'Read(Strm, Item);
else
Item := Character'First;
end if;
exception
when others =>
Item := Character'First;
end GetChar;
------------------------------------------------------------------
-- PutInteger
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
procedure PutInteger (TheFile : in out T;
Item : in Integer;
Width : in Natural)
is
Str : String := Integer'Image(Item);
PaddingCount : Integer := Width - Str'Length;
Start : Positive := Str'First;
begin
if Item >= 0 then
PaddingCount := PaddingCount + 1;
Start := Start + 1;
end if;
while PaddingCount > 0 loop
PutChar ( TheFile, ' ');
PaddingCount := PaddingCount -1;
end loop;
PutString (TheFile, Str( Start .. Str'Last), 0);
end PutInteger;
------------------------------------------------------------------
-- GetInteger
--
-- Implementation Notes:
-- If the Width is 0 then skip new lines, spaces until we reach
-- the number.
-- If the Width is not 0 then only character preceeding integer
-- is ' ' (No new lines).
------------------------------------------------------------------
procedure GetInteger (TheFile : in out T;
Item : out Integer;
Width : in Natural;
Success : out Boolean)
is
Ch : Character;
FileIndex : Ada.Streams.Stream_IO.Positive_Count;
Count : Natural := Width;
LastStrIndex : Positive := Integer'Image(Integer'First)'Last;
StrIndex : Positive := 1;
IntStr : String (1 .. LastStrIndex) := (others => ' ');
begin
Success := True;
Item := 0;
if Width = 0 then
loop
if EndOfFile(TheFile) then
Success := False;
exit;
end if;
if EndOfLine(TheFile) then
SkipLine(TheFile, 1);
Ch := ' ';
else
GetChar(TheFile, Ch);
end if;
if Ch = '+' then
IntStr(StrIndex) := Ch;
exit;
end if;
if Ch = '-' then
IntStr(StrIndex) := Ch;
exit;
end if;
if Ada.Characters.Handling.Is_Digit(Ch) then
StrIndex := StrIndex + 1;
IntStr(StrIndex) := Ch;
exit;
end if;
if Ch /= ' ' then
Success := False;
exit;
end if;
end loop;
if Success then
--Start building number
while not EndOfFile(TheFile) loop
FileIndex := Ada.Streams.Stream_IO.Index(TheFile.Handle.all);
GetChar(TheFile, Ch);
if not Ada.Characters.Handling.Is_Digit(Ch) then
Ada.Streams.Stream_IO.Set_Index(TheFile.Handle.all,
FileIndex);
exit;
end if;
StrIndex := StrIndex + 1;
IntStr(StrIndex) := Ch;
end loop;
end if;
else
-- only get Width Characters
while Count > 0 loop
exit when EndOfLine(TheFile);
Count := Count - 1;
GetChar(TheFile, Ch);
if Ch = '+' then
exit;
end if;
if Ch = '-' then
IntStr(StrIndex) := Ch;
exit;
end if;
if Ada.Characters.Handling.Is_Digit(Ch) then
StrIndex := StrIndex + 1;
IntStr(StrIndex) := Ch;
exit;
end if;
if Ch /= ' ' then
Success := False;
exit;
end if;
end loop;
while Count > 0 loop
exit when EndOfLine(TheFile);
Count := Count - 1;
GetChar(TheFile, Ch);
if not Ada.Characters.Handling.Is_Digit(Ch) then
Success := False;
else
StrIndex := StrIndex + 1;
IntStr(StrIndex) := Ch;
end if;
end loop;
end if;
Item := Integer'Value(IntStr);
exception
when others =>
Success := False;
end GetInteger;
------------------------------------------------------------------
-- NewLine
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
procedure NewLine (TheFile : in out T;
Spacing : in Natural)
is
begin
if IsOut (TheFile) then
for I in Natural range 1 .. Spacing loop
PutChar(TheFile, Ada.Characters.Latin_1.CR);
PutChar(TheFile, Ada.Characters.Latin_1.LF);
end loop;
end if;
end NewLine;
------------------------------------------------------------------
-- SkipLine
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
procedure SkipLine (TheFile : in out T;
Spacing : in Positive)
is
procedure SkipOneLine
is
DiscardedChar : Character;
begin
while not EndOfFile(TheFile) loop
GetChar (TheFile, DiscardedChar);
exit when DiscardedChar = Ada.Characters.Latin_1.LF;
end loop;
end SkipOneLine;
begin
if IsIn (TheFile) then
for I in Positive range 1 .. Spacing loop
SkipOneLine;
end loop;
end if;
end SkipLine;
------------------------------------------------------------------
-- GetLine
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
procedure GetLine (TheFile : in out T;
Item : out String;
Stop : out Natural)
is
Strm : Ada.Streams.Stream_IO.Stream_Access;
begin
Stop := Item'First - 1;
if IsIn (TheFile) then
Strm := Ada.Streams.Stream_IO.Stream(TheFile.Handle.all);
loop
if EndOfLine(TheFile) then
SkipLine(TheFile, 1);
exit;
end if;
exit when Stop = Item'Last;
Stop := Stop + 1;
Character'Read(Strm, Item(Stop));
end loop;
end if;
exception
when others => null;
end GetLine;
------------------------------------------------------------------
-- EndOfFile
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
function EndOfFile (TheFile : T) return Boolean
is
EOF : Boolean;
begin
if IsIn (TheFile) then
EOF := Ada.Streams.Stream_IO.End_Of_File(TheFile.Handle.all);
else
EOF := True;
end if;
return EOF;
end EndOfFile;
------------------------------------------------------------------
-- EndOfLine
--
-- Implementation Notes:
-- EOL is either CF.LF or just LF.
--
------------------------------------------------------------------
function EndOfLine (TheFile : T) return Boolean
is
EOLChar : Character;
FileIndex : Ada.Streams.Stream_IO.Positive_Count;
Result : Boolean;
TempF : T := TheFile;
begin
FileIndex := Ada.Streams.Stream_IO.Index(TempF.Handle.all);
if EndOfFile(TempF) then
Result := True;
else
GetChar(TheFile => TempF, Item => EOLChar);
if EOLChar = Ada.Characters.Latin_1.CR then
-- is next character a LF?
GetChar(TheFile => TempF, Item => EOLChar);
end if;
if EOLChar = Ada.Characters.Latin_1.LF then
Result := True;
else
Result := False;
end if;
Ada.Streams.Stream_IO.Set_Index(File => TempF.Handle.all,
To => FileIndex);
end if;
return Result;
end EndOfLine;
------------------------------------------------------------------
-- Create
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
procedure Create (TheFile : in out T;
Success : out Boolean)
is
begin
TheFile.Handle := new Ada.Streams.Stream_IO.File_Type;
Ada.Streams.Stream_IO.Create
( File => TheFile.Handle.all,
Mode => Ada.Streams.Stream_IO.Out_File,
Name => TheFile.Name.Text(1..TheFile.Name.Length),
Form => "" );
Success := True;
exception
when others =>
Success := False;
end Create;
------------------------------------------------------------------
-- Delete
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
procedure Delete ( TheFile : in out T;
Success : out Boolean)
is
begin
Ada.Streams.Stream_IO.Delete ( File => TheFile.Handle.all);
Success := True;
exception
when others =>
Success := False;
end Delete;
------------------------------------------------------------------
-- Compare
--
-- Implementation Notes:
-- None.
--
------------------------------------------------------------------
procedure Compare (FileA : in out T;
FileB : in out T;
FilesTheSame : out Boolean)
is
Success : Boolean;
CharA, CharB : Character;
begin
if FileA.Name = FileB.Name then
Success := True;
else
OpenRead(TheFile => FileA,
Success => Success);
if Success then
OpenRead(TheFile => FileB,
Success => Success);
if Success then
loop
if EndOfFile(FileA) and EndOfFile(FileB) then
exit;
end if;
if (EndOfFile(FileA) and not EndOfFile(FileB))
or else (not EndOfFile(FileA) and EndOfFile(FileB))
then
Success := False;
exit;
end if;
GetChar(TheFile => FileA, Item => CharA);
GetChar(TheFile => FileB, Item => CharB);
if CharA /= CharB then
Success := False;
exit;
end if;
end loop;
end if;
end if;
end if;
FilesTheSame := Success;
Close(TheFile => FileA,
Success => Success);
Close(TheFile => FileB,
Success => Success);
end Compare;
------------------------------------------------------------------
-- Copy
--
-- Implementation Notes:
-- None.
------------------------------------------------------------------
procedure Copy (FileA : in out T;
FileB : in out T;
Success : out Boolean)
is
CharA : Character;
begin
OpenRead(TheFile => FileA,
Success => Success);
if Success then
OpenAppend(TheFile => FileB,
Success => Success);
if not Success then
Create(TheFile => FileB,
Success => Success);
end if;
if Success then
while not EndOfFile(FileA) loop
GetChar(TheFile => FileA, Item => CharA);
PutChar(TheFile => FileB, Item => CharA);
end loop;
end if;
end if;
Close(TheFile => FileA,
Success => Success);
Close(TheFile => FileB,
Success => Success);
end Copy;
------------------------------------------------------------------
-- CreateDirectory
--
-- Implementation Notes:
-- None.
------------------------------------------------------------------
procedure CreateDirectory (DirName : in String;
Success : out Boolean)
is
begin
Success := True;
end CreateDirectory;
end File;
|