I have *just* started on working with C++ and i am interested in what the fellow forumers think of my Test Parser program .
(I have experience with PHP from before.)
File: main.cpp, Executable Download
Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
//Maybe not be needed
#include <algorithm>
#include <cctype>
using namespace std;
string StringToLower(string strToConvert)
{//change each element of the string to lower case
for(int i=0;i<strToConvert.length();i++)
{
strToConvert[i] = tolower(strToConvert[i]);
}
return strToConvert;//return the converted string
}
void BreakLine ( )
{
cout << "\n";
}
void PrintItOut(string eOutputText)
{
cout << "\"" << eOutputText << "\"" << endl;
}
void Parse()
{
//INIT
string Line = "";
string cParseLine = "";
char cParseCharacter;
char cParseCharacterPrev = '\0';
char cParseCharacterNext = '\0';
string FullString = "";
bool ParseString = 0;
bool NoEndLine = 0;
bool CParserContinue = 1;
int FunctionType = 0;
int cPos = 0;
int cLength = 0;
ifstream fFile ("parse.txt");
while (! fFile.eof() )
{
getline(fFile, Line);
FullString.append(Line);
}
fFile.close();
cLength = FullString.length();
//PARSE!
while (cPos < cLength)
{
//Get character; and look for escaping
cParseCharacterPrev = cParseCharacter;
cParseCharacter = FullString[cPos];
cParseCharacterNext = FullString[cPos + 1];
//cout << cParseCharacterPrev << cParseCharacter << cParseCharacterNext << " ";
//Check if valid
if(ParseString == 0 && cParseCharacter != '\t' && cParseCharacter != ' ' )
{
cParseLine += cParseCharacter;
//////////////////////////////////////////////////
//
// PRINT TEXT
//
//////////////////////////////////////////////////
if ( StringToLower(cParseLine) == "print")
{
//Parse String now.
ParseString = 1;
//Empty the Line
cParseLine.erase();
cPos++;
}
//////////////////////////////////////////////////
//
// LINE BREAK
//
//////////////////////////////////////////////////
if ( StringToLower(cParseLine) == "break" && cParseCharacterNext == ';')
{
//Parse String now.
//ParseString = 1;
//Empty the Line
BreakLine();
cParseLine.erase();
cPos++;
}
//////////////////////////////////////////////////
//
// PAUSE
//
//////////////////////////////////////////////////
if ( StringToLower(cParseLine) == "pause" && cParseCharacterNext == ';')
{
//Parse String now.
//ParseString = 1;
//Empty the Line
BreakLine();
cParseLine.erase();
cPos++;
system("pause");
BreakLine();
}
//////////////////////////////////////////////////
//
// CLEAR SCREEN
//
//////////////////////////////////////////////////
if ( StringToLower(cParseLine) == "clear" && cParseCharacterNext == ';')
{
//Parse String now.
//ParseString = 1;
//Empty the Line
system("cls");
cParseLine.erase();
cPos++;
}
//////////////////////////////////////////////////
//
// EXIT
//
//////////////////////////////////////////////////
if ( StringToLower(cParseLine) == "exit" && cParseCharacterNext == ';')
{
//Parse String now.
//ParseString = 1;
//Empty the Line
BreakLine();
cParseLine.erase();
exit(1);
}
}else if (ParseString == 1){
if (cParseCharacter == '\\')
{
//Nothing, if not next is it
if (cParseCharacterNext == '\\')
{
cParseLine += cParseCharacter;
CParserContinue = 1;
//cParseCharacter = '\\';
cPos ++;
cParseCharacter = cParseCharacterNext;
CParserContinue = 0;
}
if (cParseCharacterNext == ';')
{
NoEndLine = 1;
CParserContinue = 1;
cParseCharacter = '\0';
cPos++;
cParseCharacter = cParseCharacterNext;
}
}
if ( cParseCharacter == ';' && NoEndLine != 1 )
{
//Parse String now.
//Do stuff
if(FunctionType == 0)
{
PrintItOut(cParseLine);
cParseLine.erase();
}
CParserContinue = 0;
ParseString = 0;
}
if (CParserContinue == 1)
{
cParseLine += cParseCharacter;
}
//End String Parsing
}
NoEndLine = 0;
CParserContinue = 1;
cPos++;
}
}
int main(int argc, char *argv[])
{
system("PAUSE");
cout << "\nParse!\n\n";
Parse();
cout << "\nEnd of Parse!\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
What could be optimised here? Or should i rewrite the parser from the beginning? (I am just making this parser to learn C++ and make use of it in games . )
The compiler and program i use: Bloodshed Dev-C++ with mingw compiler.