testc_ini.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "faux/ini.h"
  4. #include "faux/testc_helpers.h"
  5. int testc_faux_ini_good(void) {
  6. char *path = NULL;
  7. path = getenv("FAUX_INI_PATH");
  8. if (path)
  9. printf("Env var is [%s]\n", path);
  10. return 0;
  11. }
  12. int testc_faux_ini_bad(void) {
  13. printf("Some debug information here\n");
  14. return -1;
  15. }
  16. int testc_faux_ini_signal(void) {
  17. char *p = NULL;
  18. printf("%s\n", p);
  19. return -1;
  20. }
  21. int testc_faux_ini_parse(void) {
  22. // Source INI file
  23. const char *src_file =
  24. "# Comment\n"
  25. "DISTRIB_ID=Ubuntu\n"
  26. "DISTRIB_RELEASE=18.04\n"
  27. "DISTRIB_CODENAME=bionic\n"
  28. "DISTRIB_DESCRIPTION=\"Ubuntu 18.04.4 LTS\"\n"
  29. "COMPLEX_VAR=\" Ubuntu\t\t1818 \"\n"
  30. "WO_QUOTES_VAR = qwerty\n"
  31. "WO_QUOTES_VAR2 = qwerty 98989898\n"
  32. "EMPTY_VAR3 = \n"
  33. "EMPTY_VAR4 =\n"
  34. " EMPTY_VAR5 = \"\"\t \n"
  35. " ANOTHER_VAR6 = \"Normal var\"\t \n"
  36. "\tTABBED_VAR = \"Normal tabbed var\"\t \n"
  37. "# Another comment\n"
  38. " # Yet another comment\n"
  39. "\t# Tabbed comment\n"
  40. "VAR_WITHOUT_EOL=zxcvbnm"
  41. ;
  42. // Etalon file
  43. const char *etalon_file =
  44. "ANOTHER_VAR6=\"Normal var\"\n"
  45. "COMPLEX_VAR=\" Ubuntu\t\t1818 \"\n"
  46. "DISTRIB_CODENAME=bionic\n"
  47. "DISTRIB_DESCRIPTION=\"Ubuntu 18.04.4 LTS\"\n"
  48. "DISTRIB_ID=Ubuntu\n"
  49. "DISTRIB_RELEASE=18.04\n"
  50. "TABBED_VAR=\"Normal tabbed var\"\n"
  51. "VAR_WITHOUT_EOL=zxcvbnm\n"
  52. "WO_QUOTES_VAR=qwerty\n"
  53. "WO_QUOTES_VAR2=qwerty\n"
  54. "\"test space\"=\"lk lk lk \"\n"
  55. ;
  56. faux_ini_t *ini = NULL;
  57. faux_ini_node_t *iter = NULL;
  58. const faux_pair_t *pair = NULL;
  59. const char *src_fn = NULL;
  60. const char *dst_fn = "/tmp/dst12";
  61. const char *etalon_fn = NULL;
  62. unsigned num_entries = 0;
  63. // Prepare files
  64. src_fn = faux_testc_tmpfile_deploy(src_file);
  65. if (!src_fn) {
  66. fprintf(stderr, "Can't create test file %s\n", src_fn);
  67. }
  68. etalon_fn = faux_testc_tmpfile_deploy(etalon_file);
  69. if (!src_fn) {
  70. fprintf(stderr, "Can't create etalon file %s\n", etalon_fn);
  71. }
  72. ini = faux_ini_new();
  73. faux_ini_parse_file(ini, src_fn);
  74. iter = faux_ini_iter(ini);
  75. while ((pair = faux_ini_each(&iter))) {
  76. num_entries++;
  77. printf("[%s] = [%s]\n", faux_pair_name(pair), faux_pair_value(pair));
  78. }
  79. if (10 != num_entries) {
  80. fprintf(stderr, "Wrong number of entries %u\n", num_entries);
  81. faux_ini_free(ini);
  82. return -1;
  83. }
  84. faux_ini_set(ini, "test space", "lk lk lk ");
  85. faux_ini_write_file(ini, dst_fn);
  86. faux_ini_free(ini);
  87. return 0;
  88. }