Ejemplo n.º 1
0
int
load_config(const char *filepath) {
    SDL_RWops *fp = SDL_RWFromFile(filepath, "r");

    if (!fp) {
        LOG_ERROR("Can't open file %s\n", filepath);
        return -1;
    }

    char buffer[256];

    while (!rweof(fp)) {
        memset(buffer, 0, sizeof(buffer));
        rwgets(buffer, sizeof(buffer), fp);

        if (buffer[0] == '#')
            continue;

        char *ptr;
        char *p = strtok_r(buffer, " \r\n", &ptr);

        if (p) {
            struct ConfigItem *item = malloc(sizeof(struct ConfigItem));

            strncpy(item->key, p, MAX_CONFIGS_STR_KEY_SIZE);
            item->hash = pjw_hash(p);

            p = strtok_r(NULL, " \r\n", &ptr);

            if (isint(p)) {
                VARIANT_SET_INT(item->var, atoi(p));
            } else if (isfloat(p)) {
                VARIANT_SET_FLOAT(item->var, atof(p));
            } else {
                VARIANT_SET_STRING(item->var, p);
            }

            // TODO: check dublicate
            slist_append(configs, &item);
        }
    }

    SDL_RWclose(fp);

    return 0;
}
Ejemplo n.º 2
0
/* See documentation in header file. */
int ini_parse_file(SDL_RWops* file,
                   int (*handler)(void*, const char*, const char*,
                                  const char*),
                   void* user)
{
    /* Uses a fair bit of stack (use heap instead if you need to) */
#if INI_USE_STACK
    char line[INI_MAX_LINE];
#else
    char* line;
#endif
    char section[MAX_SECTION] = "";
    char prev_name[MAX_NAME] = "";

    char* start;
    char* end;
    char* name;
    char* value;
    int lineno = 0;
    int error = 0;

#if !INI_USE_STACK
    line = (char*)malloc(INI_MAX_LINE);
    if (!line) {
        return -2;
    }
#endif

    /* Scan through file line by line */
    while (rwgets(line, INI_MAX_LINE, file) != NULL) {
        lineno++;

        start = line;
#if INI_ALLOW_BOM
        if (lineno == 1 && (unsigned char)start[0] == 0xEF &&
                (unsigned char)start[1] == 0xBB &&
                (unsigned char)start[2] == 0xBF) {
            start += 3;
        }
#endif
        start = lskip(rstrip(start));

        if (*start == ';' || *start == '#') {
            /* Per Python ConfigParser, allow '#' comments at start of line */
        }
#if INI_ALLOW_MULTILINE
        else if (*prev_name && *start && start > line) {
            /* Non-black line with leading whitespace, treat as continuation
               of previous name's value (as per Python ConfigParser). */
            if (!handler(user, section, prev_name, start) && !error)
                error = lineno;
        }
#endif
        else if (*start == '[') {
            /* A "[section]" line */
            end = find_char_or_comment(start + 1, ']');
            if (*end == ']') {
                *end = '\0';
                strncpy0(section, start + 1, sizeof(section));
                *prev_name = '\0';
            }
            else if (!error) {
                /* No ']' found on section line */
                error = lineno;
            }
        }
        else if (*start && *start != ';') {
            /* Not a comment, must be a name[=:]value pair */
            end = find_char_or_comment(start, '=');
            if (*end != '=') {
                end = find_char_or_comment(start, ':');
            }
            if (*end == '=' || *end == ':') {
                *end = '\0';
                name = rstrip(start);
                value = lskip(end + 1);
                end = find_char_or_comment(value, '\0');
                if (*end == ';')
                    *end = '\0';
                rstrip(value);

                /* Valid name[=:]value pair found, call handler */
                strncpy0(prev_name, name, sizeof(prev_name));
                if (!handler(user, section, name, value) && !error)
                    error = lineno;
            }
            else if (!error) {
                /* No '=' or ':' found on name[=:]value line */
                error = lineno;
            }
        }

#if INI_STOP_ON_FIRST_ERROR
        if (error)
            break;
#endif
    }

#if !INI_USE_STACK
    free(line);
#endif
    //free(fileContents);
    return error;
}