~novakane/agertu

agertu/common/ini.zig -rw-r--r-- 9.4 KiB
9ea83d8fHugo Machet build: Bump to version 2.0.0-dev 3 months ago
                                                                                
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
// This file is part of nfm, the neat file manager.
//
// Copyright © 2021 Leon Henrik Plickat
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

const std = @import("std");
const ascii = std.ascii;
const io = std.io;
const mem = std.mem;

fn IniTok(comptime T: type) type {
    return struct {
        const Content = union(enum) {
            section: []const u8,
            assign: struct {
                variable: []const u8,
                value: []const u8,
            },
        };

        const Self = @This();

        reader: T,
        stack_buffer: [1024]u8 = undefined,
        // TODO maybe there should be an additional heap buffer for very long lines.

        pub fn next(self: *Self, line: *usize) !?Content {
            while (true) {
                line.* += 1;
                if (try self.reader.readUntilDelimiterOrEof(&self.stack_buffer, '\n')) |__buf| {
                    if (__buf.len == 1) {
                        if (__buf[0] == '#') {
                            continue;
                        } else {
                            return error.InvalidLine;
                        }
                    }
                    if (__buf.len == 0) continue;

                    const buf = blk: {
                        const _buf = mem.trim(u8, __buf, &ascii.spaces);
                        if (_buf.len == 0) continue;
                        if (_buf[0] == '#') continue;
                        for (_buf[1..]) |char, i| {
                            if (char == '#' and _buf[i - 1] != '\\') {
                                break :blk mem.trim(u8, _buf[0 .. i + 1], &ascii.spaces);
                            }
                        }
                        break :blk _buf;
                    };

                    // Is this line a section header?
                    if (buf[0] == '[') {
                        if (buf[buf.len - 1] != ']') return error.InvalidLine;
                        if (buf.len < 3) return error.InvalidLine;
                        return Content{ .section = buf[1 .. buf.len - 1] };
                    }

                    // Is this line an assignment?
                    var eq_pos = blk: {
                        for (buf) |char, i| {
                            if (char == '=') {
                                if (i == buf.len - 1) return error.InvalidLine;
                                break :blk i;
                            }
                        }
                        return error.InvalidLine;
                    };
                    return Content{
                        .assign = .{
                            .variable = blk: {
                                const variable = mem.trim(u8, buf[0..eq_pos], &ascii.spaces);
                                if (variable.len == 0) return error.InvalidLine;
                                break :blk variable;
                            },
                            .value = blk: {
                                const value = mem.trim(u8, buf[eq_pos + 1 ..], &ascii.spaces);
                                if (value.len < 2 or value[value.len - 1] != ';') return error.InvalidLine;
                                // TODO[zig] should be inline, but that crashes the zig compiler right now
                                for ([_]u8{ '\'', '"' }) |q| {
                                    if (value[0] == q) {
                                        if (value[value.len - 2] == q and value.len > 3) {
                                            break :blk value[1 .. value.len - 2];
                                        } else {
                                            return error.InvalidLine;
                                        }
                                    }
                                }
                                break :blk value[0 .. value.len - 1];
                            },
                        },
                    };
                } else {
                    return null;
                }
            }
        }
    };
}

pub fn tokenize(reader: anytype) IniTok(@TypeOf(reader)) {
    return .{
        .reader = reader,
    };
}

