p7utils/src/mcsfile/print.c

131 lines
4.1 KiB
C

/* ************************************************************************** */
/* _____ _ */
/* line.c |_ _|__ _ _| |__ ___ _ _ */
/* | Project: mcsfile | |/ _ \| | | | '_ \ / _ \ | | | */
/* | | (_) | |_| | | | | __/ |_| | */
/* By: thomas <thomas@touhey.fr> |_|\___/ \__,_|_| |_|\___|\__, |.fr */
/* Last updated: 2016/12/18 01:43:52 |___/ */
/* */
/* ************************************************************************** */
#include "main.h"
#include <stdlib.h>
#include <string.h>
/* ************************************************************************** */
/* Sort */
/* ************************************************************************** */
/**
* compare:
* Compare two MCS files.
*
* @arg
*/
static int compare(const void *vpfile1, const void *vpfile2)
{
g1m_mcsfile_t *file1 = *((g1m_mcsfile_t**)vpfile1);
g1m_mcsfile_t *file2 = *((g1m_mcsfile_t**)vpfile2);
/* compare dirnames */
int dirdiff = strcmp(file1->_dirname, file2->_dirname);
if (dirdiff) return (dirdiff > 0);
/* compare names */
return (strcmp(file1->name, file2->name) > 0);
}
/**
* sort_files:
* Sort MCS files.
*
* @arg handle the handle.
*/
static void sort_files(g1m_t *handle)
{
qsort(handle->files, (size_t)handle->count,
sizeof(g1m_mcsfile_t*), &compare);
}
/* ************************************************************************** */
/* Display */
/* ************************************************************************** */
/**
* put_description:
* Put the description (with the newline).
*
* @arg file the MCS file.
*/
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) {
printf("program (");
if (g1m_has_password(file)) printf("password: '%s')\n", file->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)
printf("matrix %c (%d columns, %d rows)\n",
'A' + file->id - 1, file->columns, file->rows);
else if (file->type & g1m_mcstype_vct)
printf("vector %c (%d rows)\n",
'A' + file->id - 1, file->rows);
else if (file->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)
printf("capture %d (%dx%d)\n",
file->id, file->width, file->height);
else if (file->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)
printf("setup\n");
else if (file->type & g1m_mcstype_alphamem)
printf("alpha memory\n");
else
printf("unmanaged format\n");
}
/**
* put_files:
* Put the files.
*
* @arg handle the handle.
*/
void put_files(g1m_t *handle)
{
if (!handle->count) {
fprintf(stderr, "Provided file was empty.\n");
return ;
}
sort_files(handle);
/* get first part maximum size */
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);
if (sz > max_size) max_size = sz;
}
max_size += 2;
/* 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);
printf("%-*s ", max_size, buf);
put_description(handle->files[i]);
}
}