GithubHelp home page GithubHelp logo

robik / dini Goto Github PK

View Code? Open in Web Editor NEW
18.0 5.0 10.0 116 KB

Customisable INI parser library :ok_hand:

Home Page: http://robik.github.io/dini/

License: Boost Software License 1.0

D 100.00%
d ini ini-parser

dini's Introduction

dini

BuildStatus Version Downloads Maintained

dini is a library written in D Programming Language that allows you to read and write INI configuration files with ease.

Features

  • Easy to use

    Documentation and examples helps you understand library. It's also very nice to use :).

  • Well documented

    The code is well documented. If you find something that isn't, be sure to open issue about it.

  • Variable lookups

    You can "paste" defined variables values in values using %variable%

  • Section inheriting

    Sections can inherit values from other sections

  • Configurable

    Since version 2

    You can define custom quotes, comments and use custom type to store values (reader only).

    Also, if you want to create custom data from INI, you can use INIReader to construct one.

NOTE: Current development version - 2.0.0 is backwards API compatible, if you have any compatibility issues, please report them.

Quick start

Installation

Stable version

{
    ...
    "dependencies": {
        "dini": "~> 1.0.1"
    }
    ...
}

Latest version

{
    ...
    "dependencies": {
        "dini": "~> 2.0.0-rc"
    }
    ...
}

Usage

Let's check how it works in real life. In the examples, we'll use following configuration file:

[def]
name1=value1
name2=value2

[foo : def]
name1=Name1 from foo. Lookup for def.name2: %name2%

Now, lets try to parse it, we can do it with using code similar to:

import std.stdio;
import dini;

void main()
{
    // Parse file
    auto ini = Ini.Parse("path/to/file.conf");
    
    // Print foo.name1 value
    writeln(ini["foo"].getKey("name1"));
}

You can also set INI variables before parsing:

import std.stdio, std.path;
import dini;

void main()
{
    // Create ini struct instance
    Ini ini;
    
    // Set key value
    ini.setKey("currentdir", getcwd());
    
    // Now, you can use currentdir in ini file
    ini.parse();
    
    // Print foo.name1 value
    writeln(ini["foo"].getKey("currentdir"));
}

This allows for using %currentdir% in configuration file now.

Global Inheriting

If you would like to inherit sections that are in another one, you can use . at the beggining to start from global scope:

[a]
[a.b]

[b]
; Note the dot at beggining
[b.c : .a.b]

Global lookups

The same goes for variable lookups:

[a]
[a.b]
var=test

[b]
[b.c]
var=%.a.b.var%

dini's People

Contributors

jessekphillips avatar maaaks avatar robik avatar yevhensakara avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

dini's Issues

Comments in the middle of the line

INI format doesn't have a "standard", and indeed according to wikipedia it's not defined whether commends have to start in the beginning of a line.

But de-facto modern implementations (see python, iniparser, etc) allow the following:
var=1000 ; bigger is better

So yes, back in the DOS days comments had to start in the beginning of a line.
I don't think this is the expected behavior from a modern ini parsing library.

Saving ini files help

http://www.dprogramming.com/ini.php has ini library that has save in it. Maybe you could look at his code.

There might be a bug in his code, I can send you my version if you want.

His code uses std.stream (deprecated), so that's why I don't want to use it.

O, looks like you've seen the file. And my version has no tabs and spaces a the start of each line.

Allegro has an ini setup.

The save without comments would be better than no save at all, I think.

Support multiline values

Is there a way to store multiline values in the config?

Like, maybe, this:

key = """
    value
    value
    value
"""

or this:

key:
    value
    value
    value

How about adding dub.json?

Thanks for the library, it is really useful and simple. Great for simple projects with simple configs.

Unfortunately, adding DIni to dependencies is non-trivial, since many projects use Dub, and DIni doesn't. I think it would be better to include simple dub.json into the repository, like this:

{
    "name": "dini",
    "targetType": "library",
    "sourcePaths": ["src"]
}

For me, it works. After adding this file to DIni locally, I can use it in other Dub-based projects as a dependency:

"dependencies": {
    "dini": "~master"
}

Segfault on trying to parse empty strings

Currently the code segfaults when trying to parse an empty string. I provide here both a unittest that demonstrates the issue and a fix for it.

From: omer_barak <[email protected]>
Date: Thu, 8 Feb 2018 13:12:04 +0200
Subject: [PATCH] Handle empty files without segfault

---
 source/dini/parser.d | 6 ++++++
 source/dini/reader.d | 4 ++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/source/dini/parser.d b/source/dini/parser.d
