Beispiel #1
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 #2
0
static int editor_db_write_committed (editor_db_section *section, int id) {

	unsigned int type_id = section->type_id | TYPE_COMMITTED_FLAG;
	
   if (roadmap_file_write(EditorDataFile, &type_id, sizeof(type_id)) < 0)
      return -1;

   if (roadmap_file_write(EditorDataFile, &id, sizeof(int)) < 0)
      return -1;

	return 0;	
}
Beispiel #3
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 #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 roadmap_io_write (RoadMapIO *io, const void *data, int length, int wait) {

   switch (io->subsystem) {

      case ROADMAP_IO_FILE:
         return roadmap_file_write (io->os.file, data, length);

      case ROADMAP_IO_NET:
         return roadmap_net_send (io->os.socket, data, length, wait);

      case ROADMAP_IO_PIPE:
         return roadmap_spawn_write_pipe (io->os.pipe, data, length);

      case ROADMAP_IO_NULL:
         return length; /* It's all done, since there is nothing to do. */
   }
   return -1;
}
Beispiel #6
0
static int editor_db_write_record (editor_db_section *section, char *data, int item_id, int count) {

   static int flush_count;
   unsigned int type_id = section->type_id;
   int align;
   char dummy[EDITOR_DB_ALIGN - 1];

   if (item_id != -1) {
      type_id |= TYPE_UPDATE_FLAG;

   } else if (count > 1) {
      type_id |= TYPE_MULTIPLE_FLAG;
   }

   if (roadmap_file_write(EditorDataFile, &type_id, sizeof(type_id)) < 0)
      return -1;

   if ((item_id != -1) &&
         (roadmap_file_write(EditorDataFile, &item_id, sizeof(item_id)) < 0))
      return -1;

   if ((count > 1) &&
         (roadmap_file_write(EditorDataFile, &count, sizeof(count)) < 0))
      return -1;

	if (section->flag_committed) {
	   if (roadmap_file_write(EditorDataFile, data, section->item_offset) < 0)
	         return -1;
	}

   if (roadmap_file_write(EditorDataFile, data + section->item_offset, section->item_size * count) < 0)
         return -1;

   align = (count * section->record_size) % EDITOR_DB_ALIGN;
   if (align) {
   	memset (dummy, 0, EDITOR_DB_ALIGN - align);
   	if (roadmap_file_write (EditorDataFile, dummy, EDITOR_DB_ALIGN - align) < 0) return -1;
   }

   if (++flush_count == FLUSH_SIZE) {
      flush_count = 0;
      //editor_db_sync ();
   }


   return 0;
}
Beispiel #7
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;
}