/* ***************************************************************************** * mcsfile/print.c -- mcsfile directory printing utilities. * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey * * This file is part of p7utils. * p7utils is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2.0 of the License, * or (at your option) any later version. * * p7utils is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with p7utils; if not, see . * ************************************************************************** */ #include "main.h" #include #include #include /* ************************************************************************** */ /* 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]); } }