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]) 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++; j++;
} }
[tmp + i + j] = [[text + LOCATION_ARRAY] + i]; [tmp + i + j] = [[text + LOCATION_ARRAY] + i];

View File

@ -1,13 +1,20 @@
cmd_delete(text, args) cmd_delete(data, args)
{ {
local i; local i;
i = [text + LOCATION_CURRENT_LINE]; if ([data + LOCATION_LENGTH] == 0)
[text + LOCATION_LENGTH] = [text + LOCATION_LENGTH] - 1; {
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 loop
{ {
if (i == [text + LOCATION_LENGTH]) if (i == [data + LOCATION_LENGTH])
return 0; 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); putnum([text + LOCATION_CURRENT_LINE] + 1);
putchar('\t'); putchar('\t');
putstr([[text + LOCATION_ARRAY] + [text + LOCATION_CURRENT_LINE]]); putstr([[[text + LOCATION_ARRAY] + [text + LOCATION_CURRENT_LINE]] + MAP_VALUE]);
putchar('\n'); putchar('\n');
} }

View File

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

View File

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