mirror of
https://github.com/Tilo-K/csvu.git
synced 2026-01-09 00:01:00 +00:00
Feature/zig 0.15 upgrade (#1)
* fix: adjust build and stdout * fix: compiles on 0.15 * fix: fix io changes * fix: update github action zig version
This commit is contained in:
2
.github/workflows/release.yaml
vendored
2
.github/workflows/release.yaml
vendored
@@ -51,7 +51,7 @@ jobs:
|
|||||||
- name: Install Zig
|
- name: Install Zig
|
||||||
uses: mlugg/setup-zig@v2
|
uses: mlugg/setup-zig@v2
|
||||||
with:
|
with:
|
||||||
version: 0.14.0
|
version: 0.15.0
|
||||||
|
|
||||||
- name: Build for ${{ matrix.triple }}
|
- name: Build for ${{ matrix.triple }}
|
||||||
run: |
|
run: |
|
||||||
|
|||||||
25
build.zig
25
build.zig
@@ -15,8 +15,7 @@ pub fn build(b: *std.Build) void {
|
|||||||
// set a preferred release mode, allowing the user to decide how to optimize.
|
// set a preferred release mode, allowing the user to decide how to optimize.
|
||||||
const optimize = b.standardOptimizeOption(.{});
|
const optimize = b.standardOptimizeOption(.{});
|
||||||
|
|
||||||
const lib = b.addStaticLibrary(.{
|
const lib = b.addModule("csvu", .{
|
||||||
.name = "csvu",
|
|
||||||
// In this case the main source file is merely a path, however, in more
|
// In this case the main source file is merely a path, however, in more
|
||||||
// complicated build scripts, this could be a generated file.
|
// complicated build scripts, this could be a generated file.
|
||||||
.root_source_file = b.path("src/root.zig"),
|
.root_source_file = b.path("src/root.zig"),
|
||||||
@@ -27,13 +26,11 @@ pub fn build(b: *std.Build) void {
|
|||||||
// This declares intent for the library to be installed into the standard
|
// This declares intent for the library to be installed into the standard
|
||||||
// location when the user invokes the "install" step (the default step when
|
// location when the user invokes the "install" step (the default step when
|
||||||
// running `zig build`).
|
// running `zig build`).
|
||||||
b.installArtifact(lib);
|
//b.installArtifact(lib);
|
||||||
|
|
||||||
const exe = b.addExecutable(.{
|
const exe = b.addExecutable(.{
|
||||||
.name = "csvu",
|
.name = "csvu",
|
||||||
.root_source_file = b.path("src/main.zig"),
|
.root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "csvu", .module = lib }} }),
|
||||||
.target = target,
|
|
||||||
.optimize = optimize,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// This declares intent for the executable to be installed into the
|
// This declares intent for the executable to be installed into the
|
||||||
@@ -67,17 +64,21 @@ pub fn build(b: *std.Build) void {
|
|||||||
// Creates a step for unit testing. This only builds the test executable
|
// Creates a step for unit testing. This only builds the test executable
|
||||||
// but does not run it.
|
// but does not run it.
|
||||||
const lib_unit_tests = b.addTest(.{
|
const lib_unit_tests = b.addTest(.{
|
||||||
.root_source_file = b.path("src/root.zig"),
|
.root_module = b.createModule(.{
|
||||||
.target = target,
|
.root_source_file = b.path("src/root.zig"),
|
||||||
.optimize = optimize,
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
|
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
|
||||||
|
|
||||||
const exe_unit_tests = b.addTest(.{
|
const exe_unit_tests = b.addTest(.{
|
||||||
.root_source_file = b.path("src/main.zig"),
|
.root_module = b.createModule(.{
|
||||||
.target = target,
|
.root_source_file = b.path("src/main.zig"),
|
||||||
.optimize = optimize,
|
.target = target,
|
||||||
|
.optimize = optimize,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
|
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
|
||||||
|
|||||||
62
src/csv.zig
62
src/csv.zig
@@ -74,23 +74,24 @@ const CsvFile = struct {
|
|||||||
|
|
||||||
pub fn deinit(self: *CsvFile) void {
|
pub fn deinit(self: *CsvFile) void {
|
||||||
for (self.header.items) |col| self.alloc.free(col);
|
for (self.header.items) |col| self.alloc.free(col);
|
||||||
self.header.deinit();
|
self.header.deinit(self.alloc);
|
||||||
|
|
||||||
for (self.entries.items) |entry| {
|
for (self.entries.items) |entry_c| {
|
||||||
|
var entry = entry_c;
|
||||||
for (entry.items) |col| self.alloc.free(col);
|
for (entry.items) |col| self.alloc.free(col);
|
||||||
entry.deinit();
|
entry.deinit(self.alloc);
|
||||||
}
|
}
|
||||||
self.entries.deinit();
|
self.entries.deinit(self.alloc);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn printTable(file: CsvFile) !void {
|
pub fn printTable(file: CsvFile) !void {
|
||||||
const stdout_file = std.io.getStdOut().writer();
|
var stdout_buf: [1024]u8 = undefined;
|
||||||
var bw = std.io.bufferedWriter(stdout_file);
|
var stdout_writer = std.fs.File.stdout().writer(&stdout_buf);
|
||||||
const stdout = bw.writer();
|
var stdout = &stdout_writer.interface;
|
||||||
|
|
||||||
defer {
|
defer {
|
||||||
_ = bw.flush() catch null;
|
_ = stdout.flush() catch null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
@@ -141,7 +142,7 @@ pub fn printTable(file: CsvFile) !void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_ = try stdout.writeAll("\n");
|
_ = try stdout.writeAll("\n");
|
||||||
_ = try bw.flush();
|
_ = try stdout.flush();
|
||||||
|
|
||||||
for (0..complete_length) |_| {
|
for (0..complete_length) |_| {
|
||||||
try stdout.print("-", .{});
|
try stdout.print("-", .{});
|
||||||
@@ -149,26 +150,26 @@ pub fn printTable(file: CsvFile) !void {
|
|||||||
try stdout.print("\n", .{});
|
try stdout.print("\n", .{});
|
||||||
|
|
||||||
for (file.entries.items) |entry| {
|
for (file.entries.items) |entry| {
|
||||||
var out_line = std.ArrayList(u8).init(alloc);
|
var out_line = std.ArrayList(u8){};
|
||||||
defer out_line.deinit();
|
defer out_line.deinit(alloc);
|
||||||
|
|
||||||
try out_line.appendSlice("|");
|
try out_line.appendSlice(alloc, "|");
|
||||||
|
|
||||||
for (0..col_nums) |i| {
|
for (0..col_nums) |i| {
|
||||||
const out = entry.items[i];
|
const out = entry.items[i];
|
||||||
const missing = col_sizes[i] - out.len;
|
const missing = col_sizes[i] - out.len;
|
||||||
|
|
||||||
try out_line.appendSlice(out);
|
try out_line.appendSlice(alloc, out);
|
||||||
|
|
||||||
for (0..missing) |_| {
|
for (0..missing) |_| {
|
||||||
try out_line.appendSlice(" ");
|
try out_line.appendSlice(alloc, " ");
|
||||||
}
|
}
|
||||||
try out_line.appendSlice("|");
|
try out_line.appendSlice(alloc, "|");
|
||||||
}
|
}
|
||||||
try out_line.appendSlice("\n");
|
try out_line.appendSlice(alloc, "\n");
|
||||||
|
|
||||||
_ = try stdout.writeAll(out_line.items);
|
_ = try stdout.writeAll(out_line.items);
|
||||||
_ = try bw.flush();
|
_ = try stdout.flush();
|
||||||
}
|
}
|
||||||
for (0..complete_length) |_| {
|
for (0..complete_length) |_| {
|
||||||
try stdout.print("-", .{});
|
try stdout.print("-", .{});
|
||||||
@@ -177,25 +178,32 @@ pub fn printTable(file: CsvFile) !void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn loadFile(filepath: []const u8, alloc: std.mem.Allocator) !CsvFile {
|
pub fn loadFile(filepath: []const u8, alloc: std.mem.Allocator) !CsvFile {
|
||||||
var file = try std.fs.cwd().openFile(filepath, .{});
|
var file_buf: [4096]u8 = undefined;
|
||||||
defer file.close();
|
|
||||||
|
|
||||||
var buf_reader = std.io.bufferedReader(file.reader());
|
var file = try std.fs.cwd().openFile(filepath, .{ .mode = .read_write });
|
||||||
var in_stream = buf_reader.reader();
|
defer file.close();
|
||||||
var buffer: [4096]u8 = undefined;
|
var file_reader = file.reader(&file_buf);
|
||||||
|
const in_stream = &file_reader.interface;
|
||||||
|
|
||||||
var readHeader = false;
|
var readHeader = false;
|
||||||
var headerList: std.ArrayList([]const u8) = undefined;
|
var headerList: std.ArrayList([]const u8) = undefined;
|
||||||
var entries = std.ArrayList(std.ArrayList([]const u8)).init(alloc);
|
var entries = std.ArrayList(std.ArrayList([]const u8)){};
|
||||||
var delimiter: u8 = ' ';
|
var delimiter: u8 = ' ';
|
||||||
|
|
||||||
while (try in_stream.readUntilDelimiterOrEof(&buffer, '\n')) |line| {
|
while (true) {
|
||||||
|
const line = in_stream.takeDelimiterExclusive('\n') catch |err| {
|
||||||
|
if (err == error.EndOfStream) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return err;
|
||||||
|
};
|
||||||
|
|
||||||
if (delimiter == ' ') {
|
if (delimiter == ' ') {
|
||||||
delimiter = try determineDelimiter(line);
|
delimiter = try determineDelimiter(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
const del = delimiter;
|
const del = delimiter;
|
||||||
var entr = std.ArrayList([]const u8).init(alloc);
|
var entr = std.ArrayList([]const u8){};
|
||||||
var splitIt = std.mem.splitSequence(u8, line, &[_]u8{del});
|
var splitIt = std.mem.splitSequence(u8, line, &[_]u8{del});
|
||||||
|
|
||||||
while (splitIt.next()) |part| {
|
while (splitIt.next()) |part| {
|
||||||
@@ -208,7 +216,7 @@ pub fn loadFile(filepath: []const u8, alloc: std.mem.Allocator) !CsvFile {
|
|||||||
std.mem.copyForwards(u8, res2, res);
|
std.mem.copyForwards(u8, res2, res);
|
||||||
alloc.free(dest);
|
alloc.free(dest);
|
||||||
|
|
||||||
_ = try entr.append(res2);
|
_ = try entr.append(alloc, res2);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!readHeader) {
|
if (!readHeader) {
|
||||||
@@ -216,7 +224,7 @@ pub fn loadFile(filepath: []const u8, alloc: std.mem.Allocator) !CsvFile {
|
|||||||
readHeader = true;
|
readHeader = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
_ = try entries.append(entr);
|
_ = try entries.append(alloc, entr);
|
||||||
}
|
}
|
||||||
|
|
||||||
return CsvFile{ .entries = entries, .header = headerList, .alloc = alloc };
|
return CsvFile{ .entries = entries, .header = headerList, .alloc = alloc };
|
||||||
|
|||||||
12
src/main.zig
12
src/main.zig
@@ -2,9 +2,9 @@ const std = @import("std");
|
|||||||
const csv = @import("csv.zig");
|
const csv = @import("csv.zig");
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
const stdout_file = std.io.getStdOut().writer();
|
var stdout_buf: [1024]u8 = undefined;
|
||||||
var bw = std.io.bufferedWriter(stdout_file);
|
var stdout_writer = std.fs.File.stdout().writer(&stdout_buf);
|
||||||
const stdout = bw.writer();
|
var stdout = &stdout_writer.interface;
|
||||||
|
|
||||||
var allocator = std.heap.GeneralPurposeAllocator(.{}){};
|
var allocator = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
defer _ = allocator.deinit();
|
defer _ = allocator.deinit();
|
||||||
@@ -23,7 +23,7 @@ pub fn main() !void {
|
|||||||
|
|
||||||
if (std.mem.eql(u8, filepath, "")) {
|
if (std.mem.eql(u8, filepath, "")) {
|
||||||
_ = try stdout.write("No file specified");
|
_ = try stdout.write("No file specified");
|
||||||
_ = try bw.flush();
|
_ = try stdout.flush();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,8 +33,8 @@ pub fn main() !void {
|
|||||||
const valid = file.isValid();
|
const valid = file.isValid();
|
||||||
if (!valid) return;
|
if (!valid) return;
|
||||||
|
|
||||||
_ = try bw.flush();
|
_ = try stdout.flush();
|
||||||
|
|
||||||
try csv.printTable(file);
|
try csv.printTable(file);
|
||||||
_ = try bw.flush();
|
_ = try stdout.flush();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user