diff options
| author | Clecio Jung | 2023-04-15 11:07:01 -0300 |
|---|---|---|
| committer | Clecio Jung | 2023-04-15 11:07:01 -0300 |
| commit | d7119c21fa566d2d5834cc5aa8627655ee849160 (patch) | |
| tree | 33cfea3efe3d2d282b8cb4b4241fd07e4667781f | |
| parent | a33d389310a35bc181583b0f25dbf742e67295e9 (diff) | |
| download | libini-d7119c21fa566d2d5834cc5aa8627655ee849160.tar.xz libini-d7119c21fa566d2d5834cc5aa8627655ee849160.tar.zst | |
Update the README example
| -rw-r--r-- | README.md | 40 | ||||
| -rw-r--r-- | ini_file.c | 4 | ||||
| -rw-r--r-- | ini_file.h | 5 |
3 files changed, 41 insertions, 8 deletions
@@ -31,30 +31,56 @@ This library provides a set of functions for reading and writing INI files. Here int main(const int argc, const char **const argv) { struct Ini_File *ini_file; + size_t section_index, property_index; + char *value; + double number; - // Check that the user provided an argument specifying the INI file name + /* Check that the user provided an argument specifying the INI file name */ if (argc < 2) { - fprintf(stderr, "Usage: %s ini_file_name\n", argv[0]); + fprintf(stderr, "Usage: %s file_name.ini\n", argv[0]); return EXIT_FAILURE; } - // Parse the INI file and store the resulting data structure in a variable + /* Parse the INI file and store the resulting data structure in a variable */ ini_file = ini_file_parse(argv[1], NULL); - // Check that the INI file was parsed successfully + /* Check that the INI file was parsed successfully */ if (ini_file == NULL) { fprintf(stderr, "Was not possible to parse the ini_file \"%s\"\n", argv[1]); return EXIT_FAILURE; } - // Print the properties from the INI file to the console + /* Print the properties from the INI file to the console (useful for debug purposes) */ printf("\nThe properties retrieved from the the ini file \"%s\" are:\n\n", argv[1]); ini_file_print_to(ini_file, stdout); + + /* You can iterate over the sections and keys */ + for (section_index = 0; section_index < ini_file->sections_size; section_index++) { + struct Ini_Section *section = &ini_file->sections[section_index]; + printf("[%s]\n", section->name); + for (property_index = 0; property_index < section->properties_size; property_index++) { + struct Key_Value_Pair *property = §ion->properties[property_index]; + printf("%s = %s\n", property->key, property->value); + } + printf("\n"); + } + + /* You can search for a property */ + if (ini_file_find_property(ini_file, "section", "key", &value) != ini_no_error) { + fprintf(stderr, "\nIt was not possible to find the specified key!\n"); + } + printf("\nThe specified key equals to: %s\n", value); + + /* You can search for a property and convert it directly to a number */ + if (ini_file_find_float(ini_file, "section", "key", &number) != ini_no_error) { + fprintf(stderr, "\nIt was not possible to find the specified key!\n"); + } + printf("\nThe specified key equals to: %g\n", number); - // Free the memory used by the INI file data structure + /* Free the memory used by the INI file data structure */ ini_file_free(ini_file); - // Return 0 to indicate success + /* Return 0 to indicate success */ return EXIT_SUCCESS; } ``` @@ -211,6 +211,10 @@ static char *copy_sized_string(struct Ini_File *ini_file, const char *const size if (ini_file == NULL) { return NULL; } + /* Checks if the string fits into the maximum buffer size */ + if ((len + 1) >= STRING_ALLOCATOR_BUFFER_SIZE) { + return NULL; + } if ((ini_file->strings == NULL) || ((ini_file->string_index + len + 1) > sizeof(ini_file->strings->buffer))) { /* Insert new buffer at the beginning */ struct String_Buffer *new_strings = malloc(sizeof(struct String_Buffer)); @@ -64,8 +64,11 @@ */ #define USE_CUSTOM_STRING_ALLOCATOR #ifdef USE_CUSTOM_STRING_ALLOCATOR + +#define STRING_ALLOCATOR_BUFFER_SIZE 4096 + struct String_Buffer { - char buffer[4096]; + char buffer[STRING_ALLOCATOR_BUFFER_SIZE]; struct String_Buffer *next; }; #endif |
