Bäuchte Hilfe bei Syntax Highlight
-
Hey @all,
da dies mein erstes Posting ist, will ich zuerst mal alle Grüßen.Nun zu meinem Problem:
Ich habe schon im Forum, bei Google, usw. gesucht jedoch nichts passendes gefunden.
Und zwar geht es darum, ich habe mir einen Editor geschrieben, soweit so gut, funktioniert auch super, nur würde ich dem Editor gerne das "Syntax Highlight" beibringen, dies muss nicht unbedingt beim Eintippen geschehen, sondern es genügt vorerst mal, wenn er "Syntax Highlight" anwendet, wenn ich eine Datei öffne.Das er mir die Positionen von den Kommentaren und Strings speichert habe ich mal hinbekommen, nur wie geht es jetzt weiter?
Bitte nicht schimpfen, ich habe die Klasse vorerst in der Hauptdatei drinnen, ich weiß natürlich das, daß nicht sein soll. Wird auch wenn es funktioniert in eine separate Datei geschrieben.
Für die Tests habe ich es derweilen so erledigt, das er mir nur ein HTML-File mit den Farben ausgibt, wenn das ganze funktioniert werde ich es zum Editor hinugeben. ( Ist ja im moment egal ob gleich auf Bildschirm oder halt in HTML)Hier mal der Code (Die Zeilen die Kommentiert sind, dort liegt der Fehler):
#include <vector> #include <fstream> #include <string> #include <iostream> #include <algorithm> #include <sstream> using namespace std; const bool LineNumber = true; static int Line = 0; static int MaxLines = 0; const int BLOCK_COMMENT_BEGIN = 1; const int BLOCK_COMMENT_END = 2; const int LINE_COMMENT = 3; const int STRING_BEGIN = 4; const int STRING_END = 5; const int INFO_TYPE = 0; const int INFO_VECPOS = 1; const int INFO_STRINGPOS = 2; class Info { private: struct InfoDef { int Type; /********************************** 1 = block-comment Begin 2 = block-comment End 3 = line-comment 4 = string Begin 5 = string End **********************************/ int VectorPos; int StringPos; InfoDef(int _t, int _v, int _s) : Type(_t), VectorPos(_v), StringPos(_s) { } }; typedef vector<InfoDef> InfoVector; InfoVector info; public: Info() {} ~Info() {} void add(int _t, int _v, int _s) { info.push_back(InfoDef(_t, _v, _s)); } int get(unsigned int index, int choose) { switch(choose) { case INFO_TYPE: return info[index].Type; break; case INFO_VECPOS: return info[index].VectorPos; break; case INFO_STRINGPOS: return info[index].StringPos; break; } } unsigned int getSize() const { return info.size(); } }; int main(int argc, char* argv[]) { string Filename = argv[argc-1]; string FilenameHTML = Filename + ".html"; fstream file(Filename.c_str(), ios::in); fstream fileHTML(FilenameHTML.c_str(), ios::out); Info* myInfo = new Info(); string line; bool testbvool; typedef vector<string> StringVector; StringVector keywords; StringVector lVec; StringVector::iterator lVecIter; keywords.push_back("if"); keywords.push_back("else"); keywords.push_back("using"); keywords.push_back("while"); keywords.push_back("int"); keywords.push_back("typedef"); keywords.push_back("void"); keywords.push_back("class"); keywords.push_back("char"); keywords.push_back("float"); keywords.push_back("double"); keywords.push_back("signed"); keywords.push_back("unsigned"); keywords.push_back("namespace"); keywords.push_back("struct"); // Schreibe komplette Datei in den Vector while(getline(file, line)) lVec.push_back(line); MaxLines = lVec.size(); // Herrichten der HTML-Seite fileHTML << "<html>" << endl; fileHTML << " <head>" << endl; fileHTML << " <title>" << endl; fileHTML << " parsed file: " << Filename << endl; fileHTML << " </title>" << endl; fileHTML << " </head>" << endl << endl; fileHTML << " <body>" << endl; char *comBeg = NULL; char *comEnd = NULL; int com = 0; int com1 = 0; int inc = 0; int str = 0; int chr = 0; int word_count =0; bool found_block_comment = false; bool found_line_comment = false; bool found_string = false; //Suchen nach Ein- und Mehrzeiligen-Kommentaren und Strings for(int i=0; i<lVec.size(); i++) { for(int j=0; j<lVec[i].length(); j++) { // Kommentare mehrzeilig if(lVec[i][j] == '/' && lVec[i][j+1] == '*' && found_block_comment == false) { myInfo->add(1, i, j); found_block_comment = true; //cout << "Mehrzeiliger Kommentar BEGIN gefunden bei (Vec, String): " << i << ", " << j << endl; } else if(lVec[i][j] == '*' && lVec[i][j+1] == '/' && found_block_comment == true) { myInfo->add(2, i, j); found_block_comment = false; //cout << "Mehrzeiliger Kommentar END gefunden bei (Vec, String): " << i << ", " << j << endl; } // Kommentar einzeilig else if(lVec[i][j] == '/' && lVec[i][j+1] == '/' && found_string == false && found_block_comment == false) { found_line_comment = true; myInfo->add(3, i, j); found_line_comment = false; j++; //cout << "Einzeiliger Kommentar gefunden bei (Vec, String): " << i << ", " << j << endl; } // Strings else if(lVec[i][j-1] != '\\' && lVec[i][j-1] != '\'' && lVec[i][j] == '\"' && found_string == false && found_block_comment == false && found_line_comment==false) { found_string = true; myInfo->add(4, i, j); //cout << "String BEGIN gefunden bei (Vec, String): " << i << ", " << j << endl; } else if(lVec[i][j-1] != '\\' && lVec[i][j-1] != '\'' && lVec[i][j] == '\"' && found_string == true && found_block_comment == false && found_line_comment==false) { found_string = false; myInfo->add(5, i, j); //cout << "String END gefunden bei (Vec, String): " << i << ", " << j << endl; } } } /************************************************************************************/ // Gib Text aus aus // Gehe durch den ganzen Datei-Vector... /* for(int i=0; i<lVec.size(); i++) { // Suche nach Kommentaren int posBeg = 0, posEnd = 0; for(int l=0; l<myInfo->getSize(); l++) { // Einzeilig //cout << "IN SCHLEIFE" << endl; if(i == myInfo->get(l, INFO_VECPOS)) {// && myInfo->get(l, INFO_TYPE) == LINE_COMMENT) { if(myInfo->get(l, INFO_TYPE) == 3) { cout << "Einzeiligen Kommentar gefunden" << endl; fileHTML << lVec[i].substr(0, myInfo->get(l, INFO_STRINGPOS)); fileHTML << "<font color=\"0000AA\">"; fileHTML << lVec[i].substr(myInfo->get(l, INFO_STRINGPOS)); fileHTML << "</font><br>" << endl; i++; break; } } else break; } fileHTML << lVec[i].substr(0, posBeg); fileHTML << lVec[i].substr(posEnd) << "<br>" << endl; // Suche nach Schlüsselwörtern for(int j=0; j<keywords.size(); j++) { int StringPosBeg = 0, StringPosEnd = 0; // Wenn Schlüsselwort gefunden dann überprüfe ob es in einem Kommentar usw. ist, ansonsten gibt es aus if(lVec[i].find(keywords[j]) != string::npos) { int VecPos = i; StringPosBeg = lVec[i].find(keywords[j]); StringPosEnd = StringPosBeg + keywords[j].length(); bool nothing = true; // Gehe durch den InfoVector und schaue ob es an etwas scheitert kann for(int k=0; k<myInfo->getSize(); k++) { // Wenn diese VectorPosition auch im Infovector gibt, dann überprüfe in if(myInfo->get(k, INFO_VECPOS) == VecPos) { // Überprüfe auf einzeiligen Kommentar // Wenn der Kommentar vor dem Wort anfängt, dann brich ab if(myInfo->get(k, INFO_TYPE) == LINE_COMMENT && myInfo->get(k, INFO_STRINGPOS) < StringPosBeg) nothing = false; // Überprüfe auf String else if(myInfo->get(k, INFO_TYPE) == STRING_BEGIN || myInfo->get(k, INFO_TYPE) == STRING_END) { // Überprüfe ob sich das Wort in einem String aufhält // Wenn der Type ein String Anfang kennzeichnet und vor dem Wort sitzt, dann scheitere if(myInfo->get(k, INFO_TYPE) == STRING_BEGIN && myInfo->get(k, INFO_STRINGPOS) < StringPosBeg) nothing = false; // Wenn der Type ein String Ende kennzeichnet und nach dem Wort sitzt, dann scheitere else if(myInfo->get(k, INFO_TYPE) == STRING_END && myInfo->get(k, INFO_STRINGPOS) < StringPosEnd) nothing = false; } }//if(myInfo->get(k, INFO_VECPOS) == VecPos) { }//for(int k=0; k<myInfo->getSize(); k++) { // Wenn nichts daran scheitert, dann gib den String in GRÜN aus if(nothing) { fileHTML << "<font color=\"00FF00\">" << keywords[j] << "</font>"; fileHTML << lVec[i].substr(StringPosEnd); } }//if(lVec[i].find(keywords[j]) != string::npos) { // Wenn kein Schlüsselwort gefunden wurde, dann gib die Zeile aus (NOCH NICHT AUF STRING UND KOMMENTAR GEPRÜFT) else { fileHTML << lVec[i].substr(0, StringPosBeg); fileHTML << lVec[i].substr(StringPosEnd); fileHTML << lVec[i++]; break; } // Wenn kein Schlüsslwort gefunden wurde, überprüfe ob es sich um einen Kommentar usw. handelt. } // Gib einen Zeilenumbruch aus fileHTML << "<br>" << endl; // Untersuche den InfoVector }*/ /************************************************************************************/ // EndTags der HTML-Seite schreiben fileHTML << " </body>" << endl; fileHTML << "</html>" << endl; return 0; }
Vielen Dank im Voraus, ich hoffe mir kann jemand helfen, bin schon ziemlich am verzweifeln:(
Lg _freeze_
-
Hat den keiner eine Idee?
Lg _freeze_
-
Patrick bist dus?
-
Nö, ich heiße nicht Patrick, sorry
Lg _freeze_
-
Kann mir denn wirklich keiner helfen?
Lg _freeze_