From 731da51b6120b6157cdd1eb5cde041e2860ee3f6 Mon Sep 17 00:00:00 2001 From: starnakin Date: Mon, 24 Jul 2023 10:15:29 +0200 Subject: [PATCH] add: work --- lib/IronGolem | 2 +- src/commands/add.🗿 | 43 +++++++++++++++--------- src/commands/print.🗿 | 4 +++ src/main.🗿 | 77 ++++++++++++++++++++++++++++++++++++------- src/utils.🗿 | 6 ++-- 5 files changed, 101 insertions(+), 31 deletions(-) create mode 100644 src/commands/print.🗿 diff --git a/lib/IronGolem b/lib/IronGolem index a39e4a7..ee80b05 160000 --- a/lib/IronGolem +++ b/lib/IronGolem @@ -1 +1 @@ -Subproject commit a39e4a780d4e8e0344a05577d27a5a1af9956514 +Subproject commit ee80b05d103bceb5c4d164c969c6a3ae8b192a11 diff --git a/src/commands/add.🗿 b/src/commands/add.🗿 index 803ef5d..996db2e 100644 --- a/src/commands/add.🗿 +++ b/src/commands/add.🗿 @@ -1,26 +1,39 @@ -cmd_add(text_ptr, line_pos) +cmd_add(text) { local line; + local i, j; local tmp; - local out; - local len; - local cursor_pos; - tmp = NULL; loop { line = geadline("(add)"); - if (line == NULL | strcmp(line, ".\n") == 0) - break; - tmp = realloc(tmp, strlen(line) + strlen(tmp) + 1); + if (line == NULL) + return 1; + if (strcmp(line, ".\n") == 0) + { + [text + LOCATION_CURRENT_LINE] = [text + LOCATION_CURRENT_LINE] - 1; + return 0; + } + [text + LOCATION_LENGTH] = [text + LOCATION_LENGTH] + 1; + tmp = galloc([text + LOCATION_LENGTH]); if (tmp == NULL) return 1; - strcat(tmp, line); + i = 0; + j = 0; + loop + { + if (i == [text + LOCATION_CURRENT_LINE]) + { + [tmp + i] = line; + j++; + } + if (i + j == [text + LOCATION_LENGTH]) + break; + [tmp + i + j] = [[text + LOCATION_ARRAY] + i]; + i++; + } + free([[text + LOCATION_ARRAY]]); + [text + LOCATION_ARRAY] = tmp; + [text + LOCATION_CURRENT_LINE] = [text + LOCATION_CURRENT_LINE] + 1; } - cursor_pos = line_pos_to_cursor_pos([text_ptr], line_pos); - dbg cursor_pos; - out = replace([text_ptr], tmp, cursor_pos, cursor_pos); - free([text_ptr]); - [text_ptr] = out; - return 0; } diff --git a/src/commands/print.🗿 b/src/commands/print.🗿 new file mode 100644 index 0000000..f40eaa4 --- /dev/null +++ b/src/commands/print.🗿 @@ -0,0 +1,4 @@ +cmd_print(text) +{ + putstr([[text + LOCATION_ARRAY] + [text + LOCATION_CURRENT_LINE]]); +} diff --git a/src/main.🗿 b/src/main.🗿 index b43451a..d1534e6 100644 --- a/src/main.🗿 +++ b/src/main.🗿 @@ -1,24 +1,77 @@ -define BUFFER_SIZE=1000; +define LOCATION_LENGTH = 0; +define LOCATION_CURRENT_LINE = 1; +define LOCATION_ARRAY = 2; + +parsing(text, cmd_ptr) +{ + local input; + local tmp; + local line_application; + + input = geadline("(default)"); + if (input == NULL) + return 1; + [input + strlen(input) - 1] = 0; + if (isdigit([input]) == 1) + { + line_application = aton(input); + if (line_application >= [text + LOCATION_LENGTH]) + { + [cmd_ptr] = NULL; + putstr("invalid line\n"); + free(input); + return 0; + } + [text + LOCATION_CURRENT_LINE] = line_application; + } + tmp = input; + loop + { + if (isdigit([tmp]) == 0) + break; + tmp++; + } + if (strcmp(tmp, "") == 0) + [cmd_ptr] = strdup("p"); + else + [cmd_ptr] = strdup(input); + free(input); + if ([cmd_ptr] == NULL) + return 1; +} + main() { local text; - local line_pos; - local line; + local array; local cmd; - line_pos = 0; - text = strdup(""); + text = galloc(3); if (text == NULL) return 1; - cmd = 0; + [text + LOCATION_CURRENT_LINE] = 0; + [text + LOCATION_LENGTH] = 0; + array = [text + LOCATION_ARRAY]; + [array] = galloc(1); + if ([array] == NULL) + { + free(text); + return 1; + } + [[array]] = NULL; loop { - line = geadline("(default)"); - if (line == NULL) - return; - if (strcmp(line, "a\n") == 0) - cmd_add(&text, line_pos); - putstr(text); + if (parsing(text, &cmd)) + return 1; + if (cmd != NULL) + { + dbg [text + LOCATION_CURRENT_LINE]; + if (strcmp(cmd, "a") == 0) + cmd_add(text); + else if (strcmp(cmd, "p") == 0) + cmd_print(text); + free(cmd); + } } } diff --git a/src/utils.🗿 b/src/utils.🗿 index fa617e8..2a06ae1 100644 --- a/src/utils.🗿 +++ b/src/utils.🗿 @@ -1,13 +1,13 @@ -line_pos_to_cursor_pos(text, line_pos) +line_pos_to_cursor(text, line_pos) { local ptr = text; loop { if (line_pos == 0) - return ptr - text; + return ptr; ptr = strchr(ptr, '\n'); if (ptr == NULL) - return 0; + return NULL; line_pos--; } }