annotate graph.cpp @ 7:ccd2ab1bc9fd

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