static void replace_click(GtkWidget* widget, gpointer data) { GtkTreeIter iter; stateheader* sh; if (_filePath != NULL && gtk_tree_selection_get_selected(GTK_TREE_SELECTION(selection), NULL, &iter)) { gtk_tree_model_get(GTK_TREE_MODEL(listStore), &iter, 1, &sh, -1); GtkWidget* dialog = gtk_file_chooser_dialog_new("Replace", GTK_WINDOW(window), GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL); gint res = gtk_dialog_run(GTK_DIALOG(dialog)); if (res != GTK_RESPONSE_ACCEPT) { gtk_widget_destroy(dialog); return; } char* path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); FILE* f = fopen(path, "rb"); gtk_widget_destroy(dialog); fseek(f, 0, SEEK_END); size_t len = ftell(f); if (len > 65536) len = 65536; // prevent using too much memory on opening big file by accident char* gbc_data = (char*)malloc(len); fseek(f, 0, SEEK_SET); size_t total_bytes_read = 0; while (total_bytes_read < len) { size_t bytes_read = fread(gbc_data, 1, len - total_bytes_read, f); total_bytes_read += bytes_read; if (bytes_read <= 0) { error_msg("Could only read %lu bytes from %s", (unsigned long)total_bytes_read, path); free(gbc_data); g_free(path); fclose(f); return; } } g_free(path); fclose(f); // only first X bytes will be used, where X is uncompressed size of current data void* new_data = goomba_new_sav(loaded_file, sh, gbc_data, len); free(gbc_data); if (new_data == NULL) { error_msg("%s", goomba_last_error()); } else { memcpy(loaded_file, new_data, GOOMBA_COLOR_SRAM_SIZE); dirty = true; free(new_data); header_scan(); } } }
long search_named_array(FILE *inputfile,char* string) { char ch; //const char ocurl='{', ccurl='}'; //short ind=1; //long buffer_index=0,buffer_size=0,blocksnumber=0, long position; //extern t_keywords T_KEYWORDS; //FLOATVECTOR *vec=NULL; HEADER h; if(inputfile==NULL){ t_error("You tried to read from a closed file "); } ch=getc(inputfile); while(ch!=EOF){ position=simplefind(inputfile,":"); ch=getc(inputfile); while(!isspace(ch) && !iscntrl(ch)){ position-=1; fseek(inputfile,position,SEEK_SET); ch=getc(inputfile); } header_scan(inputfile,&h); if(strcmp(h.name,string)==0){ fseek(inputfile,position,SEEK_SET); return position; } } t_error("The searched array does not exists in file"); return -1; }
static void load(const char* path) { FILE* f = fopen(path, "rb"); if (f == NULL) { error_msg("Could not open file %s", path); return; } fseek(f, 0, SEEK_END); size_t filesize = ftell(f); // If the file is smaller than 64 KiB, pad the rest with zeroes. Some goombasav code expects a 64 KiB file. if (filesize < GOOMBA_COLOR_SRAM_SIZE) filesize = GOOMBA_COLOR_SRAM_SIZE; if (loaded_file != NULL) { free(loaded_file); } loaded_file = (char*)malloc(filesize); loaded_file_size = filesize; fseek(f, 0, SEEK_SET); size_t total_bytes_read = 0; while (total_bytes_read < filesize) { size_t bytes_read = fread(loaded_file, 1, filesize - total_bytes_read, f); if (bytes_read <= 0) { fprintf(stderr, "Could only read %lu bytes from %s\n", (unsigned long)total_bytes_read, path); memset((char*)loaded_file + total_bytes_read, 0, filesize - total_bytes_read); total_bytes_read = filesize; } else { total_bytes_read += bytes_read; } } fclose(f); char* cleaned = goomba_cleanup(loaded_file); if (cleaned == NULL) { // this should not happen error_msg("%s", goomba_last_error()); return; } else if (cleaned != loaded_file) { GtkWidget* dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_NONE, "Uncompressed SRAM found at 0xE000. Would you like to move and compress it to the proper location? (Doing this is required to extract or replace the save data.)"); gtk_dialog_add_button(GTK_DIALOG(dialog), "Cancel", GTK_RESPONSE_CANCEL); gtk_dialog_add_button(GTK_DIALOG(dialog), "Skip", GTK_RESPONSE_NO); gtk_dialog_add_button(GTK_DIALOG(dialog), "Clean", GTK_RESPONSE_YES); gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_YES); gint res = gtk_dialog_run(GTK_DIALOG(dialog)); gtk_widget_destroy(dialog); if (res == GTK_RESPONSE_YES) { memcpy(loaded_file, cleaned, GOOMBA_COLOR_SRAM_SIZE); dirty = true; } free(cleaned); if (res == GTK_RESPONSE_CANCEL) { return; } } else { dirty = false; } if (_filePath != NULL) free(_filePath); _filePath = (char*)malloc(strlen(path) + 1); strcpy(_filePath, path); update_titlebar(GTK_WINDOW(window)); header_scan(); }