add: work

This commit is contained in:
starnakin 2023-07-24 10:15:29 +02:00
parent 85ab7c292a
commit 731da51b61
5 changed files with 101 additions and 31 deletions

@ -1 +1 @@
Subproject commit a39e4a780d4e8e0344a05577d27a5a1af9956514
Subproject commit ee80b05d103bceb5c4d164c969c6a3ae8b192a11

View File

@ -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;
}

4
src/commands/print.🗿 Normal file
View File

@ -0,0 +1,4 @@
cmd_print(text)
{
putstr([[text + LOCATION_ARRAY] + [text + LOCATION_CURRENT_LINE]]);
}

View File

@ -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);
}
}
}

View File

@ -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--;
}
}