6 Commits

Author SHA1 Message Date
Tilo
1b5ad2bc40 Upgrade csvu to Zig 0.16 (#2)
* Port source to Zig 0.16

* Declare Zig 0.16 toolchain

* ci: add pr verification
2026-05-31 18:27:09 +02:00
65d157852a feat: some optimizations 2025-10-05 21:13:54 +02:00
Tilo
d17540cb33 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
2025-10-05 18:40:15 +02:00
0b60592e61 fix: fix memory leaks 2025-06-26 21:18:53 +02:00
b14907b3cc feat: ignore ds store 2025-06-25 11:53:00 +02:00
12fe0287da feat: fix macos support 2025-06-25 11:52:17 +02:00
10 changed files with 173 additions and 83 deletions

2
.gitattributes vendored Normal file
View File

@@ -0,0 +1,2 @@
*.zig text eol=lf
*.zig.zon text eol=lf

37
.github/workflows/ci.yaml vendored Normal file
View File

@@ -0,0 +1,37 @@
name: CI
on:
pull_request:
branches:
- master
push:
branches:
- master
permissions:
contents: read
jobs:
verify:
name: Format, Test, Build
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install Zig
uses: mlugg/setup-zig@v2
with:
version: 0.16.0
- name: Check formatting
run: zig fmt --check .
- name: Run tests
run: zig build test
- name: Build
run: zig build
- name: Build release mode
run: zig build -Doptimize=ReleaseSafe

View File

@@ -51,20 +51,19 @@ jobs:
- name: Install Zig
uses: mlugg/setup-zig@v2
with:
version: 0.14.0
version: 0.16.0
- name: Build for ${{ matrix.triple }}
run: |
zig build-exe "${SRC}" \
zig build \
-Dtarget=${{ matrix.triple }} \
-O ReleaseSafe \
-femit-bin="${BINARY_NAME}${{ matrix.ext }}"
-Doptimize=ReleaseSafe
- name: Package binary
run: |
mkdir -p artifacts
zip -j artifacts/"${BINARY_NAME}-${{ github.ref_name }}-${{ matrix.triple }}.zip" \
"${BINARY_NAME}${{ matrix.ext }}"
"zig-out/bin/${BINARY_NAME}${{ matrix.ext }}"
- name: Upload release asset
uses: actions/upload-release-asset@v1

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
.zig-cache
zig-out
.DS_Store

View File

@@ -86,7 +86,7 @@ The csvu is a dynamic CSV utility designed to streamline data handling. It effec
Before getting started with csvu, ensure your runtime environment meets the following requirements:
- **Programming Language:** Zig
- **Zig Version:** 0.14.0 or later
- **Zig Version:** 0.16.0 or later
### Installation

View File

@@ -15,8 +15,7 @@ pub fn build(b: *std.Build) void {
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
const lib = b.addStaticLibrary(.{
.name = "csvu",
const lib = b.addModule("csvu", .{
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.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
// location when the user invokes the "install" step (the default step when
// running `zig build`).
b.installArtifact(lib);
//b.installArtifact(lib);
const exe = b.addExecutable(.{
.name = "csvu",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "csvu", .module = lib }} }),
});
// This declares intent for the executable to be installed into the
@@ -67,17 +64,31 @@ pub fn build(b: *std.Build) void {
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const lib_unit_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
}),
});
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
const csv_unit_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/csv.zig"),
.target = target,
.optimize = optimize,
}),
});
const run_csv_unit_tests = b.addRunArtifact(csv_unit_tests);
const exe_unit_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
}),
});
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
@@ -87,5 +98,6 @@ pub fn build(b: *std.Build) void {
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
test_step.dependOn(&run_csv_unit_tests.step);
test_step.dependOn(&run_exe_unit_tests.step);
}

View File

@@ -17,7 +17,7 @@
// This field is optional.
// This is currently advisory only; Zig does not yet do anything
// with this value.
//.minimum_zig_version = "0.11.0",
.minimum_zig_version = "0.16.0",
// This field is optional.
// Each dependency must either provide a `url` and `hash`, or a `path`.

View File

