int Simulator::handleConfiguration() { // handle config file int handle = handleConfigFile(handleSlash((flags[0]).c_str()), config); if (handle < 0) { if (handle == -2) { cout << USAGE << endl; cout << ERROR_FIND_CONFIG_FILE << fullPath(handleSlash((flags[0]).c_str())) << endl; } return -1; } return 0; }
int Simulator::handleAlgorithms() { // handle algorithm files // [exercise says: - in case the directory is defect -> return // - in case the directory is empty -> return // - in case the directory is missing -> search recursively in the working directory for algorithms] int handle = 0; numOfAlgorithms = getNumberOfPotentialAlgorithms(flags[3]); if (numOfAlgorithms == -1 || numOfAlgorithms == 0) { cout << USAGE << endl; cout << ERROR_FIND_ALGORITHM_FILES << fullPath(flags[3]) << endl; return -1; } else if (numOfAlgorithms == -2) { // no usage print is necessary return -1; } handle = handleAlgorithmFiles(handleSlash(flags[3].c_str()), numOfAlgorithms, registrar); if (handle < 0) { if (handle == -1) { cout << USAGE << endl; } return -1; } walkingIntoWallsErrors = make_unique<string[]>(numOfAlgorithms); numOfAlgorithms = registrar.getAlgorithmNames().size(); return 0; }
int Simulator::handleScore() { if (!score_loaded) { score_function = NULL; return 0; } int handle = handleScoreFile(handleSlash((flags[1]).c_str()), score_hndl, &score_function); if (handle < 0) { if (handle == -2) { cout << USAGE << endl; std::cout << ERROR_FIND_SCORE_FILE << fullPath(handleSlash((flags[1]).c_str())) << std::endl; } return -1; } return 0; }
TokenTypes Scanner::handleSlash( Token * tok ) { TokenTypes result; int current = nextch(); if( current == S_ENDC ) { HCWarning( RTF_BADEOF, _source->name() ); result = TOK_END; } else if( current == '*' ) { // Certain RTF commands begin with "\*\", not "\". current = nextch(); if( current != '\\' ) { HCWarning( RTF_BADCOMMAND, _lineNum, _source->name() ); if( current != S_ENDC ) { putback( current ); } result = TOK_NONE; } else { result = handleSlash( tok ); } } else if( current == '\n' ) { // A "\" just before a new-line is the same as "\par". memcpy( tok->_text, "par", 4 ); result = TOK_COMMAND; ++_lineNum; } else if( isSpecial( current ) ) { // Some characters are escaped, like "\{". result = TOK_SPEC_CHAR; tok->_value = current; } else if( current == '\'' ) { // "\'nnn" signifies the byte with value nnn. result = TOK_SPEC_CHAR; pullHex( tok ); } else if( islower( current ) ) { // All RTF commands are in lower case. putback( current ); result = TOK_COMMAND; pullCommand( tok ); } else { HCWarning( RTF_BADCOMMAND, _lineNum, _source->name() ); result = TOK_NONE; } return( result ); }
void Scanner::getToken( Token * tok ) { int current; while( (current = nextch()) == '\n' ) { ++_lineNum; } tok->_lineNum = _lineNum; switch( current ) { case S_ENDC: tok->_type = TOK_END; break; case '{': tok->_type = TOK_PUSH_STATE; break; case '}': tok->_type = TOK_POP_STATE; break; case '\\': current = nextch(); if( current == '~' ) { tok->_type = TOK_TEXT; putback( HARD_SPACE ); pullText( tok ); } else { putback( current ); tok->_type = handleSlash( tok ); } break; case '\t': tok->_type = TOK_COMMAND; memcpy( tok->_text, "tab", 4 ); break; default: if( isFootnoteChar( current ) ) { tok->_type = TOK_SPEC_CHAR; tok->_value = current; } else { tok->_type = TOK_TEXT; putback( current ); pullText( tok ); } } }
// handlehouses should now only list the house file names in the vector and not read them! int Simulator::handleHouses() { // handle house files // [exercise says: - in case the directory is defect -> return // - in case the directory is empty -> return // - in case the directory is missing -> search recursively in the working directory for algorithms] numOfHouses = getNumberOfHouses(handleSlash((flags[2]).c_str()), houseFileNames); if (numOfHouses == -1 || numOfHouses == 0) { cout << USAGE << endl; cout << ERROR_FIND_HOUSE_FILES << fullPath(flags[2]) << endl; return -1; } isValidHouses = make_unique<bool[]>(numOfHouses); houseErrors = make_unique<string[]>(numOfHouses); return 0; }
int handlDefine(Meta_Vector & wordVector, size_t index) { int step = CHECK_SPACE; int curLine = wordVector[index].line; int defineType; for(size_t i=index+2; i < wordVector.size(); i++) { Meta_Struct meta = wordVector[i]; if (meta.line != curLine) break; if (meta.type == TYPE_SPECIAL && meta.data.chr[0] == '/') { int commentType; int ret = handleSlash(wordVector, i, commentType); if (ret > 0) { if (wordVector[i-1].line == meta.line && wordVector[i-1].type == TYPE_SPACE && wordVector[i+ret+1].line == meta.line && wordVector[i+ret+1].type == TYPE_SPACE) ret++; i += ret; // printf("skip %d after comment\n", ret); continue; } } #define CHECK_TYPE(_type) \ if (meta.type != _type) { \ printf("[%d]expect type %s after \"#define\", but actually type is %d(%s)\n", \ meta.pos, getTypeName(_type), meta.type, getTypeName(meta.type)); \ return -DEFINE_FMT_ERROR; \ } switch (step) { case CHECK_SPACE: CHECK_TYPE(TYPE_SPACE); step = CHECK_WORD; break; case CHECK_WORD: CHECK_TYPE(TYPE_WORD); printf("define name is: %s\n", meta.data.str); step = CHECK_TYPE; break; case CHECK_TYPE: if (meta.type == TYPE_SPACE) { printf("define type is constant\n"); defineType = TYPE_CONSTANT; } else if (meta.type == TYPE_SPECIAL && meta.data.chr[0] == '(') { printf("define type is function\n"); defineType = TYPE_FUNCTION; } else { printf("define type error\n"); return -1; } step = CHECK_NONE; break; } } return 0; char defineStr[1024] = {'\0'}; int defineStrLen = 0; bool addSpace = false; // #define A if (index + 3 >= wordVector.size() || wordVector[index+3].line != curLine){ restoreLine(wordVector, index, defineStr); printf("Line %d is not a full define statement:%s\n", curLine, defineStr); return 0; } if (wordVector[index+4].line != curLine) { if (wordVector[index+3].type == TYPE_WORD) printf("Line %d is a NULL constant define: %s\n", curLine, wordVector[index+3].data.str); else { restoreLine(wordVector, index, defineStr); printf("Line %d is not a valid NULL constant define statement:%s\n", curLine, defineStr); } return 0; } else{ if (wordVector[index+4].data.chr[0] == '('){ defineType = TYPE_FUNCTION; } else if (wordVector[index+4].type == TYPE_SPACE){ defineType = TYPE_CONSTANT; } else{ restoreLine(wordVector, index, defineStr); printf("Line %d is not a valid define statement:%s\n", curLine, defineStr); return 0; } } if (defineType == TYPE_CONSTANT){ bool isLineComment = false; // skip '#', "define", space, constant define and space for(size_t i=index+5; i < wordVector.size(); i++) { Meta_Struct meta = wordVector[i]; if (meta.line != curLine) break; if (isLineComment) { isLineComment = false; if (meta.data.chr[0] == '/'){ // printf("comment occurs at pos %d\n", meta.pos); defineStrLen--; defineStr[defineStrLen] = '\0'; break; } } if (meta.data.chr[0] == '\\'){ if (wordVector[i-1].type == TYPE_SPECIAL && wordVector[i-1].data.chr[0] == '\\'){ // printf("it's already escaped with this slash.\n"); } else { bool nextline = false; char escapeChar = '\0'; handleBackSlash(wordVector, i, nextline, escapeChar); } } if (meta.data.chr[0] == '/'){ isLineComment = true; } if (meta.type == TYPE_SPACE){ if (addSpace == false) { addSpace = true; } } else{ if (addSpace == true) { addSpace = false; strcat(defineStr, wordVector[i-1].data.str); defineStrLen += wordVector[i-1].len; } if (meta.type == TYPE_WORD) strcat(defineStr, meta.data.str); else strcat(defineStr, meta.data.chr); defineStrLen += meta.len; } } if (defineStrLen == 0) printf("Line %d is a NULL constant define: %s\n", curLine, wordVector[index+3].data.str); else printf("Line %d is a constant define: %s, value is %s, len is %d\n", curLine, wordVector[index+3].data.str, defineStr, defineStrLen); } else{ // restoreLine(wordVector, index, defineStr); // printf("%s\n", defineStr); bool needNextLine = false; // skip '#', "define", space, function define and () for(size_t i=index+3; i < wordVector.size(); i++) { Meta_Struct meta = wordVector[i]; if (meta.line != curLine){ if (needNextLine){ curLine = meta.line; needNextLine = false; } else break; } if (meta.type == TYPE_SPACE){ if (addSpace == false) { addSpace = true; } } else{ if (addSpace == true) { addSpace = false; strcat(defineStr, wordVector[i-1].data.str); defineStrLen += wordVector[i-1].len; } if (meta.type == TYPE_WORD) strcat(defineStr, meta.data.str); else{ if (meta.data.chr[0] == '\\'){ strcat(defineStr, "\n"); needNextLine = true; } else strcat(defineStr, meta.data.chr); } defineStrLen += meta.len; } } printf("Line %d is a function define:\n%s\n", curLine, defineStr); } return 0; }