• Stars
    star
    137
  • Rank 266,070 (Top 6 %)
  • Language
    C++
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A really stupid INI file format for C++11

Tortellini

The stupid - and I mean really, really stupid - INI file reader and writer for C++11 and above. Calorie free (no dependencies)!

#include <tortellini.hh>

// (optional)
#include <fstream>

int main() {
	tortellini::ini ini;

	// (optional) Read INI in from a file.
	// Default construction and subsequent assignment is just fine, too.
	std::ifstream in("config.ini");
	in >> ini;

	// Retrieval
	//
	// All value retrievals must be done via the "default" operator
	// (the pipe operator).
	//
	// The right-hand-side type is what is returned. Anything other
	// than a string-like type causes a parse (see caveats section below).
	//
	// All keys are case-INsensitive. This includes section headers.
	std::string s          = ini["Section"]["key"] | "default string";
	int i                  = ini["Section"]["key"] | 42;    // default int
	long l                 = ini["Section"]["key"] | 42L;   // default long
	long long ll           = ini["Section"]["key"] | 42LL;  // default long long
	unsigned int u         = ini["Section"]["key"] | 42u;   // default unsigned int
	unsigned long ul       = ini["Section"]["key"] | 42UL;  // default unsigned long
	unsigned long long ull = ini["Section"]["key"] | 42ULL; // default unsigned long long
	float f                = ini["Section"]["key"] | 42.0f; // default float
	double d               = ini["Section"]["key"] | 42.0;  // default double
	long double ld         = ini["Section"]["key"] | 42.0L; // default long double
	bool b                 = ini["Section"]["key"] | true;  // default bool

	// Assignment
	//
	// (This example uses the same key, but of course
	// in reality this would just be overwriting the
	// same key over and over again - probably not
	// what you'd really want. I would hope I wouldn't
	// have to explain this, but you never know.)
	ini["New Section"]["new-key"] = "Noodles are tasty.";
	ini["New Section"]["new-key"] = 1234;
	ini["New Section"]["new-key"] = 1234u;
	ini["New Section"]["new-key"] = 1234L;
	ini["New Section"]["new-key"] = 1234UL;
	ini["New Section"]["new-key"] = 1234LL;
	ini["New Section"]["new-key"] = 1234ULL;
	ini["New Section"]["new-key"] = 1234.0f;
	ini["New Section"]["new-key"] = 1234.0;
	ini["New Section"]["new-key"] = 1234.0L;
	ini["New Section"]["new-key"] = true;

	// "Naked" section (top-most key/value pairs, before a section header)
	// denoted by empty string in section selector
	ini[""]["naked-key"] = "I'll be at the very top, without a section";

	// You can also iterate over all sections
	for (auto &pair : ini) {
		std::cout << pair.name << "\n"; // the name of the section
		int v = pair.section["some-key"] | 1234;
		std::cout << "some-key=" << v << "\n";
	}

	// (optional) Write INI to file.
	std::ofstream out("config.ini");
	out << ini;
}

Things you should know.

This library has very few bells and whistles. It doesn't do anything fancy.

Here are the guarantees/features:

  • Case-insensitive keys and section headers. Reading from a key/section with different cases will work fine, and writing to an existing section/key will preserve the casing.
  • Untouched output (from using stream << ini) is guaranteed to be parsable (by using stream >> ini).
  • Barring I/O issues or exceptions coming from the standard library, Tortellini will not throw or abort (see below section about "invalid data").
  • Pasta in, pasta out. Source strings (from a parse) are preserved to the output. If you use yes instead of true, Tortellini will preserve that (unless the application overwrites the value).
  • yes, 1 and true are all parsable as bool(true). All other values equate to false. NOTE: This means that ini[""]["b"] | true will return false, not true, if the key exists but is not a valid, parsable truth-ey value. This may be counter-intuitive for some users.
  • All values are inherently string, and only parsed when retrieved.

