diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 4fdb85c..0000000 --- a/.gitmodules +++ /dev/null @@ -1,6 +0,0 @@ -[submodule "lib/zig-clap"] - path = lib/zig-clap - url = git@github.com:Hejsil/zig-clap.git -[submodule "lib/zig-csv"] - path = lib/zig-csv - url = git@github.com:beho/zig-csv.git diff --git a/README.md b/README.md index f218912..2db12bd 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ $ head -n /tmp/1mil.json Download the latest release from the [releases](https://github.com/knadh/csv2json/releases) page. Or, to compile with Zig (tested with v0.11.0): -- `git clone --recursive git@github.com:knadh/csv2json.git` +- `git clone git@github.com:knadh/csv2json.git` - `cd csv2json && zig build` - The binary will be in `zig-out/bin` diff --git a/build.zig b/build.zig index 0b43f64..efdcda5 100644 --- a/build.zig +++ b/build.zig @@ -1,14 +1,6 @@ const std = @import("std"); -pub const clap_module = std.Build.CreateModuleOptions{ - .source_file = .{ .path = "lib/zig-clap/clap.zig" }, -}; - -pub const csv_module = std.Build.CreateModuleOptions{ - .source_file = .{ .path = "lib/zig-csv/src/main.zig" }, -}; - -pub fn build(b: *std.build) void { +pub fn build(b: *std.Build) void { // Standard target options allows the person running `zig build` to choose // what target to build for. Here we do not override the defaults, which // means any target is allowed, and the default is native. Other options @@ -23,8 +15,10 @@ pub fn build(b: *std.build) void { .optimize = optimize, }); - exe.addAnonymousModule("clap", clap_module); - exe.addAnonymousModule("csv", csv_module); + const dep_curl = b.dependency("clap", .{}); + exe.root_module.addImport("clap", dep_curl.module("clap")); + const dep_csv = b.dependency("zig-csv", .{}); + exe.root_module.addImport("csv", dep_csv.module("zig-csv")); b.installArtifact(exe); diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..1c3d8cc --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,40 @@ +.{ + .name = "csv2json", + // This is a [Semantic Version](https://semver.org/). + // In a future version of Zig it will be used for package deduplication. + .version = "0.0.0", + + // This field is optional. + // This is currently advisory only; Zig does not yet do anything + // with this value. + //.minimum_zig_version = "0.11.0", + + // This field is optional. + // Each dependency must either provide a `url` and `hash`, or a `path`. + // `zig build --fetch` can be used to fetch all dependencies of a package, recursively. + // Once all dependencies are fetched, `zig build` no longer requires + // internet connectivity. + .dependencies = .{ + .@"zig-csv" = .{ + .url = "https://github.com/rofrol/beho--zig-csv/archive/c8bbbeb894ef6835a4c0341fc2326b476a3e86d4.tar.gz", + .hash = "1220f2a95848b5edaaf04bf6a32797ae05d39a5640a3384b3b40681f2931ea3698e7", + }, + .clap = .{ + .url = "https://github.com/Hejsil/zig-clap/archive/209ba4da76e46412acfe18f711cb0b041ff37f10.tar.gz", + .hash = "12200103e7b4a0cb162f2912df4fe97914024a25b5c9fcce6ea4119744f3f2a7f24e", + }, + }, + .paths = .{ + // This makes *all* files, recursively, included in this package. It is generally + // better to explicitly list the files and directories instead, to insure that + // fetching from tarballs, file system paths, and version control all result + // in the same contents hash. + "", + // For example... + //"build.zig", + //"build.zig.zon", + //"src", + //"LICENSE", + //"README.md", + }, +} diff --git a/lib/zig-clap b/lib/zig-clap deleted file mode 160000 index f49b947..0000000 --- a/lib/zig-clap +++ /dev/null @@ -1 +0,0 @@ -Subproject commit f49b94700e0761b7514abdca0e4f0e7f3f938a93 diff --git a/lib/zig-csv b/lib/zig-csv deleted file mode 160000 index 72f9a41..0000000 --- a/lib/zig-csv +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 72f9a41d4d24f6d5cc0333b85d5c45cebce10848 diff --git a/src/converter.zig b/src/converter.zig index 71395fc..b2051bc 100644 --- a/src/converter.zig +++ b/src/converter.zig @@ -15,7 +15,7 @@ pub const Converter = struct { // Initialize the converter. pub fn init(allocator: std.mem.Allocator, writer: anytype, rowBufSize: u32) !Self { - var s = Converter{ + const s = Converter{ .allocator = allocator, .out = std.io.bufferedWriter(writer), .csvBuf = try allocator.alloc(u8, rowBufSize), @@ -54,11 +54,11 @@ pub const Converter = struct { if (isFirst) { // Copy all the fields in the first row to a separate buffer // to be retained throughout the lifetime of the program. - std.mem.copy(u8, self.hdrBuf[f..ln], val); + std.mem.copyForwards(u8, self.hdrBuf[f..ln], val); try fields.append(self.hdrBuf[f..ln]); } else { // Row buffer can be discarded after processing each individual row. - std.mem.copy(u8, self.rowBuf[f..ln], val); + std.mem.copyForwards(u8, self.rowBuf[f..ln], val); try fields.append(self.rowBuf[f..ln]); } diff --git a/src/main.zig b/src/main.zig index 1f03ca0..63be45a 100644 --- a/src/main.zig +++ b/src/main.zig @@ -44,6 +44,9 @@ pub fn printStats(start: u64, end: u64, lines: u64) void { } pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const params = comptime clap.parseParamsComptime( \\ -h, --help \\ Display this help and exit. @@ -59,19 +62,21 @@ pub fn main() !void { }; // Help / usage. - var args = clap.parse(clap.Help, ¶ms, parsers, .{}) catch |err| { + var args = clap.parse(clap.Help, ¶ms, parsers, .{ + .allocator = gpa.allocator(), + }) catch |err| { std.debug.print("Invalid flags: {s}\n\n", .{@errorName(err)}); try printHelp(¶ms); - std.os.exit(1); + std.process.exit(1); }; defer args.deinit(); if (args.args.help != 0) { try printHelp(¶ms); - std.os.exit(1); + std.process.exit(1); } - var bufSize: u32 = 4096; + const bufSize: u32 = 4096; if (args.args.buf) |b| std.debug.print("Buffer size: {d}\n", .{b}); @@ -84,14 +89,14 @@ pub fn main() !void { var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); defer arena.deinit(); - var allocator = arena.allocator(); + const allocator = arena.allocator(); var conv = try Converter.init(allocator, std.io.getStdOut().writer(), bufSize); const lines = try conv.convert(fPath); printStats(start, timer.read(), lines); - std.os.exit(0); + std.process.exit(0); } // No flags.