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;
}
Beispiel #2
0
int roadmap_sound_list_add_buf (RoadMapSoundList list, void* buf, size_t size )
{
   char path[512];
   int file_num = list->count;
   RoadMapFile file;

   if (list->count == MAX_SOUND_LIST) return SND_LIST_ERR_LIST_FULL;

   list->buf_list[list->count] = buf;
   list->buf_list_sizes[list->count] = size;


   /*
    * Temporary solution - write the buffer to the file for further playing
    * AGA
    */
   sprintf( path, "%s/tmp/%d", roadmap_path_tts(), file_num );
   if ( file_num == 0 )
   {
      roadmap_path_create( roadmap_path_parent( path, NULL ) );
   }

   file = roadmap_file_open( path, "w" );
   roadmap_file_write( file, buf, size );
   roadmap_file_close( file );

   strncpy_safe( list->list[list->count], path, 512 );


   list->count++;

   return list->count - 1;
}
Beispiel #3
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;
}
Beispiel #4
0
/*  Name        : download_done_callback( void *context )
 *  Purpose     : Download callback: Done
 *
 */
static void download_done_callback( void *context_cb, char *last_modified, const char *format, ...  )
{
    DownloadContext* context = (DownloadContext*) context_cb;
    const char* path = context->voice_path;
    RoadMapFile file;

    roadmap_log( ROADMAP_INFO, "Download is finished. Writing %d bytes to the file: %s", context->data_size, path );

    // Save the voice to the file
    file = roadmap_file_open( path, "w" );
    if ( !file )
    {
        roadmap_log( ROADMAP_WARNING, "File openning error for file: %s", path );
    }

    roadmap_file_write( file, context->data, context->data_size );

    roadmap_file_close(file);

    ssd_progress_msg_dialog_hide();

    context->download_cb( context->context_cb, 0, context->voice_path );

    // Add file to cache
    download_cache_add( path );

    // Deallocate the download context
    free( context->data );
    roadmap_path_free( context->voice_path );
    free( context );
}
Beispiel #5
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;
}
Beispiel #6
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;
}
Beispiel #7
0
void  roadmap_io_close (RoadMapIO *io) {

   switch (io->subsystem) {

      case ROADMAP_IO_FILE:
         roadmap_file_close (io->os.file);
         break;

      case ROADMAP_IO_NET:
         roadmap_net_close (io->os.socket);
         break;

      case ROADMAP_IO_PIPE:
         roadmap_spawn_close_pipe (io->os.pipe);
         break;

      case ROADMAP_IO_NULL:
         break;
   }
   roadmap_io_invalidate( io );
}
Beispiel #8
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;
}