-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.c
More file actions
206 lines (174 loc) · 6.09 KB
/
code.c
File metadata and controls
206 lines (174 loc) · 6.09 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
//#define FILE_NAME "C:\\Users\\RAZWANUL\\Documents\\Coding_World\\c_programing\\PROJECT\\students.txt"
//#define FILE_NAME "C:\\Users\\RAZWANUL\\Desktop\\12305030\\students.txt"
#define FILE_NAME "students.txt"
void trim(char *str) {
char *start = str;
while (*start && (*start==' ' || *start=='\t')) start++;
memmove(str, start, strlen(start)+1);
char *end = str + strlen(str) -1;
while(end>=str && (*end==' ' || *end=='\t')) { *end=0; end--; }
}
int split(char *line, char arr[50][50]) {
int count=0;
char *token=strtok(line,",");
while(token) {
trim(token);
strcpy(arr[count++],token);
token=strtok(NULL,",");
}
return count;
}
int find_index(char arr[50][50], int n, char *target) {
for(int i=0;i<n;i++)
if(strcmp(arr[i],target)==0) return i;
return -1;
}
void selectAll(char selectFields[50][50], int selectCount, char tableFields[50][50], int totalFields) {
FILE *fp=fopen(FILE_NAME,"r");
if(!fp) { printf("No data found.\n"); return; }
char line[256];
fgets(line,sizeof(line),fp); // skip header
// Print header
for(int i=0;i<selectCount;i++) printf("%s\t", selectFields[i]);
printf("\n");
while(fgets(line,sizeof(line),fp)) {
line[strcspn(line,"\n")]=0;
char row[50][50];
int cnt=split(line,row);
if(cnt!=totalFields) continue;
for(int i=0;i<selectCount;i++){
int idx=find_index(tableFields,totalFields,selectFields[i]);
printf("%s\t", row[idx]);
}
printf("\n");
}
fclose(fp);
}
void selectWhere(char selectFields[50][50], int selectCount, char tableFields[50][50], int totalFields, char *whereField, char *whereValue){
FILE *fp=fopen(FILE_NAME,"r");
if(!fp) { printf("No data found.\n"); return; }
char line[256];
fgets(line,sizeof(line),fp); // skip header
int whereIdx=find_index(tableFields,totalFields,whereField);
if(whereIdx==-1){ printf("Error: Field '%s' not found.\n",whereField); fclose(fp); return; }
// Print header
for(int i=0;i<selectCount;i++) printf("%s\t", selectFields[i]);
printf("\n");
while(fgets(line,sizeof(line),fp)) {
line[strcspn(line,"\n")]=0;
char row[50][50];
int cnt=split(line,row);
if(cnt!=totalFields) continue;
if(strcmp(row[whereIdx],whereValue)==0){
for(int i=0;i<selectCount;i++){
int idx=find_index(tableFields,totalFields,selectFields[i]);
printf("%s\t", row[idx]);
}
printf("\n");
}
}
fclose(fp);
}
void parseQuery(char *query){
trim(query);
if(strlen(query)==0) return;
// Check exit
if(strcasecmp(query,"EXIT")==0){
printf("Exiting...\n"); exit(0);
}
// Must end with ;
if(query[strlen(query)-1]!=';'){ printf("Error: Query must end with ';'\n"); return; }
query[strlen(query)-1]=0;
char qCopy[256];
strcpy(qCopy,query);
char *wherePtr=strstr(qCopy," WHERE ");
char wherePart[100]="";
if(wherePtr){ strcpy(wherePart,wherePtr+7); *wherePtr=0; }
// Check SELECT
if(strncmp(qCopy,"SELECT ",7)!=0){ printf("Error: Query must start with SELECT\n"); return; }
char *fromPtr=strstr(qCopy+7," FROM ");
if(!fromPtr){ printf("Error: Missing FROM clause\n"); return; }
*fromPtr=0;
char selectPart[100];
strcpy(selectPart,qCopy+7);
trim(selectPart);
char tableName[50];
strcpy(tableName,fromPtr+6);
trim(tableName);
// Validate table name
if(strcmp(tableName,"students")!=0){ printf("Error: Table '%s' not found.\n",tableName); return; }
// Read header
FILE *fp=fopen(FILE_NAME,"r");
if(!fp){ printf("Error: File not found\n"); return; }
char line[256];
fgets(line,sizeof(line),fp);
line[strcspn(line,"\n")]=0;
char tableFields[50][50];
int totalFields=split(line,tableFields);
fclose(fp);
// Parse select fields
char selectFields[50][50];
int selectCount=0;
if(strcmp(selectPart,"*")==0){
selectCount=totalFields;
for(int i=0;i<totalFields;i++) strcpy(selectFields[i],tableFields[i]);
} else {
selectCount=split(selectPart,selectFields);
for(int i=0;i<selectCount;i++){
if(find_index(tableFields,totalFields,selectFields[i])==-1){
printf("Error: Field '%s' not found.\n",selectFields[i]);
return;
}
}
}
// If WHERE exists
if(strlen(wherePart)>0){
char whereField[50], whereValue[50];
char *eq=strchr(wherePart,'=');
if(!eq){ printf("Error: Invalid WHERE clause\n"); return; }
*eq=0;
strcpy(whereField,wherePart);
strcpy(whereValue,eq+1);
trim(whereField); trim(whereValue);
selectWhere(selectFields,selectCount,tableFields,totalFields,whereField,whereValue);
} else {
selectAll(selectFields,selectCount,tableFields,totalFields);
}
}
// int main(){
// char query[300];
// printf("=== SQL-Style Student Console ===\n");
// while(1){
// printf("\n> ");
// if(!fgets(query,sizeof(query),stdin)) break;
// query[strcspn(query,"\n")]=0;
// parseQuery(query);
// }
// return 0;
// }
int main(){
char query[300];
// User Manual
printf("=== SQL-Style Student Console ===\n\n");
printf("Welcome! You can run SQL-style queries on the 'students' table.\n");
printf("Examples of commands:\n");
printf("1. SELECT * FROM students;\n");
printf("2. SELECT name, age FROM students;\n");
printf("3. SELECT name, grade FROM students WHERE age=20;\n");
printf("4. EXIT; (to quit the program)\n");
printf("\nRules:\n");
printf("- All queries must end with a semicolon ';'\n");
printf("- Field names must match the table header (id, name, age, grade)\n");
printf("- WHERE clause format: field=value\n\n");
while(1){
printf("> ");
if(!fgets(query,sizeof(query),stdin)) break;
query[strcspn(query,"\n")]=0;
parseQuery(query);
}
return 0;
}