Git Product home page Git Product logo

ini's Introduction

ini

Zig library for opening .ini files.

usage

assuming that the file at path contains the following:

[root]
integer = -1345
string = My string
boolean = enabled

parse()

// Load file into memory.
var fd = try std.fs.cwd().openFile(path, .{ .read = true });
defer fd.close();
var fc = try allocator.alloc(u8, try fd.getEndPos());
defer allocator.free(fc);
// Construct schema type.
const ExampleConfig = struct {
    root: struct {
        integer: i64,
        string: []const u8,
        boolean: bool
    }
}
// Parse `.ini` from memory
var conf = parse(ExampleConfig, fc);

parse() in comptime block

comptime {
    // Load file.
    const fc = @embedFile("path/to/file.ini");
    // Construct schema type
    const ExampleConfig = struct {
        root: struct {
            integer: i64,
            string: []const u8,
            boolean: bool
        }
    }
    // Parse `.ini`
    var conf = parse(ExampleConfig, fc);
}

parseIntoMap()

// Load file into memory.
var fd = try std.fs.cwd().openFile(path, .{ .read = true });
defer fd.close();
var fc = try allocator.alloc(u8, try fd.getEndPos());
defer allocator.free(fc);
// Parse `.ini` from memory.
var ini = try parseIntoMap(fc, allocator);
defer ini.deinit();

// `ini.map` now contains a hash map with the contents.

spec

a .ini file is composed of:

  • comments: starting with ; up to the end of the line
  • sections: enclosed with []
  • identifiers: alphanumeric + _
  • values:
    • numbers: integers or float, parsed by Zig
    • booleans:
      • truthy: 1, true, True, t, T, yes, Yes, y, Y, on, On, enabled, Enabled
      • falsey: 0, false, False, f, F, no, No, n, N, off, Off, disabled, Disabled
    • strings: non-escaped text

types only make sense when parsing into a struct, when parsing to a map, all values get coerced to []const u8

the .ini file should have at least one section, no orphan keys are allowed

roadmap

  • implement parseIntoMap
  • make code more readable

ini's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.