-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainReader.c
More file actions
319 lines (281 loc) · 10.2 KB
/
MainReader.c
File metadata and controls
319 lines (281 loc) · 10.2 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
************************************************************
* COMPILERS COURSE - Algonquin College
* Code version: Summer, 2023
* Author: TO_DO
* Professors: Paulo Sousa
************************************************************
###################################################
# #
# ALGONQUIN @@@@@@@ COLLEGE #
# @@-----------@@ #
# @@@@|I R O N C L A D|@@@@ #
# @@@@@@@@-----------@@@@@@@@ #
# @@@@@@@@@@@@@ @@@@@@@ @@@@@@@ #
# @@@@@@@@@@@@@ @@@ @@@@@@ #
# @@@@@@@ @@@@@ @@@@ @@@@@@@@ #
# @@@@@@@ @@@@@ @@@@@@@ @@@@@@@@@@ #
# @@@@@@@ @@@@@ @@@@@ @@@@@@ @@@@@@ #
# @@@@@@@@@@ @@ @@@@ @@@@@@ #
# @@@@@@@@@@@@@@@ @@@@@ @@@@ @@@@ @@ @@ #
# @@@@@@@@@@@@@@@ @@@@@ @@@@@ @@@@@@@@@ @@ #
# @@@@@ @@@@ @@@ @@@ @@@ @@@@ @@@@@@@ #
# @@@@ @@@@ @@@ @@@ @@@ @@@ @@@@@@ #
# @@@@ @@@@@@@ @@@@@ @@@@@@ #
# @@@@@@@@@@@ @@@ @@@ @@@ @@@@@@@@@@ #
# @@@@@@@@@@@ @@@ @@@@@@ @@@@@ @@@@@@@@@ #
# @@@@@@@@@@@@@@@ @@@@@@ @@@@@@@@@@@@@@@ #
# @@@@@@@@@ @@@ @@@@@@@@@@@ #
# @@@@@@ @@ @@@@@@@@@ #
# @@@@@ @@@@@ @@@@@@@@@ #
# @@@@@@@@@@@@@@@@@@@@@@@@@ #
# @@@@@@@@@@@@@@@@@@@ #
# COMPILERS @@@@@@@@@@@ 2023-S #
# #
###################################################
*/
/*
************************************************************
* File name: MainReader.c
* Compiler: MS Visual Studio 2022
* Course: CST 8152 Compilers, Lab Section: [011, 012]
* Assignment: A12, A22, A32.
* Date: May 01 2023
* Professor: Paulo Sousa
* Purpose: This file is the main code for Buffer/Reader (A12)
* Function list: (...).
*************************************************************/
/*
*.............................................................................
* ADVICE 1:
* Please check the "TODO" labels to develop your activity.
*
* ADVICE 2: Preprocessor directives
* The #define _CRT_SECURE_NO_WARNINGS should be used in MS Visual Studio projects
* to suppress the warnings about using "unsafe" functions like fopen()
* and standard sting library functions defined in string.h.
* The define directive does not have any effect on other compiler projects
* (Gcc, VSCode, Codeblocks, etc.).
*.............................................................................
*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#ifndef COMPILERS_H_
#include "Compilers.h"
#endif
#ifndef READER_H_
#include "Reader.h"
#endif
/* Check for ANSI C compliancy */
#define ANSI_C 0
#if defined(__STDC__)
#undef ANSI_C
#define ANSI_C 1
#endif
/*
* TODO .......................................................................
* Basically, change all datatypes to your language definitions
* (see "Compilers.h")
*/
/*
* -------------------------------------------------------------
* Function declarations
* -------------------------------------------------------------
*/
Void bErrorPrint(String fmt , ...);
Void displayBuffer(BufferReader* ptr_Buffer);
i64 getFileSize(String fname);
i32 isNumber(const String ns);
Void startReader(String, String, char, i32, i32);
/*
************************************************************
* Main function from Buffer
* Parameters:
* argc / argv = Parameters from command prompt
* Return value:
* Success operation.
************************************************************
*/
i32 mainReader(i32 argc, String* argv) {
/* Create source input buffer */
String program = argv[0];
String input = argv[2];
char mode = MODE_FIXED;
i32 size = 0, increment = 0, wrongNumber = 0;
/* Missing file name or/and mode parameter */
if (argc <= 2) {
bErrorPrint("\nDate: %s Time: %s", __DATE__, __TIME__);
bErrorPrint("\nRuntime error at line %d in file %s\n", __LINE__, __FILE__);
bErrorPrint("%s\b\b\b\b%s%s", argv[0], ": ", "Missing parameters.");
bErrorPrint("Usage: <Option=0> <SourceFile> [<Mode>]");
exit(EXIT_FAILURE);
}
if (argc == 4) {
mode = *argv[3];
switch (mode) {
case MODE_FIXED: case MODE_ADDIT: case MODE_MULTI: break;
default:
bErrorPrint("%s%s%c%s%c%s%c%s", program, ": Wrong mode - choose: ",
MODE_FIXED, ", ", MODE_ADDIT, ", ", MODE_MULTI, ".");
exit(EXIT_FAILURE);
}
}
/* Read additional parameters, if any */
if (argc == 6) {
mode = *argv[3];
if (isNumber(argv[4]))size = (short)atoi(argv[4]); else wrongNumber = 1;
if (isNumber(argv[5]))increment = (short)atoi(argv[5]); else wrongNumber = 1;
if (wrongNumber) {
bErrorPrint("\nDate: %s Time: %s", __DATE__, __TIME__);
bErrorPrint("\nRuntime error at line %d in file %s\n", __LINE__, __FILE__);
bErrorPrint("%s\b\b\b\b%s", argv[0], ": Missing or wrong number parameters.");
bErrorPrint("Usage: <Option=0> <SourceFile> [<Mode> <Size> <Increment>]");
exit(EXIT_FAILURE);
}
}
startReader(program, input, mode, size, increment);
/* Return success */
return (EXIT_SUCCESS);
}
/*
************************************************************
* Buffer starting method
* Params:
* - Program: Name of the program
* - Input: Filename
* - Mode: Operational mode
* - Size: Buffer capacity
* - Increment: buffer increment.
************************************************************
*/
Void startReader(String program , String input, char mode, i32 size, i32 increment) {
ReaderPointer bufferp; /* pointer to Buffer structure */
FILE* fileHandler; /* input file handle */
i32 loadSize = 0; /* the size of the file loaded in the buffer */
char symbol; /* symbol read from input file */
/* Create buffer */
bufferp = readerCreate(size, (char)increment, mode);
if (bufferp == NULL) {
bErrorPrint("%s%s", program,
": Cannot allocate buffer - Use: buffer <input> <mode> <size> <increment>.");
bErrorPrint("Filename: %s %c %d %d\n", input, mode, size, increment);
exit(1);
}
/* Open source file */
if ((fileHandler = fopen(input, "r")) == NULL) {
bErrorPrint("%s%s%s", program, ": Cannot open file: ", input);
exit(1);
}
/* Load source file into input buffer */
printf("Reading file %s ....Please wait\n", input);
loadSize = readerLoad(bufferp, fileHandler);
/* If the input file has not been completely loaded, find the file size and print the last symbol loaded */
if (loadSize == READER_ERROR) {
printf("The input file %s %s\n", input, "has not been completely loaded.");
printf("Current size of buffer: %d.\n", readerGetSize(bufferp));
symbol = (char)fgetc(fileHandler);
printf("Last character read from the input file is: %c %d\n", symbol, symbol);
printf("Input file size: %ld\n", getFileSize(input));
}
/* Close source file */
fclose(fileHandler);
/* Finishes the buffer: add end of file character (EOF) to the buffer display again */
if ((loadSize != READER_ERROR) && (loadSize != 0)) {
if (!readerAddChar(bufferp, READER_TERMINATOR)) {
bErrorPrint("%s%s%s", program, ": ", "Error in compacting buffer.");
}
}
/* Prints the buffer property and content */
displayBuffer(bufferp);
/* Free the dynamic memory used by the buffer */
readerFree(bufferp);
bufferp = NULL;
}
/*
************************************************************
* Error printing function with variable number of arguments
* Params: Variable arguments, using formats from C language.
* - Internal vars use list of arguments and types from stdarg.h
* - NOTE: The format is using signature from C Language
************************************************************
*/
void bErrorPrint(String fmt, ...) {
/* Initialize variable list */
va_list ap;
va_start(ap, fmt);
(void)vfprintf(stderr, fmt, ap);
va_end(ap);
/* Move to new line */
if (strchr(fmt, '\n') == NULL)
fprintf(stderr, "\n");
}
/*
************************************************************
* Get buffer size
* Params:
* - Filename: Name of the file
************************************************************
*/
i64 getFileSize(String fname) {
FILE* input;
i64 flength;
input = fopen(fname, "r");
if (input == NULL) {
bErrorPrint("%s%s", "Cannot open file: ", fname);
return 0;
}
fseek(input, 0L, SEEK_END);
flength = ftell(input);
fclose(input);
return flength;
}
/*
************************************************************
* Tests for decimal-digit character string
* Params:
* - String to be evaluated as numeric
* Return:
* - Number value: Returns nonzero (true) if ns is a number; 0 (False) otherwise
************************************************************
*/
i32 isNumber(const String ns) {
char c; i32 i = 0;
if (ns == NULL) return 0;
while ((c = ns[i++]) == 0) {
if (!isdigit(c)) return 0;
}
return 1;
}
/*
************************************************************
* Print function
* - Params: buffer to print all properties.
************************************************************
*/
Void displayBuffer(BufferReader* ptr_Buffer) {
printf("\nPrinting buffer parameters:\n\n");
printf("The capacity of the buffer is: %d\n",
readerGetSize(ptr_Buffer));
printf("The current size of the buffer is: %d\n",
readerGetPosWrte(ptr_Buffer));
printf("The operational mode of the buffer is: %c\n",
readerGetMode(ptr_Buffer));
printf("The increment factor of the buffer is: %lu\n",
readerGetInc(ptr_Buffer));
printf("The first symbol in the buffer is: %c\n",
readerGetPosWrte(ptr_Buffer) ? *readerGetContent(ptr_Buffer, 0) : ' ');
printf("The value of the flags field is: %02hX\n",
readerGetFlags(ptr_Buffer));
printf("%s", "Reader statistics : \n");
readerPrintStat(ptr_Buffer);
printf("Number of errors: %d\n",
readerNumErrors(ptr_Buffer));
printf("\nPrinting buffer contents:\n\n");
readerRecover(ptr_Buffer);
if (!readerPrint(ptr_Buffer))
printf("Empty buffer\n");
}