-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs_Parsing.c
174 lines (144 loc) · 4.62 KB
/
cs_Parsing.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <string.h>
#include <stdlib.h>
#include "cs_Parsing.h"
//const size_t CS_STRING_CACHE_SIZE = 2048;
/* internalized strings for initializator and terminator edges */
char* initString = "(";
char* termString = ")";
char getTerminatorSymbol() { return ')'; }
char getInitialisatorSymbol() { return '('; }
/**
Returns a VertexList object that represents the end of a canonical String.
Currently the ) sign is used for that.
*/
struct VertexList* getTerminatorEdge(struct ListPool *p) {
struct VertexList* e = getVertexList(p);
e->label = termString;
return e;
}
/**
Returns a VertexList object that represents the beginning of a canonical String.
Currently the ( sign is used for that.
*/
struct VertexList* getInitialisatorEdge(struct ListPool *p) {
struct VertexList* e = getVertexList(p);
e->label = initString;
return e;
}
/*****************************************************************************************
************************************** Output *****************************************
*****************************************************************************************/
/**
* Conversion method to convert a string stored as a labeled path to a char array string
*/
char* canonicalStringToChar(struct ShallowGraph* string) {
char* out;
struct VertexList* e;
int size = 0;
int i;
/* count the space needed for the string (+1 for a whitespace separating two labels) */
for (e=string->edges; e; e=e->next) {
size += strlen(e->label) + 1;
}
out = malloc((size + 1) * sizeof(char));
for (e=string->edges, i=0; e; e=e->next) {
sprintf(&(out[i]), "%s ", e->label);
i += strlen(e->label) + 1;
}
return out;
}
/**
Print the canonical string, represented by the ShallowGraph s to
the screen.
*/
void printCanonicalString(struct ShallowGraph* s, FILE* stream) {
struct VertexList *i;
for (i=s->edges; i; i = i->next) {
char* j;
for (j=i->label; *j != '\0'; ++j) {
fputc(*j, stream);
}
fputc(' ', stream);
}
fputc('\n', stream);
}
/**
Print the canonical strings represented by the list of
ShallowGraphs s
*/
void printCanonicalStrings(struct ShallowGraph *s, FILE* stream) {
struct ShallowGraph* i;
for (i=s; i; i=i->next) {
printCanonicalString(i, stream);
}
}
/*****************************************************************************************
************************************** Input ******************************************
*****************************************************************************************/
/**
We are not done reading a canonical string from stream, if there is a non-space
character before the next line break or the end of file (whichever comes first)
*/
char __notDone(FILE* stream) {
int c;
for (c = fgetc(stream); c == ' '; c = fgetc(stream));
ungetc(c, stream);
return (c != '\0') && (c != '\n');
}
/**
Write chars in stream to buffer until a space is encountered.
buffer needs to be large enough to hold the number of chars
written. __readLabel consumes the space, but replaces its occurrence in
buffer with '\0'. this is actually quite good for the use case in parseCString,
as there, all labels are terminated by a space due to the printCanonicalString()
implementation.
*/
void __readLabel(FILE* stream, char* buffer) {
int i = 0;
for (buffer[i] = fgetc(stream); buffer[i] != ' '; buffer[++i] = fgetc(stream));
buffer[i] = '\0';
}
/**
parses a canonical string from stream that was written using printCanonicalString().
*/
struct ShallowGraph* parseCString(FILE* stream, char* buffer, struct ShallowGraphPool* sgp) {
struct ShallowGraph* string = getShallowGraph(sgp);
struct VertexList* e;
while (__notDone(stream)) {
__readLabel(stream, buffer);
e = getVertexList(sgp->listPool);
if (strcmp(buffer, termString) == 0) {
e->label = termString;
} else {
if (strcmp(buffer, initString) == 0) {
e->label = initString;
} else {
int length = strlen(buffer) + 1;
char* label = malloc(length * sizeof(char));
strcpy(label, buffer);
e->label = label;
e->isStringMaster = 1;
}
}
appendEdge(string, e);
}
return string;
}
static FILE* str2stream(char* str) {
// extremely dangerous and extremely nasty
FILE* f = fopen("/tmp/nasty", "w");
fprintf(f, "%s", str);
fclose(f);
return fopen("/tmp/nasty", "r");
}
/**
* create a canonical string struct given its representation as a char array string
* Not thread safe, of course. Also, extremely inefficient, I guess. Should only be used for debugging.
*/
struct ShallowGraph* string2cstring(char* str, struct ShallowGraphPool* sgp) {
FILE* f = str2stream(str);
char x[20];
struct ShallowGraph* cstr = parseCString(f, x, sgp);
fclose(f);
return cstr;
}