Made mcsfile part of the default utils, made that it can read several files in one go.

This commit is contained in:
Thomas Touhey 2018-04-25 20:26:40 +02:00
parent ecfc8f31ad
commit fb9fd2ecc2
No known key found for this signature in database
GPG Key ID: 2ECEB0517AD947FB
6 changed files with 114 additions and 47 deletions

View File

@ -17,6 +17,8 @@
* along with p7utils; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************** */
#include "main.h"
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
/* ---
@ -26,7 +28,7 @@
/* Help message. */
static const char *help_start =
"Usage: mcsfile [--version|-v] [--help|-h] <g1m file>\n"
"Usage: mcsfile [--version|-v] [--help|-h] <mcs archives>...\n"
"\n"
"Reads mcs files in a g1m file.\n"
"\n"
@ -127,23 +129,28 @@ static void put_version(void)
* @return if execution should stop.
*/
int parse_args(int ac, char **av, const char **path)
int parse_args(int ac, char **av, int *num, const char ***paths)
{
/* getopt elements */
int c, help = 0, version = 0, pc;
char **pv;
const char *optstring = "hv";
const struct option longopts[] = {
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{}
{NULL, 0, NULL, 0}
};
/* get options */
int c; opterr = 0;
int help = 0, version = 0;
/* Get options. */
opterr = 0;
while ((c = getopt_long(ac, av, optstring, longopts, NULL)) != -1)
switch (c) {
case 'h': help = 1; break;
case 'v': version = 1; break;
case 'h':
help = 1;
break;
case 'v':
version = 1;
break;
default: switch (optopt) {
default:
fprintf(stderr, "-%c: unknown option.\n", optopt);
@ -151,15 +158,20 @@ int parse_args(int ac, char **av, const char **path)
return (1);
}
/* check parameters */
int pc = ac - optind;
char **pv = &av[optind];
if (pc != 1)
help = 1;
else
*path = *pv;
/* Check parameters. */
pc = ac - optind;
pv = &av[optind];
if (!pc)
help = 1;
else {
*num = pc;
*paths = (const char **)pv;
}
/* Display version or help message. */
/* display version or help message */
if (version) {
put_version();
return (1);
@ -168,6 +180,5 @@ int parse_args(int ac, char **av, const char **path)
return (1);
}
/* no error */
return (0);
}

View File

@ -17,10 +17,16 @@
* along with p7utils; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************** */
#include "main.h"
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <locale.h>
#define cry(S, ...) fprintf(stderr, S "\n", ##__VA_ARGS__)
struct entry {
int valid;
casio_file_t *handle;
const char *path;
};
/**
* main:
@ -33,40 +39,86 @@
int main(int ac, char **av)
{
const char *path;
casio_file_t *handle;
int err;
int num, i, err, fst;
const char **paths;
struct entry *entries, *ep;
/* Set the locale and parse arguments. */
setlocale(LC_ALL, "");
if (parse_args(ac, av, &path))
if (parse_args(ac, av, &num, &paths))
return (0);
/* parse */
if ((err = casio_open_file(&handle, path, casio_filetype_mcs)))
switch (err) {
case casio_error_wrong:
cry("An MCS file was expected (g1m/g1r, g1m/g2r, g3m)");
return (1);
case casio_error_nostream:
cry("Could not open file: %s", strerror(errno));
return (1);
case casio_error_magic:
cry("Magic error: file might be corrupted");
return (1);
case casio_error_eof:
cry("Unexpected end of file");
return (1);
default:
cry("Unknown error: %s", casio_strerror(err));
return (1);
/* Prepare the list. */
if (!(entries = malloc(sizeof(struct entry) * num))) {
fprintf(stderr, "error: a memory allocation has failed\n");
return (1);
}
/* Read the files, free the handle and exit. */
for (ep = entries, i = num; i; ep++, i--) {
ep->valid = 0;
ep->handle = NULL;
ep->path = paths[num - i];
print_files(handle->casio_file_mcs);
casio_free_file(handle);
/* Decode the file. */
err = casio_open_file(&ep->handle, ep->path, casio_filetype_mcs);
if (err) {
fprintf(stderr, "error: %s: ", ep->path);
switch (err) {
case casio_error_wrong:
fprintf(stderr, "an MCS file was expected "
"(g1m, g1r, g2m, g2r, g3m)\n");
break;
case casio_error_nostream:
fprintf(stderr, "could not open file: %s\n", strerror(errno));
break;
case casio_error_magic:
case casio_error_eof:
fprintf(stderr, "file might be corrupted\n");
break;
default:
fprintf(stderr, "%s\n", casio_strerror(err));
break;
}
continue;
}
ep->valid = 1;
}
/* List files in all of the given archives, and free the
* file handles while we're at it. */
fst = 1;
for (ep = entries, i = num; i; ep++, i--) {
if (!ep->valid)
continue;
/* Print the header, with some spacing if not the first entry. */
if (fst)
fst = 0;
else
fputc('\n', stdout);
if (num > 1)
fprintf(stdout, "%s:\n\n", ep->path);
/* Print the entries in the archive. */
print_files(ep->handle->casio_file_mcs);
/* Free the handle. */
casio_free_file(ep->handle);
}
return (0);
}

View File

@ -21,6 +21,7 @@
# include <libcasio.h>
/* Some printf types */
# ifdef _WIN64
# define PRIuSIZE "l64u"
# elif _WIN32
@ -29,8 +30,9 @@
# define PRIuSIZE "zu"
# endif
/* Prototypes */
int parse_args(int ac, char **av, const char **paths);
/* Prototypes. */
int parse_args(int ac, char **av, int *num, const char ***paths);
void print_files(casio_mcs_t *handle);
#endif /* MAIN_H */

View File

@ -1,4 +1,4 @@
#!/usr/bin/make -f
disable:
#disable:
libs:
@echo libcasio

View File

@ -1,3 +1,4 @@
#!/usr/bin/make -f
#disable:
libs:
@echo libcasio

View File

@ -1,3 +1,4 @@
#!/usr/bin/make -f
#disable:
libs:
@echo libcasio sdl