zoukankan      html  css  js  c++  java
  • C语言之回调函数&模块化

    这里解析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] = ''; for (i = 0; i < num_sections; i++) { if (strcmp(sections[i].name, &line[1]) == 0) { current = &sections[i]; break; } } if (i == num_sections) current = NULL; } else if (p = strchr(line, '='), p != NULL) { if (current == NULL) continue; p[0] = ''; for (i = 0; i < current->num_keys; i++) { if (strcmp(current->keys[i].name, line) == 0) { if (handle_key(&current->keys[i], &p[1]) < 0) { fclose(fp); return -1; } break; } } } else { fprintf(stderr, "malformed config line: %s ", line); fclose(fp); return -1; } } if (current && current->done) current->done(data); fclose(fp); return 0; }
  • 相关阅读:
    8-6实战蒙版
    8-5渐变及半透明蒙版
    8-4修改蒙版
    8-3建立蒙版
    imageNamed、imageWithContentsOfFile、imageWithData
    #import、#include、@class、@protocol、@interface
    JSON解析
    控制器的生命周期
    纯代码方式实现九宫格布局
    KVC笔记
  • 原文地址:https://www.cnblogs.com/cfox/p/3147682.html
Copyright © 2011-2022 走看看