This repository has been archived on 2024-05-29. You can view files and clone it, but cannot push or open issues/pull-requests.
ndg/lib/ini
alex 729af48569
ci/woodpecker/push/woodpecker Pipeline was successful Details
zig: upgrade from 0.11 to 0.12.0
mostly lots of language improvements and bugfixes, leading to better
code here, from the programming language point of view.

zig v0.12.0 release notes:
https://ziglang.org/download/0.12.0/release-notes.html
2024-04-23 14:01:38 +02:00
..
.github lib: add simple ini file format parser library 2023-12-06 23:53:37 +01:00
example lib: add simple ini file format parser library 2023-12-06 23:53:37 +01:00
src lib/ini: sync with upstream at 19e1210 2024-04-22 14:40:53 +02:00
.gitattributes lib: add simple ini file format parser library 2023-12-06 23:53:37 +01:00
.gitignore lib: add simple ini file format parser library 2023-12-06 23:53:37 +01:00
LICENCE lib: add simple ini file format parser library 2023-12-06 23:53:37 +01:00
README.md lib: add simple ini file format parser library 2023-12-06 23:53:37 +01:00
build.zig lib/ini: sync with upstream at 19e1210 2024-04-22 14:40:53 +02:00
build.zig.zon zig: upgrade from 0.11 to 0.12.0 2024-04-23 14:01:38 +02:00

README.md

INI parser library

This is a very simple ini-parser library that provides:

  • Raw record reading
  • Leading/trailing whitespace removal
  • comments based on ; and #
  • Zig API
  • C API

Usage example

Zig

const std = @import("std");
const ini = @import("ini");

pub fn main() !void {
    const file = try std.fs.cwd().openFile("example.ini", .{});
    defer file.close();

    var parser = ini.parse(std.testing.allocator, file.reader());
    defer parser.deinit();

    var writer = std.io.getStdOut().writer();

    while (try parser.next()) |record| {
        switch (record) {
            .section => |heading| try writer.print("[{s}]\n", .{heading}),
            .property => |kv| try writer.print("{s} = {s}\n", .{ kv.key, kv.value }),
            .enumeration => |value| try writer.print("{s}\n", .{value}),
        }
    }
}

C

#include <ini.h>

#include <stdio.h>
#include <stdbool.h>

int main() {
  FILE * f = fopen("example.ini", "rb");
  if(!f)
    return 1;
  
  struct ini_Parser parser;
  ini_create_file(&parser, f);

  struct ini_Record record;
  while(true)
  {
    enum ini_Error error = ini_next(&parser, &record);
    if(error != INI_SUCCESS)
      goto cleanup;
    
    switch(record.type) {
      case INI_RECORD_NUL: goto done;
      case INI_RECORD_SECTION:
        printf("[%s]\n", record.section);
        break;
      case INI_RECORD_PROPERTY:
        printf("%s = %s\n", record.property.key, record.property.value);
        break;
      case INI_RECORD_ENUMERATION:
        printf("%s\n", record.enumeration);
        break;
    }

  }
done:

cleanup:
  ini_destroy(&parser);
  fclose(f);
  return 0;
}