Search code examples
cfileembeddedini

How do I remove extra whitespaces created by iniparser?


Recently I'm taking iniparser as primary choice. However, with its API creating too many whitespaces resulting in waste of memory.

For example, iniparser_set() will create

/*test.ini*/
[section]
key                               = value

instead

/*test.ini*/
[section]
key = value

From the point of embedded system's view, removing those extra whitespaces is a great thing for saving memory space. So, how to fix it?


Solution

  • As per the source code of iniparser (https://github.com/ndevilla/iniparser/blob/deb85ad4936d4ca32cc2260ce43323d47936410d/src/iniparser.c#L312):

    in iniparser_dumpsection_ini function, there is this line:

    fprintf(f,
        "%-30s = %s\n",
        d->key[j]+seclen+1,
        d->val[j] ? d->val[j] : "");
    

    As you can see, key is printed with format specifier %-30s which is probably causing this issue.

    So, you can clone the repo source code and make the changes. Example, replace format specifier with %s, like:

    fprintf(f,
        "%s = %s\n",        // CHANGE HERE
        d->key[j]+seclen+1,
        d->val[j] ? d->val[j] : "");