1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
/* binary.c: Win32 routines to load/save chunks of binary data
Copyright (c) 2003-2008 Philip Kendall, Marek Januszewski
$Id: binary.c 3720 2008-07-21 20:09:50Z specu $
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: philip-fuse@shadowmagic.org.uk
*/
#include <config.h>
#include <libspectrum.h>
#include <tchar.h>
#include <windows.h>
#include "fuse.h"
#include "ui/ui.h"
#include "utils.h"
#include "win32internals.h"
#include "binary.h"
struct binary_info {
TCHAR *filename;
utils_file file;
TCHAR *dialog_title;
void (*on_change_filename)( HWND hwndDlg, LONG user_data );
void (*on_execute)( HWND hwndDlg, LONG user_data );
};
static void change_load_filename( HWND hwndDlg, LONG user_data );
static void load_data( HWND hwndDlg, LONG user_data );
static void change_save_filename();
static void save_data( HWND hwndDlg, LONG user_data );
static INT_PTR CALLBACK
binarydata_proc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam );
void
menu_file_loadbinarydata( int action )
{
/* FIXME: a way to associate a long type with a window is via SetWindowLong
with GWL_USERDATA parameter - review past code and implement */
struct binary_info info;
int error;
fuse_emulation_pause();
info.dialog_title = TEXT( "Fuse - Load Binary Data" );
info.filename = ui_get_open_filename( info.dialog_title );
if( !info.filename ) { fuse_emulation_unpause(); return; }
error = utils_read_file( info.filename, &info.file );
if( error ) { free( info.filename ); fuse_emulation_unpause(); return; }
info.on_change_filename = &change_load_filename;
info.on_execute = &load_data;
/* Information display */
DialogBoxParam( fuse_hInstance, MAKEINTRESOURCE( IDD_BINARY ), fuse_hWnd,
binarydata_proc, ( LPARAM ) &info );
free( info.filename );
utils_close_file( &info.file );
fuse_emulation_unpause();
}
static INT_PTR CALLBACK
binarydata_proc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
struct binary_info *info = NULL;
switch( uMsg ) {
case WM_INITDIALOG: {
TCHAR buffer[80];
info = ( struct binary_info * ) lParam;
/* save the pointer to info with this dialog window */
SetWindowLong( hwndDlg, GWL_USERDATA, ( LONG ) info );
SendMessage( hwndDlg, WM_SETTEXT, 0, ( LPARAM ) info->dialog_title );
SendDlgItemMessage( hwndDlg, IDC_BINARY_STATIC_PATH, WM_SETTEXT,
0, ( LPARAM ) info->filename );
if( info->file.length != -1 ) {
_sntprintf( buffer, 80, "%lu", (unsigned long) info->file.length );
SendDlgItemMessage( hwndDlg, IDC_BINARY_EDIT_LENGTH, WM_SETTEXT,
0, ( LPARAM ) buffer );
}
return FALSE;
}
case WM_COMMAND:
switch( LOWORD( wParam ) ) {
case IDC_BINARY_BUTTON_BROWSE:
info = ( struct binary_info * ) GetWindowLong( hwndDlg, GWL_USERDATA );
info->on_change_filename( hwndDlg, ( LONG ) info ) ;
return 0;
case IDOK:
info = ( struct binary_info * ) GetWindowLong( hwndDlg, GWL_USERDATA );
info->on_execute( hwndDlg, ( LONG ) info ) ;
return 0;
case IDCANCEL:
EndDialog( hwndDlg, 0 );
return 0;
}
break;
case WM_CLOSE:
EndDialog( hwndDlg, 0 );
return 0;
}
return FALSE;
}
static void
change_load_filename( HWND hwndDlg, LONG user_data )
{
struct binary_info *info = ( struct binary_info * ) user_data;
TCHAR *new_filename;
utils_file new_file;
TCHAR buffer[80];
int error;
new_filename = ui_get_open_filename( "Fuse - Load Binary Data" );
if( !new_filename ) return;
error = utils_read_file( new_filename, &new_file );
if( error ) { free( new_filename ); return; }
/* Remove the data for the old file */
error = utils_close_file( &info->file );
if( error ) { free( new_filename ); return; }
free( info->filename );
/* Put the new data in */
info->filename = new_filename; info->file = new_file;
/* And update the displayed information */
SendDlgItemMessage( hwndDlg, IDC_BINARY_STATIC_PATH, WM_SETTEXT,
0, ( LPARAM ) new_filename );
_sntprintf( buffer, 80, "%lu", (unsigned long) info->file.length );
SendDlgItemMessage( hwndDlg, IDC_BINARY_EDIT_LENGTH, WM_SETTEXT,
0, ( LPARAM ) buffer );
}
static void
load_data( HWND hwndDlg, LONG user_data )
{
struct binary_info *info = ( struct binary_info * )user_data;
long start, length; size_t i;
TCHAR *temp_buffer;
size_t temp_buffer_len;
errno = 0;
temp_buffer_len = SendDlgItemMessage( hwndDlg, IDC_BINARY_EDIT_LENGTH,
WM_GETTEXTLENGTH, 0, 0 );
temp_buffer = malloc( sizeof( TCHAR ) * ( temp_buffer_len + 1 ) );
SendDlgItemMessage( hwndDlg, IDC_BINARY_EDIT_LENGTH, WM_GETTEXT,
temp_buffer_len + 1, ( LPARAM ) temp_buffer );
length = _tcstol( temp_buffer, NULL, 10 );
free( temp_buffer );
if( errno || length < 1 || length > 0x10000 ) {
ui_error( UI_ERROR_ERROR, "Length must be between 1 and 65536" );
return;
}
if( length > info->file.length ) {
ui_error( UI_ERROR_ERROR,
"'%s' contains only %lu bytes",
info->filename, (unsigned long)info->file.length );
return;
}
errno = 0;
temp_buffer_len = SendDlgItemMessage( hwndDlg, IDC_BINARY_EDIT_START,
WM_GETTEXTLENGTH, 0, 0 );
temp_buffer = malloc( sizeof( TCHAR ) * ( temp_buffer_len + 1 ) );
SendDlgItemMessage( hwndDlg, IDC_BINARY_EDIT_START, WM_GETTEXT,
temp_buffer_len + 1, ( LPARAM ) temp_buffer );
start = _tcstol( temp_buffer, NULL, 10 );
free( temp_buffer );
if( errno || start < 0 || start > 0xffff ) {
ui_error( UI_ERROR_ERROR, "Start must be between 0 and 65535" );
return;
}
if( start + length > 0x10000 ) {
ui_error( UI_ERROR_ERROR, "Block ends after address 65535" );
return;
}
for( i = 0; i < length; i++ )
writebyte( start + i, info->file.buffer[ i ] );
EndDialog( hwndDlg, 0 );
}
void
menu_file_savebinarydata( int action )
{
struct binary_info info;
fuse_emulation_pause();
info.dialog_title = TEXT( "Fuse - Save Binary Data" );
info.filename = ui_get_save_filename( info.dialog_title );
if( !info.filename ) { fuse_emulation_unpause(); return; }
info.file.length = -1; /* let the dialog know to leave length box blank */
info.on_change_filename = &change_save_filename;
info.on_execute = &save_data;
/* Information display */
DialogBoxParam( fuse_hInstance, MAKEINTRESOURCE( IDD_BINARY ), fuse_hWnd,
binarydata_proc, ( LPARAM ) &info );
free( info.filename );
fuse_emulation_unpause();
}
static void
change_save_filename( HWND hwndDlg, LONG user_data )
{
struct binary_info *info = ( struct binary_info * ) user_data;
TCHAR *new_filename;
new_filename = ui_get_save_filename( "Fuse - Save Binary Data" );
if( !new_filename ) return;
free( info->filename );
info->filename = new_filename;
SendDlgItemMessage( hwndDlg, IDC_BINARY_STATIC_PATH, WM_SETTEXT,
0, ( LPARAM ) new_filename );
}
static void
save_data( HWND hwndDlg, LONG user_data )
{
struct binary_info *info = ( struct binary_info * ) user_data;
long start, length; size_t i;
libspectrum_byte *buffer;
TCHAR *temp_buffer;
size_t temp_buffer_len;
int error;
errno = 0;
temp_buffer_len = SendDlgItemMessage( hwndDlg, IDC_BINARY_EDIT_LENGTH,
WM_GETTEXTLENGTH, 0, 0 );
temp_buffer = malloc( sizeof( TCHAR ) * ( temp_buffer_len + 1 ) );
SendDlgItemMessage( hwndDlg, IDC_BINARY_EDIT_LENGTH, WM_GETTEXT,
temp_buffer_len + 1, ( LPARAM ) temp_buffer );
length = _tcstol( temp_buffer, NULL, 10 );
free( temp_buffer );
if( errno || length < 1 || length > 0x10000 ) {
ui_error( UI_ERROR_ERROR, "Length must be between 1 and 65536" );
return;
}
buffer = malloc( length * sizeof( libspectrum_byte ) );
if( !buffer ) {
ui_error( UI_ERROR_ERROR, "out of memory at %s:%d", __FILE__, __LINE__ );
return;
}
errno = 0;
temp_buffer_len = SendDlgItemMessage( hwndDlg, IDC_BINARY_EDIT_START,
WM_GETTEXTLENGTH, 0, 0 );
temp_buffer = malloc( sizeof( TCHAR ) * ( temp_buffer_len + 1 ) );
SendDlgItemMessage( hwndDlg, IDC_BINARY_EDIT_START, WM_GETTEXT,
temp_buffer_len + 1, ( LPARAM ) temp_buffer );
start = _tcstol( temp_buffer, NULL, 10 );
free( temp_buffer );
if( errno || start < 0 || start > 0xffff ) {
ui_error( UI_ERROR_ERROR, "Start must be between 0 and 65535" );
return;
}
if( start + length > 0x10000 ) {
ui_error( UI_ERROR_ERROR, "Block ends after address 65535" );
return;
}
for( i = 0; i < length; i++ )
buffer[ i ] = readbyte( start + i );
error = utils_write_file( info->filename, buffer, length );
if( error ) { free( buffer ); return; }
free( buffer );
EndDialog( hwndDlg, 0 );
}
|