int editor_db_open (int map_id) { char name[100]; const char *map_path; char file_name[512]; int do_read = 0; editor_log_push ("editor_db_open"); #ifndef IPHONE map_path = roadmap_db_map_path(); #else map_path = roadmap_path_preferred("maps"); #endif //IPHONE if (!map_path) { editor_log (ROADMAP_ERROR, "Can't find editor path"); editor_log_pop (); return -1; } snprintf (name, sizeof(name), "edt%05d.dat", map_id); roadmap_path_format (file_name, sizeof (file_name), map_path, name); if (roadmap_file_exists (map_path, name)) { EditorDataFile = roadmap_file_open(file_name, "rw"); do_read = 1; } else { roadmap_path_create (map_path); EditorDataFile = roadmap_file_open(file_name, "w"); roadmap_file_write (EditorDataFile, &DB_SIGNATURE, sizeof (int)); } do { if (!ROADMAP_FILE_IS_VALID(EditorDataFile)) { editor_log (ROADMAP_ERROR, "Can't open/create new database: %s/%s", map_path, name); editor_log_pop (); return -1; } if (do_read) { do_read = 0; if (editor_db_read () == -1) { editor_db_free (); //roadmap_messagebox("Error", "Offline data file is currupt: Re-Initializing data"); roadmap_log (ROADMAP_ERROR, "Offline data file is currupt: Re-Initializing data"); roadmap_file_close (EditorDataFile); roadmap_file_remove (NULL, file_name); EditorDataFile = roadmap_file_open(file_name, "w"); roadmap_file_write (EditorDataFile, &DB_SIGNATURE, sizeof (int)); } } } while (do_read); EditorActiveMap = map_id; editor_log_pop (); return 0; }
int editor_db_open (int map_id) { char name[100]; const char *map_path; char *file_name; int do_read = 0; editor_log_push ("editor_db_open"); map_path = roadmap_db_map_path(); if (!map_path) { editor_log (ROADMAP_ERROR, "Can't find editor path"); editor_log_pop (); return -1; } snprintf (name, sizeof(name), "edt%05d.dat", map_id); file_name = roadmap_path_join(map_path, name); if (roadmap_file_exists (map_path, name)) { EditorDataFile = roadmap_file_open(file_name, "rw"); do_read = 1; } else { roadmap_path_create (map_path); EditorDataFile = roadmap_file_open(file_name, "w"); } #ifdef WIN32 roadmap_path_free(map_path); #endif do { if (!ROADMAP_FILE_IS_VALID(EditorDataFile)) { editor_log (ROADMAP_ERROR, "Can't open/create new database: %s/%s", map_path, name); roadmap_path_free(file_name); editor_log_pop (); return -1; } if (do_read) { do_read = 0; if (editor_db_read () == -1) { editor_db_free (); roadmap_messagebox("Error", "Offline data file is currupt: Re-Initializing data"); roadmap_file_close (EditorDataFile); roadmap_file_remove (NULL, file_name); EditorDataFile = roadmap_file_open(file_name, "w"); } } } while (do_read); roadmap_path_free(file_name); EditorActiveMap = map_id; editor_log_pop (); return 0; }
void editor_db_close (int map_id) { if (EditorActiveMap == -1) return; assert(map_id == EditorActiveMap); editor_db_free (); roadmap_file_close(EditorDataFile); EditorDataFile = ROADMAP_INVALID_FILE; EditorActiveMap = -1; }