123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- #include <stdlib.h>
- #include <stdio.h>
- #include "faux/str.h"
- #include "faux/ini.h"
- #include "faux/testc_helpers.h"
- int testc_faux_ini_parse_file(void)
- {
- // Source INI file
- const char *src_file =
- "# Comment\n"
- "DISTRIB_ID=Ubuntu\n"
- "DISTRIB_RELEASE=18.04\n"
- "DISTRIB_CODENAME=bionic\n"
- "DISTRIB_DESCRIPTION=\"Ubuntu 18.04.4 LTS\"\n"
- "COMPLEX_VAR=\" Ubuntu\t\t1818 \"\n"
- "WO_QUOTES_VAR = qwerty\n"
- "WO_QUOTES_VAR2 = qwerty 98989898\n"
- "EMPTY_VAR3 = \n"
- "EMPTY_VAR4 =\n"
- " EMPTY_VAR5 = \"\"\t \n"
- " ANOTHER_VAR6 = \"Normal var\"\t \n"
- "\tTABBED_VAR = \"Normal tabbed var\"\t \n"
- "# Another comment\n"
- " # Yet another comment\n"
- "\t# Tabbed comment\n"
- "VAR_WITHOUT_EOL=zxcvbnm"
- ;
- // Etalon file
- const char *etalon_file =
- "ANOTHER_VAR6=\"Normal var\"\n"
- "COMPLEX_VAR=\" Ubuntu\t\t1818 \"\n"
- "DISTRIB_CODENAME=bionic\n"
- "DISTRIB_DESCRIPTION=\"Ubuntu 18.04.4 LTS\"\n"
- "DISTRIB_ID=Ubuntu\n"
- "DISTRIB_RELEASE=18.04\n"
- "TABBED_VAR=\"Normal tabbed var\"\n"
- "VAR_WITHOUT_EOL=zxcvbnm\n"
- "WO_QUOTES_VAR=qwerty\n"
- "WO_QUOTES_VAR2=qwerty\n"
- "\"test space\"=\"lk lk lk \"\n"
- ;
- int ret = -1; // Pessimistic return value
- faux_ini_t *ini = NULL;
- faux_ini_node_t *iter = NULL;
- const faux_pair_t *pair = NULL;
- char *src_fn = NULL;
- char *dst_fn = NULL;
- char *etalon_fn = NULL;
- // Prepare files
- src_fn = faux_testc_tmpfile_deploy_str(src_file);
- etalon_fn = faux_testc_tmpfile_deploy_str(etalon_file);
- dst_fn = faux_str_sprintf("%s/dst", getenv(FAUX_TESTC_TMPDIR_ENV));
- ini = faux_ini_new();
- if (!faux_ini_parse_file(ini, src_fn)) {
- fprintf(stderr, "Can't parse INI file %s\n", src_fn);
- goto parse_error;
- }
- iter = faux_ini_iter(ini);
- while ((pair = faux_ini_each(&iter))) {
- printf("[%s] = [%s]\n", faux_pair_name(pair), faux_pair_value(pair));
- }
- faux_ini_set(ini, "test space", "lk lk lk ");
- if (!faux_ini_write_file(ini, dst_fn)) {
- fprintf(stderr, "Can't write INI file %s\n", dst_fn);
- goto parse_error;
- }
- if (faux_testc_file_cmp(dst_fn, etalon_fn) != 0) {
- fprintf(stderr, "Generated file %s is not equal to etalon %s\n",
- dst_fn, etalon_fn);
- goto parse_error;
- }
- ret = 0; // success
- parse_error:
- faux_ini_free(ini);
- faux_str_free(dst_fn);
- faux_str_free(src_fn);
- faux_str_free(etalon_fn);
- return ret;
- }
- int testc_faux_ini_extract_subini(void)
- {
- // Source INI file
- const char *src_file =
- "var1=value1\n"
- "var2.a=value2\n"
- "var2.=value3\n"
- "var2.b=value4\n"
- "var3=value5\n"
- "var4=value6\n"
- "var2.c=value7\n"
- ;
- // Etalon file
- const char *etalon_file =
- "a=value2\n"
- "b=value4\n"
- "c=value7\n"
- ;
- int ret = -1; // Pessimistic return value
- faux_ini_t *ini = NULL;
- faux_ini_t *subini = NULL;
- char *src_fn = NULL;
- char *dst_fn = NULL;
- char *etalon_fn = NULL;
- // Prepare files
- src_fn = faux_testc_tmpfile_deploy_str(src_file);
- etalon_fn = faux_testc_tmpfile_deploy_str(etalon_file);
- dst_fn = faux_str_sprintf("%s/dst", getenv(FAUX_TESTC_TMPDIR_ENV));
- ini = faux_ini_new();
- if (!faux_ini_parse_file(ini, src_fn)) {
- fprintf(stderr, "Can't parse INI file %s\n", src_fn);
- goto parse_error;
- }
- if (!(subini = faux_ini_extract_subini(ini, "var2."))) {
- fprintf(stderr, "Can't extract sub-INI file %s\n", dst_fn);
- goto parse_error;
- }
- if (!faux_ini_write_file(subini, dst_fn)) {
- fprintf(stderr, "Can't write sub-INI file %s\n", dst_fn);
- goto parse_error;
- }
- if (faux_testc_file_cmp(dst_fn, etalon_fn) != 0) {
- fprintf(stderr, "Generated file %s is not equal to etalon %s\n",
- dst_fn, etalon_fn);
- goto parse_error;
- }
- ret = 0; // success
- parse_error:
- faux_ini_free(subini);
- faux_ini_free(ini);
- faux_str_free(dst_fn);
- faux_str_free(src_fn);
- faux_str_free(etalon_fn);
- return ret;
- }
|