// Demonstrates almost all of libpom's API #include #include #include #include #include #include #include static size_t custom_read(void *udata, char *buf, size_t size) { int fd = (int)(intptr_t)udata; // only read up to 4 bytes at a time. why not! // it's much slower, but it is allowed. ssize_t ret = read(fd, buf, size < 4 ? size : 4); if (ret < 0) { // read error occured. // we could store an error away somewhere if we wanted to // (read errors are unusual anyways.) ret = 0; } return ret; } int main(void) { pom_error *error; // ordinary usage: load from a path pom_conf *conf = pom_load_path(NULL, "conf.pom", &error); if (error) { pom_error_print(error); free(error); return EXIT_FAILURE; } // get a key const char *indentation_type = pom_conf_get(conf, "indentation-type"); printf("indentation-type: %s\n", indentation_type ? indentation_type : "(none)"); // free configuration pom_conf_free(conf); pom_settings settings = {0}; strcpy(settings.error_lang, "fr"); // erreurs en français // load configuration with custom settings and a custom reader int fd = open("conf.pom", O_RDONLY); conf = pom_load(&settings, "conf.pom", // file name for error messages custom_read, // read function (void *)(intptr_t)fd, // will be passed as 1st argument to custom_read &error ); if (error) { pom_error_print(error); free(error); return EXIT_FAILURE; } // nicer way of doing what we did above printf("indentation-type: %s\n", pom_conf_get_or_default(conf, "indentation-type", "(none)")); // parse value as signed integer int64_t int_val; if ((error = pom_conf_get_int(conf, "tab-size", &int_val))) { pom_error_print(error); free(error); } else { printf("tab size: %" PRId64 "\n", int_val); } // parse value as unsigned integer, use default of 2 uint64_t uint_val; if ((error = pom_conf_get_uint_or_default(conf, "padding-pixels", &uint_val, 2))) { pom_error_print(error); free(error); } else { printf("padding pixels: %" PRIu64 "\n", uint_val); } // parse value as double double float_val; if ((error = pom_conf_get_float_or_default(conf, "font-size", &float_val, 12.0))) { pom_error_print(error); free(error); } else { printf("font size: %f\n", float_val); } // parse value as boolean bool bool_val; if ((error = pom_conf_get_bool_or_default(conf, "show-line-numbers", &bool_val, true))) { pom_error_print(error); free(error); } else { printf("show line numbers: %d\n", bool_val); } // extract section out of configuration const pom_conf *file_extensions = pom_conf_section(conf, "file-extensions"); // parse value as list char **Cpp_extensions = pom_conf_get_list(file_extensions, "Cpp"); if (Cpp_extensions) { for (size_t i = 0; Cpp_extensions[i]; i++) printf("- Defined C++ extension %s\n", Cpp_extensions[i]); } else { printf("no extensions defined for C++\n"); } free(Cpp_extensions); // iterate over keys in section pom_key_iter *key_iter = NULL; const char *key; const pom_conf *plugins = pom_conf_section(conf, "plug-in"); while ((key = pom_conf_next_key(plugins, &key_iter))) { const pom_conf *plugin = pom_conf_section(plugins, key); const char *path = pom_conf_get_or_default(plugin, "path", "(none)"); bool enabled; if ((error = pom_conf_get_bool_or_default(plugin, "enabled", &enabled, true))) { pom_error_print(error); free(error); // (enabled will still be true in this case) } // get location where key was defined const char *file; uint64_t line; pom_conf_location(plugins, key, &file, &line); printf("%s:%" PRIu64 ": plug-in: %s (path = %s, enabled = %d)\n", file, line, key, path, enabled); } // load config from string pom_conf *overrides = pom_load_string(NULL, "", "tab-size = 12", &error); assert(!error); // create a copy of a configuration pom_conf *copy = pom_conf_copy(conf); // merge configurations pom_conf_merge(copy, overrides); // iterate over items (key-value pairs) in configuration const pom_item *item; pom_item_iter *item_iter = NULL; while ((item = pom_conf_next_item(copy, &item_iter))) { if (strchr(item->key, 'b')) printf("\t%s: %s\n", item->key, item->value); } // iterate over all the keys which haven't been accessed directly pom_unread_key_iter *unread_iter = NULL; const char *unread; while ((unread = pom_conf_next_unread_key(copy, &unread_iter))) { printf("unrecognized key %s\n", unread); } pom_conf_free(copy); pom_conf_free(overrides); pom_conf_free(conf); return 0; }