-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.c
203 lines (187 loc) · 6.05 KB
/
parser.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
192
193
194
195
196
197
198
199
200
201
202
203
// 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(){
// ---------------------------------------------------
// Programmer: Yuyan Chen
// Created : 5/11/2021
// Purpose:
// the help command
//
// Parameters : NULL
// Returns : 1 for normal
// Side-effects : NULL
printf("\nC code evaluator version 1.0\n"
"NAME\n"
"\t ceval -- evaluate the professional quality of the source code\n"
"SYNOPSIS\n"
"\t ceval [-help | -ssf | -nodoc | -notest | -novars] [file ...]\n"
"DESCRIPTION\n"
"\t This is a C application that evaluates the professional quality of the source code in the following areas:\n "
"\t\t Modular programming\n"
"\t\t Code indentation\n"
"\t\t Commenting\n"
"\t\t Documentation\n"
"\t\t Poor variable names\n"
"\t\t Built-in test cases\n"
"\t The options are as follows:\n"
"\t\t -ssf Single Source File\n"
"\t\t -nodoc No Documentation in the file\n"
"\t\t -notest No Test cases\n"
"\t\t -novars No Variables testing\n"
"USAGE\n"
"\t ceval -ssf filename1.c filename2.c ... filename.c\n"
"\t ceval -ssf *.c\n"
"\t ceval -nodoc filename1.c filename2.c ... filenameN.c\n"
"\t ceval -nodoc *.c\n"
"\t ceval -notest filename1.c filename2.c ... filenameN.c\n"
"\t ceval -notest *.c\n"
"\t ceval -novars filename1.c filename2.c ... filenameN.c\n"
"\t ceval -novars *.c\n"
"\t ceval -ssf -nodoc -notest -novars filename1.c filename2.c ... filenameN.c\n"
"\t ceval -ssf -nodoc -notest -novars *.c\n"""
);
return 1;
}