buddylistgrapher

view graph2.cpp @ 15:449b0fc9f764

added cpp version 2
author ju
date Thu, 05 Jul 2007 18:08:31 +0200
parents graph.cpp@6c327ae23d2c
children
line source
2 /*************************
3 * GraphList2dot Converter
4 *
5 * Autor: Julian Forster
6 * www.ProgMaschine.de.vu
7 * Autor: Markus Schnalke
8 * http://prog.marmaro.de
9 *
10 *************************/
12 #include <iostream>
13 #include <fstream>
15 using namespace std;
17 #define n 1000 //Size of Namelist
18 #define l 20 //MaxNameLength
20 //Inputfile
21 ifstream inFile;
23 //Namelist
24 char names [n][l];
26 void readUntil(char *untilText) {
27 //cout << "readUntil" << endl;
28 int i = 0;
29 while (!inFile.eof()){
30 char cChar = inFile.get();
31 //cout << cChar;
32 if (cChar == untilText[i]){
33 i++;
34 if (i >= strlen(untilText)) return;
35 } else {
36 i = 0;
37 }
38 // cout << i;
39 }
40 //cout << "endReadUntil" << endl;
41 }
43 //Liest bis zum untilText, aber nicht weiter als bis zum lastText
44 //wenn lastText vor untilText kommt wird bis dahin gelesen
45 //und false zurückgegeben
46 bool readUntil(char *untilText, char *lastText) {
47 //cout << "readUntil" << endl;
48 int i = 0;
49 int ii = 0;
50 while (!inFile.eof()){
51 char cChar = inFile.get();
52 //cout << cChar;
53 if (cChar == untilText[i]){
54 i++;
55 if (i >= strlen(untilText)) return true;
56 } else {
57 i = 0;
58 }
59 if (cChar == lastText[ii]){
60 ii++;
61 if (ii >= strlen(lastText)) return false;
62 } else {
63 ii = 0;
64 }
65 // cout << i;
66 }
67 return false;
68 //cout << "endReadUntil" << endl;
69 }
70 int getIndex(char * name) {
71 for (int i = 0; i < n; i++) {
72 for (int ii = 0; ii < l; ii++) {
73 if (names[i][ii] != name[ii]) {
74 break;
75 }
76 if (names[i][ii] == '\0') return i;
77 }
78 }
79 return -1;
80 }
82 void readGraph(char *file) {
83 if (inFile.open(file)) {
84 //header
85 cout << "digraph G {" << endl;
86 cout << "size=\"25,22\";" << endl;
88 int i = -1;
89 int mode = 0; //0: start, 1: read headbuddy, 2: no whitespace 3: read buddy
90 while (!inFile.eof()) {
91 //read one line
92 int ii = 0;
93 char line [10 + l];
94 while (!inFile.eof()) {
95 char c = inFile.get();
96 if (c == '\n') {
97 line[ii] = '\0';
98 break;
99 } else {
100 line[ii] = c;
101 ii++;
102 }
103 }
104 cout << line << endl;
105 }
106 /*
107 if (!line[0] == '\0') {
108 if (line[0] == ' ') {
110 int begin = 0;
111 char buddy[l];
112 for (begin = 1; begin < 10 + l; begin++) {
113 if (line[begin] != ' ') {
114 break;
115 }
116 }
117 if (line[begin] != '\0') {
118 for (int j = begin; j < 10 + l; j++) {
119 if ((line[j] == ' ') || (line[j] == '\0')) {
120 buddy[j-begin] = '\0';
121 break;
122 } else {
123 if (j >= l - 1) {
124 cerr << "name too long" << line << endl;
125 return;
126 }
127 buddy[j-begin] = line[j];
128 }
129 }
130 //Add Buddy
133 }
134 } else {
135 i++;
136 for (int j = 0; j < 10 + l; j++) {
137 if ((line[j] == ' ') || (line[j] == '\0')) {
138 names[i][j] = '\0';
139 break;
140 } else {
141 if (j >= l - 1) {
142 cerr << "name too long" << line << endl;
143 return;
144 }
145 names[i][j] = line[j];
146 }
147 }
149 }
151 }
154 }*/
156 //footer
157 cout << "}" << endl;
158 } else {
159 cerr << "could not open " << file << endl;
160 }
162 /* if (inFile) {
163 int i = 0;
164 while (!inFile.eof()){
165 readUntil("Profil von: ");
166 for (int ii = 0; (ii < l) && !inFile.eof(); ii++) {
167 char cChar = inFile.get();
168 if ((cChar == '\n') || (cChar == ' ')) {
169 names[i][ii] = '\0';
170 break;
171 }
172 names[i][ii] = cChar;
173 }
174 // cout <<"#"<< names[i] <<"#"<< endl;
175 i++;
176 }
177 inFile.close();
178 }
180 inFile.open(file);
181 if (inFile) {
182 int i = 0;
183 while (!inFile.eof()){
184 readUntil("Profil von: ");
185 char name [l];
186 for (int ii = 0; (ii < l) && !inFile.eof(); ii++) {
187 char cChar = inFile.get();
188 if ((cChar == '\n') || (cChar == ' ')) {
189 name[ii] = '\0';
190 break;
191 }
192 name[ii] = cChar;
193 }
194 //Vertex suchen
195 int index = getIndex(name);
196 if (index > -1) {
198 readUntil("Buddyliste:");
200 // cout <<"#buddylist von:#"<< names[index] <<"#"<< endl;
202 while (readUntil("Nachricht schreiben ", "Bilder")) {
203 char buddy [l];
204 //cout << endl;
205 for (int ii = 0; (ii < l) && !inFile.eof(); ii++) {
206 char cChar = inFile.get();
207 if ((cChar == '\n') || (cChar == ' ')) {
208 buddy[ii] = '\0';
209 break;
210 }
211 buddy[ii] = cChar;
212 }
213 //Vertex suchen
214 int indexB = getIndex(buddy);
215 if (indexB > -1) {
216 cout << " \"" << names[index] << "\" -> \"" << names[indexB]
217 << "\";" << endl;
218 }
220 //cout <<"#buddy#"<< buddy <<"#"<< endl;
221 }
222 }
223 i++;
224 }
225 inFile.close();
226 }
227 cout << "}" << endl;*/
228 }
231 int main(int argc, char **argv){
232 if (argc == 2) {
233 if (strcmp(argv[1], "--version") == 0) {
234 cout << "Buddylistgrapher (C++)" << endl;
235 cout << "======================" << endl;
236 cout << "version 2.0" << endl;
237 cout << endl;
238 cout << "by Julian Forster (http://progmaschine.de.vu)" << endl;
239 cout << "and a little bit by Markus Schnalke (http://prog.marmaro.de)" << endl;
240 } else if (strcmp(argv[1], "--help") == 0) {
241 cout << "Buddylistgrapher" << endl;
242 cout << "================" << endl;
243 cout << endl;
244 cout << "usage: " << argv[0] << " <input.txt>" << endl;
245 cout << endl;
246 cout << "The grapher generates output to stdout." << endl;
247 cout << "This output is the input for the graphviz tools." << endl;
248 cout << endl;
249 cout << "You can use it like this:" << endl;
250 cout << argv[0] << " input.txt | dot -Tpng > pic.png" << endl;
251 cout << endl;
252 cout << "ToDo:" << endl;
253 cout << "The program was written for one specific kind of input data," << endl;
254 cout << "so there is still some work to do to use it for general input." << endl;
255 } else {
256 readGraph(argv[1]);
257 }
258 return 0;
259 }
260 cerr << "usage: " << argv[0] << " <input.txt>" << endl;
261 return 1;
262 }