-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.c
192 lines (174 loc) · 6.18 KB
/
database.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
// clear all string char by char
void clearString(char *text, int row, int column){
for (int i = 0; i < row; i++) {
*(text+i*column) = '\0';
for (int j = 0; j < column; j++) {
*(text+i*column+j) = '\0';
}
}
}
int mySplit(char *input, char **words){
char *text;
text = strtok(input," =\n");
int i = 0, length = 0;
while(text != NULL){
words[i] = text;
text = strtok(NULL," =\n");
i++; length++;
}
return length;
}
void fileOperations(char **queries, char *filePath, int length, char *allText){
printf("\n-----%s-----\n",filePath);
FILE *file;
file = fopen(filePath,"r+");
char concatenedText[3600] = {'\0'};
char line[15] = {'\0'};
char names[10] = {"\0"};
char number[3] = {"\0"};
char newLine = '\n';
// Query controls
if(strcmp(queries[1],"*") == 0){
if(length > 4){
if(strcmp(queries[4],"where") == 0){
if(strcmp(queries[5],"ad") == 0){
while(!feof(file)){
fgets(line,15,file);
sscanf(line,"%s %s",names,number);
if(strcmp(names,queries[6]) == 0){
printf("%s\n",line);
strcat(line,&newLine);
strcat(concatenedText,line);
}
}
} else if (strcmp(queries[5],"number") == 0){
while(!feof(file)){
fgets(line,15,file);
sscanf(line,"%s %s",names,number);
if(strcmp(number,queries[6]) == 0){
printf("%s\n",line);
strcat(line,&newLine);
strcat(concatenedText,line);
}
}
}
}
}else{
while(!feof(file)){
fgets(line,15,file);
printf("%s\n",line);
strcat(line,&newLine);
strcat(concatenedText,line);
}
}
}else if(strcmp(queries[1],"ad") == 0){
if(length > 4){
if(strcmp(queries[4],"where") == 0){
if(strcmp(queries[5],"ad") == 0){
while(!feof(file)){
fgets(line,15,file);
sscanf(line,"%s %s",names,number);
if(strcmp(names,queries[6]) == 0){
printf("%s\n",names);
strcat(names,&newLine);
strcat(concatenedText,names);
}
}
} else if (strcmp(queries[5],"number") == 0){
while(!feof(file)){
fgets(line,15,file);
sscanf(line,"%s %s",names,number);
if(strcmp(number,queries[6])==0){
printf("%s\n",names);
strcat(names,&newLine);
strcat(concatenedText,names);
}
}
}
}
}else{
while(!feof(file)){
fgets(line,15,file);
sscanf(line,"%s %s",names,number);
printf("%s\n",names);
strcat(names,&newLine);
strcat(concatenedText,names);
}
}
}else if(strcmp(queries[1],"number") == 0){
if(length > 4){
if(strcmp(queries[4],"where") == 0){
if(strcmp(queries[5],"ad") == 0){
while(!feof(file)){
fgets(line,15,file);
sscanf(line,"%s %s",names,number);
if(strcmp(names,queries[6]) == 0){
printf("%s\n",number);
strcat(concatenedText,number); // add line to end of contanetedText
}
}
} else if (strcmp(queries[5],"number") == 0){
while(!feof(file)){
fgets(line,15,file);
sscanf(line,"%s %s",names,number);
if(strcmp(number,queries[6]) == 0){
printf("%s\n",number);
strcat(number, &newLine);
strcat(concatenedText,number); // add line to end of contanetedText
}
}
}
}
}else{
while(!feof(file)){
fgets(line,15,file);
sscanf(line,"%s %s",names,number);
printf("%s\n",number);
strcat(concatenedText,number); // add line to end of contanetedText
}
}
}
// copy concatenedText to allText to send data with pipe
strcpy(allText,concatenedText);
fclose(file);
}
int main() {
int pipe;
char * myfifo = "/tmp/myfifo";
mkfifo(myfifo, 0666);
int length;
char enteredQuery[100] = {"\0"}, *queryWords[100];
// file paths
char *path01 = "veri1.txt";
char *path02 = "veri2.txt";
char concatanedText[3600] = {"\0"};
while (1) {
pipe = open(myfifo,O_RDONLY);
read(pipe, enteredQuery, 100);
// holding query length
length = mySplit(enteredQuery,queryWords);
// if 4th word "veri1.txt"
if (strncmp(path01,queryWords[3],9) == 0){
fileOperations(queryWords,path01,length,concatanedText);
}else if (strncmp(path02,queryWords[3],9) == 0){
fileOperations(queryWords,path02,length,concatanedText);
}
close(pipe);
// send results to program.c with pipe
pipe = open(myfifo,O_WRONLY);
write(pipe,concatanedText,strlen(concatanedText));
close(pipe);
// cleaning data after every launch
for (int i = 0; i < 3600; i++) {
concatanedText[i] = '\0';
}
}
return 0;
}