diff options
| author | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-08-23 11:18:44 +0200 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-08-23 11:18:44 +0200 |
| commit | 359422c97cce93bbb27051f9df3efb45bd0b9052 (patch) | |
| tree | 2e352bb852a25390d40d45e199f835d218ad497f /src/administration_writer.cpp | |
| parent | 8ea59863c5d13e68e080cf7612047ea4c655292c (diff) | |
settings file writing
Diffstat (limited to 'src/administration_writer.cpp')
| -rw-r--r-- | src/administration_writer.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/administration_writer.cpp b/src/administration_writer.cpp new file mode 100644 index 0000000..5f23d78 --- /dev/null +++ b/src/administration_writer.cpp @@ -0,0 +1,78 @@ +#define _CRT_SECURE_NO_WARNINGS + +#include <zip.h> +#include <xml.h> +#include <stdlib.h> + +#include "administration_writer.hpp" + +bool administration_writer_save_cost_centers() +{ + return false; +} + +bool administration_writer_save_tax_brackets() +{ + struct zip_t *zip = zip_open(administration_file_path_get(), 0, 'w'); + if (!zip) { + zip = zip_open(administration_file_path_get(), 0, 'a'); + if (!zip) { + return false; + } + } + + if (zip_entry_open(zip, ADMIN_FILE_SETTINGS) < 0) { + return false; + } + + // Get all data. + u32 num_brackets = administration_tax_bracket_count(); + u32 buffer_size = sizeof(country_tax_bracket) * num_brackets; + country_tax_bracket* bracket_buffer = (country_tax_bracket*)malloc(buffer_size); + num_brackets = administration_tax_bracket_get_all(bracket_buffer); + + int buf_length = num_brackets * 500; // Ballpark file content size. + char* settings_content = (char*)malloc(buf_length); + memset(settings_content, 0, buf_length); + + char* orig_content = settings_content; + + //// Tax brackets. + settings_content += sprintf(settings_content, "<country_tax_brackets>\n"); + + for (u32 i = 0; i < num_brackets; i++) { + country_tax_bracket c = bracket_buffer[i]; + + settings_content += sprintf(settings_content, "\t<country_tax_bracket>\n"); + + settings_content += sprintf(settings_content, "\t\t<id>%s</id>\n", c.id); + settings_content += sprintf(settings_content, "\t\t<country_code>%s</country_code>\n", c.country_code); + settings_content += sprintf(settings_content, "\t\t<rate>%.2f</rate>\n", c.rate); + settings_content += sprintf(settings_content, "\t\t<description>%s</description>\n", c.description); + + settings_content += sprintf(settings_content, "\t</country_tax_bracket>\n"); + } + + settings_content += sprintf(settings_content, "</country_tax_brackets>"); + + int final_length = (int)strlen(orig_content); + + bool result = true; + if (!xml_parse_document((uint8_t*)orig_content, final_length)) { + result = false; + } + + + if (zip_entry_write(zip, orig_content, final_length) < 0) { + result = false; + } + + zip_entry_close(zip); + + zip_close(zip); + + free(bracket_buffer); + free(orig_content); + + return result; +}
\ No newline at end of file |
