summaryrefslogtreecommitdiff
path: root/src/csv.adb
blob: 656f99f2701b4fd0aeeba1e08ceb5d53d28729df (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


--  This source is licensed under the Sunset License v1.0


with

    Ada.Characters.Latin_1,
    Ada.Strings.Fixed,
    Ada.Strings.Maps,
    Ada.Text_IO,
    Datatypes;

use

    Ada.Text_IO;


package body CSV is


    package Latin renames Ada.Characters.Latin_1;
    package Strfix renames Ada.Strings.Fixed;
    package Strmap renames Ada.Strings.Maps;




    procedure Put_Cell
           (File_Handle : in Ada.Text_IO.File_Type;
            Data        : in String) is
    begin
        Put_Cell (File_Handle, +Data);
    end Put_Cell;


    procedure Put_Cell
           (File_Handle : in Ada.Text_IO.File_Type;
            Data        : in Ada.Strings.Unbounded.Unbounded_String)
    is
        Processed : SU.Unbounded_String := Data;
        Position  : Natural;
    begin
        --  Deal with any lurking linefeed characters
        loop
            Position := SU.Index (Processed, Strmap.To_Set (Latin.LF));
            exit when Position = 0;
            SU.Replace_Element (Processed, Position, ' ');
        end loop;

        --  Escape any quizzical quotation characters
        Position := SU.Index (Processed, Strmap.To_Set (Quote_Char));
        while Position /= 0 loop
            SU.Insert (Processed, Position, Strfix."*" (1, Escape_Char));
            Position := Position + 2;  --  skip over the \" we already know about
            exit when Position > SU.Length (Processed);
            Position := SU.Index (Processed, Strmap.To_Set (Quote_Char), Position);
        end loop;

        --  Surround any suspicious separator characters
        if SU.Index (Processed, Strmap.To_Set (Separator_Char)) /= 0 then
            SU.Insert (Processed, 1, Strfix."*" (1, Quote_Char));
            SU.Append (Processed, Quote_Char);
        end if;

        Put (File_Handle, -Processed);
    end Put_Cell;


    procedure Put_Header
           (File_Handle : in Ada.Text_IO.File_Type;
            Titles      : in Datatypes.Field_ID_Vector) is
    begin
        for Position in Datatypes.Field_Ordinal range
            Titles.First_Index .. Datatypes.Field_Ordinal'Pred (Titles.Last_Index)
        loop
            Put_Cell (File_Handle, SU.Unbounded_String (Titles.Element (Position)));
            Put (File_Handle, Separator_Char);
        end loop;
        Put_Cell (File_Handle, SU.Unbounded_String (Titles.Last_Element));
        New_Line (File_Handle);
    end Put_Header;


    procedure Put_Row
           (File_Handle : in Ada.Text_IO.File_Type;
            Cells       : in Datatypes.Field_Vector;
            Quantity    : in Positive)
    is
        use type Datatypes.Field_Ordinal;
        Counter  : Positive := 1;
        Position : Datatypes.Field_Ordinal := Cells.First_Index;
    begin
        while Counter < Quantity loop
            if Position <= Cells.Last_Index then
                Put_Cell (File_Handle, SU.Unbounded_String (Cells.Element (Position)));
            end if;
            Put (File_Handle, Separator_Char);
            Position := Datatypes.Field_Ordinal'Succ (Position);
            Counter := Counter + 1;
        end loop;
        if Position <= Cells.Last_Index then
            Put_Cell (File_Handle, SU.Unbounded_String (Cells.Element (Position)));
        end if;
        New_Line (File_Handle);
    end Put_Row;


end CSV;