@@ -1,20 +1,9 @@
const std = @import("std");
const term = @import("term.zig");
const CsvError = error{
NoDelimiterFound,
};
fn concat(one: []const u8, two: []const u8) ![]const u8 {
const allocator = std.heap.page_allocator;
var result = try allocator.alloc(u8, one.len + two.len);
std.mem.copyForwards(u8, result[0..], one);
std.mem.copyForwards(u8, result[one.len..], two);
return result;
}
fn contains(arr: []const u8, target: u8) bool {
for (arr) |element| {
if (element == target) {
@@ -26,10 +15,13 @@ fn contains(arr: []const u8, target: u8) bool {
}
pub fn determineDelimiter(str: []const u8) !u8 {
const alloc = std.heap.page_allocator;
var allocator = std.heap.DebugAllocator(.{}){};
defer _ = allocator.deinit();
const alloc = allocator.allocator();
const possibleDelimiter = [_]u8{ ',', ';', '\t', '|' };
var countMap = std.AutoHashMap(u8, u32).init(alloc);
defer countMap.deinit();
for (possibleDelimiter) |del| {
try countMap.put(del, 0);
@@ -66,6 +58,8 @@ pub fn determineDelimiter(str: []const u8) !u8 {
const CsvFile = struct {
header: std.ArrayList([]const u8),
entries: std.ArrayList(std.ArrayList([]const u8)),
alloc: std.mem.Allocator,
p_lines: std.ArrayList([]const u8),
pub fn isValid(self: CsvFile) bool {
const colNum = self.header.items.len;
@@ -77,24 +71,37 @@ const CsvFile = struct {
return true;
}
pub fn deinit(self: *CsvFile) void {
for (self.p_lines.items) |line| {
self.alloc.free(line);
}
self.p_lines.deinit(self.alloc);
self.header.deinit(self.alloc);
for (self.entries.items) |entry_c| {
var entry = entry_c;
entry.deinit(self.alloc);
}
self.entries.deinit(self.alloc);
}
};
pub fn printTable(file: CsvFile) !void {
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
pub fn printTable(io: std.Io, file: CsvFile) !void {
var stdout_buf: [1024]u8 = undefined;
var stdout_writer = std.Io.File.stdout().writerStreaming(io, &stdout_buf);
var stdout = &stdout_writer.interface;
defer {
_ = bw.flush() catch null;
_ = stdout.flush() catch null;
}
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var gpa = std.heap.DebugAllocator(.{}){};
defer {
_ = gpa.deinit();
}
const alloc = gpa.allocator();
const dimensions = try term.getTerminalDimensions();
const col_nums = file.header.items.len;
const col_sizes = try alloc.alloc(usize, col_nums);
defer alloc.free(col_sizes);
@@ -111,8 +118,6 @@ pub fn printTable(file: CsvFile) !void {
}
}
_ = dimensions;
var complete_length = col_nums + 1;
for (col_sizes) |col_size| {
complete_length += col_size;
@@ -136,7 +141,6 @@ pub fn printTable(file: CsvFile) !void {
}
_ = try stdout.writeAll("\n");
_ = try bw.flush();
for (0..complete_length) |_| {
try stdout.print("-", .{});
@@ -144,22 +148,25 @@ pub fn printTable(file: CsvFile) !void {
try stdout.print("\n", .{});
for (file.entries.items) |entry| {
var out_line: []const u8 = "|";
var out_line: std.ArrayList(u8) = .empty;
defer out_line.deinit(alloc);
try out_line.appendSlice(alloc, "|");
for (0..col_nums) |i| {
const out = entry.items[i];
const missing = col_sizes[i] - out.len;
out_line = try concat(out_line, out);
try out_line.appendSlice(alloc, out);
for (0..missing) |_| {
out_line = try concat(out_line, " ");
try out_line.appendSlice(alloc, " ");
}
out_line = try concat(out_line, "|");
try out_line.appendSlice(alloc, "|");
}
out_line = try concat(out_line, "\n");
_ = try stdout.writeAll(out_line);
_ = try bw.flush();
try out_line.appendSlice(alloc, "\n");
_ = try stdout.writeAll(out_line.items);
}
for (0..complete_length) |_| {
try stdout.print("-", .{});
@@ -167,35 +174,44 @@ pub fn printTable(file: CsvFile) !void {
try stdout.print("\n", .{});
}
pub fn loadFile(filepath: []const u8) !CsvFile {
const alloc = std.heap.page_allocator;
var file = try std.fs.cwd().openFile(filepath, .{});
defer file.close();
pub fn loadFile(io: std.Io, filepath: []const u8, alloc: std.mem.Allocator) !CsvFile {
var file_buf: [4096]u8 = undefined;
var buf_reader = std.io.bufferedReader(file.reader());
var in_stream = buf_reader.reader();
var buffer: [4096]u8 = undefined;
var file = try std.Io.Dir.cwd().openFile(io, filepath, .{ .mode = .read_write });
defer file.close(io);
var file_reader = file.readerStreaming(io, &file_buf);
const in_stream = &file_reader.interface;
var readHeader = false;
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)) = .empty;
var lines: std.ArrayList([]const u8) = .empty;
var delimiter: u8 = ' ';
while (try in_stream.readUntilDelimiterOrEof(&buffer, '\n')) |line| {
while (true) {
const line = in_stream.takeDelimiter('\n') catch |err| switch (err) {
error.ReadFailed => return file_reader.err.?,
else => return err,
};
if (line == null) break;
if (delimiter == ' ') {
delimiter = try determineDelimiter(line);
delimiter = try determineDelimiter(line.?);
}
const del = delimiter;
var entr = std.ArrayList([]const u8).init(alloc);
var splitIt = std.mem.splitSequence(u8, line, &[_]u8{del});
var entr: std.ArrayList([]const u8) = .empty;
var splitIt = std.mem.splitSequence(u8, line.?, &[_]u8{del});
while (splitIt.next()) |part| {
const dest = try alloc.alloc(u8, part.len);
lines.append(alloc, dest) catch unreachable;
std.mem.copyForwards(u8, dest, part);
const res = std.mem.trim(u8, dest, &[_]u8{ '\n', '\t', '\r', ' ' });
_ = try entr.append(res);
_ = try entr.append(alloc, res);
}
if (!readHeader) {
@@ -203,20 +219,21 @@ pub fn loadFile(filepath: []const u8) !CsvFile {
readHeader = true;
continue;
}
_ = try entries.append(entr);
_ = try entries.append(alloc, entr);
}
return CsvFile{ .entries = entries, .header = headerList };
return CsvFile{ .entries = entries, .header = headerList, .alloc = alloc, .p_lines = lines };
}
test "Determine delimiter" {
const del = try determineDelimiter("this,is,a,test");
std.testing.expect(del == ',');
try std.testing.expect(del == ',');
const del2 = try determineDelimiter("th#is; is,a; test; with,many;symbols");
std.testing.expect(del2 == ';');
try std.testing.expect(del2 == ';');
determineDelimiter("This does not have an delimiter") catch |err| {
try std.testing.expect(err == CsvError.NoDelimiterFound);
};
try std.testing.expectError(
CsvError.NoDelimiterFound,
determineDelimiter("This does not have an delimiter"),
);
}

View File

@@ -1,13 +1,18 @@
const std = @import("std");
const csv = @import("csv.zig");
pub fn main() !void {
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
const alloc = std.heap.page_allocator;
pub fn main(init: std.process.Init) !void {
var stdout_buf: [1024]u8 = undefined;
var stdout_writer = std.Io.File.stdout().writerStreaming(init.io, &stdout_buf);
var stdout = &stdout_writer.interface;
var allocator = std.heap.DebugAllocator(.{}){};
defer _ = allocator.deinit();
const alloc = allocator.allocator();
var args = try init.minimal.args.iterateAllocator(alloc);
defer args.deinit();
var args = try std.process.ArgIterator.initWithAllocator(alloc);
_ = args.next();
var filepath: [:0]const u8 = "";
@@ -18,16 +23,18 @@ pub fn main() !void {
if (std.mem.eql(u8, filepath, "")) {
_ = try stdout.write("No file specified");
_ = try bw.flush();
_ = try stdout.flush();
return;
}
var file = try csv.loadFile(filepath);
var file = try csv.loadFile(init.io, filepath, alloc);
defer file.deinit();
const valid = file.isValid();
if (!valid) return;
_ = try bw.flush();
_ = try stdout.flush();
try csv.printTable(file);
_ = try bw.flush();
try csv.printTable(init.io, file);
_ = try stdout.flush();
}

View File

@@ -9,15 +9,18 @@ pub fn getTerminalDimensions() !Dimensions {
const os_tag = builtin.os.tag;
switch (os_tag) {
.linux, .macos, .freebsd, .openbsd, .netbsd => {
.linux, .freebsd, .openbsd, .netbsd => {
return try getUnixTerminalDimensions();
},
.windows => {
return try getWindowsTerminalDimensions();
},
.macos => {
return try getMacOsTerminalDimensions();
},
else => {
return Error.UnsupportedOs;
}
},
}
}
@@ -32,6 +35,18 @@ fn getUnixTerminalDimensions() !Dimensions {
return Dimensions{ .width = size.ws_col, .height = size.ws_row };
}
pub fn getMacOsTerminalDimensions() !Dimensions {
var ws: std.posix.winsize = undefined;
const fd = std.io.getStdOut().handle;
_ = std.c.ioctl(fd, std.posix.T.IOCGWINSZ, @intFromPtr(&ws));
return Dimensions{
.width = ws.col,
.height = ws.row,
};
}
fn getWindowsTerminalDimensions() !Dimensions {
const win = std.os.windows;
const handle = win.GetStdHandle(win.STD_OUTPUT_HANDLE) catch return Error.ErrorFetchingDimensions;