cake
/
p7utils
Archived
1
0
Fork 0

Adapted mcsfile to latest libg1m interface.

This commit is contained in:
Thomas Touhey 2017-03-16 14:26:12 +01:00
parent f1ea300340
commit 24ffe2d0c3
2 changed files with 45 additions and 33 deletions

View File

@ -10,6 +10,7 @@
#include "main.h"
#include <string.h>
#include <errno.h>
#define cry(S, ...) fprintf(stderr, (S), ##__VA_ARGS__)
/**
* main:
@ -31,16 +32,16 @@ int main(int ac, char **av)
g1m_t *handle; int err;
if ((err = g1m_open(&handle, path, g1m_type_mcs))) switch (err) {
case g1m_error_wrong_type:
fprintf(stderr, "An MCS file was expected (g1m/g1r, g1m/g2r, g3m)\n");
cry("An MCS file was expected (g1m/g1r, g1m/g2r, g3m)\n");
return (0);
case g1m_error_nostream:
fprintf(stderr, "Could not open file: %s\n", strerror(errno));
cry("Could not open file: %s\n", strerror(errno));
return (0);
case g1m_error_magic:
fprintf(stderr, "Magic error: file might be corrupted\n");
cry("Magic error: file might be corrupted\n");
return (0);
case g1m_error_eof:
fprintf(stderr, "Unexpected end of file\n");
cry("Unexpected end of file\n");
return (0);
}

View File

@ -18,7 +18,9 @@
* compare:
* Compare two MCS files.
*
* @arg
* @arg vpfile pointer to the first file.
* @arg vpfile pointer to the second file.
* @return if we should reverse.
*/
static int compare(const void *vpfile1, const void *vpfile2)
@ -27,11 +29,13 @@ static int compare(const void *vpfile1, const void *vpfile2)
g1m_mcsfile_t *file2 = *((g1m_mcsfile_t**)vpfile2);
/* compare dirnames */
int dirdiff = strcmp(file1->_dirname, file2->_dirname);
const char *dir1 = (const char*)file1->head._dirname;
const char *dir2 = (const char*)file2->head._dirname;
int dirdiff = strcmp(dir1, dir2);
if (dirdiff) return (dirdiff > 0);
/* compare names */
return (strcmp(file1->name, file2->name) > 0);
return (strcmp(file1->head.name, file2->head.name) > 0);
}
/**
@ -59,37 +63,38 @@ static void sort_files(g1m_t *handle)
static void put_description(g1m_mcsfile_t *file)
{
if (!file->type)
printf("unknown content (%" PRIuSIZE" octets)\n", file->content_size);
else if (file->type & g1m_mcstype_program) {
if (!file->head.type)
printf("unknown content (%" PRIuSIZE" octets)\n", file->head.size);
else if (file->head.type & g1m_mcstype_program) {
printf("program (");
if (g1m_has_password(file)) printf("password: '%s')\n", file->password);
if (g1m_has_password(file))
printf("password: '%s')\n", file->head.password);
else printf("no password)\n");
} else if (file->type & g1m_mcstype_list) {
printf("list %d (", g1m_get_id_minor(file->id));
if (g1m_get_id_major(file->id))
printf("from listfile %d, ", g1m_get_id_major(file->id));
printf("%d columns)\n", file->columns);
} else if (file->type & g1m_mcstype_mat)
} else if (file->head.type & g1m_mcstype_list) {
printf("list %d (", g1m_get_id_minor(file->head.id));
if (g1m_get_id_major(file->head.id))
printf("from listfile %d, ", g1m_get_id_major(file->head.id));
printf("%d columns)\n", file->head.width);
} else if (file->head.type & g1m_mcstype_mat)
printf("matrix %c (%d columns, %d rows)\n",
'A' + file->id - 1, file->columns, file->rows);
else if (file->type & g1m_mcstype_vct)
'A' + file->head.id - 1, file->head.width, file->head.height);
else if (file->head.type & g1m_mcstype_vct)
printf("vector %c (%d rows)\n",
'A' + file->id - 1, file->rows);
else if (file->type & g1m_mcstype_pict)
'A' + file->head.id - 1, file->head.height);
else if (file->head.type & g1m_mcstype_pict)
printf("picture %d (double %dx%d image)\n",
file->id, file->width, file->height);
else if (file->type & g1m_mcstype_capt)
file->head.id, file->head.width, file->head.height);
else if (file->head.type & g1m_mcstype_capt)
printf("capture %d (%dx%d)\n",
file->id, file->width, file->height);
else if (file->type & g1m_mcstype_spreadsheet)
file->head.id, file->head.width, file->head.height);
else if (file->head.type & g1m_mcstype_spreadsheet)
printf("spreadsheet (%d columns, %d rows)\n",
file->columns, file->rows);
else if (file->type & g1m_mcstype_string)
printf("string %d\n", file->id);
else if (file->type & g1m_mcstype_setup)
file->head.width, file->head.height);
else if (file->head.type & g1m_mcstype_string)
printf("string %d\n", file->head.id);
else if (file->head.type & g1m_mcstype_setup)
printf("setup\n");
else if (file->type & g1m_mcstype_alphamem)
else if (file->head.type & g1m_mcstype_alphamem)
printf("alpha memory\n");
else
printf("unmanaged format\n");
@ -114,16 +119,22 @@ void put_files(g1m_t *handle)
int max_size = 0;
for (int i = 0; i < handle->count; i++) {
g1m_mcsfile_t *file = handle->files[i];
int sz = strlen(file->name) + strlen(file->_dirname);
int sz = strlen(file->head.name);
if (file->head._dirname[0])
sz += strlen((char*)file->head._dirname) + 1;
if (sz > max_size) max_size = sz;
}
max_size += 2;
max_size++;
/* put the lines */
char buf[max_size + 1];
for (int i = 0; i < handle->count; i++) if (handle->files[i]) {
g1m_mcsfile_t *file = handle->files[i];
sprintf(buf, "%s/%s:", file->_dirname, file->name);
buf[0] = 0;
if (file->head._dirname[0])
sprintf(buf, "%s/", (char*)file->head._dirname);
sprintf(&buf[strlen(buf)], "%s:", file->head.name);
printf("%-*s ", max_size, buf);
put_description(handle->files[i]);
}