Code Monkey home page Code Monkey logo

inifile-cpp's Issues

Disallow section duplication

I think that INI files with duplicated sections are confusing and error prone. For example:

[Foo]
var1 = hi
[Bar]
var2 = 12
[Foo]
var3 = 14

[Foo] will end up having both variables var1 and var3.
It is easy to spot this duplication in such a small example, but with bigger INI files this can be problematic.

I've made the pull request #14 to make the code throw an exception in this case.

Comments and line separators getting deleted while saving

@Rookfighter Thank you for this useful library.

I noticed that after updating the values and calling save option, all the comments and empty lines in the ini file gets removed.
Since most of these files are edited manually by users, retaining comments and white spaces are essential while saving.

Sample ini file

# Send a message to the world
[world]
message=Hello

# Just foo :)
[foo]
f1=123

Sample code

#include <inicpp.h>
int main()
{
    ini::IniFile myIni;
    myIni.load("some/ini/path");

    myIni["world"]["message"] = "Hello world";

    myIni.save("some/ini/path");
    
}

Result after saving the file

[world]
message=Hello world
[foo]
f1=123

Do let me know if there is already an option to avoid them being removed.

Ignore non-significant spaces

Many INI files are edited directly by end users, and some of them want to use spaces to make the contents easy to read.

For example:

[SECT1]
  #This comment is indented, as is the following lines, which are also aligned
  John   = 1
  Evelyn = 2
  Lucas  = 3

I've made the pull request #11 to implement that.

Disable sorting

How to disable ALPHABET sorting in output file?

It always broke my file structure...

For example...

[General]
NUM_AITEMS = 5
AITEMS_ENABLED = false
[AITEM1]
DATA = X

After save it make fully unreadabled:

[AITEM1]
DATA = X
[General]
AITEMS_ENABLED = false
NUM_AITEMS = 5

ini parsing failed, section not closed

I have a ini file:

[/home/xxx/.gnupg/]
Acls=user::rwx;group::---;other::---

[/home/xxx/.gnupg/.#lkxxx.29271]
Acls=user::rw-;group::r--;other::r--

[/home/xxx/.gnupg/openpgp-revocs.d/xxx.rev]
Acls=user::rw-;group::---;other::---

[/home/xxx/.gnupg/private-keys-v1.d/xxx.key]
Acls=user::rw-;group::---;other::---

[/home/xxx/.gnupg/private-keys-v1.d/xxx.key]
Acls=user::rw-;group::---;other::---

[/home/xxx/.gnupg/pubring.kbx]
Acls=user::rw-;group::r--;other::r--

[/home/xxx/.gnupg/pubring.kbx~]
Acls=user::rw-;group::---;other::---

[/home/xxx/.gnupg/trustdb.gpg]
Acls=user::rw-;group::---;other::---

[/home/xxx/.ssh]
Acls=user::rwx;group::r--;other::r--
[/home/xxx/.ssh/id_rsa]
Acls=user::rw-;group::---;other::---
[/home/xxx/.ssh/id_rsa.pem]
Acls=user::rw-;group::r--;other::r--
[/home/xxx/.ssh/id_rsa.pub]
Acls=user::rw-;group::r--;other::r--
[/home/xxx/.ssh/known_hosts]
Acls=user::rw-;group::---;other::---
[/home/xxx/.ssh/known_hosts.old]
Acls=user::rw-;group::r--;other::r--
[Defaults]
Excludes=
Includes=/home/xxx/.ssh;/home/xxx/.gnupg/;
SaveAcls=true

Then I get a error when I rerun my programe:

erminate called after throwing an instance of 'std::logic_error'
what(): l4: ini parsing failed, section not closed

But I can't find any error about this ini file :(

By not existing keys returns 0

Hi, @Rookfighter. thank you for a great tool.

I'm trying to read an example.ini. And if the group-key-pair not exist in the ini-file, it returns "" that by converting to int gives 0 and by converting to bool gives false. It is in my opinion an unexpected behavior.

example.ini

[Group_1]
key_1 = 1;

main.cpp

#include <iostream>
#include "inicpp.h"
int main() {
	 ini::IniFile inifile;
	 inifile.load("example.ini");

	 int val_1 = inifile["Group_1"]["key_1"].as<int>();
	 int val_2 = inifile["Group_1"]["key_2"].as<int>();
	 
	 std::cout << "value 1 = " << val_1 << std::endl;
	 std::cout << "value 2 = " << val_2 << std::endl;
	 return 0;
}

Output:

value 1 = 1
value 2 = 0

My workaround for now is

#include <iostream>
#include <stdexcept>
#include "inicpp.h"

int readInt(ini::IniFile &inifile, const std::string &group, const std::string &key) {
	if (inifile[group][key].as<std::string>() == "")
		throw std::runtime_error(key + " is not found in " + group + "\n");
	else
		return inifile[group][key].as<int>();
}

int main() {
	ini::IniFile inifile;
	inifile.load("example.ini");

	try
	{
		int val_1 = readInt(inifile, "Group_1", "key_1");
		std::cout << "value 1 = " << val_1 << std::endl;
	} catch (...)
	{
		std::cout << "key_1 is not found in Group_1" << std::endl;
		inifile.clear();
		return 1;
	}

	try
	{
		int val_2 = readInt(inifile, "Group_1", "key_2");
		std::cout << "value 2 = " << val_2 << std::endl;
	} catch (...)
	{
		std::cout << "key_2 is not found in Group_1" << std::endl;
		inifile.clear();
		return 1;
	}

	return 0;
}

Restoring the master branch

Hello, my library figcone depends on inifile-cpp, which is downloaded during CMake configuration using FetchContent. As inifile-cpp doesn't provide any version tags, I specified the master branch to download it. Recently (?) the master branch was removed, and it rendered all versions of figcone broken unless the user disables the support of the ini format. By default, all config formats are supported, so the build is failing during configuration while trying to download inifile-cpp.

It will teach me to only use forked repositories for projects that don't have any versioning, but can you please restore the master branch to fix all previous releases of my library? I'm not asking to make it the main branch again, just to create it in the repo. There were no incompatible changes, so it can be set on the latest commit.

Why not

Вы можете использовать такой код (inicpp.h:234)?
void decode(const std::string &value, double &result) {
std::stringstream stream;
stream << value;
stream >> result;
}

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.