From 6961e4237f24306ab2475f4f527a7c802134c4d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20L=2E=20=C5=A0ijanec?= Date: Fri, 1 May 2020 15:18:12 +0200 Subject: added working inline command tag detector, more constants ability to change inline command tag characters, variable length and count and buffer size from define headers. TODO: line-commands (lines, starting with ?) comments (lines, starting with #) --- a.out | Bin 0 -> 12260 bytes lib/jsmin.c | 2 +- lib/mkdirp.c | 5 +-- lib/randstring.c | 21 ++++++++++ src/.jsbundle.c.swp | Bin 1024 -> 0 bytes src/fopenmkdir.c | 9 +++-- src/jsbundle.c | 7 ++-- src/tape.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++++ test/ftell-test.c | 17 +++++++++ test/neg-test.c | 12 ++++++ test/randstring-test.c | 12 ++++++ test/tape-test.bvr | 1 + test/tape-test.c | 10 +++++ tmp/output.htm | 1 + 14 files changed, 186 insertions(+), 12 deletions(-) create mode 100755 a.out create mode 100644 lib/randstring.c delete mode 100644 src/.jsbundle.c.swp create mode 100644 test/ftell-test.c create mode 100644 test/neg-test.c create mode 100644 test/randstring-test.c create mode 100644 test/tape-test.bvr create mode 100644 test/tape-test.c create mode 100644 tmp/output.htm diff --git a/a.out b/a.out new file mode 100755 index 0000000..02f64f1 Binary files /dev/null and b/a.out differ diff --git a/lib/jsmin.c b/lib/jsmin.c index 402c6c9..3cb0e9b 100644 --- a/lib/jsmin.c +++ b/lib/jsmin.c @@ -29,7 +29,7 @@ This file was modified by Anton Šijanec in 2020 in order to allow it being used as a library for minifying JS inside other programs. All I've added is the option to read from a source file and output to the minified file. */ - +#pragma once #include #include #include diff --git a/lib/mkdirp.c b/lib/mkdirp.c index 8c79cf4..a6c58e6 100644 --- a/lib/mkdirp.c +++ b/lib/mkdirp.c @@ -1,12 +1,9 @@ // borrowed from https://gist.github.com/JonathonReinhart/8c0d90191c38af2dcadb102c4e202950 - +#pragma once #include #include /* PATH_MAX */ #include /* mkdir(2) */ #include -#ifndef PATH_MAX -#define PATH_MAX 255 -#endif int mkdir_p(const char *path) { /* Adapted from http://stackoverflow.com/a/2336245/119527 */ const size_t len = strlen(path); diff --git a/lib/randstring.c b/lib/randstring.c new file mode 100644 index 0000000..2eeed8f --- /dev/null +++ b/lib/randstring.c @@ -0,0 +1,21 @@ +#pragma once +char *randstring(size_t length) { + + static char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + char *randomString = NULL; + + if (length) { + randomString = malloc(sizeof(char) * (length +1)); + + if (randomString) { + for (int n = 0;n < length;n++) { + int key = rand() % (int)(sizeof(charset) -1); + randomString[n] = charset[key]; + } + + randomString[length] = '\0'; + } + } + + return randomString; +} diff --git a/src/.jsbundle.c.swp b/src/.jsbundle.c.swp deleted file mode 100644 index c512f34..0000000 Binary files a/src/.jsbundle.c.swp and /dev/null differ diff --git a/src/fopenmkdir.c b/src/fopenmkdir.c index e4c4d68..afca4f1 100644 --- a/src/fopenmkdir.c +++ b/src/fopenmkdir.c @@ -1,13 +1,14 @@ +#pragma once #include #include #include #include - +#include FILE * fopen_mkdir(char* filename, char* mode) { FILE * file_to_return; file_to_return = fopen(filename, mode); if(file_to_return == NULL) { - char folder_of_file[256]; + char folder_of_file[PATH_MAX]; strcpy(folder_of_file, filename); char * p; p = strrchr(folder_of_file, '/'); @@ -15,8 +16,8 @@ FILE * fopen_mkdir(char* filename, char* mode) { printf("[fopenmkdir.c] filename contains no slash.\n"); return NULL; } else { - printf("folder is %s\n", folder_of_file); - printf("filename is %s\n", filename); + // printf("folder is %s\n", folder_of_file); + // printf("filename is %s\n", filename); *p = '\0'; } if(mkdir_p(folder_of_file) != 0) { diff --git a/src/jsbundle.c b/src/jsbundle.c index c585ff5..a2f9b61 100644 --- a/src/jsbundle.c +++ b/src/jsbundle.c @@ -1,20 +1,21 @@ +#pragma once #include #include #include #include #include #include - +#include int bundle_js(char* bundle_filename, char* js_source_folder_name, char* file_extension) { // char bundle_filename[64] = "../dist/js/bundle.js"; // char js_source_folder_name[64] = "../assets/js/"; // char file_extension[64] = ".js"; - char source_filename[64]; + char source_filename[NAME_MAX]; FILE * bundle_file; FILE * source_file; bundle_file = fopen_mkdir(bundle_filename, "w"); if(bundle_file == NULL) { - char bundle_folderpath[256]; + char bundle_folderpath[PATH_MAX]; strcpy(bundle_folderpath, bundle_filename); char *p = strchr(bundle_folderpath, '/'); if (!p) { diff --git a/src/tape.c b/src/tape.c index 8f09ba6..4cda521 100644 --- a/src/tape.c +++ b/src/tape.c @@ -1,6 +1,107 @@ +#pragma once + #include #include #include #include #include #include +#include +#include +#define BVR_MAX_VARIABLE_SIZE 128 +#define BVR_INITIAL_VARIABLES_COUNT 128 +#define SUCCESS 0 +#define FAILURE -1 +#define COPY_BUFFER_SIZE 2 +#define OPENING_COMMAND_TAG_LENGTH 2 +#define OPENING_COMMAND_TAG_CHAR_1 '<' +#define OPENING_COMMAND_TAG_CHAR_2 '@' +#define CLOSING_COMMAND_TAG_CHAR_1 '@' +#define CLOSING_COMMAND_TAG_CHAR_2 '>' +char bvr_variables[BVR_INITIAL_VARIABLES_COUNT][BVR_MAX_VARIABLE_SIZE]; +// char* bvr_variables[128] = malloc(BVR_INITIAL_VARIABLES_COUNT * BVR_MAX_VARIABLE_SIZE); +int bvr_variables_multiplier = 1; + +// int bvr_increase_variables() { // this can be costly https://stackoverflow.com/a/3827922 +// char* more_variables = realloc(bvr_variables, ++bvr_variables_multiplier * BVR_INITIAL_VARIABLES_COUNT * sizeof(BVR_MAX_VARIABLE_SIZE)); +// if (!more_variables) { +// printf("[tape.c] bvr_increase_variables: unable to increase variables count. Program will continue, but may run out of variable space and fail\n"); +// bvr_variables_multiplier = bvr_variables_multiplier - 1; +// return FAILURE; +// } +// return SUCCESS; +// } + + +int bvr_command_processor(FILE * page_source_file, FILE * temp_output_file, char copy_buffer[]) { + for(int i = 1; i <= OPENING_COMMAND_TAG_LENGTH; i++) { + copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] = fgetc(page_source_file); // remove opening command tag characters + } + while(copy_buffer[(ftell(page_source_file)-1)% COPY_BUFFER_SIZE] != CLOSING_COMMAND_TAG_CHAR_1 || copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] != CLOSING_COMMAND_TAG_CHAR_2) { + copy_buffer[(ftell(page_source_file))% COPY_BUFFER_SIZE] = fgetc(page_source_file); // advance file pointer to the end of command without rewriting. + fputc('a', temp_output_file); + if(copy_buffer[(ftell(page_source_file)-1)% COPY_BUFFER_SIZE] == EOF) { + fprintf(temp_output_file, "\nbVerbose syntax error: EOF reached before closing command tag --- stopped with this bvr file\n"); + fprintf(stderr, "[tape.c] bvr_command_processor: syntax error: EOF reached before @> --- stopped with this bvr file\n"); + return FAILURE; + } + } + copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] = fgetc(page_source_file); // remove closing command tag character + return SUCCESS; +} + +int bvr_compose_page(char page_source_file_path[], int this_is_a_top_level_page, char * temp_output_path) { + if(temp_output_path != NULL) { + char * temp_output_filename = randstring(16); + char temp_output_path[80] = "tmp/"; + strcat(temp_output_path, temp_output_filename); + } + char copy_buffer[COPY_BUFFER_SIZE]; + FILE * temp_output_file = fopen_mkdir(temp_output_path, "w"); + FILE * page_source_file = fopen(page_source_file_path, "r"); + + for(int i = 0; i < sizeof(copy_buffer); i++) { // da garbage vrednosti ne bodo slučajno ukazi! + copy_buffer[i] = ' '; + } + copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] = fgetc(page_source_file); + if(copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] == EOF) { + goto done_reading_write_file; + } + while (1) { + copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] = fgetc(page_source_file); + if(copy_buffer[(ftell(page_source_file)-1)% COPY_BUFFER_SIZE] == OPENING_COMMAND_TAG_CHAR_1 && copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] == OPENING_COMMAND_TAG_CHAR_2) { + if(bvr_command_processor(page_source_file, temp_output_file, copy_buffer)) { + fprintf(temp_output_file, "\nbvr_command_processor returned an error status whilst composing %s\n", page_source_file_path); + } + } else { + if(copy_buffer[ftell(page_source_file)% COPY_BUFFER_SIZE] == EOF) { + break; + } + fputc(copy_buffer[(ftell(page_source_file)-1)% COPY_BUFFER_SIZE], temp_output_file); + } + } + + + done_reading_write_file: + if(this_is_a_top_level_page) { + char folder_of_file[PATH_MAX]; + strcpy(folder_of_file, page_source_file_path); + char * p; + p = strrchr(folder_of_file, '/'); + if (!p) { + printf("[fopenmkdir.c] filename contains no slash.\n"); + return FAILURE; + } else { + *p = '\0'; + } + char *type_of_page = strrchr(folder_of_file, '/'); + if (type_of_page == NULL) { + printf("[tape.c] bvr_compose_page: folder of file contains no slash, hope %s is a page category.", folder_of_file); + type_of_page = folder_of_file; + } else { + type_of_page++; + } + // type_of_page+1 is a page category (skip delimeter pointer.) + } + return SUCCESS; +} diff --git a/test/ftell-test.c b/test/ftell-test.c new file mode 100644 index 0000000..e0dd374 --- /dev/null +++ b/test/ftell-test.c @@ -0,0 +1,17 @@ +#include +#include + +extern int main(int argc, char* argv[]) { + if(argc != 2) { + printf("usage: %s filename-to-read\n", argv[0]); + return 1; + } + FILE * temp_input_file = fopen(argv[1], "r"); + char char_buffer = fgetc(temp_input_file); + while(char_buffer != EOF) { + printf("I have character %c on position %ld\n", char_buffer, ftell(temp_input_file)-1); + char_buffer = fgetc(temp_input_file); + } + printf("reached EOF!\n"); + return 0; +} diff --git a/test/neg-test.c b/test/neg-test.c new file mode 100644 index 0000000..3629737 --- /dev/null +++ b/test/neg-test.c @@ -0,0 +1,12 @@ +#include +#include +#include +extern int main(int argc, char* argv[]) { + char array[] = {'s','i','j','a','n','e','c',' '}; + printf("it should always be sijanec and a space character for 2000 characters or OB1\n"); + for(int i=0;i<=2000;i++) { + printf("%c", array[i% sizeof(array)]); + fflush(stdout); + } + return 0; +} diff --git a/test/randstring-test.c b/test/randstring-test.c new file mode 100644 index 0000000..644a0d9 --- /dev/null +++ b/test/randstring-test.c @@ -0,0 +1,12 @@ +#include +#include +#include "randstring.c" + +extern int main(int argc, char* argv[]) { + if(argc != 2) { + printf("usage: %s length\n", argv[0]); + return 1; + } + printf("generated string:\n%s\n", randstring(atoi(argv[1]))); + return 0; +} diff --git a/test/tape-test.bvr b/test/tape-test.bvr new file mode 100644 index 0000000..8d934c5 --- /dev/null +++ b/test/tape-test.bvr @@ -0,0 +1 @@ +

<@this should be replaced with constant aaaaa@>

diff --git a/test/tape-test.c b/test/tape-test.c new file mode 100644 index 0000000..f1cca25 --- /dev/null +++ b/test/tape-test.c @@ -0,0 +1,10 @@ +#include +#include +#include +extern int main(int argc, char* argv[]) { + if(argc != 3) { + printf("usage: %s source-file file-with-commands-replaced-with-a's\n", argv[0]); + return 1; + } + return bvr_compose_page(argv[1], 0, argv[2]); +} diff --git a/tmp/output.htm b/tmp/output.htm new file mode 100644 index 0000000..a52de17 --- /dev/null +++ b/tmp/output.htm @@ -0,0 +1 @@ +

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

\ No newline at end of file -- cgit v1.2.3