bool CVBRHeader::CheckXING( CMPAFile* pMPAFile, DWORD& dwOffset ) { // XING ID found? if( !CheckID( pMPAFile, 'X', 'i', 'n', 'g', dwOffset) && !CheckID( pMPAFile, 'I', 'n', 'f', 'o', dwOffset) ) return false; return true; }
// currently not used bool CVBRHeader::ExtractLAMETag( DWORD dwOffset ) { // LAME ID found? if( !CheckID( m_pMPAFile, 'L', 'A', 'M', 'E', dwOffset ) && !CheckID( m_pMPAFile, 'G', 'O', 'G', 'O', dwOffset ) ) return false; return true; }
DWORD XmlParser::ParseColor(char **buffer, int id) { DWORD color = 0; char data[512]; CheckID(id); if (m_Success) { if (GetXmlData(buffer, data, sizeof(data))) { if (strlen(data) == 6) { // Convert hex to decimal char *endptr; color = strtoul(data, &endptr, 16); } else { printf("Invalid XML color value \"%s\" = %s\n", m_Tag, data); m_Success = false; } } else { printf("Error parsing XML data: \"%s\"\n", m_Tag); m_Success = false; } } return (color); }
double XmlParser::ParseDouble(char **buffer, int id, double min, double max, bool allow_minus_one) { double value = 0.0; char data[512]; CheckID(id); if (m_Success) { if (GetXmlData(buffer, data, sizeof(data))) { value = atof(data); if (allow_minus_one && (value == -1)) { // negative one is a legal value } else if ((value < min) || (value > max)) { printf("Invalid XML value \"%s\" = %s (min=%g max=%g)\n", m_Tag, data, min, max); m_Success = false; } } else { printf("Error parsing XML data: \"%s\"\n", m_Tag); m_Success = false; } } return (value); }
int XmlParser::ParseInt(char **buffer, int id, int min, int max) { int value = 0; char data[512]; CheckID(id); if (m_Success) { if (GetXmlData(buffer, data, sizeof(data))) { value = atoi(data); if ((value < min) || (value > max)) { printf("Invalid XML value \"%s\" = %s (min=%d max=%d)\n", m_Tag, data, min, max); m_Success = false; } } else { printf("Error parsing XML data: \"%s\"\n", m_Tag); m_Success = false; } } return (value); }
bool CVBRHeader::CheckVBRI( CMPAFile* pMPAFile, DWORD& dwOffset ) { // VBRI ID found? if( !CheckID( pMPAFile, 'V', 'B', 'R', 'I', dwOffset ) ) return false; return true; }
char * XmlParser::ParseString(char **buffer, int id) { char data[512]; CheckID(id); if (m_Success) { if (!GetXmlData(buffer, data, sizeof(data))) { printf("Error parsing XML data: \"%s\"\n", m_Tag); m_Success = false; } } return (DecodeXmlString(data)); }
bool GetAssignment(std::string line) { if (line.find(";") != string::npos) { //check for characters after the ; std::string lineS = line.substr(line.find_first_of(";"), line.length() - line.find_first_of("=")); for (int i = 0; i < lineS.length(); i++) { if (CheckChar(lineS[i])) { error = "charaters after semicolon!"; return false; } } line = line.substr(0, line.find_first_of(";")); // lineS = line.substr(0, line.find_first_of("=")); //check if line contains white space if (lineS.find(" ") != string::npos) { int indexOfWhiteSpace = lineS.find_first_of(" "); if (indexOfWhiteSpace == lineS.length() - 1) lineS = lineS.substr(0, indexOfWhiteSpace); else if (indexOfWhiteSpace == 0) lineS = lineS.substr(1); else { error = "incorrent space in id"; return false; } } if (!CheckID(lineS)) return false; if (!GetExpression(line.substr(line.find_first_of("=") + 1, (line.length() - line.find_first_of("=") - 1)))) return false; return true; } error = "No semicolon in the assignment!"; return false; }
enum VariableType GetExpressionType(TreeNode* currentNode, MethodSymbols* methodTable) { enum VariableType temp1,temp2; switch(currentNode->type) { case TREE_OR : return CheckOperatorBoolsOnly("||",currentNode,methodTable); case TREE_AND : return CheckOperatorBoolsOnly("&&",currentNode,methodTable); case TREE_EQ : return CheckOperatorComparison("==",currentNode,methodTable); case TREE_NEQ : return CheckOperatorComparison("!=",currentNode,methodTable); case TREE_LT : return CheckOperatorCompareIntsOnly("<",currentNode,methodTable); case TREE_GT : return CheckOperatorCompareIntsOnly(">",currentNode,methodTable); case TREE_LEQ : return CheckOperatorCompareIntsOnly("<=",currentNode,methodTable); case TREE_GEQ : return CheckOperatorCompareIntsOnly(">=",currentNode,methodTable); case TREE_ADD : return CheckOperatorIntsOnly("+",currentNode,methodTable); case TREE_SUB : return CheckOperatorIntsOnly("-",currentNode,methodTable); case TREE_MUL : return CheckOperatorIntsOnly("*",currentNode,methodTable); case TREE_DIV : return CheckOperatorIntsOnly("/",currentNode,methodTable); case TREE_MOD : return CheckOperatorIntsOnly("%",currentNode,methodTable); case TREE_NOT : if((temp1=GetExpressionType(currentNode->sons->node,methodTable))==VARIABLE_BOOL) return VARIABLE_BOOL; else { PrintSemanticError("Operator %s cannot be applied to type %s\n","!",VarTypeToString(temp1)); return VARIABLE_INVALID; } case TREE_PLUS : if((temp1=GetExpressionType(currentNode->sons->node,methodTable))==VARIABLE_INT) return VARIABLE_INT; else { PrintSemanticError("Operator %s cannot be applied to type %s\n","+",VarTypeToString(temp1)); return VARIABLE_INVALID; } case TREE_MINUS : if((temp1=GetExpressionType(currentNode->sons->node,methodTable))==VARIABLE_INT) return VARIABLE_INT; else { PrintSemanticError("Operator %s cannot be applied to type %s\n","-",VarTypeToString(temp1)); return VARIABLE_INVALID; } case TREE_LENGTH : temp1=GetExpressionType(currentNode->sons->node,methodTable); switch (temp1) { case VARIABLE_BOOLARRAY : case VARIABLE_INTARRAY : case VARIABLE_STRINGARRAY : return VARIABLE_INT; default : PrintSemanticError("Operator %s cannot be applied to type %s\n",".length",VarTypeToString(temp1)); return VARIABLE_INVALID; } return VARIABLE_INT; case TREE_LOADARRAY : temp1=GetExpressionType(currentNode->sons->node,methodTable); /*look up variable type*/ temp2=GetExpressionType(currentNode->sons->next->node,methodTable); /*Look up indexer type*/ if (temp2!=VARIABLE_INT) { /*indexer value is not an int*/ PrintSemanticError("Operator %s cannot be applied to types %s, %s\n","[",VarTypeToString(temp1),VarTypeToString(temp2)); return VARIABLE_INVALID; } /*Check if the indexed variable is a valid array type*/ switch (temp1) { case VARIABLE_BOOLARRAY : return VARIABLE_BOOL; case VARIABLE_INTARRAY : return VARIABLE_INT; case VARIABLE_STRINGARRAY : /*Can't do anything with this particular array type*/ default : /*operator [] being used in something not indexable*/ PrintSemanticError("Operator %s cannot be applied to types %s, %s\n","[",VarTypeToString(temp1),VarTypeToString(temp2)); return VARIABLE_INVALID; } case TREE_CALL : return CheckCall(currentNode,methodTable); case TREE_NEWINT : temp1 = GetExpressionType(currentNode->sons->node,methodTable); if (temp1!=VARIABLE_INT) { /*Size must be an int*/ PrintSemanticError("Operator %s cannot be applied to type %s\n","new int",VarTypeToString(temp1)); return VARIABLE_INVALID; } return VARIABLE_INTARRAY; case TREE_NEWBOOL : temp1 = GetExpressionType(currentNode->sons->node,methodTable); if (temp1!=VARIABLE_INT) { /*Size must be an int*/ PrintSemanticError("Operator %s cannot be applied to type %s\n","new boolean",VarTypeToString(temp1)); return VARIABLE_INVALID; } return VARIABLE_BOOLARRAY; case TREE_PARSEARGS : temp1=GetExpressionType(currentNode->sons->node,methodTable); /*look up variable type*/ temp2=GetExpressionType(currentNode->sons->next->node,methodTable); /*Look up indexer type*/ if (temp2!=VARIABLE_INT) { /*indexer value is not an int*/ PrintSemanticError("Operator %s cannot be applied to types %s, %s\n","Integer.parseInt",VarTypeToString(temp1),VarTypeToString(temp2)); return VARIABLE_INVALID; } switch (temp1) { /*parseArgs only takes string arrays*/ case VARIABLE_STRINGARRAY : return VARIABLE_INT; /*Parseargs always returns an int since it's Integer.parseInt*/ break; default : /*operator [] being used in something not indexable*/ PrintSemanticError("Operator %s cannot be applied to types %s, %s\n","Integer.parseInt",VarTypeToString(temp1),VarTypeToString(temp2)); return VARIABLE_INVALID; } return VARIABLE_INVALID; case TREE_ID : return CheckID(currentNode,methodTable); case TREE_INTLIT : return CheckLitInt((char*)currentNode->args); case TREE_BOOLLIT : return CheckLitBool((char*)currentNode->args); default : return VARIABLE_INVALID; } }
bool GetExpression(std::string line) { bool op = false; int parens = 0; while (line.length() > 0) { //first count the parens for (int i = 0; i < line.length(); i ++) { if (line[i] == '(') { parens++; } else if (line[i] == ')') { parens--; } } if (parens != 0) { error = "parenthesis do not match"; return false; } while (line[0] == ' ' && line[0] != NULL) { if (line.length() == 1) goto Finish; else line = line.substr(1); } if (line[0] == '(') { if (!op) { //first count the parens for (int i = 0; i < line.length(); i ++) { if (line[i] == '(') { parens++; } else if (line[i] == ')') { parens--; if (parens == 0) { if (!GetExpression(line.substr(1).substr(0, i - 1))) return false; line = line.substr(i + 1, line.length() - i); op = true; break; } } } } else { error = "incorrect use of parenthesis"; return false; } } else if (line.find(' ') != string::npos) { if (op) { if (!CheckOp(line.substr(0, line.find_first_of(' ')))) { error = "invalid expression"; return false; } op = false; line = line.substr(line.find_first_of(' '), line.length() - line.find_first_of(' ')); } else { if (!CheckID(line.substr(0, line.find_first_of(' ')))) { error = "invalid expression"; return false; } op = true; line = line.substr(line.find_first_of(' '), line.length() - line.find_first_of(' ')); } } else { if (!op) { if (!CheckID(line)) { error = "invalid expression"; return false; } op = true; line = line.substr(line.length()); } else { error = "cannot end expression in a op"; return false; } } } Finish: return op; }
/********************************************************************************** * * Friend Functions and Operator Overloading * _________________________________________________________________________________ * This program will test the CheckID friend function, == operator, additional * member function which added an integer to age, and the + operator to add * a constant to a CS1CStudent's age * _________________________________________________________________________________ * INPUTS: * <none> * * OUTPUTS: * This program will output information of the students **********************************************************************************/ int main() { /****************************************************************************** * CONSTANTS * ---------------------------------------------------------------------------- * LINE_LENGTH : The length of the line *****************************************************************************/ const int LINE_LENGTH = 60; int addedAge; //The years being added to the student's age addedAge = 5; //CS1C Student Pujols CS1CStudent pujols("Alberto Pujols", 668899, 2496651234, 35, 'M', "Freshman", 3.3, 778899, true, 5, 12, 2016); //CS1C Student Kershaw CS1CStudent kershaw("Clayton Kershaw", 156789, 2485556543, 29, 'M', "Sophomore", 2.5, 456789, false, 5, 11, 2015); //Math Student Calculus MathStudent calculus("Joe Calculus",668888, 2495551234, 23, 'm', "Freshman", 3.3, "1234 Main", "Laguna Niguel", "CA", 92677); //OUTPUT - Class Headings cout << PrintHeader("Elva Nguyen", 'a', "Friend Functions and Operator Overloading", 8); cout << setfill('-'); //1 - Test Friend Function CheckID cout << "Test CheckID Friend Funtion" << endl << endl; cout << "Calculus's ID: " << calculus.getID() << endl; cout << "Pujols' ID:" << pujols.getID() << endl; //Check ID of Pujols and Calculus cout << (CheckID(calculus, pujols)? "Same": "Not the same") << endl << endl; //Change Pujols' ID to Calculus' ID cout << "Change Pujols' ID" << endl << endl; pujols.setID(calculus.getID()); cout << "Calculus's ID: " << calculus.getID() << endl; cout << "Pujols' ID:" << pujols.getID() << endl; cout << (CheckID(calculus, pujols)? "Same": "Not the same") << endl; cout << setw(LINE_LENGTH) << '-' << endl; //2 - Test == operator of CS1CStudent cout << "Test == Operator of CS1CStudent\n" << endl; cout << "Kershaw's ID: " << kershaw.getID() << endl; cout << "Pujols' ID:" << pujols.getID() << endl; cout << (pujols == kershaw? "Same": "Not the same") << endl << endl << endl; //Change Pujols' ID to Kershaw's ID cout << "Change Pujols' ID\n" << endl; pujols.setID(kershaw.getID()); cout << "Kershaw's ID: " << kershaw.getID() << endl; cout << "Pujols' ID:" << pujols.getID() << endl; cout << (pujols == kershaw? "Same": "Not the same") << endl; cout << setw(LINE_LENGTH) << '-' << endl; //3 - Test the additional member function which add the age cout <<"Test the additional member function which added an integer to age\n\n"; cout << "Pujols' initial age: " << pujols.getAge() << endl; pujols.ChangeAge(addedAge); cout << addedAge << " is added to the student's age.\n";; cout << "Pujols' current age: " << pujols.getAge() << endl; cout << setw(LINE_LENGTH) << '-' << endl; //4 - Test the + operator to add a constant to a CS1CStudent's age cout << "Test the + operator to add a constant to a CS1CStudent's age\n\n"; cout << "Kershaw' initial age: " << kershaw.getAge() << endl; kershaw = kershaw + addedAge; cout << addedAge << " is added to the student's age.\n";; cout << "Kershaw' current age: " << kershaw.getAge() << endl; cout << setw(LINE_LENGTH) << '-' << endl; return 0; }