changeset 15:449b0fc9f764

added cpp version 2
author ju
date Thu, 05 Jul 2007 18:08:31 +0200
parents 3baea9ea9103
children dbaa8a943271
files graph2.cpp
diffstat 1 files changed, 263 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graph2.cpp	Thu Jul 05 18:08:31 2007 +0200
@@ -0,0 +1,263 @@
+
+/*************************
+ * GraphList2dot Converter
+ *  
+ * Autor: Julian Forster
+ * www.ProgMaschine.de.vu
+ * Autor: Markus Schnalke 
+ * http://prog.marmaro.de
+ * 
+ *************************/ 
+
+#include <iostream> 
+#include <fstream>
+
+using namespace std;
+
+#define n 1000 //Size of Namelist
+#define l 20   //MaxNameLength 
+
+//Inputfile
+ifstream inFile;
+
+//Namelist
+char names [n][l];
+
+void readUntil(char *untilText) {
+    //cout << "readUntil" << endl;
+    int i = 0;
+    while (!inFile.eof()){
+        char cChar = inFile.get();
+        //cout << cChar;
+        if (cChar == untilText[i]){
+            i++;
+            if (i >= strlen(untilText)) return;
+        } else {
+            i = 0;
+        }
+       // cout << i;
+    }
+    //cout << "endReadUntil" << endl;
+}
+
+//Liest bis zum untilText, aber nicht weiter als bis zum lastText
+//wenn lastText vor untilText kommt wird bis dahin gelesen 
+//und false zurückgegeben
+bool readUntil(char *untilText, char *lastText) {
+    //cout << "readUntil" << endl;
+    int i = 0;
+    int ii = 0;
+    while (!inFile.eof()){
+        char cChar = inFile.get();
+        //cout << cChar;
+        if (cChar == untilText[i]){
+            i++;
+            if (i >= strlen(untilText)) return true;
+        } else {
+            i = 0;
+        }
+        if (cChar == lastText[ii]){
+            ii++;
+            if (ii >= strlen(lastText)) return false;
+        } else {
+            ii = 0;
+        }
+       // cout << i;
+    }
+    return false;
+    //cout << "endReadUntil" << endl;
+}
+int getIndex(char * name) {
+    for (int i = 0; i < n; i++) {
+        for (int ii = 0; ii < l; ii++) {
+            if (names[i][ii] != name[ii]) {
+                break;
+            }
+            if (names[i][ii] == '\0') return i;
+        }
+    }
+    return -1;
+}
+
+void readGraph(char *file) {
+    if (inFile.open(file)) {
+        //header
+        cout << "digraph G {" << endl;
+        cout << "size=\"25,22\";" << endl;
+        
+        int i = -1; 
+        int mode = 0; //0: start, 1: read headbuddy, 2: no whitespace 3: read buddy
+        while (!inFile.eof()) {
+            //read one line
+            int ii = 0;
+            char line [10 + l];
+            while (!inFile.eof()) {
+                char c = inFile.get();
+                if (c == '\n') {
+                    line[ii] = '\0';
+                    break;
+                } else {
+                    line[ii] = c;
+                    ii++;
+                }
+            }
+            cout << line << endl; 
+        }
+            /*
+            if (!line[0] == '\0') {
+                if (line[0] == ' ') {
+                    
+                    int begin = 0;
+                    char buddy[l];
+                    for (begin = 1; begin < 10 + l; begin++) {
+                        if (line[begin] != ' ') {
+                            break;
+                        }
+                    }
+                    if (line[begin] != '\0') {
+                        for (int j = begin; j < 10 + l; j++) {
+                            if ((line[j] == ' ') || (line[j] == '\0')) {
+                                buddy[j-begin] = '\0';
+                                break;
+                            } else {
+                                if (j >= l - 1) {
+                                    cerr << "name too long" << line << endl;
+                                    return;
+                                }
+                                buddy[j-begin] = line[j];
+                            }
+                        }
+                        //Add Buddy
+                        
+                        
+                    }
+                } else {
+                    i++;
+                    for (int j = 0; j < 10 + l; j++) {
+                        if ((line[j] == ' ') || (line[j] == '\0')) {
+                            names[i][j] = '\0';
+                            break;
+                        } else {
+                            if (j >= l - 1) {
+                                cerr << "name too long" << line << endl;
+                                return;
+                            }
+                            names[i][j] = line[j];
+                        }
+                    }
+                    
+                }
+                
+            }
+                
+            
+        }*/ 
+        
+        //footer
+        cout << "}" << endl;
+    } else {
+        cerr << "could not open " << file << endl;
+    }
+    
+   /* if (inFile) {
+        int i = 0;
+        while (!inFile.eof()){
+            readUntil("Profil von: ");
+            for (int ii = 0; (ii < l) && !inFile.eof(); ii++) {
+                char cChar = inFile.get();
+                if ((cChar == '\n') || (cChar == ' ')) {
+                    names[i][ii] = '\0';
+                    break;
+                }
+                names[i][ii] = cChar;
+            }
+    //        cout <<"#"<< names[i] <<"#"<< endl;
+            i++;
+        }
+        inFile.close();        
+    }
+    
+    inFile.open(file);
+    if (inFile) {
+        int i = 0;
+        while (!inFile.eof()){
+            readUntil("Profil von: ");
+            char name [l];
+            for (int ii = 0; (ii < l) && !inFile.eof(); ii++) {
+                char cChar = inFile.get();
+                if ((cChar == '\n') || (cChar == ' ')) {
+                    name[ii] = '\0';
+                    break;
+                }
+                name[ii] = cChar;
+            }
+            //Vertex suchen
+            int index = getIndex(name);
+            if (index > -1) {
+            
+            readUntil("Buddyliste:");
+            
+           //     cout <<"#buddylist von:#"<< names[index] <<"#"<< endl;
+                  
+                while (readUntil("Nachricht schreiben ", "Bilder")) {
+                    char buddy [l];
+                    //cout << endl;
+                    for (int ii = 0; (ii < l) && !inFile.eof(); ii++) {
+                        char cChar = inFile.get();
+                        if ((cChar == '\n') || (cChar == ' ')) {
+                            buddy[ii] = '\0';
+                            break;
+                        }
+                        buddy[ii] = cChar;
+                    }
+                    //Vertex suchen
+                    int indexB = getIndex(buddy);
+                    if (indexB > -1) {
+                        cout << "  \"" << names[index] << "\" -> \"" << names[indexB]
+                                << "\";" << endl;
+                    }
+                    
+                    //cout <<"#buddy#"<< buddy <<"#"<< endl;
+                }
+            }
+            i++;
+        }
+        inFile.close();        
+    } 
+    cout << "}" << endl;*/
+}
+
+
+int main(int argc, char **argv){
+    if (argc == 2) {
+        if (strcmp(argv[1], "--version") == 0) {
+          cout << "Buddylistgrapher (C++)" << endl;
+          cout << "======================" << endl;
+          cout << "version 2.0" << endl;
+          cout << endl;
+          cout << "by Julian Forster (http://progmaschine.de.vu)" << endl;
+          cout << "and a little bit by Markus Schnalke (http://prog.marmaro.de)" << endl;
+        } else if (strcmp(argv[1], "--help") == 0) {
+          cout << "Buddylistgrapher" << endl;
+          cout << "================" << endl;
+          cout << endl;
+          cout << "usage: " << argv[0] << " <input.txt>" << endl;
+          cout << endl;
+          cout << "The grapher generates output to stdout." << endl;
+          cout << "This output is the input for the graphviz tools." << endl;
+          cout << endl;
+          cout << "You can use it like this:" << endl;
+          cout << argv[0] << " input.txt | dot -Tpng > pic.png" << endl;
+          cout << endl;
+          cout << "ToDo:" << endl;
+          cout << "The program was written for one specific kind of input data," << endl;
+          cout << "so there is still some work to do to use it for general input." << endl;
+        } else {
+          readGraph(argv[1]); 
+        }
+        return 0;
+    }
+    cerr << "usage: " << argv[0] << " <input.txt>" << endl;
+    return 1;
+}
+