Here are the caveats:

  • Do not store or share anything returned from the subscript operators ([]). They return temporary objects that are meant for short-lived interactions. They are NOT lifetime safe and expect the underlying tortellini::ini instance to subsist beyond their own lifetimes.
  • Invalid keys, values or section names skip the line entirely. This condition is henceforth referred to as "invalid data".
  • Integer overflows when parsing are invalid data.
  • Mismatched ] for a [ line is invalid data. Yes, keys will bleed into the preceding section name. That's user error, not your application's. Embrace it.
  • Empty keys or empty values (excluding leading/trailing whitespace!) are "invalid data" in that they might be valid but not included in the resulting data and thus won't be re-emitted.
  • Empty sections (or sections with 100% invalid data as key/value pairs) are themselves invalid data and are not emitted.
  • [] is a valid section name. It means the "naked" section. Re-emitting the INI will move those keys to the top regardless of where [] is positioned in the input file.
  • No comments are supported; ; is a valid (string) character.
  • No caching or memoization; if you retrieve anything but a std::string, there will be a parse. I never said Tortellini was hyper-over-optimized.

Testing

You can test by running:

mkdir build && cd build
cmake .. -DBUILD_TESTING=ON -DCMAKE_BUILD_TYPE=Debug
cmake --build .
ctest

License

You have two choices in license. Pick whichever one you want. Tortellini is uncucumbered. Go crazy. Eat some pasta.

Unlicense

This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>

MIT License

Copyright (c) 2020 Josh Junon

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

More Repositories

1

color

🌈 Javascript color conversion and manipulation library
JavaScript
4,775
star
2

better-exceptions

Pretty and useful exceptions in Python, automatically.
Python
4,531
star
3

color-convert

Plain color conversion functions in JavaScript
JavaScript
746
star
4

color-string

Parser and generator for CSS color strings
JavaScript
214
star
5

tabs-are-better-than-spaces

They are. Don't fight it.
HTML
73
star
6

node-error-ex

Easy error subclassing and stack customization
CoffeeScript
62
star
7

arua-meta

Standards, RFCs and discussion of the Arua language
Vim Script
44
star
8

node-editor

Generic node editor web components
TypeScript
27
star
9

xopt

ANSI C command line options parser
C
22
star
10

node-man-api

Eases the generation of (proper) man pages
JavaScript
18
star
11

node-is-arrayish

Check if an object can be used like an Array
CoffeeScript
16
star
12

flexicon

A lightweight, regex-based lexer framework for Python
Python
12
star
13

node-simple-swizzle

Simply swizzle your arguments
CoffeeScript
11
star
14

terminfo

Parse the Terminfo database
JavaScript
8
star
15

pet

Dead simple HTTP requests
JavaScript
8
star
16

dot-dot-dash-demo

Quick demo for Dot Dot Dash
C++
7
star
17

arua.io

The Arua language website
HTML
6
star
18

xp

A regular expression search (and replace) command line tool
JavaScript
6
star
19

handler-agent

Zero-dependency (req,res) handler callback agent for use in client requests
JavaScript
6
star
20

casscade

Experimental Cassowary (Kiwi) constraint-based layouts using CSS layout worklets (Houdini layout API)
JavaScript
5
star
21

starfuse

Mount StarBound .pak files as regular directories
Python
4
star
22

scaly

Minimalistic, composable cache and database layering inspired by CPU caches
JavaScript
4
star
23

this-image-is

A Reddit bot that tells you what's in an image using neural networks.
Python
4
star
24

CaptainJack

[ABANDONED] The unofficial JACK audio device driver for OS/X
C
3
star
25

qunit

Super simple unit testing in C99
C
3
star
26

mds-lava

Lava mod for Mindustry
3
star
27

nesmap

Universal NES Map editor
CoffeeScript
3
star
28

node-smart-pipe

Pipe to standard input, cross platform
CoffeeScript
3
star
29

docopt.md

Keep your docopt configuration directly in your readme
JavaScript
2
star
30

node-high-order

Find the length of a UTF-8 glyph by its first byte
CoffeeScript
2
star
31

mds-ancients-units

ANCiENTS Mindustry Mod (Units pack)
JavaScript
2
star
32

factorio-hand-crank

Hand Crank mod for Factorio
Makefile
2
star
33

fugu

(this is a mirror, I didn't write this)
C++
2
star
34

s-storage

localStorage bindings for S.js
JavaScript
2
star
35

node-man-api-mdoc

MDoc macro extensions for man-api
JavaScript
2
star
36

node-backslash

Parse backslashed strings
JavaScript
2
star
37

bellingbot

Bellingcat discord bot
Python
2
star
38

sig-router

A URL/history API router for S.js and Surplus applications
JavaScript
2
star
39

cssquery

Simple object queries using CSS-like selector syntax. I don't recommend you actually use this.
Python
2
star
40

lucky-the-eclectus

My parrot in Discord bot format. Keeps you company; nothing more.
JavaScript
1
star
41

node-transform-domain

Transforms a value given a source and destination domain
JavaScript
1
star
42

match-file-tree

Lightweight regex based file tree traversal
JavaScript
1
star
43

node-find-api

Finds all of the API calls and docs in a source file
JavaScript
1
star
44

arua-bootstrap-2

The Arua bootstrap compiler
C++
1
star
45

libcopynes

Git mirror of libcopynes
C
1
star
46

Codicle

Just Learn.
CoffeeScript
1
star
47

node-ddb

Fixed-sized frame decoding for Node.js using ffmpeg
C++
1
star
48

test-repo

JavaScript
1
star
49

node-legos

Snap your streams together
JavaScript
1
star
50

node-require-with-globals

Safely require a module with a modified global object.
JavaScript
1
star
51

cbind

Real C-binding / argument injection (some seriously cool shit)
C
1
star
52

test-layout-api-bug

Blink engine Houdini Layout API + WebAssembly access violation reproduction
HTML
1
star
53

josh-manders-made-this

But not really.
1
star
54

beep-osx

"Hardware speaker" beep for OS X
Objective-C
1
star
55

eloquent

Make expressive APIs easily and cleanly
CoffeeScript
1
star
56

cassowary-playground

A playground for Cassowary constraints
Rust
1
star
57

mist

Mist build system
CoffeeScript
1
star
58

eslint-plugin-smelly

Find code patterns that might be code smell
JavaScript
1
star
59

mud-json

JSON driver for the Mud ORM
JavaScript
1
star
60

node-call-with-globals

Safely call a function with a modified global object.
JavaScript
1
star
61

lib-llvm-clang

LLVM.js/Clang.js dependencies for Codicle
1
star
62

node-abyss

Deep and asynchronous.
JavaScript
1
star
63

tap-dot-neo

Spiritual successor to `tap-dot`
JavaScript
1
star
64

prisoners-dilemma

Prisoners Dilemma simulator
C
1
star
65

clang-extra-npm

Installs `clang-tools-extra` for you.
Shell
1
star
66

eslint-plugin-wtf

Catch mistakes literally no developer should be making.
JavaScript
1
star
67

node-atomic-fs

Atomically perform file operations
CoffeeScript
1
star
68

did-they-seriously-rename-master

Oh my god what bullshit is this
1
star
69

riffusion-cli

Hacked together CLI for Riffusion. Please, please don't abuse this.
JavaScript
1
star
70

node-find-higher-file

Traverses a path to find a file in a higher directory
JavaScript
1
star
71

node-emplace

Emplace objects and arrays - pointer to pointer style!
CoffeeScript
1
star
72

node-has-module

Checks for the existence of a module
JavaScript
1
star
73

autotest

C/C++ unit testing on Linux as simple as writing functions
C
1
star
74

qix-.github.io

My portfolio site.
Roff
1
star
75

node-consumer-stream

Readline-like functionality that keeps track of line counts and properly handles final lines without trailing newlines
CoffeeScript
1
star
76

duplicant

Finds similar lines in a codebase and shows them as a heatmap in VSCode
Rust
1
star