{"id":201,"date":"2012-07-06T20:15:57","date_gmt":"2012-07-06T20:15:57","guid":{"rendered":"http:\/\/greg-kennedy.com\/?p=201"},"modified":"2013-01-21T19:07:04","modified_gmt":"2013-01-21T19:07:04","slug":"cfg_parse-simple-config-file-parser-in-c","status":"publish","type":"post","link":"https:\/\/greg-kennedy.com\/wordpress\/2012\/07\/06\/cfg_parse-simple-config-file-parser-in-c\/","title":{"rendered":"cfg_parse &#8211; Simple Config File Parser in C"},"content":{"rendered":"<p>There is no standard way in C to parse config files. Several libraries have sprung up to solve the problem, but perhaps that is overkill for what should be a simple operation.<\/p>\n<p>Presenting: cfg_parse &#8211; a compile-in solution for reading key-value pairs from file(s), looking up settings, and writing a config file back to disk. I could write more about it, but the included main.c shows all the functionality.<\/p>\n<p><code>\/\/ driver test program for cfg_parse<\/p>\n<p>#include \"cfg_parse.h\"<\/p>\n<p>#include &lt;stdio.h&gt;<\/p>\n<p>int main(int argc, char **argv)<br \/>\n{<br \/>\n\/\/ Pointer to a cfg_struct structure<br \/>\nstruct cfg_struct *cfg;<\/p>\n<p>\/\/ Initialize config struct<br \/>\ncfg = cfg_init();<\/p>\n<p>\/\/ Specifying some defaults<br \/>\ncfg_set(cfg,\"KEY\",\"VALUE\");<br \/>\ncfg_set(cfg,\"KEY_A\",\"DEFAULT_VALUE_A\");<\/p>\n<p>\/\/ \"Required\" file<br \/>\nif (cfg_load(cfg,\"config.ini\") &lt; 0)<br \/>\n{<br \/>\nfprintf(stderr,\"Unable to load cfg.ini\\n\");<br \/>\nreturn -1;<br \/>\n}<\/p>\n<p>\/\/ Several \"optional\" files can be added as well<br \/>\n\/\/ \u00a0Each subsequent call upserts values already in<br \/>\n\/\/ \u00a0the cfg structure.<br \/>\ncfg_load(cfg,\"\/usr\/local\/etc\/config.ini\");<br \/>\ncfg_load(cfg,\"~\/.config\");<\/p>\n<p>\/\/ Retrieve the value for key INFINITY, and print<br \/>\nprintf(\"INFINITY = %s\\n\",cfg_get(cfg,\"INFINITY\"));<\/p>\n<p>\/\/ Retrieve the value for key \"KEY\", and print<br \/>\nprintf(\"KEY = %s\\n\",cfg_get(cfg,\"KEY\"));<\/p>\n<p>\/\/ Delete the key-value pair for \"DELETE_ME\"<br \/>\ncfg_delete(cfg,\"DELETE_ME\");<\/p>\n<p>\/\/ Dump cfg-struct to disk.<br \/>\ncfg_save(cfg,\"config_new.ini\");<\/p>\n<p>\/\/ All done, clean up.<br \/>\ncfg_free(cfg);<\/p>\n<p>return 0;<br \/>\n}<\/code><\/p>\n<p><strong>Download: <a href=\"http:\/\/greg-kennedy.com\/wordpress\/wp-content\/uploads\/2012\/07\/cfg_parse.1.0.tar.gz\">cfg_parse.1.0.tar<\/a><\/strong> (version 1.0, 4kb .tar.gz)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is no standard way in C to parse config files. Several libraries have sprung up to solve the problem, but perhaps that is overkill for what should be a simple operation. Presenting: cfg_parse &#8211; a compile-in solution for reading key-value pairs from file(s), looking up settings, and writing a config file back to disk. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-201","post","type-post","status-publish","format-standard","hentry","category-software"],"_links":{"self":[{"href":"https:\/\/greg-kennedy.com\/wordpress\/wp-json\/wp\/v2\/posts\/201","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/greg-kennedy.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/greg-kennedy.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/greg-kennedy.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/greg-kennedy.com\/wordpress\/wp-json\/wp\/v2\/comments?post=201"}],"version-history":[{"count":7,"href":"https:\/\/greg-kennedy.com\/wordpress\/wp-json\/wp\/v2\/posts\/201\/revisions"}],"predecessor-version":[{"id":206,"href":"https:\/\/greg-kennedy.com\/wordpress\/wp-json\/wp\/v2\/posts\/201\/revisions\/206"}],"wp:attachment":[{"href":"https:\/\/greg-kennedy.com\/wordpress\/wp-json\/wp\/v2\/media?parent=201"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/greg-kennedy.com\/wordpress\/wp-json\/wp\/v2\/categories?post=201"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/greg-kennedy.com\/wordpress\/wp-json\/wp\/v2\/tags?post=201"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}