forked from 852866031/C-code-evaluator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
173 lines (157 loc) · 4.85 KB
/
parser.c
File metadata and controls
173 lines (157 loc) · 4.85 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
// Programmer : Jiaxuan Chen
// Created : Jiaxuan Chen at 5/11/2021
// Purpose:
// Parse the command line input
//
// Modifications:
// Initial Date Short Description
// <none>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<dirent.h>
#include"main.h"
#include"parser.h"
char *valid_modes[MODENUM]={"-help", "-ssf", "-nodoc", "-notest", "-novars"};
int is_mode_valid(char *mode){
// ---------------------------------------------------
// Created : 5/11/2021
// Purpose:
// check if a mode that user entered is valid
//
// Parameters : the mode string
// Returns : if the mode is valid, return the index in valid_modes list. if not valid, return -1
// Side-effects : NULL
for(int j=0; j<MODENUM; j++){
if(strcmp(mode, valid_modes[j])==0){
return j;
}
}
return -1;
}
int mode_parser(int* modes, char **arguments, int argc){
// ---------------------------------------------------
// Created : 5/11/2021
// Purpose:
// from the command line arguments, parse all the modes and check validation
//
// Parameters : 1. a list that will be filled with 1 or 0 to show if the mode in valid_mode list is on. 2.command line arguments
// Returns : 1 for normal
// Side-effects : mode should not be duplicated
int index;
for(int i=0; i<MODENUM; i++) modes[i]=0;
for(int i=1; i<argc; i++){
if(arguments[i][0]=='-'){
index=is_mode_valid(arguments[i]);
if(index>=0){
if(modes[index]==1){
printf("Error: mode %s duplicated\n", valid_modes[index]);
error_dealer(-1);
}
else modes[index]=1;
}
else{
printf("Error: command %s is not valid\n", arguments[i]);
error_dealer(-1);
}
}
else break;
}
return 1;
}
int is_filename_valid(char *filename){
// ---------------------------------------------------
// Created : 5/11/2021
// Purpose:
// check if the user entered filename really exists
//
// Parameters : given filename
// Returns : 1 for exist, 0 for not
// Side-effects : NULL
FILE *f = fopen(filename, "r");
if(f==NULL) return 0;
fclose(f);
return 1;
}
char **all_c_files_list(int *file_num){
// ---------------------------------------------------
// Created : 5/11/2021
// Purpose:
// get all c files in the current directory
//
// Parameters : the address of the number of files that will be rated
// Returns : the filenames of all c files, if no file exists, return NULL
// Side-effects : NULL
DIR *dr;
int num=0;
int length;
struct dirent *en;
dr = opendir(".");
while ((en = readdir(dr)) != NULL) {
length = strlen(en->d_name);
if(length>=2 && en->d_name[length-1]=='c' && en->d_name[length-2]=='.') num++;
}
if(num==0) {
printf("Error: no c-extension file exists\n");
return NULL;
}
closedir(dr);
char **filenames=malloc(num*sizeof(char*));
num=0;
dr = opendir(".");
while ((en = readdir(dr)) != NULL) {
length = strlen(en->d_name);
if(length>=2 && en->d_name[length-1]=='c' && en->d_name[length-2]=='.'){
filenames[num]=strdup(en->d_name);
num++;
}
}
closedir(dr);
*file_num=num;
return filenames;
}
char **filenames_parser(int *file_num, char **arguments, int argc, int index){
// ---------------------------------------------------
// Created : 5/11/2021
// Purpose:
// get all the filename that user want to check and check whether the names are valid
//
// Parameters : the address of the number of files that will be rated, command line arguments and the index where the filename starts
// Returns : the filenames wanted
// Side-effects : NULL
if(argc==index){
printf("Error: filenames missing\n");
error_dealer(-1);
}
for(int i=0; i<argc-index; i++){
if(strcmp(arguments[index+i], "*.c")==0){
return all_c_files_list(file_num);
}
}
int num=0;
char **filenames=malloc((argc-index)*sizeof(char*));
for(int i=0; i<argc-index; i++){
if(is_filename_valid(arguments[index+i])){
filenames[i]=strdup(arguments[index+i]);
num++;
}
else{
printf("Error: file %s does not exist\n", arguments[index+i]);
error_dealer(-1);
}
}
*file_num=num;
return filenames;
}
int help_command(){
// ---------------------------------------------------
// Created : 5/11/2021
// Purpose:
// the help command
//
// Parameters : NULL
// Returns : 1 for normal
// Side-effects : NULL
printf("\nThis is the help command, put description later here\n\n");
return 1;
}