这里解析weston代码的部分
/* 解析配置文件并回调 */ /* 配置文件格式 [shell] background-image=/usr/share/backgrounds/bg.jpg background-color=0xff002244 panel-color=0x90ff0000 locking=true [launcher] icon=/usr/share/icons/gnome/24x24/apps/utilities-terminal.png path=/usr/bin/gnome-terminal */ /* 实例 */ static void launcher_section_done(void *data); static const struct config_key shell_config_keys[] = { { "background-image", CONFIG_KEY_STRING, &key_background_image }, { "background-type", CONFIG_KEY_STRING, &key_background_type }, { "panel-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_panel_color }, { "background-color", CONFIG_KEY_UNSIGNED_INTEGER, &key_background_color }, { "locking", CONFIG_KEY_BOOLEAN, &key_locking }, }; static const struct config_key launcher_config_keys[] = { { "icon", CONFIG_KEY_STRING, &key_launcher_icon }, { "path", CONFIG_KEY_STRING, &key_launcher_path }, }; static const struct config_section config_sections[] = { { "shell", shell_config_keys, ARRAY_LENGTH(shell_config_keys) }, { "launcher", launcher_config_keys, ARRAY_LENGTH(launcher_config_keys), launcher_section_done } }; static void launcher_section_done(void *data) { struct desktop *desktop = data; struct output *output; if (key_launcher_icon == NULL || key_launcher_path == NULL) { fprintf(stderr, "invalid launcher section "); return; } wl_list_for_each(output, &desktop->outputs, link) { panel_add_launcher(output->panel, key_launcher_icon, key_launcher_path); } free(key_launcher_icon); key_launcher_icon = NULL; free(key_launcher_path); key_launcher_path = NULL; } int main() { config_file = config_file_path("weston.ini"); ret = parse_config_file(config_file, config_sections, ARRAY_LENGTH(config_sections), &desktop); free(config_file); } /**以下为共享部分******shared/config-parser.c*************************/ static int
handle_key(const struct config_key *key, const char *value)
{
char *end, *s;
int i, len;
unsigned int ui;
switch (key->type) {
case CONFIG_KEY_INTEGER:
i = strtol(value, &end, 0);
if (*end != ' ') {
fprintf(stderr, "invalid integer: %s ", value);
return -1;
}
*(int *)key->data = i;
return 0;
case CONFIG_KEY_UNSIGNED_INTEGER:
ui = strtoul(value, &end, 0);
if (*end != ' ') {
fprintf(stderr, "invalid integer: %s ", value);
return -1;
}
*(unsigned int *)key->data = ui;
return 0;
case CONFIG_KEY_STRING:
len = strlen(value);
while (len > 0 && isspace(value[len - 1]))
len--;
s = malloc(len + 1);
if (s == NULL)
return -1;
memcpy(s, value, len);
s[len] = ' ';
*(char **)key->data = s;
return 0;
case CONFIG_KEY_BOOLEAN:
if (strcmp(value, "false ") == 0)
*(int *)key->data = 0;
else if (strcmp(value, "true ") == 0)
*(int *)key->data = 1;
else {
fprintf(stderr, "invalid bool: %s ", value);
return -1;
}
return 0;
default:
assert(0);
break;
}
return -1;
}
int parse_config_file(const char *path, const struct config_section *sections, int num_sections, void *data) { FILE *fp; char line[512], *p; const struct config_section *current = NULL; int i; fp = fopen(path, "r"); if (fp == NULL) { fprintf(stderr, "couldn't open %s ", path); return -1; } while (fgets(line, sizeof line, fp)) { if (line[0] == '#' || line[0] == ' ') { continue; } if (line[0] == '[') { p = strchr(&line[1], ']'); if (!p || p[1] != ' ') { fprintf(stderr, "malformed " "section header: %s ", line); fclose(fp); return -1; } if (current && current->done) current->done(data); p[0] = '