Mercurial > msvpvf
changeset 85:1acd477da42f
posix: improvements
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Mon, 17 Jun 2024 21:18:37 -0400 |
parents | 7d4db3e24487 |
children | f4b0ff679229 |
files | Makefile src/main.c |
diffstat | 2 files changed, 96 insertions(+), 75 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Fri Mar 22 22:30:04 2024 -0400 +++ b/Makefile Mon Jun 17 21:18:37 2024 -0400 @@ -1,4 +1,4 @@ -_CFLAGS = -Wall -O2 -Iinclude $(CFLAGS) +_CFLAGS = -Wall -g -O2 -Iinclude $(CFLAGS) _LDFLAGS = $(LDFLAGS) .c.o:
--- a/src/main.c Fri Mar 22 22:30:04 2024 -0400 +++ b/src/main.c Mon Jun 17 21:18:37 2024 -0400 @@ -1,6 +1,7 @@ #include <stdio.h> #include <stdint.h> #include <stdlib.h> +#include <stdarg.h> #include <string.h> #include <getopt.h> @@ -9,7 +10,7 @@ #include "common.h" /* non-portable functions */ -static inline char* msvpvf_internal_strdup(const char* str) { +static char* msvpvf_internal_strdup(const char* str) { size_t len = strlen(str) + 1; char* copy = malloc(len); @@ -20,8 +21,38 @@ return copy; } +static char *msvpvf_internal_asnprintf(const char *format, ...) { + va_list args; + int needed_size; + char *output = NULL; + + va_start(args, format); + + needed_size = vsnprintf(NULL, 0, format, args); + if (needed_size < 0) + return NULL; + + va_end(args); + + output = malloc((needed_size + 1) * sizeof(char)); + if (!output) + return NULL; + + va_start(args, format); + + needed_size = vsnprintf(output, needed_size + 1, format, args); + if (needed_size < 0) { + free(output); + return NULL; + } + + va_end(args); + + return output; +} + /* source needs read permissions, target needs write permissions, both must be in binary mode */ -static inline int copy_file(FILE* source, FILE* target) { +static int copy_file(FILE* source, FILE* target) { char ch[4096]; while (!feof(source)) { @@ -33,30 +64,24 @@ return 0; } -static inline const char* type_to_string(enum types type) { - switch (type) { - case TYPES_VF: return "Movie Studio"; - case TYPES_VEG: return "Vegas Pro"; - case TYPES_UNKNOWN: - default: return "Unknown"; - } -} +static const char* type_names[] = { + [TYPES_VF] = "Movie Studio", + [TYPES_VEG] = "Vegas Pro", + [TYPES_UNKNOWN] = "Unknown", +}; -static inline const char* type_to_extension(enum types type) { - switch (type) { - case TYPES_VF: return "vf"; - case TYPES_VEG: - case TYPES_UNKNOWN: - default: return "veg"; - } -} +static const char* type_extensions[] = { + [TYPES_VF] = "vf", + [TYPES_VEG] = "veg", +}; #ifndef ARRAYSIZE #define ARRAYSIZE(x) (sizeof(x) / sizeof((x)[0])) #endif static const char* help_text = - "msvpvf by Paper\n" + "msvpvf help text\n" + "author: Paper <paper@paper.us.eu.org>\n" "usage: %s <input>... (arguments)\n" "\n" "arguments:\n" @@ -67,12 +92,12 @@ static struct option options_long[] = { {"version", required_argument, NULL, 'v'}, {"type", required_argument, NULL, 't'}, - {"help", 0, NULL, 'h'} + {"help", 0, NULL, 'h'}, }; int main(int argc, char *argv[]) { uint8_t version = 0; - enum types type = TYPES_UNKNOWN; + enum types type = TYPES_VEG; /* ...? */ int c; int option_index = 0; @@ -101,7 +126,7 @@ } if (argc <= optind) { - fprintf(stderr, "[ERROR]: Missing input file!\n"); + fprintf(stderr, "[ERROR]: Missing input file(s)!\n"); printf(help_text, argv[0]); return 1; } @@ -111,72 +136,68 @@ return 1; } - /* this progressively */ while (optind < argc) { const char* input = argv[optind++]; - FILE* output_file = NULL; + FILE *input_file, *output_file = NULL; + uint8_t file_version = 0; + enum types file_type = TYPES_UNKNOWN; + + printf("Input file name: %s\n", input); + + /* print information about the input file */ + input_file = fopen(input, "rb"); + if (!input_file) { + fprintf(stderr, "[ERROR]: Error opening input file %s!\n\n", input); + continue; + } + + if (fgetc(input_file) == EOF) { + fprintf(stderr, "[ERROR]: Input file \"%s\" is empty.\n", input); + fclose(input_file); + continue; + } + + if (get_file_information(input_file, &file_version, &file_type)) { + fprintf(stderr, "[ERROR]: Failed to get file information for input file \"%s\"!\n", input); + fclose(input_file); + continue; + } + + printf("Input file version: %u\n", file_version); + printf("Input file type: %s\n\n", type_names[file_type]); { - uint8_t file_version = 0; - enum types file_type = TYPES_UNKNOWN; + /* open the output file... */ + char* basec = msvpvf_internal_strdup(input); + char* bname = basename(basec); + int ext = strrchr(bname, '.') - bname; - printf("Input file name: %s\n", input); - /* print information about the input file */ - FILE* input_file = fopen(input, "rb"); - if (!input_file) { - fprintf(stderr, "[ERROR]: Error opening input file %s!\n\n", input); - continue; - } - - if (fgetc(input_file) == EOF) { - fprintf(stderr, "[ERROR]: Input file \"%s\" is empty.\n", input); - fclose(input_file); - continue; - } - - if (get_file_information(input_file, &file_version, &file_type)) { - fprintf(stderr, "[ERROR]: Failed to get file information for input file \"%s\"!\n", input); + char *output = msvpvf_internal_asnprintf("V%u_%.*s.%s", version, ext, bname, type_extensions[type]); + if (!output) { + fprintf(stderr, "[ERROR]: msvpvf_internal_asnprintf failed!\n"); + free(basec); + free(output); fclose(input_file); continue; } - printf("Input file version: %u\n", file_version); - printf("Input file type: %s\n\n", type_to_string(file_type)); - - { - /* open the output file... */ - char* basec = msvpvf_internal_strdup(input); - char* bname = basename(basec); - int ext = strrchr(bname, '.') - bname; - - /* create the output filename */ - int needed_size = snprintf(NULL, 0, "V%u_%.*s.%s", version, ext, bname, type_to_extension(type)); - char* output = malloc((needed_size + 1) * sizeof(char)); - if (!output) { - fprintf(stderr, "[ERROR]: Failed to allocate memory for output string!\n"); - free(basec); - return 1; - } - - snprintf(output, needed_size + 1, "V%u_%.*s.%s", version, ext, bname, type_to_extension(type)); - printf("Output filename: %s\n", output); - - output_file = fopen(output, "w+b"); - if (!output_file) { - fprintf(stderr, "[ERROR]: Failed to open output file %s! Do you have write permissions?\n", output); - free(basec); - free(output); - continue; - } + output_file = fopen(output, "w+b"); + if (!output_file) { + fprintf(stderr, "[ERROR]: Failed to open output file %s! Do you have write permissions?\n", output); free(basec); free(output); + fclose(input_file); + continue; } - copy_file(input_file, output_file); + free(basec); + free(output); + } - fflush(stdout); - fclose(input_file); - } + copy_file(input_file, output_file); + + fflush(stdout); + fclose(input_file); set_file_information(output_file, version, type); fclose(output_file);