buddylistgrapher

view graph.cpp @ 3:2064a5a65b7e

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