diff --git a/Makefile b/Makefile index 2791f86..17c08d6 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ define build_baml - gcc main.c baml.c -o baml + gcc main.c -o baml endef define test_baml @@ -12,11 +12,14 @@ endef build: $(build_baml) + @cp libbaml/baml.h /usr/include/ + install: $(build_baml) install -m 755 ./baml /usr/bin/baml @$(test_baml) + @cp libbaml/baml.h /usr/include/ uninstall: rm /usr/bin/baml diff --git a/baml.h b/baml.h deleted file mode 100644 index a44bafa..0000000 --- a/baml.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef BAML_H -# define BAML_H -# define ENTRYTOKEN "->" -# define HEADERTOKEN ';' -#include -#include -#include - -/* - * "valid_arg" takes the first argument and returns output format string list - * if the argument is valid. returns NULL if the argument is invalid. - * "valid_token" takes the line read from the file and check whether it has a - * valid entry token, returns index position located after the token. returns - * zero if the argument is invalid. - * "pheader" takes the line "hstr" comprising a valid token and prints header - * output according to the "format"; "hopen" is passed 0 when there is no - * previous header in the file. - * "pentry" takes the line "hstr" comprising a valid token and prints entry - * output according to the "format"; "hopen" is passed 1 when there is no - * previous entry in the header, it is set to 2 when an entry is inserted. - * "pitem" takes the target string and prints it by adding '\' for any quote - * character and trimming the end of it. - * "dstr" takes a "base" string and concatenates "tail" by dynamically - * allocating memory. this function is used to store a complete line from - * the file. - */ - -char **valid_arg(char *arg); -size_t valid_token(char *store); -void pitem(char *istr); -void pheader(char *hstr, char **format, char *hopen); -void pentry(char *estr, char **format, char *hopen); -size_t dstr(char *base, char *tail); - -#endif diff --git a/baml.c b/libbaml/baml.h similarity index 53% rename from baml.c rename to libbaml/baml.h index c198255..e66c680 100644 --- a/baml.c +++ b/libbaml/baml.h @@ -1,5 +1,32 @@ -#include "baml.h" +#pragma once +# define ENTRYTOKEN "->" +# define HEADERTOKEN ';' +#include +#include +#include +/* + * "valid_arg" takes the first argument and returns output format string list + * if the argument is valid. returns NULL if the argument is invalid. + * "valid_token" takes the line read from the file and check whether it has a + * valid entry token, returns index position located after the token. returns + * zero if the argument is invalid. + * "pheader" takes the line "hstr" comprising a valid token and prints header + * output according to the "format"; "hopen" is passed 0 when there is no + * previous header in the file. + * "pentry" takes the line "hstr" comprising a valid token and prints entry + * output according to the "format"; "hopen" is passed 1 when there is no + * previous entry in the header, it is set to 2 when an entry is inserted. + * "pitem" takes the target string and prints it by adding '\' for any quote + * character and trimming the end of it. + * "dstr" takes a "base" string and concatenates "tail" by dynamically + * allocating memory. this function is used to store a complete line from + * the file. + */ + +/* + * Checks valid arguments for baml + */ char **valid_arg(char *arg) { char **format; @@ -30,6 +57,9 @@ char **valid_arg(char *arg) } +/* + * Checks valid tokens like space and tab + */ size_t valid_token(char *store) { size_t offset = 0; @@ -41,28 +71,41 @@ size_t valid_token(char *store) return (0); } +/* + * Check items for any bugs in parsing (in 69. line) + */ void pitem(char *istr) { + + // Required definitions size_t isize = strlen(istr); size_t scan = 0; size_t checkp = 0; size_t trimmer = 0; unsigned char memory = 0; + // If size of istr greater than scan -> loop while (scan < isize) { + // If 0 numbered member of istr greater than 32 -> trimmer equals to scan if (istr[scan] > 32 ) trimmer = scan; + // If scan numbered member of istr equals to \' or \" or ' --> if (istr[scan] == '\'' || istr[scan] == '\"' || istr[scan] == '`') { + // Memory equals scan numbered member of istr memory = istr[scan]; + // Scan numbered member of istr is equals to 0 istr[scan] = 0; + // Print result printf("%s\\%c", &istr[checkp], memory); + // Scan numbered member of istr to memory istr[scan] = memory; + // Increase and equals to checkp(0) checkp = ++scan; continue ; } - scan++; + scan++; // Increase scan for while } istr[trimmer + 1] = 0; if (memory == 0) @@ -71,6 +114,7 @@ void pitem(char *istr) printf("%s", &istr[checkp]); } +// Check headers for tabs and spaces void pheader(char *hstr, char **format, char *hopen) { size_t scan; @@ -94,6 +138,7 @@ void pheader(char *hstr, char **format, char *hopen) *hopen = 1; } +// Check entry ```->hey``` with spaces and tabs again void pentry(char *estr, char **format, char *hopen) { size_t scan; @@ -119,12 +164,13 @@ void pentry(char *estr, char **format, char *hopen) printf("%s", format[2]); } +// Change some things with strings size_t dstr(char *base, char *tail) { size_t blen = strlen(base); size_t tlen = strlen(tail); - base = realloc(base, sizeof(char) * (blen + tlen + 1)); + base = realloc(base, sizeof(char) * (blen + tlen + 1)); // Allocate memory for (size_t i = blen; i < blen + tlen; i++) { base[i] = *tail; diff --git a/main.c b/main.c index 4916906..99feab4 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,6 @@ #include #include -#include "baml.h" +#include "libbaml/baml.h" int main(int ac, char **av) {