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


--  This source is licensed under Creative Commons CC0 v1.0.
--
--  To read the full text, see license.txt in the main directory of this repository
--  or go to https://creativecommons.org/publicdomain/zero/1.0/legalcode.txt
--
--  For a human readable summary, go to https://creativecommons.org/publicdomain/zero/1.0/


package body CSV is


    package SU renames Ada.Strings.Unbounded;




    function Quoted
           (Input     : in     SU.Unbounded_String;
            Output    :    out SU.Unbounded_String;
            Remaining :    out SU.Unbounded_String)
        return Boolean
    is
        Result : SU.Unbounded_String;
        This_In : SU.Unbounded_String;
    begin
        if  SU.Length (Input) > 0 and then
            SU.Element (Input, 1) = Quote
        then
            Result := SU.To_Unbounded_String (0);
            This_In := SU.Tail (Input, SU.Length (Input) - 1);
            while SU.Length (This_In) > 0 and then
                  SU.Element (This_In, 1) /= Quote
            loop
                SU.Append (Result, SU.Head (This_In, 1));
                This_In := SU.Tail (This_In, SU.Length (This_In) - 1);
            end loop;
            Output := Result;
            if SU.Length (This_In) > 0 then
                Remaining := SU.Tail (This_In, SU.Length (This_In) - 1);
            else
                Remaining := SU.To_Unbounded_String (0);
            end if;
            return True;
        else
            return False;
        end if;
    end Quoted;




    function Escape_Char
           (Input     : in     SU.Unbounded_String;
            Output    :    out SU.Unbounded_String;
            Remaining :    out SU.Unbounded_String)
        return Boolean is
    begin
        if  SU.Length (Input) > 1 and then
            SU.Element (Input, 1) = Escape
        then
            Output := SU.Unbounded_Slice (Input, 2, 1);
            Remaining := SU.Tail (Input, SU.Length (Input) - 2);
            return True;
        else
            return False;
        end if;
    end Escape_Char;




    function Field_Char
           (Input     : in     SU.Unbounded_String;
            Output    :    out SU.Unbounded_String;
            Remaining :    out SU.Unbounded_String)
        return Boolean is
    begin
        if  SU.Length (Input) > 0 and then
            SU.Element (Input, 1) /= Delimiter and then
            SU.Element (Input, 1) /= Quote and then
            SU.Element (Input, 1) /= Escape
        then
            Output := SU.Head (Input, 1);
            Remaining := SU.Tail (Input, SU.Length (Input) - 1);
            return True;
        else
            return False;
        end if;
    end Field_Char;




    procedure Field
           (Input     : in     SU.Unbounded_String;
            Output    :    out SU.Unbounded_String;
            Remaining :    out SU.Unbounded_String)
    is
        Result : SU.Unbounded_String;
        This_In, This_Out, This_Remaining : SU.Unbounded_String;
    begin
        Result := SU.To_Unbounded_String (0);
        This_In := Input;
        while
            Field_Char (This_In, This_Out, This_Remaining) or else
            Escape_Char (This_In, This_Out, This_Remaining) or else
            Quoted (This_In, This_Out, This_Remaining)
        loop
            SU.Append (Result, This_Out);
            This_In := This_Remaining;
        end loop;
        Output := Result;
        Remaining := This_In;
    end Field;




    function Separator
           (Input     : in     SU.Unbounded_String;
            Remaining :    out SU.Unbounded_String)
        return Boolean is
    begin
        if  SU.Length (Input) > 0 and then
            SU.Element (Input, 1) = Delimiter
        then
            Remaining := SU.Tail (Input, SU.Length (Input) - 1);
            return True;
        else
            return False;
        end if;
    end Separator;




    function Parse_Line
           (Input : in String)
        return CSV_Record
    is
        Result : CSV_Record := String_Vectors.Empty_Vector;
        This_In, This_Out, This_Remaining : SU.Unbounded_String;
    begin
        This_In := SU.To_Unbounded_String (Input);
        loop
            Field (This_In, This_Out, This_Remaining);
            Result.Append (This_Out);
            exit when not Separator (This_Remaining, This_In);
        end loop;
        return Result;
    end Parse_Line;




    function Encode_String
           (Input : in SU.Unbounded_String)
        return SU.Unbounded_String
    is
        Result : SU.Unbounded_String;
        Elem : Character;
        Needs_Quotes : Boolean := False;
    begin
        Result := SU.To_Unbounded_String (0);
        for I in Integer range 1 .. SU.Length (Input) loop
            Elem := SU.Element (Input, I);
            if  Elem = Quote or
                Elem = Escape
            then
                SU.Append (Result, Escape & Elem);
            else
                SU.Append (Result, Elem);
            end if;
            if Elem = Delimiter then
                Needs_Quotes := True;
            end if;
        end loop;
        if Needs_Quotes then
            Result := Quote & Result & Quote;
        end if;
        return Result;
    end Encode_String;




    function Unparse_Record
           (Input : in CSV_Record)
        return String
    is
        Result : SU.Unbounded_String;
    begin
        Result := SU.To_Unbounded_String (0);
        for Item of Input loop
            SU.Append (Result, Encode_String (Item) & Delimiter);
        end loop;
        return SU.Slice (Result, 1, SU.Length (Result) - 1);
    end Unparse_Record;


end CSV;