Browse Source

Allocate the right buffer size for dumping

master
Damien Goutte-Gattat 9 years ago
parent
commit
4e8878a54e
  1. 54
      src/kmxtool.c

54
src/kmxtool.c

@ -27,6 +27,7 @@
#include <err.h>
#include <hexio.h>
#include <xmem.h>
#include <microx.h>
@ -149,6 +150,24 @@ print_microx_status(struct kmx_microx_status *s)
printf(" Current Controller: %d\n", s->ext_ctrl.current);
}
struct kmx_data_type
{
enum kmx_microx_data_type code;
size_t size;
const char *name;
};
struct kmx_data_type kmx_data_types[] =
{
{ PROGRAM_DATA, MICROX_PROGRAM_SIZE, "program" },
{ COMBINATION_DATA, MICROX_COMBINATION_SIZE, "combination" },
{ MULTI_DATA, MICROX_MULTI_SIZE, "multi" },
{ DRUMKIT_DATA, MICROX_DRUMKIT_SIZE, "drumkit" },
{ ARPEGGIO_DATA, MICROX_ARPEGGIO_SIZE, "arpeggio" },
{ EXTSET_DATA, MICROX_EXTSET_SIZE, "extset" },
{ 0, 0, NULL }
};
/* Main function. */
@ -159,7 +178,7 @@ main(int argc, char **argv)
int e, mode;
struct kmx_microx_version version;
struct kmx_microx_status status;
enum kmx_microx_data_type type;
struct kmx_data_type *type;
struct option options[] = {
{ "help", 0, NULL, 'h' },
@ -178,7 +197,7 @@ main(int argc, char **argv)
port = DEFAULT_MIDI_PORT;
mode = MODE_STATUS;
type = PROGRAM_DATA;
type = kmx_data_types;
while ( (c = getopt_long(argc, argv, "hvp:sd:l:t:",
options, NULL)) != -1 ) {
@ -214,20 +233,14 @@ main(int argc, char **argv)
break;
case 't':
if ( strcmp(optarg, "program") == 0 )
type = PROGRAM_DATA;
else if ( strcmp(optarg, "combination") == 0 )
type = COMBINATION_DATA;
else if ( strcmp(optarg, "multi") == 0 )
type = MULTI_DATA;
else if ( strcmp(optarg, "drumkit") == 0 )
type = DRUMKIT_DATA;
else if ( strcmp(optarg, "arpeggio") == 0 )
type = ARPEGGIO_DATA;
else if ( strcmp(optarg, "extset") == 0 )
type = EXTSET_DATA;
else
err(EXIT_FAILURE, "invalid data type: %s", optarg);
while ( type->name ) {
if ( strcmp(type->name, optarg) == 0 )
break;
type += 1;
}
if ( type->name == NULL )
errx(EXIT_FAILURE, "invalid data type: %s", optarg);
break;
}
}
@ -250,10 +263,13 @@ main(int argc, char **argv)
print_microx_status(&status);
}
else if ( mode == MODE_DATA_DUMP ) {
unsigned char program[MICROX_DRUMKIT_SIZE];
unsigned char *program;
int n;
if ( (n = kmx_microx_dump(midi, type, param, program, sizeof(program))) < 0 )
program = xmalloc(type->size);
if ( (n = kmx_microx_dump(midi, type->code, param, program,
type->size)) < 0 )
errx(EXIT_FAILURE, "cannot get program: %s",
kmx_microx_error(midi, n));
@ -268,7 +284,7 @@ main(int argc, char **argv)
if ( n < 1 )
err(EXIT_FAILURE, "cannot read program");
kmx_microx_load(midi, type, param, program, n);
kmx_microx_load(midi, type->code, param, program, n);
}
return EXIT_SUCCESS;

Loading…
Cancel
Save