File: iniwrite.c

package info (click to toggle)
iniparser 4.2.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 464 kB
  • sloc: ansic: 2,291; sh: 39; python: 9; makefile: 5
file content (77 lines) | stat: -rw-r--r-- 1,548 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "iniparser.h"

void create_empty_ini_file(void)
{
    FILE *ini;

    if ((ini = fopen("example.ini", "w")) == NULL) {
        fprintf(stderr, "iniparser: cannot create example.ini\n");
        return;
    }

    fclose(ini);
}

int write_to_ini(const char *ini_name)
{
    void *dictionary;
    FILE *ini_file;
    int ret = 0;

    if (!ini_name) {
        fprintf(stderr, "Invalid argurment\n");
        return -1;
    }

    dictionary = iniparser_load(ini_name);

    if (!dictionary) {
        fprintf(stderr, "cannot parse file: %s\n", ini_name);
        return -1;
    }

    /* set section */
    ret = iniparser_set(dictionary, "Pizza", NULL);

    if (ret < 0) {
        fprintf(stderr, "cannot set section in: %s\n", ini_name);
        ret = -1;
        goto free_dict;
    }

    /* set key/value pair */
    ret = iniparser_set(dictionary, "Pizza:Cheese", "TRUE");

    if (ret < 0) {
        fprintf(stderr, "cannot set key/value in: %s\n", ini_name);
        ret = -1;
        goto free_dict;
    }

    ini_file = fopen(ini_name, "w+");

    if (!ini_file) {
        fprintf(stderr, "iniparser: cannot create example.ini\n");
        ret = -1;
        goto free_dict;
    }

    iniparser_dump_ini(dictionary, ini_file);
    fclose(ini_file);
free_dict:
    iniparser_freedict(dictionary);
    return ret;
}

int main(int argc, char *argv[])
{
    int ret;

    if (argc < 2) {
        create_empty_ini_file();
        ret = write_to_ini("example.ini");
    } else
        ret = write_to_ini(argv[1]);

    return ret ;
}