/* * This is a rather ugly program written for Project 12 * J Dean Brock, 2011 * * DO NOT MODIFY THIS FILE */ #include #include #include #include "parsecommand.h" #define NUMCOMMANDS 5 const char *commands[] = { "dcl" , "inc" , "prt" , "row" , "col" } ; int parseCommand(char *inBuff, char *command, int *row, int *col) { char mybuff[3] ; char matchMe ; int i, myRow, myCol ; char *leftBrace, *rightBrace ; /* See if there is a command followed by X */ if (sscanf(inBuff, " %c%c%c %*[xX] [ %c", &mybuff[0], &mybuff[1], &mybuff[2], &matchMe) != 4) { return -1 ; } for(i=0; i<3; ++i) { mybuff[i] = tolower((int)mybuff[i]) ; } for(i=0; i rightBrace) { return -1 ; } /* Make sure nothing but whitespace follows the ] */ if (sscanf(rightBrace+1, " %c", &matchMe) == 1) { return -1 ; } /* See if the commands match row and column as needed */ if (mybuff[0] == 'r') { if (sscanf(leftBrace+1, "%d , * %*[]]%c", &myRow, &matchMe) != 2) { return -1 ; } } else if (mybuff[0] == 'c') { if (sscanf(leftBrace+1, " * ,%d %*[]]%c", &myCol, &matchMe) != 2) { return -1 ; } } else { if (sscanf(leftBrace+1, " %d ,%d %*[]]%c", &myRow, &myCol, &matchMe) != 3) { return -1 ; } } /* If we get here, it must have worked. Let's store some results!*/ strncpy(command, mybuff, 3) ; if (mybuff[0] != 'c') { *row = myRow ; } if (mybuff[0] != 'r') { *col = myCol ; } return 0 ; }