Skip to content

Commit

Permalink
update to master
Browse files Browse the repository at this point in the history
  • Loading branch information
rofrol committed Mar 22, 2024
1 parent d01ddd8 commit aea92f7
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 29 deletions.
6 changes: 0 additions & 6 deletions .gitmodules

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]:knadh/csv2json.git`
- `git clone [email protected]:knadh/csv2json.git`
- `cd csv2json && zig build`
- The binary will be in `zig-out/bin`

Expand Down
16 changes: 5 additions & 11 deletions build.zig
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);

Expand Down
40 changes: 40 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -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",
},
}
1 change: 0 additions & 1 deletion lib/zig-clap
Submodule zig-clap deleted from f49b94
1 change: 0 additions & 1 deletion lib/zig-csv
Submodule zig-csv deleted from 72f9a4
6 changes: 3 additions & 3 deletions src/converter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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]);
}

Expand Down
17 changes: 11 additions & 6 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -59,19 +62,21 @@ pub fn main() !void {
};

// Help / usage.
var args = clap.parse(clap.Help, &params, parsers, .{}) catch |err| {
var args = clap.parse(clap.Help, &params, parsers, .{
.allocator = gpa.allocator(),
}) catch |err| {
std.debug.print("Invalid flags: {s}\n\n", .{@errorName(err)});
try printHelp(&params);
std.os.exit(1);
std.process.exit(1);
};
defer args.deinit();

if (args.args.help != 0) {
try printHelp(&params);
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});

Expand All @@ -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.
Expand Down

0 comments on commit aea92f7

Please sign in to comment.