This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.c
75 lines (59 loc) · 1.58 KB
/
loader.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
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
#define TOTAL_MEMORY_SIZE 5000
#define LEXEME_MEMORY_SIZE 100
/*
* Объявление переменных
*/
struct variable {
char name[LEXEME_MEMORY_SIZE];
int value;
};
char *pointer_program;
//-------------------------------------------------------------
/*
* Объявление функций
*/
int readFile(char *);
//-------------------------------------------------------------
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Wrong format.\nUse: <executable file>.exe <out variables>.txt");
exit(1);
}
char *filename_program = argv[1];
char *filename_variables = argv[2];
if (!(pointer_program = (char *) malloc(TOTAL_MEMORY_SIZE))) {
printf("Unable to allocate the memory.");
exit(1);
}
//Загрузка программы
if (!readFile(filename_program)) {
printf("Unable to read the program file.");
exit(1);
}
execute(pointer_program, filename_variables);
return 0;
}
int readFile(char *file_name) {
FILE *file;
if (!(file = fopen(file_name, "r")))
return 0;
char *point = pointer_program;
int i = 0, k = 1;
do {
*point = (char) getc(file);
point++;
i++;
if (i == k*TOTAL_MEMORY_SIZE){
k++;
pointer_program = (char*) realloc(pointer_program, (size_t) (k * TOTAL_MEMORY_SIZE));
point = pointer_program;
point += i;
}
} while (!feof(file));
*(point - 1) = 0;
fclose(file);
return 1;
}