void ini_add_item(ini_t *ini, gchar *section, gchar *key, gchar *value) { if (!g_hash_table_contains(ini->sections, section)) { ini_add_section(ini, section); // XXX: This may cause some weird memory issues (who owns section?) } GHashTable *sect = (GHashTable *)g_hash_table_lookup(ini->sections, section); g_hash_table_insert(sect, key, value); }
void parse_section_header(ini_parser_t *parser) { // XXX: Assert that current token is left bracket? next_token(parser); gchar *name; gsize len = 0; switch (parser->curr_token->type) { case INI_TOKEN_BRACKET_RIGHT: name = g_strndup("", 0); break; case INI_TOKEN_TEXT: len = parser->curr_token->text.len; name = g_strndup(parser->curr_token->text.value, len); break; default: // Raise error break; } if (next_token(parser)->type == INI_TOKEN_BRACKET_RIGHT) { parser->curr_section = name; ini_add_section(parser->ini, name); } else { g_free(name); // TODO: Raise error } }
/* 最初のセクションの解釈 */ static void ini_parse_first_section(Ini *ini, const char **data) { char *line; /* 空行をとばす */ while (1) { line = sgetline(data); if (line == NULL) return; if (!isingnorableline(line)) break; free(line); } ini->current = section_parse(line); free(line); if (ini->current == NULL) return; if (ini_add_section(ini, ini->current)) section_delete(ini->current); }
static void ini_parse_sections_and_keys(Ini *ini, const char *data) { Key *key; Section *section; char *line; while ((line = sgetline(&data)) != NULL) { if (isingnorableline(line)) continue; /* キーとして解釈 */ if ((key = key_parse(line)) != NULL) { free(line); if (section_add_key(ini->current, key)) { key_delete(key); return; } continue; } /* セクションとして解釈 */ if ((section = section_parse(line)) != NULL) { ini->current = section; free(line); if (ini_add_section(ini, ini->current)) { section_delete(ini->current); return; } continue; } free(line); return; /* 無効な文字列の場合にここまで来る */ } }
int ini_read_ini(s_ini_handle* hdl) { unsigned int i, current_line; s_ini_section* current_section = 0; char* current_section_name = 0; char* current_key_name = 0; char* current_value = 0; unsigned int name_start, value_start; int current_state; if (hdl == 0) return -1; if (hdl->buffer == 0) return -1; /* printf("ini_read_ini\n"); */ current_state = STATE_EMPTY; name_start = 0; value_start = 0; current_line = 1; for (i = 0; i < hdl->buffer_size; ++i) { switch (current_state) { case STATE_EMPTY: switch (hdl->buffer[i]) { case '[': name_start = i; current_state = STATE_SECTION_NAME; if (current_section_name != 0) { free(current_section_name); current_section_name = 0; } break; case ' ': case '\t': case '\n': case '\r': /* Skip white spaces */ break; case '#': /* Skip comment line */ current_state = STATE_SKIP; break; default: if ( IS_ALPHA_NUMERIC(hdl->buffer[i]) ) { current_state = STATE_KEYNAME; name_start = i; if (current_key_name != 0) { free(current_key_name); current_key_name = 0; } } else { printf("Unkown char %c found at line %d\n", hdl->buffer[i], current_line); } break; } break; case STATE_SECTION_NAME: switch(hdl->buffer[i]) { case '\r': case '\n': printf("Misformated INI file (missing ']') at line %d\n", current_line); case ']': { unsigned int section_name_len = i - (name_start); current_section_name = (char*)malloc(section_name_len); memcpy(current_section_name, &hdl->buffer[name_start+1], section_name_len); current_section_name[section_name_len-1] = 0x00; /* printf("Found section: '%s' at line %d\n", current_section_name, current_line); */ current_section = ini_add_section(hdl, current_section_name); current_section_name = 0; /* prevent current_section_name from being freed */ current_state = STATE_EMPTY; } break; } break; case STATE_KEYNAME: if (hdl->buffer[i] == '=') { unsigned int key_name_len = i - name_start + 1; current_key_name = (char*)malloc(key_name_len); memcpy(current_key_name, &hdl->buffer[name_start], key_name_len); current_key_name[key_name_len-1] = 0x00; /* printf("Found key: '%s' at line %d\n", current_key_name, current_line); */ value_start = i; if (current_value != 0) { free(current_value); current_value = 0; } current_state = STATE_VALUE; } else if (hdl->buffer[i] == '\n' || hdl->buffer[i] == '\r') { printf("Missing '=' at line %d\n", current_line); current_state = STATE_SKIP; } #if 0 else if ( IS_ALPHA_NUMERIC(hdl->buffer[i]) || IS_POINT(hdl->buffer[i]) || hdl->buffer[i] == '[' || hdl->buffer[i] == ']' ) { /* Do nothing */ } else { printf("Unkown char %c found at line %d\n", hdl->buffer[i], current_line); current_state = STATE_SKIP; } #endif break; case STATE_VALUE: if (hdl->buffer[i] == '\n' || hdl->buffer[i] == '\r') { unsigned int value_len = i - value_start; current_value = (char*)malloc(value_len); memcpy(current_value, &hdl->buffer[value_start+1], value_len); current_value[value_len-1] = 0x00; /* printf("Value: %s\n", current_value); */ current_state = STATE_EMPTY; ini_add_parameter(hdl, current_section, current_key_name, current_value); current_key_name = 0; current_value = 0; } #if 0 else if ( IS_ALPHA_NUMERIC(hdl->buffer[i]) || IS_POINT(hdl->buffer[i]) || hdl->buffer[i] == '[' || hdl->buffer[i] == ']' ) { /* Do nothing */ } else { printf("Unkown char %c found at line %d\n", hdl->buffer[i], current_line); current_state = STATE_SKIP; } #endif break; case STATE_SKIP: if (hdl->buffer[i] == '\n' || hdl->buffer[i] == '\r') { current_state = STATE_EMPTY; } break; } if (hdl->buffer[i] == '\n') { ++current_line; } } /* Free used memory */ if (current_section_name != 0) { free(current_section_name); } if (current_value != 0) { free(current_value); } if (current_key_name != 0) { free(current_key_name); } return 0; }