index d2f7fd5..e333390 100644
--- a/source/dini/parser.d
+++ b/source/dini/parser.d
@@ -681,4 +681,10 @@ unittest {
     );
     auto ini = Ini.ParseStringWith!MyReader(`path=C:\Path`);
     assert(ini("path") == `C:\Path`);
+}
+
+unittest {
+    // Make sure there isn't a segfault on empty files
+    Ini ini;
+    ini.parseString("");
 }
\ No newline at end of file
diff --git a/source/dini/reader.d b/source/dini/reader.d
index 6a03199..06c5127 100644
--- a/source/dini/reader.d
+++ b/source/dini/reader.d
@@ -22,7 +22,7 @@
 module dini.reader;

 import std.algorithm  : countUntil, canFind, map;
-import std.array 	  : array;
+import std.array 	  : array, empty;
 import std.functional : unaryFun;
 import std.string 	  : representation, assumeUTF, strip,
 	stripLeft, stripRight, split, join, format;
@@ -319,7 +319,7 @@ struct INIReader(INIFormatDescriptor Format, ubyte Flags = 0x00, alias Boxer)
 	this(string source)
 	{
 		// Make source end with newline
-		if (source[$-1] != '\n')
+		if (source.empty || (source[$-1] != '\n'))
 			this.source = (source ~ "\n").representation;
 		else
 			this.source = source.representation;
--
2.6.4

Feature: save to file

The library is great, only ability to write down refreshed (or manually composed) configs is missing.

P.S. It is pretty easy to implement, I open issue to note that there is a need in the feature and there would be a PR with it one day .

docs gen issue

When I run generation docs I got next issues:

Performing "docs" build using dmd for x86.
dini 2.0.0: building configuration "library"...
..\..\Users\Dima\AppData\Roaming\dub\packages\dini-2.0.0\dini\source\dini\parser.d(301,17): Warning: Ddoc: parameter count mismatch
..\..\Users\Dima\AppData\Roaming\dub\packages\dini-2.0.0\dini\source\dini\parser.d(455,17): Warning: Ddoc: parameter count mismatch
..\..\Users\Dima\AppData\Roaming\dub\packages\dini-2.0.0\dini\source\dini\parser.d(494,16): Warning: Ddoc: parameter count mismatch
..\..\Users\Dima\AppData\Roaming\dub\packages\dini-2.0.0\dini\source\dini\parser.d(511,16): Warning: Ddoc: parameter count mismatch
..\..\Users\Dima\AppData\Roaming\dub\packages\dini-2.0.0\dini\source\dini\reader.d(319,2): Warning: Ddoc: parameter count mismatch
app ~master: building configuration "application"...

on string comments

It would be nice to have ability to set on string comments:

key=value ;this is comment

Support fileless INI parsing

Instead of requiring that a file be used to load INI formatted date, provide the means to pass a string as the source data. I.e utilize a range based interface.

Doesn't compile with D 2.067

I have just upgraded dmd from 2.066.1 to 2.067.0 on my Ubuntu 14.10, and my project which uses DIni stopped compiling because of this line in dini.d:

import std.array  : split, indexOf, replaceInPlace, join;

The indexOf() function seems to be moved into std.string. However, as far as I can see, you don't really use indexOfin the code, so you can just remove import of indexOf from the line. If you do so, the library successfully compiles.

P.S. If you will update version of package when fixing it, tell me, and I will update http://code.dlang.org/packages/dini.

error parsing config

My case is not standard. I am storing in config part of POST request, but I really not found nothing better.
dini fail on next config:

key1 = NW_Pacific%2FOverview%2Fvis_ir_background%2Fgms_6_lowcloud&DIR=%2Fnexsat_data%2FPUBLIC%2FCONUS%2Ffocus_regions%2FNW_Pacific%2FOverview%2Fvis_ir_background=

module std.array import 'indexOf' not found

DIni tries to import unexistent symbol indexOf:

../../home/maaaks/.dub/packages/dini-1.0.0/src/dini.d(12): Error: module std.array import 'indexOf' not found, did you mean 'alias IndexOf'?

However, if I remove the indexOf from that line, everything compiles successfully. I think you should remove it from source (and maybe check for other old unused imports).

Add ability to save the INI file

This is challenging since the comments and blank lines need to be stored only for the purpose of being output in a save.

Maybe a struct IniCommentStripped could be created that can be used for saving as a temporary quick fix:

myIni.stripComments.save();

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.