buddylistgrapher
annotate graph.cpp @ 15:449b0fc9f764
added cpp version 2
author | ju |
---|---|
date | Thu, 05 Jul 2007 18:08:31 +0200 |
parents | 8d8b41f7c0bc |
children |
rev | line source |
---|---|
meillo@0 | 1 |
meillo@0 | 2 #include <iostream> |
meillo@0 | 3 #include <fstream> |
meillo@0 | 4 |
meillo@0 | 5 using namespace std; |
meillo@0 | 6 |
meillo@0 | 7 #define n 1000 |
meillo@0 | 8 #define l 20 |
meillo@0 | 9 |
meillo@0 | 10 //Graph File |
meillo@0 | 11 ifstream inFile; |
meillo@0 | 12 |
meillo@0 | 13 char names [n][l]; |
meillo@0 | 14 |
meillo@0 | 15 void readUntil(char *untilText) { |
meillo@0 | 16 //cout << "readUntil" << endl; |
meillo@0 | 17 int i = 0; |
meillo@0 | 18 while (!inFile.eof()){ |
meillo@0 | 19 char cChar = inFile.get(); |
meillo@0 | 20 //cout << cChar; |
meillo@0 | 21 if (cChar == untilText[i]){ |
meillo@0 | 22 i++; |
meillo@0 | 23 if (i >= strlen(untilText)) return; |
meillo@0 | 24 } else { |
meillo@0 | 25 i = 0; |
meillo@0 | 26 } |
meillo@0 | 27 // cout << i; |
meillo@0 | 28 } |
meillo@0 | 29 //cout << "endReadUntil" << endl; |
meillo@0 | 30 } |
meillo@0 | 31 |
meillo@0 | 32 //Liest bis zum untilText, aber nicht weiter als bis zum lastText |
meillo@0 | 33 //wenn lastText vor untilText kommt wird bis dahin gelesen |
meillo@0 | 34 //und false zurückgegeben |
meillo@0 | 35 bool readUntil(char *untilText, char *lastText) { |
meillo@0 | 36 //cout << "readUntil" << endl; |
meillo@0 | 37 int i = 0; |
meillo@0 | 38 int ii = 0; |
meillo@0 | 39 while (!inFile.eof()){ |
meillo@0 | 40 char cChar = inFile.get(); |
meillo@0 | 41 //cout << cChar; |
meillo@0 | 42 if (cChar == untilText[i]){ |
meillo@0 | 43 i++; |
meillo@0 | 44 if (i >= strlen(untilText)) return true; |
meillo@0 | 45 } else { |
meillo@0 | 46 i = 0; |
meillo@0 | 47 } |
meillo@0 | 48 if (cChar == lastText[ii]){ |
meillo@0 | 49 ii++; |
meillo@0 | 50 if (ii >= strlen(lastText)) return false; |
meillo@0 | 51 } else { |
meillo@0 | 52 ii = 0; |
meillo@0 | 53 } |
meillo@0 | 54 // cout << i; |
meillo@0 | 55 } |
meillo@0 | 56 return false; |
meillo@0 | 57 //cout << "endReadUntil" << endl; |
meillo@0 | 58 } |
meillo@0 | 59 int getIndex(char * name) { |
meillo@0 | 60 for (int i = 0; i < n; i++) { |
meillo@0 | 61 for (int ii = 0; ii < l; ii++) { |
meillo@0 | 62 if (names[i][ii] != name[ii]) { |
meillo@0 | 63 break; |
meillo@0 | 64 } |
meillo@0 | 65 if (names[i][ii] == '\0') return i; |
meillo@0 | 66 } |
meillo@0 | 67 } |
meillo@0 | 68 return -1; |
meillo@0 | 69 } |
meillo@0 | 70 |
meillo@0 | 71 void readGraph(char *file) { |
meillo@0 | 72 inFile.open(file); |
meillo@0 | 73 cout << "digraph G {" << endl; |
meillo@0 | 74 cout << "size=\"25,22\";" << endl; |
meillo@0 | 75 if (inFile) { |
meillo@0 | 76 int i = 0; |
meillo@0 | 77 while (!inFile.eof()){ |
meillo@0 | 78 readUntil("Profil von: "); |
meillo@0 | 79 for (int ii = 0; (ii < l) && !inFile.eof(); ii++) { |
meillo@0 | 80 char cChar = inFile.get(); |
meillo@0 | 81 if ((cChar == '\n') || (cChar == ' ')) { |
meillo@0 | 82 names[i][ii] = '\0'; |
meillo@0 | 83 break; |
meillo@0 | 84 } |
meillo@0 | 85 names[i][ii] = cChar; |
meillo@0 | 86 } |
meillo@0 | 87 // cout <<"#"<< names[i] <<"#"<< endl; |
meillo@0 | 88 i++; |
meillo@0 | 89 } |
meillo@0 | 90 inFile.close(); |
meillo@0 | 91 } |
meillo@0 | 92 |
meillo@0 | 93 inFile.open(file); |
meillo@0 | 94 if (inFile) { |
meillo@0 | 95 int i = 0; |
meillo@0 | 96 while (!inFile.eof()){ |
meillo@0 | 97 readUntil("Profil von: "); |
meillo@0 | 98 char name [l]; |
meillo@0 | 99 for (int ii = 0; (ii < l) && !inFile.eof(); ii++) { |
meillo@0 | 100 char cChar = inFile.get(); |
meillo@0 | 101 if ((cChar == '\n') || (cChar == ' ')) { |
meillo@0 | 102 name[ii] = '\0'; |
meillo@0 | 103 break; |
meillo@0 | 104 } |
meillo@0 | 105 name[ii] = cChar; |
meillo@0 | 106 } |
meillo@0 | 107 //Vertex suchen |
meillo@0 | 108 int index = getIndex(name); |
meillo@0 | 109 if (index > -1) { |
meillo@0 | 110 |
meillo@0 | 111 readUntil("Buddyliste:"); |
meillo@0 | 112 |
meillo@0 | 113 // cout <<"#buddylist von:#"<< names[index] <<"#"<< endl; |
meillo@0 | 114 |
meillo@0 | 115 while (readUntil("Nachricht schreiben ", "Bilder")) { |
meillo@0 | 116 char buddy [l]; |
meillo@0 | 117 //cout << endl; |
meillo@0 | 118 for (int ii = 0; (ii < l) && !inFile.eof(); ii++) { |
meillo@0 | 119 char cChar = inFile.get(); |
meillo@0 | 120 if ((cChar == '\n') || (cChar == ' ')) { |
meillo@0 | 121 buddy[ii] = '\0'; |
meillo@0 | 122 break; |
meillo@0 | 123 } |
meillo@0 | 124 buddy[ii] = cChar; |
meillo@0 | 125 } |
meillo@0 | 126 //Vertex suchen |
meillo@0 | 127 int indexB = getIndex(buddy); |
meillo@0 | 128 if (indexB > -1) { |
meillo@0 | 129 cout << " \"" << names[index] << "\" -> \"" << names[indexB] |
meillo@0 | 130 << "\";" << endl; |
meillo@0 | 131 } |
meillo@0 | 132 |
meillo@0 | 133 //cout <<"#buddy#"<< buddy <<"#"<< endl; |
meillo@0 | 134 } |
meillo@0 | 135 } |
meillo@0 | 136 i++; |
meillo@0 | 137 } |
meillo@0 | 138 inFile.close(); |
meillo@0 | 139 } |
meillo@0 | 140 cout << "}" << endl; |
meillo@0 | 141 } |
meillo@0 | 142 |
meillo@0 | 143 |
meillo@0 | 144 int main(int argc, char **argv){ |
meillo@0 | 145 if (argc == 2) { |
meillo@0 | 146 if (strcmp(argv[1], "--version") == 0) { |
meillo@2 | 147 cout << "Buddylistgrapher (C++)" << endl; |
meillo@2 | 148 cout << "======================" << endl; |
meillo@2 | 149 cout << "version 2.0" << endl; |
meillo@2 | 150 cout << endl; |
meillo@2 | 151 cout << "by Julian Forster (http://progmaschine.de.vu)" << endl; |
meillo@2 | 152 cout << "and a little bit by Markus Schnalke (http://prog.marmaro.de)" << endl; |
meillo@1 | 153 } else if (strcmp(argv[1], "--help") == 0) { |
meillo@2 | 154 cout << "Buddylistgrapher" << endl; |
meillo@2 | 155 cout << "================" << endl; |
meillo@2 | 156 cout << endl; |
meillo@1 | 157 cout << "usage: " << argv[0] << " <input.txt>" << endl; |
meillo@1 | 158 cout << endl; |
meillo@1 | 159 cout << "The grapher generates output to stdout." << endl; |
meillo@1 | 160 cout << "This output is the input for the graphviz tools." << endl; |
meillo@1 | 161 cout << endl; |
meillo@1 | 162 cout << "You can use it like this:" << endl; |
meillo@1 | 163 cout << argv[0] << " input.txt | dot -Tpng > pic.png" << endl; |
meillo@1 | 164 cout << endl; |
meillo@1 | 165 cout << "ToDo:" << endl; |
meillo@1 | 166 cout << "The program was written for one specific kind of input data," << endl; |
meillo@1 | 167 cout << "so there is still some work to do to use it for general input." << endl; |
meillo@0 | 168 } else { |
meillo@0 | 169 readGraph(argv[1]); |
meillo@0 | 170 } |
meillo@0 | 171 return 0; |
meillo@0 | 172 } |
meillo@0 | 173 cerr << "usage: " << argv[0] << " <input.txt>" << endl; |
meillo@0 | 174 return 1; |
meillo@0 | 175 } |
meillo@0 | 176 |