cse320validargs

.

School

Stony Brook University *

*We aren’t endorsed by this school

Course

320

Subject

Computer Science

Date

Jan 9, 2024

Type

Pages

5

Uploaded by PresidentHeat8133

Report
#include <stdlib.h> #include "reverki.h" #include "global.h" #include "debug.h" int convertStrtoInt(char *str); /** * @brief Validates command line arguments passed to the program. * @details This function will validate all the arguments passed to the * program, returning 0 if validation succeeds and -1 if validation fails. * Upon successful return, the various options that were specified will be * encoded in the global variable 'global_options', where it will be * accessible elsewhere in the program. For details of the required * encoding, see the assignment handout. * * @param argc The number of arguments passed to the program from the CLI. * @param argv The argument strings passed to the program from the CLI. * @return 0 if validation succeeds and -1 if validation fails. * @modifies global variable "global_options" to contain an encoded representation * of the selected program options. */ int validargs(int argc, char **argv) { if (argc < 1){ // if there are no arguments given return -1; } if (argc == 1) { // if there is only one argument return -1; } if ( *(*(argv + 1)) == '-' && *((*(argv + 1)) + 1) == 'h'){ // regardless of the other arguments if arg1 is -h ignore other flags printf("success -h ignore other arguments"); global_options = HELP_OPTION; return 0; } if(argc == 2) { // if there's 2 arguments and it's one of the positional arguments -h or -v or -r if ( *(*(argv + 1)) == '-' && *((*(argv + 1)) + 1) == 'h'){ // arg1 is -h printf("success -h only"); global_options = HELP_OPTION; return 0; } if ( *(*(argv + 1)) == '-' && *((*(argv + 1)) + 1) == 'v'){ // arg1 is -v printf("success -v only"); global_options = VALIDATE_OPTION ; return 0; } if ( *(*(argv + 1)) == '-' && *((*(argv + 1)) + 1) == 'r'){ // arg1 is -r printf("success -r only"); global_options = REWRITE_OPTION; return 0; }
else { //if the first flag isn't -h or -v or -r then return -1 for exit_failure printf("failure neither -h, -v or -r are first"); return -1; } } if(argc >= 3) { if(argc==3) { //if there's only 2 flags if ( *(*(argv + 1)) == '-' && *((*(argv + 1)) + 1) == 'v') { //if its -v -s if(*(*(argv+1)+2) == '\0') { if(*(*(argv+2)) == '-' && *(*(argv+2)+1) == 's') { printf("in -v -s success"); global_options = VALIDATE_OPTION | STATISTICS_OPTION; return 0; } } } if ( *(*(argv + 1)) == '-' && *((*(argv + 1)) + 1) == 'r') { //if its -r -s if(*(*(argv+1)+2) == '\0') { if(*(*(argv+2)) == '-' && *(*(argv+2)+1) == 's') { printf("in -r -s success"); global_options = REWRITE_OPTION | STATISTICS_OPTION; return 0; } } } if ( *(*(argv + 1)) == '-' && *((*(argv + 1)) + 1) == 'r') { //if its -r -t if(*(*(argv+1)+2) == '\0') { if(*(*(argv+2)) == '-' && *(*(argv+2)+1) == 't') { printf("in -r -t success"); global_options = REWRITE_OPTION | TRACE_OPTION; return 0; } } } } //if there's more than 3 arguments if ( *(*(argv + 1)) == '-' && *((*(argv + 1)) + 1) == 'r') { //if its -r -l [#LIMIT] if(*(*(argv+1)+2) == '\0') { if(*(*(argv+2)) == '-' && *(*(argv+2)+1) == 'l') { //*(*(argv+2)+1) == 'l' int limitSteps = convertStrtoInt(*(argv + 3)); if(limitSteps > 0 || limitSteps < 2147483647) { //checking if limit is within the range [1, 2^31-1] long limit = (long)limitSteps; //converting from int to long int to store in global options which is in long int limit = limit << 32; global_options = REWRITE_OPTION | LIMIT_OPTION | limit; // printf("after: %X\n",limit); printf("in -r -l success"); global_options = REWRITE_OPTION | LIMIT_OPTION;
return 0; } else { //limit out of range printf("failure limit out of range"); return -1; } } } } //all other valid argument combinations //Three combos: -r -s -t, -r -t -s, -r -l -t, -r -t -l, -r -s -l, -r -l -s if ( *(*(argv + 1)) == '-' && *((*(argv + 1)) + 1) == 'r') { //-r -s -t if(*(*(argv+1)+2) == '\0') { if(*(*(argv+2)) == '-' && *(*(argv+2)+1) == 's') { if(*(*(argv+1)+3) != '\0') { if(*(*(argv+3)) == '-' && *(*(argv+3)+1) == 't') { printf("in -r -S -t success"); global_options = REWRITE_OPTION | STATISTICS_OPTION | TRACE_OPTION; return 0; } } } } } if ( *(*(argv + 1)) == '-' && *((*(argv + 1)) + 1) == 'r') { //-r -t -s if(*(*(argv+1)+2) == '\0') { if(*(*(argv+2)) == '-' && *(*(argv+2)+1) == 't') { if(*(*(argv+1)+3) != '\0') { if(*(*(argv+3)) == '-' && *(*(argv+3)+1) == 's') { printf("in -r -t -s success"); global_options = REWRITE_OPTION | TRACE_OPTION | STATISTICS_OPTION ; return 0; } } } } } if ( *(*(argv + 1)) == '-' && *((*(argv + 1)) + 1) == 'r') { //-r -t -l if(*(*(argv+1)+2) == '\0') { if(*(*(argv+2)) == '-' && *(*(argv+2)+1) == 't') { if(*(*(argv+1)+3) != '\0') { if(*(*(argv+3)) == '-' && *(*(argv+3)+1) == 'l') { int limitSteps = convertStrtoInt(*(*(argv+4)); if(limitSteps > 0 || limitSteps < 2147483647) { //checking if limit is within the range [1, 2^31-1] long limit = (long)limitSteps; //converting from int to long int to store in global options which is in long int limit = limit << 32;
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help