void load_server_markings(){ char fname[128]; FILE *fp; server_mark sm; int rf; init_server_markers(); //open server markings file safe_snprintf(fname, sizeof(fname), "servermarks_%s.dat",get_lowercase_username()); /* sliently ignore non existing file */ if (file_exists_config(fname)!=1) return; fp = open_file_config(fname,"r"); if(fp == NULL){ LOG_ERROR("%s: %s \"%s\": %s\n", reg_error_str, cant_open_file, fname, strerror(errno)); return; } while((rf=fscanf(fp,"%d %d %d %s %[^\n]s\n",&sm.id,&sm.x,&sm.y,sm.map_name,sm.text))==5){ server_mark *nm = calloc(1,sizeof(server_mark)); memcpy(nm,&sm,sizeof(server_mark)); hash_add(server_marks,(NULL+sm.id),(void*) nm); } fclose (fp); LOG_DEBUG("Read server markings from file '%s'", fname); add_server_markers(); }
/* load saved recipe names from file */ static void load_recipe_names(void) { char fname[128]; FILE *fp; char line [128]; size_t recipe_no; recipe_names_changed = 0; safe_snprintf(fname, sizeof(fname), "recipes_%s.names",username_str); my_tolower(fname); /* sliently ignore non existing file */ if (!file_exists_config(fname)) return; fp = open_file_config(fname,"r"); if(fp == NULL) { LOG_ERROR("%s() %s \"%s\": %s\n", __FUNCTION__, cant_open_file, fname, strerror(errno)); return; } recipe_no = 0; while (fgets(line, sizeof(line), fp) != NULL) { size_t len = strlen(line); while ((len > 0) && ((line[len-1] == '\r') || (line[len-1] == '\n') || (line[len-1] == ' '))) { line[len-1] = '\0'; len--; } if (len > 0) new_recipe_name(recipe_no, line); recipe_no++; } fclose(fp); }
/* load recipes, done just after login when we have the player name */ void load_recipes (){ char fname[128]; FILE *fp; size_t i; int logged = 0; off_t file_size; const size_t recipe_size = sizeof(item)*NUM_MIX_SLOTS; if (recipes_loaded) { /* * save existing recipes instead of loading them if we are already logged in * this will take place when relogging after disconnection */ save_recipes(); save_recipe_names(); return; } safe_snprintf(fname, sizeof(fname), "recipes_%s.dat",get_lowercase_username()); /* get file length, if a valid length adjust the number of recipe slots if required */ file_size = get_file_size_config(fname); if ((file_size > 0) && (file_size % recipe_size == 0)) { int num_recipes_in_file = file_size / recipe_size - 1; // -1 as last is current in pipline if ((num_recipes_in_file > wanted_num_recipe_entries) && (num_recipes_in_file < max_num_recipe_entries)) { wanted_num_recipe_entries = num_recipes_in_file; set_var_OPT_INT("wanted_num_recipe_entries", wanted_num_recipe_entries); } } /* allocate and initialise the recipe store */ num_recipe_entries = max_prev_num_recipe_entries = wanted_num_recipe_entries; recipes_store = (recipe_entry *)calloc(num_recipe_entries, sizeof(recipe_entry)); if (recipes_store == NULL) { max_prev_num_recipe_entries = num_recipe_entries = 0; return; } recipes_loaded=1; init_recipe_names(); /* if the file exists but is not a valid size, don't use it */ if ((file_size > 0) && (file_size % recipe_size != 0)) { LOG_ERROR("%s: Invalid format (size mismatch) \"%s\"\n", reg_error_str, fname); return; } /* sliently ignore non existing file */ if (file_exists_config(fname)!=1) return; fp = open_file_config(fname,"rb"); if(fp == NULL){ LOG_ERROR("%s: %s \"%s\": %s\n", reg_error_str, cant_open_file, fname, strerror(errno)); return; } /* attempt to read all the recipies we're expecting */ for (i=0; !feof(fp) && i<num_recipe_entries; i++) { if (fread (recipes_store[i].items,recipe_size,1, fp) != 1) { if (!logged) { LOG_ERROR("%s() fail during read of file [%s] : %s\n", __FUNCTION__, fname, strerror(errno)); logged = 1; } memset(recipes_store[i].items, 0, recipe_size); break; } fix_recipe_uids(recipes_store[i].items); } /* if there is another, use it as the current recipe in the manufacturing pipeline */ if (!feof(fp)) { if (fread (manu_recipe.items,recipe_size,1, fp) != 1) memset(manu_recipe.items, 0, recipe_size); else fix_recipe_uids(manu_recipe.items); } fclose (fp); load_recipe_names(); }