static int32_t constdb_load(FILE * file_stm, void * db_mem, int32_t db_size) { const_t * db = (const_t *) db_mem; int32_t cnt = DB_BEGIN; char buf[BUF_SIZE]; char fld[BUF_SIZE]; int32_t read_buf = 0; int32_t read_fld = 0; int32_t data_fld = 0; while(fgets(buf, BUF_SIZE, file_stm) != NULL) { read_buf = 0; read_fld = 0; data_fld = 0; while(1) { /* check if delimiter for field */ if(isspace(buf[read_buf]) || buf[read_buf] == ',' || buf[read_buf] == '\0') { fld[read_fld] = '\0'; switch(data_fld) { case 0: db[cnt].name = convert_string(fld); break; case 1: /* constant can be represented as hexadecimal or decimal */ if(strlen(fld) > 2 && fld[0] == '0' && fld[1] == 'x') db[cnt].value = convert_integer(fld, 16); else db[cnt].value = convert_integer(fld, 10); break; case 2: db[cnt].type = convert_integer(fld, 10); break; default: fprintf(stdout,"warn: constdb_load; invalid field column %s in %s", fld, buf); break; } read_fld = 0; data_fld++; /* skip all other whitespace */ while(isspace(buf[read_buf+1]) && buf[read_buf+1] != '\0') read_buf++; } else { fld[read_fld] = buf[read_buf]; read_fld++; } /* finish reading the item */ if(buf[read_buf] == '\0' || buf[read_buf] == '\n') break; read_buf++; } /* check for missing fields */ if(data_fld < CONST_COLUMNS_MIN || data_fld > CONST_COLUMNS_MAX) fprintf(stdout,"warn: constdb_load; missing field expected %d < %d < %d %s", CONST_COLUMNS_MIN, data_fld, CONST_COLUMNS_MAX, buf); cnt++; /* check for exceed size of allocated memory */ if(cnt > db_size) { fprintf(stdout,"warn: constdb_load; exceeding the size of the database; %d < %d.\n%s\n", db_size, cnt, buf); exit(EXIT_FAILURE); } } return cnt; }
int main() { int i,cnt,sum=0; char number[100]="\0"; for(i=1;i<1000;i++) { number[0]=0; cnt = convert_integer(i, number); printf("%4d: %s %d\n",i, number, cnt); sum +=cnt; } printf("1000: OneThousand %d\n",i, number, strlen("onethousand")); printf("Total: %d\n", sum+strlen("onethousand")); return 1; }
static int32_t skilldb_load(FILE * file_stm, void * db_mem, int32_t db_size) { skill_t * db = (skill_t *) db_mem; int32_t cnt = DB_BEGIN; char buf[BUF_SIZE]; char fld[BUF_SIZE]; int32_t read_buf = 0; int32_t read_fld = 0; int32_t data_fld = 0; while(fgets(buf, BUF_SIZE, file_stm) != NULL) { read_buf = 0; read_fld = 0; data_fld = 0; while(1) { /* check if delimiter for field */ if(buf[read_buf] == ',' || buf[read_buf] == '\n' || buf[read_buf] == '\0') { fld[read_fld] = '\0'; switch(data_fld) { case 0: db[cnt].id = convert_integer(fld,10); break; case 1: convert_integer_list(fld, ":", &(db[cnt].range)); break; case 2: db[cnt].hit = convert_integer(fld,10); break; case 3: db[cnt].inf = convert_integer(fld,10); break; case 4: convert_integer_list(fld, ":", &(db[cnt].element)); break; case 5: db[cnt].nk = convert_uinteger(fld,16); break; case 6: convert_integer_list(fld, ":", &(db[cnt].splash)); break; case 7: db[cnt].max = convert_integer(fld,10); break; case 8: convert_integer_list(fld, ":", &(db[cnt].hit_amount)); break; case 9: db[cnt].cast_cancel = convert_string(fld); break; case 10: db[cnt].cast_def_reduce_rate = convert_integer(fld,10); break; case 11: db[cnt].inf2 = convert_uinteger(fld,16); break; case 12: convert_integer_list(fld, ":", &(db[cnt].maxcount)); break; case 13: db[cnt].type = convert_string(fld); break; case 14: convert_integer_list(fld, ":", &(db[cnt].blow_count)); break; case 15: db[cnt].inf3 = convert_uinteger(fld,16); break; case 16: db[cnt].name = convert_string(fld); break; case 17: db[cnt].desc = convert_string(fld); break; default: fprintf(stdout,"warn: skilldb_load; invalid field column %s in %s", fld, buf); break; } read_fld = 0; data_fld++; } else { /* skip initial whitespace */ if(!(isspace(buf[read_buf]) && read_fld <= 0)) { fld[read_fld] = buf[read_buf]; read_fld++; } } /* finish reading the item */ if(buf[read_buf] == '\0' || buf[read_buf] == '\n') break; read_buf++; } /* check for missing fields */ if(data_fld != SKILL_COLUMN) fprintf(stdout,"warn: skilldb_load; missing field expected %d got %d; %s", SKILL_COLUMN, data_fld, buf); cnt++; /* check for exceed size of allocated memory */ if(cnt > db_size) { fprintf(stdout,"warn: skilldb_load; exceeding the size of the database; %d < %d.\n%s\n", db_size, cnt, buf); exit(EXIT_FAILURE); } } return cnt; }
/* Load a eAthena / rAthena item database file // into main memory for processing. // char * filename - Path directory to the item database // file, i.e. item_db.txt; both relative and absolute // directory paths. */ item_w * itemdb_init(const char * filename) { /* item wrapper data */ item_w * item_wrapper = NULL; item_t * item_database = NULL; char * item_filename = NULL; int32_t item_size = 0; int32_t item_count = DB_BEGIN; /* item load data */ FILE * item_file = NULL; char item_buf[BUF_SIZE]; /* item buffer */ char item_fld[BUF_SIZE]; /* field buffer */ int32_t read_buf = 0; /* current item buffer index */ int32_t read_fld = 0; /* current item field index */ int32_t script_level = 0; /* script nesting using brackets */ int32_t field_level = 0; /* current item field, i.e. id, aegis, eathena, .. */ /* filter the database for valid entries per line */ item_filename = itemdb_trim(filename, &item_size); if(item_filename == NULL) { fprintf(stdout,"warn: itemdb_load failed to filter original database file %s.\n", filename); return NULL; } /* check if the file have at least one valid entry */ if(item_size <= 0) { fprintf(stdout,"warn: itemdb_load failed to detect valid entries in the original database file %s\n", filename); if(item_filename != NULL) free(item_filename); return NULL; } /* allocate memory for the database */ item_database = malloc(sizeof(item_t) * (item_size + DB_BEGIN)); if(item_database == NULL) { fprintf(stdout,"warn: itemdb_load failed to allocate memory for database.\n"); if(item_filename != NULL) free(item_filename); return NULL; } /* open filtered database for reading */ item_file = fopen(item_filename, "r"); if(item_file == NULL) { fprintf(stdout,"warn: itemdb_load failed to open filtered database file %s.\n", item_filename); if(item_database != NULL) free(item_database); if(item_filename != NULL) free(item_filename); return NULL; } /* load the database into memory */ while(fgets(item_buf, BUF_SIZE, item_file) != NULL) { read_buf = 0; read_fld = 0; script_level = 0; field_level = 0; /* initialize item entry */ item_database[item_count].id = FIELD_INT_EMPTY; item_database[item_count].aegis = NULL; item_database[item_count].eathena = NULL; item_database[item_count].type = FIELD_INT_EMPTY; item_database[item_count].buy = FIELD_INT_EMPTY; item_database[item_count].sell = FIELD_INT_EMPTY; item_database[item_count].weight = FIELD_INT_EMPTY; item_database[item_count].atk = FIELD_INT_EMPTY; item_database[item_count].matk = FIELD_INT_EMPTY; item_database[item_count].def = FIELD_INT_EMPTY; item_database[item_count].range = FIELD_INT_EMPTY; item_database[item_count].slots = FIELD_INT_EMPTY; item_database[item_count].job = FIELD_INT_EMPTY; item_database[item_count].upper = FIELD_INT_EMPTY; item_database[item_count].gender = FIELD_INT_EMPTY; item_database[item_count].loc = FIELD_INT_EMPTY; item_database[item_count].wlv = FIELD_INT_EMPTY; item_database[item_count].elv = FIELD_INT_EMPTY; item_database[item_count].refineable = FIELD_INT_EMPTY; item_database[item_count].view = FIELD_INT_EMPTY; item_database[item_count].script = NULL; item_database[item_count].onequip = NULL; item_database[item_count].onunequip = NULL; /* read the item entry */ while(1) { /* check if entering script */ if(item_buf[read_buf] == '{') script_level++; /* check if leaving script */ else if(item_buf[read_buf] == '}') script_level--; /* check if delimiter for field */ if(!script_level && (item_buf[read_buf] == ',' || item_buf[read_buf] == '\n' || item_buf[read_buf] == '\0')) { item_fld[read_fld] = '\0'; switch(field_level) { case 0: item_database[item_count].id = convert_integer(item_fld, 10); break; case 1: item_database[item_count].aegis = convert_string(item_fld); break; case 2: item_database[item_count].eathena = convert_string(item_fld); break; case 3: item_database[item_count].type = convert_integer(item_fld, 10); break; case 4: item_database[item_count].buy = convert_integer(item_fld, 10); break; case 5: item_database[item_count].sell = convert_integer(item_fld, 10); break; case 6: item_database[item_count].weight = convert_integer(item_fld, 10); break; case 7: convert_delimit_integer(item_fld, ':', 2, &item_database[item_count].atk, &item_database[item_count].matk); break; case 8: item_database[item_count].def = convert_integer(item_fld, 10); break; case 9: item_database[item_count].range = convert_integer(item_fld, 10); break; case 10: item_database[item_count].slots = convert_integer(item_fld, 10); break; case 11: item_database[item_count].job = convert_uinteger(item_fld, 16); break; case 12: item_database[item_count].upper = convert_integer(item_fld, 10); break; case 13: item_database[item_count].gender = convert_integer(item_fld, 10); break; case 14: item_database[item_count].loc = convert_integer(item_fld, 10); break; case 15: item_database[item_count].wlv = convert_integer(item_fld, 10); break; case 16: item_database[item_count].elv = convert_integer(item_fld, 10); break; case 17: item_database[item_count].refineable = convert_integer(item_fld, 10); break; case 18: item_database[item_count].view = convert_integer(item_fld, 10); break; case 19: item_database[item_count].script = convert_string(item_fld); break; case 20: item_database[item_count].onequip = convert_string(item_fld); break; case 21: item_database[item_count].onunequip = convert_string(item_fld); break; default: fprintf(stdout,"warn: itemdb_load invalid field column %s\n", item_fld); break; } read_fld = 0; field_level++; /* copy from item buffer to field buffer */ } else { item_fld[read_fld] = item_buf[read_buf]; read_fld++; } /* finish reading the item */ if(item_buf[read_buf] == '\0' || item_buf[read_buf] == '\n') break; read_buf++; } /* check for missing fields */ if(field_level != ITEM_COLUMNS) fprintf(stdout,"warn: itemdb_load missing field expected %d got %d from %s: %s", ITEM_COLUMNS, field_level, filename, item_buf); /* check for missing brackets */ if(script_level) fprintf(stdout,"warn: itemdb_load missing bracket %s: %s", filename, item_buf); item_count++; } /* clean up loading resources */ fclose(item_file); remove(item_filename); free(item_filename); /* return the item wrapper */ item_wrapper = malloc(sizeof(item_w)); item_wrapper->db = item_database; item_wrapper->size = item_size + DB_BEGIN; return item_wrapper; }
static int32_t producedb_load(FILE * file_stm, void * db_mem, int32_t db_size) { produce_t * db = (produce_t *) db_mem; int32_t cnt = DB_BEGIN; char buf[BUF_SIZE]; char fld[BUF_SIZE]; int32_t read_buf = 0; int32_t read_fld = 0; int32_t data_fld = 0; int32_t fld_cnt = 0; int32_t material_cnt = 0; int32_t alternate = 0; while(fgets(buf, BUF_SIZE, file_stm) != NULL) { read_buf = 0; read_fld = 0; data_fld = 0; fld_cnt = 0; material_cnt = 0; alternate = 0; /* retrieve the total number of materials */ while(buf[read_buf] != '\0') { if(buf[read_buf] == ',') fld_cnt++; read_buf++; } read_buf = 0; fld_cnt -= PRODUCE_HEADER; fld_cnt /= 2; /* check if materials exist */ if(fld_cnt <= 0) { fprintf(stdout,"warn: producedb_load missing material or amount %s",buf); continue; } /* allocate memory for material and amount */ db[cnt].material = malloc(sizeof(int32_t) * fld_cnt); if(db[cnt].material == NULL) { fprintf(stdout,"warn: producedb_load failed to allocate memory for material."); continue; } db[cnt].amount = malloc(sizeof(int32_t) * fld_cnt); if(db[cnt].amount == NULL) { fprintf(stdout,"warn: producedb_load failed to allocate memory for amount."); continue; } db[cnt].count = fld_cnt; while(1) { /* check if delimiter for field */ if(buf[read_buf] == ',' || buf[read_buf] == '\n' || buf[read_buf] == '\0') { fld[read_fld] = '\0'; switch(data_fld) { case 0: db[cnt].item_id = convert_integer(fld,10); break; case 1: db[cnt].item_lv = convert_integer(fld,10); break; case 2: db[cnt].req_skill = convert_integer(fld,10); break; case 3: db[cnt].req_skill_lv = convert_integer(fld,10); break; default: if(!alternate) db[cnt].material[material_cnt] = convert_integer(fld,10); else { db[cnt].amount[material_cnt] = convert_integer(fld,10); material_cnt++; } alternate = !alternate; break; } read_fld = 0; data_fld++; } else { /* skip initial whitespace */ if(!(isspace(buf[read_buf]) && read_fld <= 0)) { fld[read_fld] = buf[read_buf]; read_fld++; } } /* finish reading the item */ if(buf[read_buf] == '\0' || buf[read_buf] == '\n') break; read_buf++; } cnt++; /* check for exceed size of allocated memory */ if(cnt > db_size) { fprintf(stdout,"warn: producedb_load; exceeding the size of the database; %d < %d.\n%s\n", db_size, cnt, buf); exit(EXIT_FAILURE); } } return cnt; }
extern "C" char* itoa(int num, char* dest, int base) { convert_integer(dest, num, base, "0123456789abcdefghijklmnopqrstuvwxyz"); return dest; }
static void emit_item(FILE *of, egg_token *item) { char *s; egg_token *atom; egg_token *phrase_name; egg_token *literal; egg_token *absolute; egg_token *integer; egg_token *quoted; egg_token *string; egg_token *qty; egg_token *qty_option; egg_token *qty_item; egg_token *asterisk; long i; // Sanity check parameters if (!of) return; if (!item) return; if (item->type != egg_token_type_item) return; // Emit XML for this item fprintf(of, "%8.8s", " "); fprintf(of, "<item>\n"); // Look for the atom in this item if (atom = egg_token_find(item->descendant, egg_token_type_atom)) { // If this atom is a phrase-name, then emit the XML for it if (phrase_name = egg_token_find(atom->descendant, egg_token_type_phrase_name)) { fprintf(of, "%10.10s", " "); fprintf(of, "<phrase-name>"); fprintf(of, "%s", s = egg_token_to_string(phrase_name->descendant, NULL)); free(s); fprintf(of, "</phrase-name>"); fprintf(of, "\n"); } // If the atom is a literal, then process the literal else if (literal = egg_token_find(item->descendant, egg_token_type_literal)) { // If this is an absolute (integer) literal emit the XML for it if (absolute = egg_token_find(literal->descendant, egg_token_type_absolute_literal)) { if (integer = egg_token_find(absolute->descendant, egg_token_type_integer)) { fprintf(of, "%10.10s", " "); fprintf(of, "<literal type=\"absolute\">"); fprintf(of, "%ld", convert_integer(integer)); fprintf(of, "</literal>"); fprintf(of, "\n"); } } // If this is a string literal, then process that else if (quoted = egg_token_find(literal->descendant, egg_token_type_quoted_literal)) { // Emit XML for quoted string if (string = egg_token_find(quoted->descendant, egg_token_type_quoted_character)) { fprintf(of, "%10.10s", " "); fprintf(of, "<literal type=\"string\">"); s = egg_token_to_string(string, NULL); if (strrchr(s, '"')) *(strrchr(s, '"')) = 0; fprintf(of, "%s", s); free(s); fprintf(of, "</literal>"); fprintf(of, "\n"); } } // Emit XML for single quoted string else if (quoted = egg_token_find(literal->descendant, egg_token_type_single_quoted_literal)) { if (string = egg_token_find(quoted->descendant, egg_token_type_single_quoted_character)) { fprintf(of, "%10.10s", " "); fprintf(of, "<literal type=\"string\">"); s = egg_token_to_string(string, NULL); if (strrchr(s, '\'')) *(strrchr(s, '\'')) = 0; fprintf(of, "%s", s); free(s); fprintf(of, "</literal>"); fprintf(of, "\n"); } } } } // Process any quantifier that may exist for this item if (qty = egg_token_find(item->descendant, egg_token_type_quantifier)) { if (integer = egg_token_find(qty->descendant, egg_token_type_integer)) { fprintf(of, "%10.10s", " "); fprintf(of, "<quantity>\n"); // We always have a minimum for any quantifier i = convert_integer(integer); fprintf(of, "%12.12s", " "); fprintf(of, "<minimum>%ld</minimum>\n", i); if (qty_option = egg_token_find(qty->descendant, egg_token_type_quantifier_option)) { if (qty_item = egg_token_find(qty_option->descendant, egg_token_type_quantifier_item)) { // We may have an optional maximum fprintf(of, "%12.12s", " "); fprintf(of, "<maximum>"); // Absolute (integer) maximum if (integer = egg_token_find(qty_item->descendant, egg_token_type_integer)) { fprintf(of, "%ld", convert_integer(integer)); } // Infinite maximum else if (asterisk = egg_token_find(qty_item->descendant, egg_token_type_asterisk)) { fprintf(of, "unlimited"); } fprintf(of, "</maximum>"); fprintf(of, "\n"); } } // Emit quantifier closure fprintf(of, "%10.10s", " "); fprintf(of, "</quantity>"); fprintf(of, "\n"); } } // Emit item closure fprintf(of, "%8.8s", " "); fprintf(of, "</item>"); fprintf(of, "\n"); return; }