Add mempak_rm and mempak_convert

This commit is contained in:
Raphael Assenat 2015-10-25 16:29:11 -04:00
parent d15e46b2b1
commit ab137f2edb
3 changed files with 181 additions and 1 deletions

View File

@ -6,13 +6,19 @@ LDFLAGS=-g
PREFIX=/usr/local
PROGRAMS=mempak_ls mempak_format mempak_extract_note mempak_insert_note
PROGRAMS=mempak_ls mempak_format mempak_extract_note mempak_insert_note mempak_rm mempak_convert
MEMPAKLIB_OBJS=mempak.o mempak_fs.o
.PHONY : clean install
all: $(PROGRAMS)
mempak_convert: mempak_convert.o $(MEMPAKLIB_OBJS)
$(LD) $^ $(LDFLAGS) -o $@
mempak_rm: mempak_rm.o $(MEMPAKLIB_OBJS)
$(LD) $^ $(LDFLAGS) -o $@
mempak_insert_note: mempak_insert_note.o $(MEMPAKLIB_OBJS)
$(LD) $^ $(LDFLAGS) -o $@

View File

@ -0,0 +1,84 @@
#include <stdio.h>
#include <getopt.h>
#include "mempak.h"
#define DEFAULT_FORMAT_STR "n64"
static void print_usage(void)
{
printf("Usage: ./mempak_convert in_file out_file <options>\n");
printf("\n");
printf("Options:\n");
printf(" -h Display help\n");
printf(" -f format Write file in specified format (default: %s)\n", DEFAULT_FORMAT_STR);
printf("\n");
printf("Formats:\n");
printf(" mpk Standard 32kB .mpk file format\n");
printf(" mpk4 128kB .mpk file (4 copies or the 32kB block)\n");
printf(" n64 .N64 file format\n");
}
int main(int argc, char **argv)
{
mempak_structure_t *mpk;
const char *infile;
const char *outfile;
unsigned char type;
struct option long_options[] = {
{ "format", required_argument, 0, 'f' },
{ "help", no_argument, 0, 'h' },
{ }, // terminator
};
const char *format = DEFAULT_FORMAT_STR;
if (argc < 2) {
print_usage();
return 1;
}
while(1) {
int c;
c = getopt_long(argc, argv, "f:h", long_options, NULL);
if (c==-1)
break;
switch(c)
{
case 'h':
print_usage();
return 0;
case 'f':
format = optarg;
break;
case '?':
fprintf(stderr, "Unknown argument. Try -h\n");
return -1;
}
}
type = mempak_string2format(format);
if (type == MPK_FORMAT_INVALID) {
fprintf(stderr, "Unknown format specified\n");
return -1;
}
infile = argv[optind];
outfile = argv[optind+1];
mpk = mempak_loadFromFile(infile);
if (!mpk) {
fprintf(stderr, "Could not load mempak file '%s'\n", infile);
return 1;
}
printf("Loaded file '%s' (%s format)\n", infile, mempak_format2string(mpk->file_format));
mempak_saveToFile(mpk, outfile, type);
mempak_free(mpk);
printf("Wrote file '%s' in %s format\n", outfile, mempak_format2string(type));
return 0;
}

View File

@ -0,0 +1,90 @@
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include "mempak.h"
static void print_usage(void)
{
printf("Usage: ./mempak_rm pakfile note_id\n");
printf("\n");
printf("Options:\n");
printf(" -h, --help Display help\n");
}
int main(int argc, char **argv)
{
const char *pakfile;
mempak_structure_t *mpk;
struct option long_options[] = {
{ "help", no_argument, 0, 'h' },
{ }, // terminator
};
int noteid;
entry_structure_t entry;
if (argc < 3) {
print_usage();
return 1;
}
while(1) {
int c;
c = getopt_long(argc, argv, "h", long_options, NULL);
if (c==-1)
break;
switch(c)
{
case 'h':
print_usage();
return 0;
case '?':
fprintf(stderr, "Unknown argument. Try -h\n");
return -1;
}
}
pakfile = argv[optind];
noteid = atoi(argv[optind+1]);
if (noteid < 0 || noteid > 15) {
fprintf(stderr, "Note id out of range (0-15)\n");
return -1;
}
mpk = mempak_loadFromFile(pakfile);
if (!mpk) {
return -1;
}
printf("Loaded pakfile in %s format.\n", mempak_format2string(mpk->file_format));
if (0 != get_mempak_entry(mpk, noteid, &entry)) {
fprintf(stderr, "Could not get note id %d\n", noteid);
mempak_free(mpk);
return -1;
}
if (!entry.valid) {
fprintf(stderr, "Note %d is already free\n", noteid);
mempak_free(mpk);
return -1;
}
printf("Deleting note %d (%d blocks)\n", noteid, entry.blocks);
if (0 != delete_mempak_entry(mpk, &entry)) {
fprintf(stderr, "Error deleting entry\n");
mempak_free(mpk);
return -1;
}
if (0 != mempak_saveToFile(mpk, pakfile, mpk->file_format)) {
fprintf(stderr, "could not write to memory pak file\n");
}
mempak_free(mpk);
return 0;
}