static int load_index(int fips, void **base, size_t *size) { const char *map_path = roadmap_db_map_path (); char name[30]; char filename[512]; const char *suffix = "index"; snprintf (name, sizeof (name), "%05d_%s%s", fips, suffix, ROADMAP_DATA_TYPE); roadmap_path_format (filename, sizeof (filename), map_path, name); roadmap_log(ROADMAP_DEBUG,"index filename: %s", filename); RoadMapFile file = roadmap_file_open (filename, "r"); if (!ROADMAP_FILE_IS_VALID(file)) { return -1; } *size = favail(file); *base = malloc (*size); int res = roadmap_file_read (file, *base, *size); roadmap_file_close (file); if (res != (int)*size) { free (*base); return -1; } return 0; }
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; }
static int roadmap_httpcopy (RoadMapDownloadCallbacks *callbacks, const char *source, const char *destination) { RoadMapSocket fd; RoadMapFile file; int size; int loaded; int received; char buffer[ROADMAP_HTTP_MAX_CHUNK]; fd = roadmap_net_connect("http_get", source, 80, NULL); if (!ROADMAP_NET_IS_VALID(fd)) return 0; if (roadmap_net_send(fd, "\r\n", 2, 0) == -1) return 0; received = sizeof(buffer); size = roadmap_http_decode_header (fd, buffer, &received, callbacks->error); if (size <= 0) { roadmap_net_close (fd); return 0; /* We did not get the size. */ } if (! callbacks->size (size)) { roadmap_net_close (fd); return 0; } callbacks->progress (received); roadmap_file_remove (NULL, destination); file = roadmap_file_open(destination, "w"); if (!ROADMAP_FILE_IS_VALID(file)) { roadmap_net_close (fd); return 0; } if (received > 0) { if (roadmap_file_write(file, buffer, received) != received) { callbacks->error ("Error writing data"); goto cancel_download; } } loaded = received; while (loaded < size) { received = roadmap_net_receive (fd, buffer, sizeof(buffer)); if (received <= 0) { callbacks->error ("Receive error after %d data bytes", loaded); goto cancel_download; } if (roadmap_file_write(file, buffer, received) != received) { callbacks->error ("Error writing data"); goto cancel_download; } loaded += received; callbacks->progress (loaded); } if (loaded != size) { callbacks->error ("Receive error after %d data bytes", loaded); goto cancel_download; } roadmap_net_close (fd); roadmap_file_close(file); return 1; cancel_download: roadmap_file_close(file); roadmap_file_remove (NULL, destination); roadmap_net_close (fd); return 0; }