Zig: Reading file line by line and parsing results

Created on 20 Feb 2020  ยท  24Comments  ยท  Source: ziglang/zig

I'm trying to read /etc/os-release on Linux using Zig. This is what I've gotten:

pub func getOSReleaseEntry(key: [*]u8) [*]u8 {
  var osrelease = try fs.openFileAbsoluteC("/etc/os-release", fs.file.OpenFlags{
    .read = true,
    .write = false
  });
  defer osrelease.close();
  const stream = &osrelease.inStream().stream;
  const buff = try std.Buffer.initSize(null, 0);
  defer buff.deinit();
  var line = io.readLineFrom(stream, buff);
  while (line != null) {
    // TODO: split line by equals
    line = io.readLineFrom(stream, buff);
  }
  return undefined;
}

Anyone know if this is correct?

question

Most helpful comment

We can make an exception here to work around the school's content censorship.

That said, you'll need to figure out a way to run this code yourself and test it, and update this question to be more actionable from members of the community. Put yourself in the shoes of someone trying to help- what would your response be?

A good question will have something like this:

  • Here's an explanation of what I'm trying to do, my use case. (In your question, this part is fairly clear. :+1: )
  • Here's what I tried. (You also have this component :+1: )
  • Here's what happens when I try this. (Your question is missing this part)
  • Here's what I would expect to happen instead, or what I would want to happen.

If you're not able to run zig code, you'll either have to wait until you have access to do so, or figure out a way to subvert your school's blocking system to gain access to ability to the testing environment that you need. I promise you that it is possible to run zig code at your school without a human proxy.

All 24 comments

Thanks for trying Zig! The issue's page is for Zig specific issues only (bugs in the compiler, standard library, etc), and not for user code review. Feel free to post on Zig's IRC channel or the other community centers, some of which are listed here: https://github.com/ziglang/zig/wiki/Community

The IRC is blocked, Discord is blocked, Reddit is blocked.

We can make an exception here to work around the school's content censorship.

That said, you'll need to figure out a way to run this code yourself and test it, and update this question to be more actionable from members of the community. Put yourself in the shoes of someone trying to help- what would your response be?

A good question will have something like this:

  • Here's an explanation of what I'm trying to do, my use case. (In your question, this part is fairly clear. :+1: )
  • Here's what I tried. (You also have this component :+1: )
  • Here's what happens when I try this. (Your question is missing this part)
  • Here's what I would expect to happen instead, or what I would want to happen.

If you're not able to run zig code, you'll either have to wait until you have access to do so, or figure out a way to subvert your school's blocking system to gain access to ability to the testing environment that you need. I promise you that it is possible to run zig code at your school without a human proxy.

I'm able to compile but I don't know how to read the file line by line and split it by the equals sign. I'm trying to pull the info from /etc/os-release for my OS's API. I've got GitPod set up for development.

Maybe Godbolt's 'execute binary' ability would be of value.

Why? And what's that?

I opened #4514 which removes io.readLineFrom. What you want is to look at some of the functions of InStream, such as readUntilDelimiterBuffer, readUntilDelimiterAlloc, or readUntilDelimiterOrEof. If you search the standard library for these functions you can see some uses of them.

Also useful is to look at people's Advent-Of-Code repositories, which you can find in various places on the Internet, since they nearly all to text-processing of input streams.

Ok but also don't those need the Allocator? I've only seen references to the Allocator in the tests but how do I access it when not using tests?

pub func getReleaseEntry(alloc: *mem.Allocator, key: [*]u8) [*]u8 {
  var osrelease = try fs.openFileAbsoluteC("/etc/os-release", fs.file.OpenFlags{
    .read = true,
    .write = false
  });
  defer osrelease.close();
  const stream = &osrelease.inStream().stream;
  var line = stream.readUntilDelimiterAlloc(alloc, '\n', 1024);
  while (line != null) {
    // TODO: split line by equals
    line = stream.readUntilDelimiterAlloc(alloc, '\n', 1024);
  }
  return undefined;
}

Alright, I got this now I just need to split line by =.

https://godbolt.org/z/dDpASU

I do not have time to correct the compilation errors.

Godbolt is an online source -> assembly viewer tool that has some execution features.

Ok but also don't those need the Allocator?

Look at the parameters and doc comments for each the function.

Alright, thank you. I've fixed some of the errors which now the code looks like this:

pub fn getReleaseEntry(alloc: *mem.Allocator, key: [*]const u8) ?[*]u8 {
  const osrelease = fs.openFileAbsoluteC("/etc/os-release", fs.File.OpenFlags{
    .read = true,
    .write = false
  }) catch unreachable;
  defer osrelease.close();
  const stream = &osrelease.inStream().stream;
  var line = stream.readUntilDelimiterAlloc(alloc, '\n', 1024) catch |err| return undefined;
  while (true) {
    var splitidx = ascii.indexOfIgnoreCase(line, "=").?;
    splitidx = splitidx - 1;
    const linekey = line[0..splitidx];
    const value = line[(splitidx + 1) .. line.len];
    if (mem.eql(u8, linekey, key)) {
      return value;
    }
    line = stream.readUntilDelimiterAlloc(alloc, '\n', 1024) catch |err| return undefined;
  }
  return undefined;
}

