//initialize the board void BogglePlayer::setBoard(unsigned int rows, unsigned int cols, string** diceArray) { if (isBoardSet) { clearBoard(); } mRows = rows; mCols = cols; //use a linear vector to represent the matrix for (int i = 0; i < mRows; i++) { for (int j = 0; j < mCols; j++) { board.push_back(toLowercase(diceArray[i][j])); isUsed.push_back(false); } } isBoardSet = true; }
/*This method is used to validate loops*/ int isValidLoop(FILE* inputFile) { /*Used to keep track of tokens, and store lines and tokens*/ char tokens[1][20], line[100], lineCopy[100], *token; /*Used to keep track of current position within text file*/ fpos_t currentPosition; /*Stores current position within text file so the location can be read from again*/ fgetpos (inputFile,¤tPosition); /*This loop is almost the same as the one in the isValidMove() function*/ /*The only difference is that it checks for an endloop statement and then resets the position in the file after validation*/ while ( !feof(inputFile) ) { fgets( line, 99, inputFile ); toLowercase(line); strcpy(lineCopy, line); token = removeWhiteSpace(strtok( lineCopy, " \t\n" )); if (strcmp(token, "\0") == 0 || strncmp(token, "#", 1) == 0) continue; strcpy(tokens[0], token); token = removeWhiteSpace(strtok( NULL, " \t\n" )); /*Check for endloop command*/ if (strcmp(tokens[0], "endloop") == 0 && token == NULL) { /*Go back to position in file before validation*/ fsetpos (inputFile,¤tPosition); return true; } /*Check if moves within file are valid*/ if (isValidMove(line, inputFile) == false) return false; } return false; }
IObject* ObjectFactory::CreateModifier(std::string & name, std::istream & str) { IObject* obj(nullptr); IObject *a = nullptr, *b = nullptr; std::string tmp; try { if (name == "boolean") { str >> tmp; toLowercase(tmp); if (tmp != "add" && tmp != "+" && tmp != "sub" && tmp != "-" && tmp != "subtract" && tmp != "union" && tmp != "&" && tmp != "blend" && tmp != "~" && tmp != "@") throw MakeParserException("Bad parameter for boolean operation."); a = Construct(str); b = Construct(str); if (tmp == "add" || tmp == "+") obj = new Boolean(a, b, Boolean::BOOL_ADD); else if (tmp == "sub" || tmp == "-" || tmp == "subtract") obj = new Boolean(a, b, Boolean::BOOL_SUBTRACT_B); else if (tmp == "union" || tmp == "&") obj = new Boolean(a, b, Boolean::BOOL_UNION); else if (tmp == "blend" || tmp == "~" || tmp == "@") obj = new Boolean(a, b, Boolean::BOOL_BLEND); else throw MakeParserException("Bad boolean option, expected 'add', 'sub', 'blend' or 'union'"); } else if (name == "duplicate") { Vector3D mod; str >> mod; a = Construct(str); if (a == NULL) throw MakeParserException("Bad object in duplicate."); obj = new Duplicate(a, mod); }
ControllerMetadata loadControllerMetadata(string path, string packageName, bool formatName) { string line; string key; string value; ControllerMetadata result; if (formatName) result.name = cleanUpPackageName(packageName); else result.name = packageName; result.description = string("No description"); result.version = string("Unknown version"); result.author = string("Unknown author"); result.startScriptPath = path + "/start.ops"; result.stopScriptPath = path + "/stop.ops"; result.guiLibPath = path + "/lib/libcontroller_gui.so"; result.guiDescriptionPath = path + "/controller_gui.glade"; result.guiConfigPath = path + "/gui_config.yaml"; result.guiTabWidgetName = string("controller_tab"); result.loadSuccessful = false; ifstream metadataFile((path + "/controller.txt").c_str(), ios::in); if (metadataFile.is_open()) { while (metadataFile.good()) { getline(metadataFile, line); key = getKey(line); trim(key); toLowercase(&key); if (key == "name") { value = getValue(line); trim(value); result.name = value; } else if (key == "description") { value = getValue(line); trim(value); result.description = value; } else if (key == "version") { value = getValue(line); trim(value); result.version = value; } else if (key == "author") { value = getValue(line); trim(value); result.author = value; } else if (key == "startscriptpath") { value = getValue(line); trim(value); result.startScriptPath = value; } else if (key == "stopscriptpath") { value = getValue(line); trim(value); result.stopScriptPath = value; } else if (key == "guilibpath") { value = getValue(line); trim(value); result.guiLibPath = value; } else if (key == "guitabwidgetname") { value = getValue(line); trim(value); result.guiTabWidgetName = value; } else if (key == "guidescriptionpath") { value = getValue(line); trim(value); result.guiDescriptionPath = value; } } metadataFile.close(); result.loadSuccessful = true; } return result; }
/*Main program*/ int main(void) { /*Variable Declarations*/ /*Used to store lines from text, copies of lines, file name of text, and tokens, respectively*/ char line[100], lineCopy[100], fileName[100], *token; /*Array of keywords that are valid commands*/ char keywords[8][14] = {"forward", "reverse", "turnright", "turnleft", "pause", "light", "reset", "loop"}; /*Variable used as a counter*/ int counter; /*Variable used to keep track of line number in text file*/ int lineCount = 0; /*Initializr status of the light*/ LIGHT_ON = false; /*Variable for getting the file from the user*/ FILE *inputFile; /*Makes sure the motors are stationary and the LED is off*/ parallelput(0); /*Prompts user to enter a file name*/ printf("Enter a file name: "); gets(fileName); /* Main loop in the program */ /*If the file is valid and can be read*/ if ( (inputFile = fopen( fileName, "r" )) != NULL ) { /*While there is another line in the text file*/ while ( !feof(inputFile) ) { /*Get the line*/ fgets( line, 99, inputFile ); /*Convert line to lowecase*/ toLowercase(line); /*Creates a copy of the line for tokenization (which changes original line)*/ strcpy(lineCopy, line); /*Increase line count by 1*/ lineCount++; /*Prevents iteration of previous line if very last line in text file is an empty line*/ if( feof(inputFile) ) break; /*If the command in the text file is valid*/ if (isValidMove(lineCopy, inputFile) == true) { /*Display message*/ printf("Line #%d: ", lineCount); /*creates a copy of the string*/ strcpy(lineCopy, line); /*create a token using line copy*/ token = removeWhiteSpace(strtok( lineCopy, " \t\n" )); /*Checks to see if line is not blank or a comment and displays a message indicating what was on the line*/ if (token == NULL) { printf("empty line\n"); continue; } if(strncmp(token, "#", 1) == 0) { printf("comment\n"); continue; } /*Loop used to check if move is one of the following:*/ /*Forward, backward, turnleft, turnright, pause*/ for (counter = 0; counter < 5; counter++) { /*If the command is one of the following*/ if (strcmp(token, keywords[counter]) == 0) { /*Select function to execture based on counter and break out of loop*/ chooseMove(counter, atoi(removeWhiteSpace(strtok(NULL, " \t\n")))); break; } } /*Check to see if the command was to configure the light*/ if (strcmp(token, keywords[5]) == 0) /*Execute light function*/ light(removeWhiteSpace(strtok( NULL, " \t\n" ))) ; /*Check to see if the command was reset*/ if (strcmp(token, keywords[6]) == 0) /*Execute reset function*/ reset(); /*Check to see if the command was a loop*/ if (strcmp(token, keywords[7]) == 0) /*Adds appropriate number of lines to get current line number and execute loop function*/ lineCount += loop(line, atoi(removeWhiteSpace(strtok(NULL, " \t\n"))), inputFile); } /*If the move is not valid*/ else { /*isplay error message indicating the line number in the text file where the error was found*/ errorMessage(lineCount); /*Reset motors and LED and waits for user to press a key then exits*/ parallelput(0); getch(); return 0; } } /* Display the results */ /*If there are no more line in the file*/ /*Notify user that commands are finished execuing*/ printf("\nReached the end of the file.\nThe program will now exit.\n"); /*Reset motors and LED and waits for user to press a key then exits*/ parallelput(0); getch(); } /*If file does not exist*/ else { /*Notify user that file does not exist*/ printf("\nThe file does not exist.\nThe program will now exit.\n"); /*Wait for user to press a key then exits*/ getch(); } }
/*This method is used to perform the loop command*/ int loop(char line[], int iterations, FILE* inputFile) { /*This method is very similar to the main() function*/ /*The only difference is it exectues the instructions iterations number of times*/ /*In addition, this function returns the number of lines within the loop as the last iteration*/ /*does not alter current position within text file*/ /*Same as main() function*/ char lineCopy[100], *token; char keywords[7][14] = {"forward", "reverse", "turnright", "turnleft", "pause", "light", "reset"}; int counter, counter2; int lineCount = 0; /*Used to keep track of current position within text file*/ fpos_t currentPosition; /*Stores current position within text file so the location can be read from again*/ fgetpos (inputFile,¤tPosition); strcpy(lineCopy, line); /*Display message*/ printf("loop %d times\n", iterations); for (counter = 0; counter < iterations; counter++) { /*Display message*/ printf("\niteration #%d:\n", counter+1); /*Sets location within text file to line right after loop comand*/ fsetpos (inputFile,¤tPosition); /*random string used so token does not become null after each iteration*/ token = "reset token"; /*Executes commands like in main() function as long as command is not endloop*/ while (strcmp(token, "endloop") != 0) { fgets( line, 99, inputFile ); lineCount++; strcpy(lineCopy, line); toLowercase(lineCopy); token = removeWhiteSpace(strtok( lineCopy, " \t\n" )); if (token == NULL || strncmp(token, "#", 1) == 0) continue; for (counter2 = 0; counter2 < 5; counter2++) { if (strcmp(token, keywords[counter2]) == 0 && counter2 < 5) { chooseMove(counter2, atoi(removeWhiteSpace(strtok(NULL, " \t\n")))); break; } } if (strcmp(token, keywords[5]) == 0) light(removeWhiteSpace(strtok( NULL, " \t\n" ))) ; if (strcmp(token, keywords[6]) == 0) reset(); if (strcmp(token, keywords[7]) == 0) loop(line, atoi(removeWhiteSpace(strtok(NULL, " \t\n"))), inputFile); } } /*Display message*/ printf("\nend loop\n\n"); /*Returns number of line within the loop (including endloop statement)*/ return (lineCount / iterations); }
static int test_toLowercase() { widechar upper = 'A'; widechar lower = 'a'; return lower != toLowercase(upper); }