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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
|
/* rzxtool.c: Simple modifications to RZX files
Copyright (c) 2007-2015 Philip Kendall, Sergio Baldovi
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 <errno.h>
#include <getopt.h>
#include <unistd.h>
#include <string.h>
#ifdef HAVE_LIB_GLIB
#include <glib.h>
#endif /* #ifdef HAVE_LIB_GLIB */
#include <libspectrum.h>
#include "utils.h"
#define PROGRAM_NAME "rzxtool"
const char *progname;
static libspectrum_creator *creator;
typedef enum action_type_t {
ACTION_DELETE_BLOCK,
ACTION_EXTRACT_SNAP,
ACTION_INSERT_SNAP,
ACTION_FINALISE_RZX,
} action_type_t;
typedef struct action_t {
action_type_t type;
size_t where;
char *filename;
} action_t;
typedef struct options_t {
const char *rzxfile;
const char *outfile;
int uncompressed;
} options_t;
static libspectrum_rzx_iterator
get_block( libspectrum_rzx *rzx, size_t where )
{
libspectrum_rzx_iterator it;
for( it = libspectrum_rzx_iterator_begin( rzx );
it && where;
where--, it = libspectrum_rzx_iterator_next( it ) )
; /* Do nothing */
return it;
}
static int
delete_block( libspectrum_rzx *rzx, size_t where )
{
libspectrum_rzx_iterator it;
it = get_block( rzx, where );
if( !it ) {
fprintf( stderr, "%s: not enough blocks in RZX file\n", progname );
return 1;
}
libspectrum_rzx_iterator_delete( rzx, it );
return 0;
}
static int
write_snapshot( libspectrum_snap *snap, const char *filename )
{
unsigned char *buffer = NULL; size_t length = 0;
int error, flags;
libspectrum_id_t type;
libspectrum_class_t class;
error = libspectrum_identify_file_with_class( &type, &class, filename, NULL,
0 );
if( error ) return error;
if( class != LIBSPECTRUM_CLASS_SNAPSHOT || type == LIBSPECTRUM_ID_UNKNOWN )
type = LIBSPECTRUM_ID_SNAPSHOT_SZX;
error = libspectrum_snap_write( &buffer, &length, &flags, snap, type,
creator, 0 );
if( error ) return error;
error = write_file( filename, buffer, length );
if( error ) { free( buffer ); return error; }
free( buffer );
return 0;
}
static int
extract_snap( libspectrum_rzx *rzx, size_t where, const char *filename )
{
libspectrum_rzx_iterator it;
libspectrum_snap *snap;
int e;
it = get_block( rzx, where );
if( !it ) {
fprintf( stderr, "%s: not enough blocks in RZX file\n", progname );
return 1;
}
snap = libspectrum_rzx_iterator_get_snap( it );
if( !snap ) {
fprintf( stderr, "%s: not a snapshot block\n", progname );
return 1;
}
e = write_snapshot( snap, filename );
if( e ) return e;
return 0;
}
static libspectrum_snap*
read_snap( const char *filename )
{
unsigned char *buffer = NULL;
size_t length = 0;
int error;
libspectrum_snap *snap;
if( read_file( filename, &buffer, &length ) ) {
return NULL;
}
snap = libspectrum_snap_alloc();
error = libspectrum_snap_read( snap, buffer, length, LIBSPECTRUM_ID_UNKNOWN,
filename );
if( error ) {
libspectrum_snap_free( snap );
snap = NULL;
}
free( buffer );
return snap;
}
static int
insert_snap( libspectrum_rzx *rzx, size_t where, const char *filename )
{
libspectrum_snap *snap;
snap = read_snap( filename );
if( !snap ) {
fprintf( stderr, "%s: couldn't read `%s'\n", progname, filename );
return 1;
}
libspectrum_rzx_insert_snap( rzx, snap, where );
return 0;
}
static void
apply_action( void *data, void *user_data )
{
const action_t *action = data;
libspectrum_rzx *rzx = user_data;
int where = action->where;
switch( action->type ) {
case ACTION_DELETE_BLOCK:
delete_block( rzx, where );
break;
case ACTION_EXTRACT_SNAP:
extract_snap( rzx, where, action->filename );
break;
case ACTION_INSERT_SNAP:
insert_snap( rzx, where, action->filename );
break;
case ACTION_FINALISE_RZX:
libspectrum_rzx_finalise( rzx );
break;
default:
fprintf( stderr, "%s: unknown action type %d\n", progname, action->type );
}
/* Really would like to handle errors */
}
static int
parse_argument( const char *argument, int *where, const char **filename )
{
char *comma;
char *buffer;
buffer = strdup( argument );
if( !buffer ) {
fprintf( stderr, "%s: out of memory at %s:%d\n", progname,
__func__, __LINE__ );
return 1;
}
comma = strchr( buffer, ',' );
if( !comma ) {
fprintf( stderr, "%s: no comma found in argument `%s'\n", progname,
argument );
free( buffer );
return 1;
}
*comma = 0;
*where = atoi( buffer );
*filename = &argument[ comma + 1 - buffer ];
free( buffer );
return 0;
}
static int
add_action( GSList **actions, action_type_t type, const char *argument )
{
int error, where;
const char *filename;
action_t *action;
action = malloc( sizeof( *action ) );
if( !action ) {
fprintf( stderr, "%s: out of memory at %s:%d\n", progname,
__func__, __LINE__ );
return 1;
}
action->type = type;
if( type == ACTION_DELETE_BLOCK ) {
action->where = atoi( argument );
action->filename = NULL;
} else if( type == ACTION_FINALISE_RZX ) {
action->where = 0;
action->filename = NULL;
} else {
error = parse_argument( argument, &where, &filename );
if( error ) return error;
action->where = where;
action->filename = strdup( filename );
if( !action->filename ) {
fprintf( stderr, "%s: out of memory at %s:%d\n", progname,
__func__, __LINE__ );
return 1;
}
}
*actions = g_slist_append( *actions, action );
return 0;
}
static void
show_version( void )
{
printf(
PROGRAM_NAME " (" PACKAGE ") " PACKAGE_VERSION "\n"
"Copyright (c) 2007-2014 Philip Kendall\n"
"License GPLv2+: GNU GPL version 2 or later "
"<http://gnu.org/licenses/gpl.html>\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n" );
}
static void
show_help( void )
{
printf(
"Usage: %s [OPTION]... <rzxfile> [<outfile>]\n"
"Modify Sinclair ZX Spectrum input recording files.\n"
"\n"
"Options:\n"
" -d <block> Delete block number.\n"
" -e <block>,<filename>\n"
" The snapshot in the specified block number will be written\n"
" to the specified filename.\n"
" -i <block>,<filename>\n"
" The snapshot specified in filename will be added to the file\n"
" at the position specified by block number.\n"
" -f Finalise the RZX file, removing any interspersed snapshot.\n"
" -u Write uncompressed data to the output RZX file.\n"
" -h, --help Display this help and exit.\n"
" -V, --version Output version information and exit.\n"
"\n"
"Report %s bugs to <%s>\n"
"%s home page: <%s>\n"
"For complete documentation, see the manual page of %s.\n",
progname,
PROGRAM_NAME, PACKAGE_BUGREPORT, PACKAGE_NAME, PACKAGE_URL, PROGRAM_NAME
);
}
static int
parse_options( int argc, char **argv, GSList **actions,
struct options_t *options )
{
int c, error = 0;
int output_needed = 0;
options->uncompressed = 0;
struct option long_options[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'V' },
{ 0, 0, 0, 0 }
};
while( ( c = getopt_long( argc, argv, "d:e:i:ufhV", long_options,
NULL ) ) != -1 ) {
switch( c ) {
case 'd':
error = add_action( actions, ACTION_DELETE_BLOCK, optarg );
output_needed = 1;
break;
case 'e':
error = add_action( actions, ACTION_EXTRACT_SNAP, optarg );
break;
case 'i':
error = add_action( actions, ACTION_INSERT_SNAP, optarg );
output_needed = 1;
break;
case 'f':
error = add_action( actions, ACTION_FINALISE_RZX, optarg );
output_needed = 1;
break;
case 'u':
options->uncompressed = 1;
output_needed = 1;
break;
case 'h':
show_help();
exit( 0 );
case 'V':
show_version();
exit( 0 );
case '?':
/* getopt prints an error message to stderr */
error = 1;
break;
default:
error = 1;
fprintf( stderr, "%s: unknown option `%c'\n", progname, (char) c );
break;
}
if( error ) break;
}
if( error ) return error;
if( !argv[ optind ] ) {
fprintf( stderr, "%s: no RZX file specified\n", progname );
return 1;
}
options->rzxfile = argv[ optind++ ];
if( output_needed ) {
if( !argv[ optind ] ) {
fprintf( stderr, "%s: no RZX output file specified\n", progname );
return 1;
}
options->outfile = argv[ optind++ ];
} else {
options->outfile = NULL;
}
if( argv[ optind ] ) {
fprintf( stderr, "%s: extra argument on command line\n", progname );
return 1;
}
return 0;
}
static int
write_rzx( const char *filename, libspectrum_rzx *rzx, int compressed )
{
unsigned char *buffer = NULL; size_t length = 0;
int error;
error = libspectrum_rzx_write( &buffer, &length, rzx, LIBSPECTRUM_ID_UNKNOWN,
creator, compressed, NULL );
if( error ) return error;
error = write_file( filename, buffer, length );
if( error ) { free( buffer ); return error; }
free( buffer );
return 0;
}
int
main( int argc, char *argv[] )
{
GSList *actions = NULL;
options_t options;
unsigned char *buffer = NULL; size_t length = 0;
libspectrum_rzx *rzx;
int error;
progname = argv[0];
error = init_libspectrum(); if( error ) return error;
error = get_creator( &creator, "rzxtool" ); if( error ) return error;
error = parse_options( argc, argv, &actions, &options );
if( error ) {
fprintf( stderr, "Try `%s --help' for more information.\n", progname );
return error;
}
error = read_file( options.rzxfile, &buffer, &length );
if( error ) return error;
rzx = libspectrum_rzx_alloc();
error = libspectrum_rzx_read( rzx, buffer, length );
if( error ) return error;
free( buffer );
g_slist_foreach( actions, apply_action, rzx );
if( options.outfile ) {
error = write_rzx( options.outfile, rzx, !options.uncompressed );
if( error ) return error;
}
return 0;
}
|