Compare commits
10 commits
89e6561297
...
db7be633a2
Author | SHA1 | Date | |
---|---|---|---|
db7be633a2 | |||
daef20ac59 | |||
a3d17fdc6c | |||
f115e01586 | |||
c728f0f0e6 | |||
0b0a306736 | |||
c00e0fa7e6 | |||
baafe10b17 | |||
fb85533933 | |||
dcdbbff878 |
4 changed files with 31 additions and 102 deletions
4
README
Normal file
4
README
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
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>.
|
76
README.md
76
README.md
|
@ -1,76 +0,0 @@
|
||||||
## initial - INI parser for the D programming language
|
|
||||||
|
|
||||||
My attempt at making a sane and high-quality INI parsing library for D.
|
|
||||||
|
|
||||||
```ini
|
|
||||||
[section]
|
|
||||||
key = value
|
|
||||||
```
|
|
||||||
|
|
||||||
The contents of an INI file is stored in an `INIUnit` structure. This
|
|
||||||
contains an associative array of `INISection`s 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
|
|
||||||
|
|
||||||
Copy initial.d to 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
|
|
||||||
|
|
||||||
Documentation can be found by reading the documentation comments in the code.
|
|
||||||
|
|
||||||
## Progress
|
|
||||||
|
|
||||||
### Finished
|
|
||||||
|
|
||||||
- Keys
|
|
||||||
- Sections
|
|
||||||
- Default section
|
|
||||||
- Comments
|
|
||||||
|
|
||||||
### Unimplemented
|
|
||||||
|
|
||||||
- Key referencing
|
|
||||||
|
|
||||||
Currently initial.d does all I need it to, and I consider it complete.
|
|
||||||
However if you have any questions, feel free to send an email to [my
|
|
||||||
public inbox][1] or directly to my address (jeremy@baxters.nz). Patches
|
|
||||||
are also welcome which you can generate with `git format-patch` and attach
|
|
||||||
to an email or just copy into the body of an email ([`git send-email`][2]
|
|
||||||
can do this automatically).
|
|
||||||
|
|
||||||
[1]: https://lists.sr.ht/~jeremy/public-inbox
|
|
||||||
[2]: https://git-send-email.io
|
|
6
dub.json
6
dub.json
|
@ -1,9 +1,9 @@
|
||||||
{
|
{
|
||||||
"name": "initial",
|
"name": "initial",
|
||||||
"description": "INI parsing library for the D programming language",
|
"description": "INI parser",
|
||||||
|
|
||||||
"authors": ["Jeremy Baxter"],
|
"authors": ["Jeremy Baxter <jeremy@baxters.nz>"],
|
||||||
"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,27 +3,29 @@
|
||||||
*
|
*
|
||||||
* 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 organization
|
* Permission is hereby granted, free of charge, to any person or
|
||||||
* obtaining a copy of the software and accompanying documentation covered by
|
* organization obtaining a copy of the software and accompanying
|
||||||
* this license (the "Software") to use, reproduce, display, distribute,
|
* documentation covered by this license (the "Software") to use,
|
||||||
* execute, and transmit the Software, and to prepare derivative works of the
|
* reproduce, display, distribute, execute, and transmit the Software,
|
||||||
* Software, and to permit third-parties to whom the Software is furnished to
|
* and to prepare derivative works of the Software, and to permit
|
||||||
* do so, all subject to the following:
|
* third-parties to whom the Software is furnished to do so, all
|
||||||
|
* subject to the following:
|
||||||
*
|
*
|
||||||
* The copyright notices in the Software and this entire statement, including
|
* The copyright notices in the Software and this entire statement,
|
||||||
* the above license grant, this restriction and the following disclaimer,
|
* including the above license grant, this restriction and the following
|
||||||
* must be included in all copies of the Software, in whole or in part, and
|
* disclaimer, must be included in all copies of the Software, in whole or
|
||||||
* all derivative works of the Software, unless such copies or derivative
|
* in part, and all derivative works of the Software, unless such copies or
|
||||||
* works are solely in the form of machine-executable object code generated by
|
* derivative works are solely in the form of machine-executable object
|
||||||
* a source language processor.
|
* code generated by a source language processor.
|
||||||
*
|
*
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
|
||||||
* SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
* NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE
|
||||||
* FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
* DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY,
|
||||||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
* WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
* DEALINGS IN THE SOFTWARE.
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/++
|
/++
|
||||||
|
@ -215,10 +217,9 @@ 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.
|
||||||
+/
|
+/
|
||||||
|
@ -234,10 +235,10 @@ struct INISection
|
||||||
+ with a message containing location information.
|
+ with a message containing location information.
|
||||||
+/
|
+/
|
||||||
T
|
T
|
||||||
keyAs(T)(string k, string defaultValue = null)
|
keyAs(T)(string k, T defaultValue)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
return key(k, defaultValue).to!T();
|
return k in keys ? keys[k].to!T() : defaultValue;
|
||||||
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