Compare commits
No commits in common. "master" and "v0.1.0" have entirely different histories.
4 changed files with 93 additions and 31 deletions
4
README
4
README
|
@ -1,4 +0,0 @@
|
||||||
This is initial, an INI parsing library for the D programming language.
|
|
||||||
|
|
||||||
Reference documentation can be found in the source code as Ddoc comments.
|
|
||||||
A quick start guide can be found at <https://reformers.dev/initial>.
|
|
67
README.md
Normal file
67
README.md
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
## initial - INI parsing library for the D programming language
|
||||||
|
|
||||||
|
My attempt at making a sane and high-quality INI parsing library for D.
|
||||||
|
|
||||||
|
```ini
|
||||||
|
[section]
|
||||||
|
key = value
|
||||||
|
```
|
||||||
|
|
||||||
|
An INI file's contents is stored in an `INIUnit` structure. This contains
|
||||||
|
multiple `INISection`s stored in an associative array that you can read
|
||||||
|
or set yourself.
|
||||||
|
|
||||||
|
initial also provides serialisation facilities that can turn `INIUnit`s
|
||||||
|
and `INISection`s back into an INI document. Handy for if your program
|
||||||
|
has some sort of GUI configuration interface or if you're passing around
|
||||||
|
INI through a network connection.
|
||||||
|
|
||||||
|
## Demo
|
||||||
|
|
||||||
|
```d
|
||||||
|
INIUnit ini;
|
||||||
|
|
||||||
|
/* write INI data to an INIUnit */
|
||||||
|
ini["section"]["key"] = "value";
|
||||||
|
ini["section"]["num"] = "4.8";
|
||||||
|
|
||||||
|
/* read INI data */
|
||||||
|
readINI(ini, `
|
||||||
|
[section]
|
||||||
|
num = 5.3
|
||||||
|
`);
|
||||||
|
|
||||||
|
assert(ini["section"]["key"] == "value");
|
||||||
|
assert(ini["section"]["num"] == "5.3");
|
||||||
|
|
||||||
|
/* read INI from file */
|
||||||
|
readINIFile(ini, "config.ini");
|
||||||
|
|
||||||
|
/* write INI to file */
|
||||||
|
import std.file : write;
|
||||||
|
write("config.ini", ini.serialise());
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Download initial.d and put it somewhere in your import path, then compile it alongside
|
||||||
|
your program and link it in. Or you can do it all at once by using -i.
|
||||||
|
|
||||||
|
```
|
||||||
|
dmd program.d initial.d
|
||||||
|
dmd -i program.d
|
||||||
|
```
|
||||||
|
|
||||||
|
## Progress
|
||||||
|
|
||||||
|
### Finished
|
||||||
|
|
||||||
|
- Keys
|
||||||
|
- Sections
|
||||||
|
- Default section
|
||||||
|
- Comments
|
||||||
|
|
||||||
|
### Todo
|
||||||
|
|
||||||
|
- Key referencing
|
||||||
|
- Testing D:
|
6
dub.json
6
dub.json
|
@ -1,9 +1,9 @@
|
||||||
{
|
{
|
||||||
"name": "initial",
|
"name": "initial",
|
||||||
"description": "INI parser",
|
"description": "INI parsing library for the D programming language",
|
||||||
|
|
||||||
"authors": ["Jeremy Baxter <jeremy@baxters.nz>"],
|
"authors": ["Jeremy Baxter"],
|
||||||
"copyright": "Copyright © 2024 Jeremy Baxter",
|
"copyright": "Copyright © 2024, Jeremy Baxter",
|
||||||
"license": "BSL-1.0",
|
"license": "BSL-1.0",
|
||||||
|
|
||||||
"targetType": "library",
|
"targetType": "library",
|
||||||
|
|
47
initial.d
47
initial.d
|
@ -3,29 +3,27 @@
|
||||||
*
|
*
|
||||||
* Boost Software License - Version 1.0 - August 17th, 2003
|
* Boost Software License - Version 1.0 - August 17th, 2003
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person or
|
* Permission is hereby granted, free of charge, to any person or organization
|
||||||
* organization obtaining a copy of the software and accompanying
|
* obtaining a copy of the software and accompanying documentation covered by
|
||||||
* documentation covered by this license (the "Software") to use,
|
* this license (the "Software") to use, reproduce, display, distribute,
|
||||||
* reproduce, display, distribute, execute, and transmit the Software,
|
* execute, and transmit the Software, and to prepare derivative works of the
|
||||||
* and to prepare derivative works of the Software, and to permit
|
* Software, and to permit third-parties to whom the Software is furnished to
|
||||||
* third-parties to whom the Software is furnished to do so, all
|
* do so, all subject to the following:
|
||||||
* subject to the following:
|
|
||||||
*
|
*
|
||||||
* The copyright notices in the Software and this entire statement,
|
* The copyright notices in the Software and this entire statement, including
|
||||||
* including the above license grant, this restriction and the following
|
* the above license grant, this restriction and the following disclaimer,
|
||||||
* disclaimer, must be included in all copies of the Software, in whole or
|
* must be included in all copies of the Software, in whole or in part, and
|
||||||
* in part, and all derivative works of the Software, unless such copies or
|
* all derivative works of the Software, unless such copies or derivative
|
||||||
* derivative works are solely in the form of machine-executable object
|
* works are solely in the form of machine-executable object code generated by
|
||||||
* code generated by a source language processor.
|
* a source language processor.
|
||||||
*
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
|
* FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||||
* NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE
|
* SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||||
* DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY,
|
* FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||||
* WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
* DEALINGS IN THE SOFTWARE.
|
||||||
* SOFTWARE.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/++
|
/++
|
||||||
|
@ -217,9 +215,10 @@ struct INISection
|
||||||
this(string name) nothrow
|
this(string name) nothrow
|
||||||
{
|
{
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
keys = new string[string];
|
||||||
}
|
}
|
||||||
|
|
||||||
/++
|
/+
|
||||||
+ Returns the value of `k` in this section or
|
+ Returns the value of `k` in this section or
|
||||||
+ `defaultValue` if the key is not present.
|
+ `defaultValue` if the key is not present.
|
||||||
+/
|
+/
|
||||||
|
@ -235,10 +234,10 @@ struct INISection
|
||||||
+ with a message containing location information.
|
+ with a message containing location information.
|
||||||
+/
|
+/
|
||||||
T
|
T
|
||||||
keyAs(T)(string k, T defaultValue)
|
keyAs(T)(string k, string defaultValue = null)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
return k in keys ? keys[k].to!T() : defaultValue;
|
return key(k, defaultValue).to!T();
|
||||||
catch (ConvException)
|
catch (ConvException)
|
||||||
throw new INITypeException(
|
throw new INITypeException(
|
||||||
format!"unable to convert [%s].%s to %s"(name, k, T.stringof));
|
format!"unable to convert [%s].%s to %s"(name, k, T.stringof));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue