cake
/
p7utils
Archived
1
0
Fork 0
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
p7utils/src/mcsfile/print.c

145 lines
4.7 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>
#include <libfontcharacter.h>
/* ************************************************************************** */
/* Sort */
/* ************************************************************************** */
/**
* compare:
* Compare two MCS files.
*
* @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)
{
g1m_mcsfile_t *file1 = *((g1m_mcsfile_t**)vpfile1);
g1m_mcsfile_t *file2 = *((g1m_mcsfile_t**)vpfile2);
/* compare dirnames */
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->head.name, file2->head.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->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->head.password);
else printf("no password)\n");
} 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->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->head.id - 1, file->head.height);
else if (file->head.type & g1m_mcstype_pict)
printf("picture %d (double %dx%d image)\n",
file->head.id, file->head.width, file->head.height);
else if (file->head.type & g1m_mcstype_capt)
printf("capture %d (%dx%d)\n",
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->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->head.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 = fc_mbstous(NULL, file->head.name, 0);
if (file->head._dirname[0])
sz += strlen((char*)file->head._dirname) + 1;
if (sz > max_size) max_size = sz;
}
max_size++;
/* put the lines */
wchar_t buf[max_size + 1], endbuf[max_size + 2];
for (int i = 0; i < handle->count; i++) if (handle->files[i]) {
g1m_mcsfile_t *file = handle->files[i];
buf[0] = 0;
if (file->head._dirname[0])
swprintf(buf, max_size + 1, L"%s/", (char*)file->head._dirname);
fc_mbstous(&buf[wcslen(buf)], file->head.name, 0);
wcscpy(&buf[wcslen(buf)], L":");
swprintf(endbuf, max_size + 2, L"%-*ls ", max_size, buf);
printf("%ls", endbuf);
put_description(handle->files[i]);
}
}