view graph.cpp @ 16:dbaa8a943271 default tip

merge
author ju
date Thu, 05 Jul 2007 18:13:01 +0200
parents 6c327ae23d2c
children
line wrap: on
line source


#include <iostream> 
#include <fstream>

using namespace std;

#define n 1000
#define l 20

//Graph File
ifstream inFile;

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) {
    inFile.open(file);
    cout << "digraph G {" << endl;
    cout << "size=\"25,22\";" << 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;
}