laytan / odin-ini-parser

INI file format parser for odin.
MIT License
17 stars 1 forks source link
odin-lib

Odin INI

Package ini provides a parser (and lexer) for .ini files.

Format implemented:

Example usage:

  package main

  import "core:os"
  import "ini"

  main :: proc() {
    config, ok := get_config("foo.ini").?
    if !ok {
        return
    }
    defer ini.ini_delete(config)

    for k, v in config {
        fmt.printf("[%q]\n", k)
        for kk, vv in v {
            fmt.printf("%q = %q\n", kk, vv)
        }
    }
  }

  get_config :: proc(config_file_path: string) -> Maybe(ini.INI) {
    bytes, ok := os.read_entire_file_from_filename(config_file_path)
    if !ok {
        fmt.printf("[ERROR]: could not read %q\n", config_file_path)
        return nil
    }
    defer delete(bytes)

    config, res := parse(bytes)
    using res.pos
    switch res.err {
        case .EOF:              return config
        case .IllegalToken:     fmt.printf("[ERROR]: Illegal token encountered in %q at %d:%d", config_file_path, line+1, col+1)
        case .KeyWithoutEquals: fmt.printf("[ERROR]: Key token found, but not assigned in %q at %d:%d", config_file_path, line+1, col+1)
        case .ValueWithoutKey:  fmt.printf("[ERROR]: Value token found, but not preceeded by a key token in %q at %d:%d", config_file_path, line+1, col+1)
        case .UnexpectedEquals: fmt.printf("[ERROR]: Equals sign found in an unexpected location in %q at %d:%d", config_file_path, line+1, col+1)
    }
  }