core: add an id to list

This commit is contained in:
starnakin 2023-07-26 18:40:34 +02:00
parent 0358eefcae
commit 31f89f44ec
6 changed files with 61 additions and 30 deletions

View File

@ -20,7 +20,11 @@ cmd_add(text, args)
{
if (i == [text + LOCATION_CURRENT_LINE] + 1 | i == [text + LOCATION_LENGTH])
{
[tmp + i] = line;
[tmp + i] = galloc(MAP_SIZE);
if ([tmp + i] == NULL)
return 1;
[[tmp + i] + MAP_KEY] = id++;
[[tmp + i] + MAP_VALUE] = line;
j++;
}
[tmp + i + j] = [[text + LOCATION_ARRAY] + i];

View File

@ -1,13 +1,20 @@
cmd_delete(text, args)
cmd_delete(data, args)
{
local i;
i = [text + LOCATION_CURRENT_LINE];
[text + LOCATION_LENGTH] = [text + LOCATION_LENGTH] - 1;
if ([data + LOCATION_LENGTH] == 0)
{
error(data, "line doesn't exist");
return 1;
}
i = [data + LOCATION_CURRENT_LINE];
[data + LOCATION_LENGTH] = [data + LOCATION_LENGTH] - 1;
free([[[data + LOCATION_ARRAY] + LOCATION_CURRENT_LINE] + MAP_VALUE]);
free([[data + LOCATION_ARRAY] + LOCATION_CURRENT_LINE]);
loop
{
if (i == [text + LOCATION_LENGTH])
if (i == [data + LOCATION_LENGTH])
return 0;
[[text + LOCATION_ARRAY] + i] = [[text + LOCATION_ARRAY] + i + 1];
[[data + LOCATION_ARRAY] + i] = [[data + LOCATION_ARRAY] + i + 1];
}
}

View File

@ -7,6 +7,6 @@ cmd_numbered(text, args)
}
putnum([text + LOCATION_CURRENT_LINE] + 1);
putchar('\t');
putstr([[text + LOCATION_ARRAY] + [text + LOCATION_CURRENT_LINE]]);
putstr([[[text + LOCATION_ARRAY] + [text + LOCATION_CURRENT_LINE]] + MAP_VALUE]);
putchar('\n');
}

View File

@ -5,6 +5,6 @@ cmd_print(text, args)
putstr("Empty buffer\n");
return 1;
}
putstr([[text + LOCATION_ARRAY] + [text + LOCATION_CURRENT_LINE]]);
putstr([[[text + LOCATION_ARRAY] + [text + LOCATION_CURRENT_LINE]] + MAP_VALUE]);
putchar('\n');
}

View File

@ -1,6 +1,8 @@
enum LOCATION_LENGTH, LOCATION_CURRENT_LINE, LOCATION_ARRAY;
enum LOCATION_LENGTH, LOCATION_CURRENT_LINE, LOCATION_ARRAY, LOCATION_MODE;
enum MODE_PRINT, MODE_NO_PRINT;
define DATA_SIZE=4;
parsing(text, cmd_ptr)
parsing(data, cmd_ptr)
{
local input;
local tmp;
@ -18,14 +20,14 @@ parsing(text, cmd_ptr)
if (isdigit([input]) == 1)
{
line_application = aton(input);
if (line_application == 0 | line_application >= [text + LOCATION_LENGTH] + 1)
if (line_application == 0 | line_application >= [data + LOCATION_LENGTH] + 1)
{
[cmd_ptr] = NULL;
putstr("invalid line\n");
free(input);
return 0;
}
[text + LOCATION_CURRENT_LINE] = line_application - 1;
[data + LOCATION_CURRENT_LINE] = line_application - 1;
}
tmp = input;
loop
@ -43,43 +45,50 @@ parsing(text, cmd_ptr)
return 1;
}
global id=0;
main()
{
local text;
local data;
local cmd;
text = galloc(3);
if (text == NULL)
data = galloc(DATA_SIZE);
if (data == NULL)
return 1;
[text + LOCATION_CURRENT_LINE] = 0 - 1;
[text + LOCATION_LENGTH] = 0;
[text + LOCATION_ARRAY];
[text + LOCATION_ARRAY] = galloc(0);
if ([text + LOCATION_ARRAY] == NULL)
[data + LOCATION_MODE] = 0;
[data + LOCATION_CURRENT_LINE] = 0 - 1;
[data + LOCATION_LENGTH] = 0;
[data + LOCATION_ARRAY];
[data + LOCATION_ARRAY] = galloc(2);
if ([data + LOCATION_ARRAY] == NULL)
{
free(text);
free(data);
return 1;
}
[[data + LOCATION_ARRAY] + MAP_KEY] = id++;
[[data + LOCATION_ARRAY] + MAP_VALUE] = galloc(0);
if ([[data + LOCATION_ARRAY] + MAP_VALUE] == NULL)
{
free([data + LOCATION_ARRAY]);
free(data);
return 1;
}
loop
{
if (parsing(text, &cmd))
if (parsing(data, &cmd))
return 1;
if (cmd != NULL)
{
if (strcmp(cmd, "a") == 0)
cmd_add(text, NULL);
cmd_add(data, NULL);
else if (strcmp(cmd, "p") == 0)
cmd_print(text, NULL);
cmd_print(data, NULL);
else if (strcmp(cmd, "n") == 0)
cmd_numbered(text, NULL);
cmd_numbered(data, NULL);
else if (strcmp(cmd, "d") == 0)
cmd_delete(text, NULL);
cmd_delete(data, NULL);
else
{
putstr("cmd not foud");
putchar('\n');
}
error(data, "cmd not foud");
free(cmd);
}
}

View File

@ -11,3 +11,14 @@ line_pos_to_cursor(text, line_pos)
line_pos--;
}
}
error(data, description)
{
if ([data + LOCATION_MODE] == MODE_PRINT)
{
putstr(description);
putchar('\n');
}
else
putstr("?\n");
}