File: tst-write-string-data.c

package info (click to toggle)
libeconf 0.7.9%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,644 kB
  • sloc: ansic: 7,483; python: 1,204; cs: 651; sh: 214; xml: 182; makefile: 7
file content (83 lines) | stat: -rw-r--r-- 2,520 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include "libeconf.h"

/* Test case:
 * Reading, merging and writing string data
*/

int
main(void)
{
  econf_err error;

  // Read test data
  __attribute__((cleanup(econf_freeFilep))) econf_file *kf_fromfile = NULL;
  error = econf_readFile(&kf_fromfile,
			 TESTSDIR"tst-write-string-data/usr/etc/tst-string-data.conf",
			 "=", "#");
  if (error || kf_fromfile == NULL)
  {
    fprintf (stderr, "ERROR: couldn't read /usr/etc configuration file: %s\n", econf_errString(error));
    return 1;
  }

  // Error handling if directory does not exist
  error = econf_writeFile(kf_fromfile, "not_exists/", "out.ini");
  if (error != ECONF_NOFILE) {
    fprintf (stderr, "econf_writeFile with no existing directory ERROR: expected: %s, got: %s\n",
	     econf_errString(ECONF_NOFILE), econf_errString(error));
    return 1;
  }

  // Rewrite file to disk
  econf_writeFile(kf_fromfile, TESTSDIR"tst-write-string-data/", "out.ini");

  // And reading it again
  econf_file *kf_compare = NULL;
  error = econf_readFile(&kf_compare,
			 TESTSDIR"tst-write-string-data/out.ini", "=", "#");
  if (error || kf_compare == NULL) {
    fprintf (stderr, "ERROR: couldn't read written /usr/etc configuration file: %s\n", econf_errString(error));
    return 1;
  }
  remove(TESTSDIR"tst-write-string-data/out.ini");

  // Comparing the data
  const char *compare[] = {"test", "test2", "test3", "test4", "test5", "test6"};
  for (int i = 0; i < 6; i++) {
    char *val_fromfile, *val_compare;
    econf_getStringValue(kf_fromfile, "test", compare[i], &val_fromfile);
    econf_getStringValue(kf_compare, "test", compare[i], &val_compare);
    if (val_fromfile == NULL && val_compare == NULL)
      continue;
    if (val_fromfile == NULL || val_compare == NULL) {
      fprintf (stderr, "ERROR: saved values are different\n");
      return 1;
    }
    if(strcmp(val_fromfile, val_compare) != 0) {
      fprintf (stderr, "ERROR: saved values are different '%s' - '%s'\n",
	       val_fromfile, val_compare);
      return 1;
    }
    free(val_fromfile);
    free(val_compare);
  }

  // Merge two econf_files. One value is a NULL value.
  econf_file *kf_freshini = NULL;
  econf_newIniFile(&kf_freshini);
  econf_setStringValue(kf_freshini, "test", "test", NULL);
  econf_file *kf_merged = NULL;
  econf_mergeFiles(&kf_merged, kf_freshini, kf_fromfile);

  econf_free(kf_freshini);
  econf_free(kf_merged);
  econf_free(kf_compare);

  return 0;
}