-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparser.zig
More file actions
48 lines (38 loc) · 1.91 KB
/
parser.zig
File metadata and controls
48 lines (38 loc) · 1.91 KB
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
const std = @import("std");
/// Parser is a simpel .env parser to extract the Key & Value from the given input (env) file.
pub const Parser = struct {
env_values: std.StringHashMap([]const u8), // Store all Key Values in string hashmap for quick iteration and storage in local child process env
allocator: std.mem.Allocator,
file: std.fs.File,
const Self = @This();
pub fn init(allocator: std.mem.Allocator, file: std.fs.File) !Self {
return Self{ .allocator = allocator, .file = file, .env_values = std.StringHashMap([]const u8).init(allocator) };
}
pub fn deinit(self: *Self) void {
var values = self.env_values;
values.clearAndFree();
}
// parse is a simple parsing function. Its simple on purpose, this process should not be lofty or complex. No AST's or complex symbol resolution. Just take the Key and Value from the K=V from an .env and avoid comments (#)
pub fn parse(self: *Self) !std.StringHashMap([]const u8) {
var buf: [1024 * 2 * 2]u8 = undefined;
var reader = self.file.reader(&buf);
while (reader.interface.takeDelimiterExclusive('\n')) |line| {
// Skip comments (i.e. #)
if (std.mem.startsWith(u8, line, "#")) continue;
if (std.mem.eql(u8, line, "")) continue;
var split_iter = std.mem.splitScalar(u8, line, '=');
var key = split_iter.next() orelse "";
var value = split_iter.next() orelse "";
key = std.mem.trim(u8, key, "\"");
value = std.mem.trim(u8, value, "\"");
// One must dupe to avoid pointer issues in map
const d_key = try self.allocator.dupe(u8, key);
const d_val = try self.allocator.dupe(u8, value);
try self.env_values.put(d_key, d_val);
} else |err| {
if (err != error.EndOfStream)
return err;
}
return self.env_values;
}
};