aboutsummaryrefslogtreecommitdiff
path: root/Converter
diff options
context:
space:
mode:
authordx <aryadevchavali1@gmail.com>2020-05-22 19:13:15 +0100
committerdx <aryadevchavali1@gmail.com>2020-05-22 20:02:18 +0100
commit99cf6e8eca2e3d29874b8b80e8c4312cd1bc87ba (patch)
treed19f8f6177b0be1938f4f9c9744eda39f43e2072 /Converter
parent1a2d0ffdbfb69a2858fb45facfb18468106edd9a (diff)
downloadmdhtml-99cf6e8eca2e3d29874b8b80e8c4312cd1bc87ba.tar.gz
mdhtml-99cf6e8eca2e3d29874b8b80e8c4312cd1bc87ba.tar.bz2
mdhtml-99cf6e8eca2e3d29874b8b80e8c4312cd1bc87ba.zip
~compile_lines -> compile_file, shuffle
I've redid the compiler to use a FILE object pointer, which means that we can leave error checking for FILES on the main program. Furthermore, I've removed the use of the array_t type with a two pass system. One pass is done for the number of lines (along with potentially other diagnostics), another is done for the actual compilation. This means at runtime we have an understanding about the size of the file and can optimize for it. Also, we have a single memory block to assign to, where we know the size of the array.
Diffstat (limited to 'Converter')
-rw-r--r--Converter/includes/compiler.h7
-rw-r--r--Converter/src/compiler.c51
2 files changed, 39 insertions, 19 deletions
diff --git a/Converter/includes/compiler.h b/Converter/includes/compiler.h
index 8d84b71..4540164 100644
--- a/Converter/includes/compiler.h
+++ b/Converter/includes/compiler.h
@@ -2,6 +2,7 @@
#define __COMPILER_H_
#include <stdlib.h>
+#include <stdio.h>
#include <stdbool.h>
typedef char *string;
@@ -15,7 +16,9 @@ int find_string(string src, string query, size_t sz_src, size_t sz_query);
/* Check if a given character is a text token*/
bool is_token(char c);
-/* Given a file, get the lines */
-char **get_lines(char *filename);
+/* Given a file, get number of lines */
+int nlines(FILE *fp);
+/* Given a file, compile the lines */
+string *compile_file(FILE *fp, string filename, int nlines);
#endif // __COMPILER_H_
diff --git a/Converter/src/compiler.c b/Converter/src/compiler.c
index ddbe6ff..04e97e7 100644
--- a/Converter/src/compiler.c
+++ b/Converter/src/compiler.c
@@ -1,6 +1,5 @@
#include "../includes/compiler.h"
#include "../includes/array.h"
-#include <stdio.h>
#include <string.h>
int find_string(string src, string query, size_t sz_src, size_t sz_query)
@@ -115,7 +114,7 @@ void compile_inner_text(string dest, string src, size_t sz_src,
else
{
// error, no matching asterisk
- fprintf(stderr, "No corresponding %s for %s:%d\n", md_tag,
+ fprintf(stderr, "No corresponding %s for %s:%lu\n", md_tag,
line_or_source, i + md_tag_size);
exit(0);
}
@@ -191,31 +190,49 @@ string compile_line(string line, size_t size_of_line, string line_or_source)
return compiled_line;
}
-char **get_lines(char *filename)
+int nlines(FILE *fp)
{
- FILE *fp;
- char **lines, *line;
- array_t *arr;
- int i;
+ fseek(fp, 0, SEEK_SET);
+ int line_number = 0;
+ char ch;
- array_init(arr);
+ while (!feof(fp))
+ {
+ if (ch == '\n')
+ ++line_number;
+ ch = fgetc(fp);
+ }
+ return line_number;
+}
+
+string *compile_file(FILE *fp, string filename, int nlines)
+{
+ fseek(fp, 0, SEEK_SET);
+ string current_header, compiled_line, line, *lines;
+ int i, line_num;
+ size_t sz_filename;
+
+ current_header = malloc(sizeof(*current_header) * sz_filename);
+ strcpy(current_header, filename);
- fp = fopen(filename, "r");
+ sz_filename = strlen(filename);
+ lines = malloc(sizeof(*lines) * nlines);
- while(!feof(fp))
+ for (line_num = 1; (line_num - 1) < nlines; ++line_num)
{
- // Get line
line = malloc(sizeof(*line) * 1024);
fgets(line, 1024, fp);
- // Terminate line
- for (i = 0; line[i] != '\n'; ++i);
+ // Null terminate line
+ for (i = 0; line[i] != '\n' && line[i] != '\0'; ++i)
+ ;
line[i] = '\0';
- array_append(arr, line);
- }
+ sprintf(current_header + sz_filename, ":%d", line_num);
- lines = (char**)arr->data;
- free(arr);
+ // Compile and append line
+ compiled_line = compile_line(line, strlen(line), current_header);
+ lines[line_num - 1] = compiled_line;
+ }
return lines;
}