test "ini tokenizer good input" {
    const reader = io.fixedBufferStream(
        \\[header] # I am a comment
        \\a = b;
        \\
        \\c=d;
        \\e= f;# Another comment
        \\
        \\[header2]
        \\[header3]
        \\#
        \\
        \\hello = this has spaces;
        \\hello = this one; is weird;
        \\hello = test=test;
        \\
    ).reader();

    var it = tokenize(reader);
    var line: usize = 0;

    if (try it.next(&line)) |a| {
        try std.testing.expect(a == .section);
        try std.testing.expectEqualSlices(u8, "header", a.section);
        try std.testing.expect(line == 1);
    } else {
        unreachable;
    }

    if (try it.next(&line)) |a| {
        try std.testing.expect(a == .assign);
        try std.testing.expectEqualSlices(u8, "a", a.assign.variable);
        try std.testing.expectEqualSlices(u8, "b", a.assign.value);
        try std.testing.expect(line == 2);
    } else {
        unreachable;
    }

    if (try it.next(&line)) |a| {
        try std.testing.expect(a == .assign);
        try std.testing.expectEqualSlices(u8, "c", a.assign.variable);
        try std.testing.expectEqualSlices(u8, "d", a.assign.value);
        try std.testing.expect(line == 4);
    } else {
        unreachable;
    }

    if (try it.next(&line)) |a| {
        try std.testing.expect(a == .assign);
        try std.testing.expectEqualSlices(u8, "e", a.assign.variable);
        try std.testing.expectEqualSlices(u8, "f", a.assign.value);
        try std.testing.expect(line == 5);
    } else {
        unreachable;
    }

    if (try it.next(&line)) |a| {
        try std.testing.expect(a == .section);
        try std.testing.expectEqualSlices(u8, "header2", a.section);
        try std.testing.expect(line == 7);
    } else {
        unreachable;
    }

    if (try it.next(&line)) |a| {
        try std.testing.expect(a == .section);
        try std.testing.expectEqualSlices(u8, "header3", a.section);
        try std.testing.expect(line == 8);
    } else {
        unreachable;
    }

    if (try it.next(&line)) |a| {
        try std.testing.expect(a == .assign);
        try std.testing.expectEqualSlices(u8, "hello", a.assign.variable);
        try std.testing.expectEqualSlices(u8, "this has spaces", a.assign.value);
        try std.testing.expect(line == 11);
    } else {
        unreachable;
    }

    if (try it.next(&line)) |a| {
        try std.testing.expect(a == .assign);
        try std.testing.expectEqualSlices(u8, "hello", a.assign.variable);
        try std.testing.expectEqualSlices(u8, "this one; is weird", a.assign.value);
        try std.testing.expect(line == 12);
    } else {
        unreachable;
    }

    if (try it.next(&line)) |a| {
        try std.testing.expect(a == .assign);
        try std.testing.expectEqualSlices(u8, "hello", a.assign.variable);
        try std.testing.expectEqualSlices(u8, "test=test", a.assign.value);
        try std.testing.expect(line == 13);
    } else {
        unreachable;
    }

    try std.testing.expect((try it.next(&line)) == null);
}

test "ini tokenizer bad input" {
    {
        const reader = io.fixedBufferStream(
            \\[section
            \\
        ).reader();
        var it = tokenize(reader);
        var line: usize = 0;
        try std.testing.expectError(error.InvalidLine, it.next(&line));
        try std.testing.expect(line == 1);
    }
    {
        const reader = io.fixedBufferStream(
            \\section]
            \\
        ).reader();
        var it = tokenize(reader);
        var line: usize = 0;
        try std.testing.expectError(error.InvalidLine, it.next(&line));
        try std.testing.expect(line == 1);
    }
    {
        const reader = io.fixedBufferStream(
            \\[]
            \\
        ).reader();
        var it = tokenize(reader);
        var line: usize = 0;
        try std.testing.expectError(error.InvalidLine, it.next(&line));
        try std.testing.expect(line == 1);
    }
    {
        const reader = io.fixedBufferStream(
            \\ =B;
            \\
        ).reader();
        var it = tokenize(reader);
        var line: usize = 0;
        try std.testing.expectError(error.InvalidLine, it.next(&line));
        try std.testing.expect(line == 1);
    }
    {
        const reader = io.fixedBufferStream(
            \\a =
            \\
        ).reader();
        var it = tokenize(reader);
        var line: usize = 0;
        try std.testing.expectError(error.InvalidLine, it.next(&line));
        try std.testing.expect(line == 1);
    }
    {
        const reader = io.fixedBufferStream(
            \\a =  ;
            \\
        ).reader();
        var it = tokenize(reader);
        var line: usize = 0;
        try std.testing.expectError(error.InvalidLine, it.next(&line));
        try std.testing.expect(line == 1);
    }
}