File: ex_config.c

package info (click to toggle)
allegro5 2%3A5.2.10.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,820 kB
  • sloc: ansic: 109,795; cpp: 12,976; objc: 4,592; java: 2,845; python: 2,595; javascript: 1,238; sh: 1,008; makefile: 40; xml: 27; pascal: 24
file content (119 lines) | stat: -rw-r--r-- 3,337 bytes parent folder | download | duplicates (5)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 *    Example program for the Allegro library.
 *
 *    Test config file reading and writing.
 */

#include <stdio.h>
#include "allegro5/allegro.h"

#include "common.c"

static int passed = true;

#define TEST(name, expr)                  \
do {                                      \
   if (expr)                              \
      log_printf(" PASS - %s\n", name);   \
   else {                                 \
      log_printf("!FAIL - %s\n", name);   \
      passed = false;                     \
   }                                      \
} while (0)

int main(int argc, char **argv)
{
   ALLEGRO_CONFIG *cfg;
   const char *value;
   ALLEGRO_CONFIG_SECTION *iterator;
   ALLEGRO_CONFIG_ENTRY *iterator2;

   (void)argc;
   (void)argv;

   if (!al_init()) {
      abort_example("Could not init Allegro.\n");
   }
   open_log();

   cfg = al_load_config_file("data/sample.cfg");
   if (!cfg) {
      abort_example("Couldn't load data/sample.cfg\n");
   }

   value = al_get_config_value(cfg, NULL, "old_var");
   TEST("global var", value && !strcmp(value, "old global value"));

   value = al_get_config_value(cfg, "section", "old_var");
   TEST("section var", value && !strcmp(value, "old section value"));

   value = al_get_config_value(cfg, "", "mysha.xpm");
   TEST("long value", value && strlen(value) == 1394);

   /* Test removing. */
   al_set_config_value(cfg, "empty", "key_remove", "to be removed");
   al_remove_config_key(cfg, "empty", "key_remove");
   
   al_set_config_value(cfg, "schrödinger", "box", "cat");
   al_remove_config_section(cfg, "schrödinger");
   
   /* Test whether iterating through our whole sample.cfg returns all
    * sections and entries, in order.
    */

   value = al_get_first_config_section(cfg, &iterator);
   TEST("section1", value && !strcmp(value, ""));
   
   value = al_get_first_config_entry(cfg, value, &iterator2);
   TEST("entry1", value && !strcmp(value, "old_var"));
   
   value = al_get_next_config_entry(&iterator2);
   TEST("entry2", value && !strcmp(value, "mysha.xpm"));
   
   value = al_get_next_config_entry(&iterator2);
   TEST("entry3", value == NULL);

   value = al_get_next_config_section(&iterator);
   TEST("section2", value && !strcmp(value, "section"));
   
   value = al_get_first_config_entry(cfg, value, &iterator2);
   TEST("entry4", value && !strcmp(value, "old_var"));
   
   value = al_get_next_config_entry(&iterator2);
   TEST("entry5", value == NULL);

   value = al_get_next_config_section(&iterator);
   TEST("section3", value);
   
   value = al_get_first_config_entry(cfg, value, &iterator2);
   TEST("entry6", value);
   
   value = al_get_next_config_entry(&iterator2);
   TEST("entry7", value == NULL);
   
   value = al_get_next_config_section(&iterator);
   TEST("empty", value && !strcmp(value, "empty"));
   
   value = al_get_first_config_entry(cfg, value, &iterator2);
   TEST("empty entry", value == NULL);
   
   value = al_get_next_config_section(&iterator);
   TEST("section4", value == NULL);
   
   

   al_set_config_value(cfg, "", "new_var", "new value");
   al_set_config_value(cfg, "section", "old_var", "new value");

   TEST("save_config", al_save_config_file("test.cfg", cfg));

   log_printf("Done\n");

   al_destroy_config(cfg);

   close_log(true);

   return passed ? 0 : 1;
}

/* vim: set sts=3 sw=3 et: */