Skip to content

Commit f22f92f

Browse files
committed
Output compressor
1 parent dcf4acc commit f22f92f

File tree

10 files changed

+213
-284
lines changed

10 files changed

+213
-284
lines changed

ecr/ecr.cpp

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,96 @@
55
// Main program
66
int main(void)
77
{
8-
// Read the code file into memory as an array of lines
9-
StringArray* codes = readFile((char*)"test.code");
8+
char* filename = (char*)"test.eco"; // During development
9+
10+
FILE *f = fopen(filename, "rb");
11+
if (f == NULL) {
12+
printf("Could not read file %s\n", filename);
13+
return 1;
14+
}
15+
16+
// Get the size of the file
17+
fseek(f, 0, SEEK_END);
18+
long fsize = ftell(f);
19+
rewind(f);
20+
21+
// Read the file as a single chunk
22+
char* string = (char*)malloc(fsize + 1);
23+
if (string != NULL) {
24+
fread(string, fsize, 1, f);
25+
fclose(f);
26+
string[fsize] = 0;
27+
} else {
28+
printf("Could not allocate memory for %s\n", filename);
29+
return 1;
30+
}
31+
32+
// Split the code and key portions
33+
char* codes;
34+
char* keys;
35+
char* keywords;
36+
int codeCount = 0;
37+
int keyCount = 0;
38+
int codeSize;
39+
int keySize;
40+
int count = 0;
41+
int size = 0;
42+
43+
char* token = strtok(string, "\n");
44+
while (token != NULL) {
45+
if (strcmp(token, (char*)"-") == 0) {
46+
// When - is reached, convert to a list of codes
47+
codeCount = count;
48+
codeSize = size;
49+
codes = (char*)malloc(++codeSize);
50+
memcpy(codes, string, codeSize);
51+
// Reset the scanner for the keyword list
52+
keywords = string + codeSize + 1;
53+
count = 0;
54+
size = 0;
55+
token = strtok(keywords, "\n");
56+
} else if (strcmp(token, (char*)"--") == 0) {
57+
// When -- is reached, convert to a list of keywords
58+
keyCount = count;
59+
keySize = size;
60+
keys = (char*)malloc(++keySize);
61+
memcpy(keys, keywords, keySize);
62+
token = strtok(NULL, "\n");
63+
} else {
64+
// printf("%s\n", token);
65+
count++;
66+
size += strlen(token) +1;
67+
token = strtok(NULL, "\n");
68+
}
69+
}
70+
// Build a StringArray for each part
71+
72+
char* ptr = codes;
73+
for (int n = 0; n < codeCount; n++) {
74+
printf("%s\n", ptr);
75+
76+
ptr += strlen(ptr) + 1;
77+
}
78+
printf("-\n");
79+
ptr = keys;
80+
for (int n = 0; n < keyCount; n++) {
81+
printf("%s\n", ptr);
82+
ptr += strlen(ptr) + 1;
83+
}
84+
85+
// Convert the file to an array of lines
86+
// StringArray* lines = getLines(string, NEWLINE);
87+
88+
// Dispose of the original file
89+
free(string);
90+
91+
92+
93+
94+
95+
/*
96+
// Read the compiled script into memory as an array of lines
97+
StringArray* codes = readFile((char*)"test.eco");
1098
if (codes == NULL) {
1199
return 1;
12100
}
@@ -18,10 +106,13 @@ int main(void)
18106
return 1;
19107
}
20108
printf("Keys: %d\n", keys->size);
109+
*/
21110

22111
// debug(codes, keys);
23112

24-
int result = run(codes, keys);
113+
// int result = run(codes, keys);
114+
115+
// return result;
25116

26-
return result;
117+
return 0;
27118
}

ecr/ecr.h

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
// A struct to hold an array of string buffers and the array size
1111
struct StringArray {
12-
char** array;
1312
int size;
13+
char** array;
1414
};
1515

1616
// Replace one character with another in situ
@@ -34,7 +34,7 @@ int strpos(char* haystack, char needle) {
3434
return -1;
3535
}
3636

37-
// Convert a string to an array of lines, using a specified delimiter.
37+
// Convert a string to an array of lines, using a specified delimiter. Stop at an empty line.
3838
StringArray* getLines(char* string, char delimiter) {
3939
int length = strlen(string);
4040
int nlines = 0;
@@ -47,6 +47,9 @@ StringArray* getLines(char* string, char delimiter) {
4747
if (c == delimiter || c == '\0') {
4848
string[pos] = '\0';
4949
start = pos + 1;
50+
if (string[start] == '\0') {
51+
break;
52+
}
5053
nlines++;
5154
}
5255
pos++;
@@ -74,35 +77,5 @@ StringArray* getLines(char* string, char delimiter) {
7477
return mylist;
7578
}
7679

77-
// Read a file and convert it into an array of strings
78-
StringArray* readFile(char* filename) {
79-
FILE *f = fopen(filename, "rb");
80-
if (f == NULL) {
81-
printf("Could not read file %s\n", filename);
82-
return NULL;
83-
};
84-
85-
fseek(f, 0, SEEK_END);
86-
long fsize = ftell(f);
87-
rewind(f);
88-
89-
char* string = (char*)malloc(fsize + 1);
90-
if (string != NULL) {
91-
fread(string, fsize, 1, f);
92-
fclose(f);
93-
string[fsize] = 0;
94-
} else {
95-
printf("Could not allocate memory for %s\n", filename);
96-
return NULL;
97-
}
98-
99-
// Convert the file to an array of lines
100-
StringArray* lines = getLines(string, NEWLINE);
101-
102-
// Dispose of the original file
103-
free(string);
104-
return lines;
105-
}
106-
10780

10881
#endif

ecr/out

Lines changed: 0 additions & 185 deletions
This file was deleted.

ecr/test.code

Lines changed: 0 additions & 3 deletions
This file was deleted.

ecr/test.keys

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)