-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.c
68 lines (62 loc) · 1.6 KB
/
utilities.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
#include <stdio.h>
#include <stdlib.h>
#include "headers/executor.h"
#include "headers/variables.h"
#include "headers/loader.h"
void printError(char *errorText);
int countFileChars(char *fileName) {
int lenght = 0;
FILE *inputFile = fopen(fileName, "r");
if (!inputFile) {
printError("Unable to read the program file.");
}
int ch = fgetc(inputFile);
while (ch != EOF) {
++lenght;
ch = fgetc(inputFile);
}
fclose(inputFile);
return lenght;
}
void printError(char *errorText) {
printf("Error!\n");
printf(errorText);
exit(1);
}
void writeResult(char *fileName, struct Program *program) {
FILE *inFile, *outFile;
inFile = fopen(fileName, "r");
outFile = fopen(program->fileNameResult, "w");
if (!inFile) {
printError("Unable to open file with out variables.");
}
if (!outFile) {
printError("Unable to open file with results.");
}
char name[SIZE_LEXEM];
char *t = name;
do {
*t = (char) fgetc(inFile);
while (*t != '\n' && !feof(inFile)) {
t++;
*t = (char) fgetc(inFile);
}
*t='\0';
struct Variable *currVariable = findVariable(name, program);
if (currVariable == NULL) {
printError("Variable not init");
}
fprintf(outFile, "%s = %d\n", currVariable->name, currVariable->value);
t = name;
} while (!feof(inFile));
fclose(inFile);
fclose(outFile);
}
int lenght(char *name) {
int counter = 0;
char *temp = name;
for (; *temp; temp++) {
counter++;
}
return counter;
}