cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

ihxcheck.c (2111B)


      1// This is free and unencumbered software released into the public domain.
      2// For more information, please refer to <https://unlicense.org>
      3// bbbbbr 2020
      4
      5#include <stdio.h>
      6#include <string.h>
      7#include <stdlib.h>
      8#include <unistd.h>
      9#include <stdbool.h>
     10#include <stdint.h>
     11
     12#include "ihx_file.h"
     13#include "areas.h"
     14
     15#define MAX_STR_LEN     4096
     16
     17void display_help(void);
     18int handle_args(int argc, char * argv[]);
     19
     20char filename_in[MAX_STR_LEN] = {'\0'};
     21
     22
     23void display_help(void) {
     24    fprintf(stdout,
     25           "ihx_check input_file.ihx [options]\n"
     26           "\n"
     27           "Options\n"
     28           "-h : Show this help\n"
     29           "-e : Treat warnings as errors\n"
     30           "\n"
     31           "Use: Read a .ihx and warn about overlapped areas.\n"
     32           "Example: \"ihx_check build/MyProject.ihx\"\n"
     33           );
     34}
     35
     36
     37int handle_args(int argc, char * argv[]) {
     38
     39    int i;
     40
     41    if( argc < 2 ) {
     42        display_help();
     43        return false;
     44    }
     45
     46    // Copy input filename (if not preceded with option dash)
     47    if (argv[1][0] != '-')
     48        snprintf(filename_in, sizeof(filename_in), "%s", argv[1]);
     49
     50    // Start at first optional argument, argc is zero based
     51    for (i = 1; i <= (argc -1); i++ ) {
     52
     53        if (strstr(argv[i], "-h") == argv[i]) {
     54            display_help();
     55            return false;  // Don't parse input when -h is used
     56
     57        } else if (strstr(argv[i], "-e") == argv[i]) {
     58            set_option_warnings_as_errors(true);
     59        }
     60
     61    }
     62
     63    return true;
     64}
     65
     66
     67int matches_extension(char * filename, char * extension) {
     68    return (strcmp(filename + (strlen(filename) - strlen(extension)), extension) == 0);
     69}
     70
     71
     72int main( int argc, char *argv[] )  {
     73
     74    int ret = EXIT_FAILURE; // Exit with failure by default
     75
     76    if (handle_args(argc, argv)) {
     77
     78        // Must at least have extension
     79        if (strlen(filename_in) >=5) {
     80            // detect file extension
     81            if (matches_extension(filename_in, (char *)".ihx")) {
     82                ret = ihx_file_process_areas(filename_in);
     83            }
     84        }
     85    }
     86
     87    return ret; // Exit with failure by default
     88}