However, I cannot fix the error with compairing linekey and key.

./src/sys.zig:22:30: error: expected type '[]const u8', found '[*]const u8'
    if (mem.eql(u8, linekey, key)) {
                             ^
./src/sys.zig:22:16: note: referenced here
    if (mem.eql(u8, linekey, key)) {

Is Stackoverflow blocked too? I would say this question would fit perfectly there, although it is quite hard to have a discussion with follow up questions.

https://stackoverflow.com/questions/tagged/zig

My stack overflow account was banned years ago for asking too many questions.

    var splitidx = ascii.indexOfIgnoreCase(line, "=").?;
    const linekey = line[0..(splitidx - 1)];
    const value = line[(splitidx + 1) .. line.len];
    if (mem.eql(u8, linekey, key)) {
      return value;
    }

This works but the return does not work.

Semantic Analysis [632/994] ./src/sys.zig:22:14: error: expected type '[*]u8', found '[]u8'
      return value;

So it built but failed the test, https://github.com/ExpidusOS/api/blob/master/src/sys.zig

Test [1/1] test "getReleaseEntry"...VERSION = u8@aaaaaaaaaaaaaaaa
error - detected leaked allocations without matching free: 12

/workspace/api/zig-linux-x86_64-0.5.0+1483ae37f/lib/zig/std/special/test_runner.zig:52:46: 0x22ad2d in std.special.main (test)
                error.Leak => std.debug.panic("", .{}),
                                             ^
/workspace/api/zig-linux-x86_64-0.5.0+1483ae37f/lib/zig/std/start.zig:250:37: 0x20564b in std.start.posixCallMainAndExit (test)
            const result = root.main() catch |err| {
                                    ^
/workspace/api/zig-linux-x86_64-0.5.0+1483ae37f/lib/zig/std/start.zig:114:5: 0x20547f in std.start._start (test)
    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
    ^

Tests failed. Use the following command to reproduce the failure:
/workspace/api/zig-cache/o/oz8gtzKVOAirfZA6z1mZjIH_9WstwrvSJgqSVhy4oAKMQXFoEFPQmd3adOzSFCd_/test
The following command exited with error code 255:
/workspace/api/zig-linux-x86_64-0.5.0+1483ae37f/zig test /workspace/api/src/sys.zig --cache-dir /workspace/api/zig-cache --name test 

Build failed. The following command failed:
/workspace/api/zig-cache/o/iCNhPY0L8bzrdgHGDMXAujz1BIwisent1naIbSB0Ze7cha7BgHYDufwHLi_ePawF/build /workspace/api/zig-linux-x86_64-0.5.0+1483ae37f/zig /workspace/api /workspace/api/zig-cache test

I have no idea what is causing the problem right now.

Test [1/1] test "getReleaseEntry"...VERSION = "19.04 (Disco Dingo)"
error - detected leaked allocations without matching free: 2

/workspace/api/zig-linux-x86_64-0.5.0+1483ae37f/lib/zig/std/special/test_runner.zig:52:46: 0x22ac9d in std.special.main (test)
                error.Leak => std.debug.panic("", .{}),
                                             ^
/workspace/api/zig-linux-x86_64-0.5.0+1483ae37f/lib/zig/std/start.zig:250:37: 0x20564b in std.start.posixCallMainAndExit (test)
            const result = root.main() catch |err| {
                                    ^
/workspace/api/zig-linux-x86_64-0.5.0+1483ae37f/lib/zig/std/start.zig:114:5: 0x20547f in std.start._start (test)
    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
    ^

Tests failed. Use the following command to reproduce the failure:
/workspace/api/zig-cache/o/yMC1N2OIryGZIZ1FtSbYEI_yBo_3a-YEGkD-cQ4LT3u--wKuxyBmdp5iVj0Vvtkf/test
The following command exited with error code 255:
/workspace/api/zig-linux-x86_64-0.5.0+1483ae37f/zig test /workspace/api/src/sys.zig --cache-dir /workspace/api/zig-cache --name test

Ayye, it "works"

hey so I know this is not exactly on-topic and my deepest condolences to those who will be getting a notif for this, but @RossComputerGuy, as a high-schooler with services like Discord, IRC, Reddit, etc blocked, I highly recommend getting a free VPN/proxy for your phone or computer so you can try to unblock some services like this. I use Hotspot Shield for my phone and I use the onion router on my school chromebook. Cheers, and good luck!

I don't have my phone nor laptop. My school doesn't allow VPNs. If I had my laptop then everything would be working since I bypass the DNS by using 1.0.0.1 as the DNS server.

Is freenode's WebIRC an option?

That's what I tried.

โ€โ€โ€โ€โ€โ€โ€ Original Message โ€โ€โ€โ€โ€โ€โ€
On Friday, February 21, 2020 5:12 PM, pixelherodev notifications@github.com wrote:

Is freenode's WebIRC an option?

โ€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.

Was this page helpful?
0 / 5 - 0 ratings