From 0fc77a6828c1522b0ef2700f369a4b8631a77ca3 Mon Sep 17 00:00:00 2001 From: Jacob Date: Wed, 27 Apr 2016 12:03:51 -0400 Subject: [PATCH] Moved print_usage and parse_int_arg below main. --- csim.c | 86 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/csim.c b/csim.c index f5e9713..b007cb4 100644 --- a/csim.c +++ b/csim.c @@ -6,62 +6,26 @@ #define mprintf(...) if (VERBOSE) printf(__VA_ARGS__) +void print_usage(); +size_t parse_int_arg(char *arg, char opt); + /** If VERBOSE is non-zero, mprintf will not print. Otherwise, it will. Set in main(). */ int8_t VERBOSE = 0; -/* As defined in conference. */ +/* USURE. As defined in conference. */ struct line_s { int8_t validity; int tag; }; typedef struct line_s line; -void print_usage() -{ - printf("Usage: ./csim [-hv] -s -E -b -t \n" - "Options:\n" - " -h Print this help.\n" - " -v Display trace info.\n" - " -s The number of set index bits. Must be a positive integer.\n" - " -E Associativity (lines per set). Must be a positive integer.\n" - " -b The number of block bits. Must be a positive integer.\n" - " -t Name of the file to read a valgrind trace from."); -} - -/* TODO: Move the exit call from parse_int_arg into the getopt cases. */ -/** - Parses a string - @param arg The string to parse. - @param opt The option being parsed. - @return The parsed integer if the argument was an integer. - @note Exits if argument is invalid - */ -size_t parse_int_arg(char* arg, char opt) -{ - char* end; - long i = strtol(arg, &end, 0); - - if (!i || *end != '\0') { - printf("Invalid argument \"%s\" for option -%c\n", arg, opt); - printf("Usage: ./csim [-hv] -s -E -b -t \n"); - exit(1); - } - else if (i < 0) { - printf("Argument %ld for option %c too small (must be greater than 0)", i, opt); - exit(1); - } - else { - return (size_t)i; - } -} - int main(int argc, char* argv[]) { - size_t index_bits = 0, lines = 0, block_bits = 0; // Useful variables. + size_t set_index_bits = 0, lines = 0, block_bits = 0; // Useful variables. char *filename = NULL; int opt = 0, argflags = 0; while ((opt=getopt(argc, argv, "hvs:E:b:t:")) != -1) { @@ -101,3 +65,43 @@ int main(int argc, char* argv[]) printSummary(0, 0, 0); return 0; } + + +void print_usage() +{ + printf("Usage: ./csim [-hv] -s -E -b -t \n" + "Options:\n" + " -h Print this help.\n" + " -v Display trace info.\n" + " -s The number of set index bits. Must be a positive integer.\n" + " -E Associativity (lines per set). Must be a positive integer.\n" + " -b The number of block bits. Must be a positive integer.\n" + " -t Name of the file to read a valgrind trace from."); +} + +/** + Parses a string + @param arg The string to parse. + @param opt The option being parsed. + @return The parsed integer if the argument was an integer. + @note Exits if argument is invalid + */ +size_t parse_int_arg(char* arg, char opt) +{ + char* end; + long i = strtol(arg, &end, 0); + + if (!i || *end != '\0') { + printf("Invalid argument \"%s\" for option -%c\n", arg, opt); + printf("Usage: ./csim [-hv] -s -E -b -t \n"); + exit(1); + } + else if (i < 0) { + printf("Argument %ld for option %c too small (must be greater than 0)", i, opt); + exit(1); + } + else { + return (size_t)i; + } +} +