Coder Social home page Coder Social logo

ini's Introduction

ini

A tiny ANSI C library for loading .ini config files

Usage

The files ini.c and ini.h should be dropped into an existing project.

The library has support for sections, comment lines and quoted string values (with escapes). Unquoted values and keys are trimmed of whitespace when loaded.

; last modified 1 April 2001 by John Doe
[owner]
name = John Doe
organization = Acme Widgets Inc.

[database]
; use IP address in case network name resolution is not working
server = 192.0.2.62     
port = 143
file = "payroll.dat"

An ini file can be loaded into memory by using the ini_load() function. NULL is returned if the file cannot be loaded.

ini_t *config = ini_load("config.ini");

The library provides two functions for retrieving values: the first is ini_get(). Given a section and a key the corresponding value is returned if it exists. If the section argument is NULL then all sections are searched.

const char *name = ini_get(config, "owner", "name");
if (name) {
  printf("name: %s\n", name);
}

The second, ini_sget(), takes the same arguments as ini_get() with the addition of a scanf format string and a pointer for where to store the value.

const char *server = "default";
int port = 80;

ini_sget(config, "database", "server", NULL, &server);
ini_sget(config, "database", "port", "%d", &port);

printf("server: %s:%d\n", server, port);

The ini_free() function is used to free the memory used by the ini_t* object when we are done with it. Calling this function invalidates all string pointers returned by the library.

ini_free(config);

License

This library is free software; you can redistribute it and/or modify it under the terms of the MIT license. See LICENSE for details.

ini's People

Contributors

rxi avatar

Stargazers

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

Watchers

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

ini's Issues

Idea: testing for existence of a particular section

It'd be useful to be able to test if a particular section exists before trying to read anything from it. This would allow for creating relevant structures in advance. And it would be especially useful in situations when you cannot rely on any key/value pair being always present if a section exists.

Is this something that could be supported without expanding existing API? e.g.,

ini_get(config, "owner", NULL) == NULL // section doesn't exist
ini_get(config, "owner", NULL) != NULL // section exists

If that sounds OK to you I could create a PR.

When I used ini_free ,I still got a segment error.

Program received signal SIGSEGV, Segmentation fault.
0x0000000000402964 in ini_get (ini=0x0, section=0x42b5eb "syslog", key=0x42b5e0 "serverAddr")
at CloudWaf/src/dt/modules/sign_check/ini.c:233
233 char *p = ini->data;
Missing separate debuginfos, use: debuginfo-install glibc-2.17-292.el7.x86_64 libgcc-4.8.5-39.el7.x86_64 libstdc++-4.8.5-39.el7.x86_64

ini_sget doesn't work correctly in forking programs

It's not really a problem or bug rather then a little inconvenience and warning to others.

I am working on screen locker, and I tried to tie it in with .ini parser - and this one was best of them all.

However, because this screen locker forks at some point, .ini get freed and char array contents get corrupted.

Again, I understand why such method was selected, but anyway, using *((const char**) dst) = val; is somewhat unreliable in such programs. I am now using strcpy to get content of string after getting value with ini_sget or ini_get.

free(ini->data) crashes on non-standard libc

Some non-standard libc's (such as on game SDKs or other embedded systems) do not do null checks before freeing memory. I think at least the free(ini->data) line in ini.c should have a check added there so there would not be a crash if a file is not found. Could this be done?

Thanks

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.