buddylistgrapher

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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/graph2.cpp	Thu Jul 05 18:08:31 2007 +0200
     1.3 @@ -0,0 +1,263 @@
     1.4 +
     1.5 +/*************************
     1.6 + * GraphList2dot Converter
     1.7 + *  
     1.8 + * Autor: Julian Forster
     1.9 + * www.ProgMaschine.de.vu
    1.10 + * Autor: Markus Schnalke 
    1.11 + * http://prog.marmaro.de
    1.12 + * 
    1.13 + *************************/ 
    1.14 +
    1.15 +#include <iostream> 
    1.16 +#include <fstream>
    1.17 +
    1.18 +using namespace std;
    1.19 +
    1.20 +#define n 1000 //Size of Namelist
    1.21 +#define l 20   //MaxNameLength 
    1.22 +
    1.23 +//Inputfile
    1.24 +ifstream inFile;
    1.25 +
    1.26 +//Namelist
    1.27 +char names [n][l];
    1.28 +
    1.29 +void readUntil(char *untilText) {
    1.30 +    //cout << "readUntil" << endl;
    1.31 +    int i = 0;
    1.32 +    while (!inFile.eof()){
    1.33 +        char cChar = inFile.get();
    1.34 +        //cout << cChar;
    1.35 +        if (cChar == untilText[i]){
    1.36 +            i++;
    1.37 +            if (i >= strlen(untilText)) return;
    1.38 +        } else {
    1.39 +            i = 0;
    1.40 +        }
    1.41 +       // cout << i;
    1.42 +    }
    1.43 +    //cout << "endReadUntil" << endl;
    1.44 +}
    1.45 +
    1.46 +//Liest bis zum untilText, aber nicht weiter als bis zum lastText
    1.47 +//wenn lastText vor untilText kommt wird bis dahin gelesen 
    1.48 +//und false zur├╝ckgegeben
    1.49 +bool readUntil(char *untilText, char *lastText) {
    1.50 +    //cout << "readUntil" << endl;
    1.51 +    int i = 0;
    1.52 +    int ii = 0;
    1.53 +    while (!inFile.eof()){
    1.54 +        char cChar = inFile.get();
    1.55 +        //cout << cChar;
    1.56 +        if (cChar == untilText[i]){
    1.57 +            i++;
    1.58 +            if (i >= strlen(untilText)) return true;
    1.59 +        } else {
    1.60 +            i = 0;
    1.61 +        }
    1.62 +        if (cChar == lastText[ii]){
    1.63 +            ii++;
    1.64 +            if (ii >= strlen(lastText)) return false;
    1.65 +        } else {
    1.66 +            ii = 0;
    1.67 +        }
    1.68 +       // cout << i;
    1.69 +    }
    1.70 +    return false;
    1.71 +    //cout << "endReadUntil" << endl;
    1.72 +}
    1.73 +int getIndex(char * name) {
    1.74 +    for (int i = 0; i < n; i++) {
    1.75 +        for (int ii = 0; ii < l; ii++) {
    1.76 +            if (names[i][ii] != name[ii]) {
    1.77 +                break;
    1.78 +            }
    1.79 +            if (names[i][ii] == '\0') return i;
    1.80 +        }
    1.81 +    }
    1.82 +    return -1;
    1.83 +}
    1.84 +
    1.85 +void readGraph(char *file) {
    1.86 +    if (inFile.open(file)) {
    1.87 +        //header
    1.88 +        cout << "digraph G {" << endl;
    1.89 +        cout << "size=\"25,22\";" << endl;
    1.90 +        
    1.91 +        int i = -1; 
    1.92 +        int mode = 0; //0: start, 1: read headbuddy, 2: no whitespace 3: read buddy
    1.93 +        while (!inFile.eof()) {
    1.94 +            //read one line
    1.95 +            int ii = 0;
    1.96 +            char line [10 + l];
    1.97 +            while (!inFile.eof()) {
    1.98 +                char c = inFile.get();
    1.99 +                if (c == '\n') {
   1.100 +                    line[ii] = '\0';
   1.101 +                    break;
   1.102 +                } else {
   1.103 +                    line[ii] = c;
   1.104 +                    ii++;
   1.105 +                }
   1.106 +            }
   1.107 +            cout << line << endl; 
   1.108 +        }
   1.109 +            /*
   1.110 +            if (!line[0] == '\0') {
   1.111 +                if (line[0] == ' ') {
   1.112 +                    
   1.113 +                    int begin = 0;
   1.114 +                    char buddy[l];
   1.115 +                    for (begin = 1; begin < 10 + l; begin++) {
   1.116 +                        if (line[begin] != ' ') {
   1.117 +                            break;
   1.118 +                        }
   1.119 +                    }
   1.120 +                    if (line[begin] != '\0') {
   1.121 +                        for (int j = begin; j < 10 + l; j++) {
   1.122 +                            if ((line[j] == ' ') || (line[j] == '\0')) {
   1.123 +                                buddy[j-begin] = '\0';
   1.124 +                                break;
   1.125 +                            } else {
   1.126 +                                if (j >= l - 1) {
   1.127 +                                    cerr << "name too long" << line << endl;
   1.128 +                                    return;
   1.129 +                                }
   1.130 +                                buddy[j-begin] = line[j];
   1.131 +                            }
   1.132 +                        }
   1.133 +                        //Add Buddy
   1.134 +                        
   1.135 +                        
   1.136 +                    }
   1.137 +                } else {
   1.138 +                    i++;
   1.139 +                    for (int j = 0; j < 10 + l; j++) {
   1.140 +                        if ((line[j] == ' ') || (line[j] == '\0')) {
   1.141 +                            names[i][j] = '\0';
   1.142 +                            break;
   1.143 +                        } else {
   1.144 +                            if (j >= l - 1) {
   1.145 +                                cerr << "name too long" << line << endl;
   1.146 +                                return;
   1.147 +                            }
   1.148 +                            names[i][j] = line[j];
   1.149 +                        }
   1.150 +                    }
   1.151 +                    
   1.152 +                }
   1.153 +                
   1.154 +            }
   1.155 +                
   1.156 +            
   1.157 +        }*/ 
   1.158 +        
   1.159 +        //footer
   1.160 +        cout << "}" << endl;
   1.161 +    } else {
   1.162 +        cerr << "could not open " << file << endl;
   1.163 +    }
   1.164 +    
   1.165 +   /* if (inFile) {
   1.166 +        int i = 0;
   1.167 +        while (!inFile.eof()){
   1.168 +            readUntil("Profil von: ");
   1.169 +            for (int ii = 0; (ii < l) && !inFile.eof(); ii++) {
   1.170 +                char cChar = inFile.get();
   1.171 +                if ((cChar == '\n') || (cChar == ' ')) {
   1.172 +                    names[i][ii] = '\0';
   1.173 +                    break;
   1.174 +                }
   1.175 +                names[i][ii] = cChar;
   1.176 +            }
   1.177 +    //        cout <<"#"<< names[i] <<"#"<< endl;
   1.178 +            i++;
   1.179 +        }
   1.180 +        inFile.close();        
   1.181 +    }
   1.182 +    
   1.183 +    inFile.open(file);
   1.184 +    if (inFile) {
   1.185 +        int i = 0;
   1.186 +        while (!inFile.eof()){
   1.187 +            readUntil("Profil von: ");
   1.188 +            char name [l];
   1.189 +            for (int ii = 0; (ii < l) && !inFile.eof(); ii++) {
   1.190 +                char cChar = inFile.get();
   1.191 +                if ((cChar == '\n') || (cChar == ' ')) {
   1.192 +                    name[ii] = '\0';
   1.193 +                    break;
   1.194 +                }
   1.195 +                name[ii] = cChar;
   1.196 +            }
   1.197 +            //Vertex suchen
   1.198 +            int index = getIndex(name);
   1.199 +            if (index > -1) {
   1.200 +            
   1.201 +            readUntil("Buddyliste:");
   1.202 +            
   1.203 +           //     cout <<"#buddylist von:#"<< names[index] <<"#"<< endl;
   1.204 +                  
   1.205 +                while (readUntil("Nachricht schreiben ", "Bilder")) {
   1.206 +                    char buddy [l];
   1.207 +                    //cout << endl;
   1.208 +                    for (int ii = 0; (ii < l) && !inFile.eof(); ii++) {
   1.209 +                        char cChar = inFile.get();
   1.210 +                        if ((cChar == '\n') || (cChar == ' ')) {
   1.211 +                            buddy[ii] = '\0';
   1.212 +                            break;
   1.213 +                        }
   1.214 +                        buddy[ii] = cChar;
   1.215 +                    }
   1.216 +                    //Vertex suchen
   1.217 +                    int indexB = getIndex(buddy);
   1.218 +                    if (indexB > -1) {
   1.219 +                        cout << "  \"" << names[index] << "\" -> \"" << names[indexB]
   1.220 +                                << "\";" << endl;
   1.221 +                    }
   1.222 +                    
   1.223 +                    //cout <<"#buddy#"<< buddy <<"#"<< endl;
   1.224 +                }
   1.225 +            }
   1.226 +            i++;
   1.227 +        }
   1.228 +        inFile.close();        
   1.229 +    } 
   1.230 +    cout << "}" << endl;*/
   1.231 +}
   1.232 +
   1.233 +
   1.234 +int main(int argc, char **argv){
   1.235 +    if (argc == 2) {
   1.236 +        if (strcmp(argv[1], "--version") == 0) {
   1.237 +          cout << "Buddylistgrapher (C++)" << endl;
   1.238 +          cout << "======================" << endl;
   1.239 +          cout << "version 2.0" << endl;
   1.240 +          cout << endl;
   1.241 +          cout << "by Julian Forster (http://progmaschine.de.vu)" << endl;
   1.242 +          cout << "and a little bit by Markus Schnalke (http://prog.marmaro.de)" << endl;
   1.243 +        } else if (strcmp(argv[1], "--help") == 0) {
   1.244 +          cout << "Buddylistgrapher" << endl;
   1.245 +          cout << "================" << endl;
   1.246 +          cout << endl;
   1.247 +          cout << "usage: " << argv[0] << " <input.txt>" << endl;
   1.248 +          cout << endl;
   1.249 +          cout << "The grapher generates output to stdout." << endl;
   1.250 +          cout << "This output is the input for the graphviz tools." << endl;
   1.251 +          cout << endl;
   1.252 +          cout << "You can use it like this:" << endl;
   1.253 +          cout << argv[0] << " input.txt | dot -Tpng > pic.png" << endl;
   1.254 +          cout << endl;
   1.255 +          cout << "ToDo:" << endl;
   1.256 +          cout << "The program was written for one specific kind of input data," << endl;
   1.257 +          cout << "so there is still some work to do to use it for general input." << endl;
   1.258 +        } else {
   1.259 +          readGraph(argv[1]); 
   1.260 +        }
   1.261 +        return 0;
   1.262 +    }
   1.263 +    cerr << "usage: " << argv[0] << " <input.txt>" << endl;
   1.264 +    return 1;
   1.265 +}
   1.266 +