add: cmd range

This commit is contained in:
starnakin 2023-07-29 20:17:09 +02:00
parent 828cb3cdcd
commit 1b7bb91cf1
3 changed files with 136 additions and 36 deletions

View File

@ -8,13 +8,14 @@ cmd_delete(data, args)
return 1;
}
i = [data + LOCATION_CURRENT_LINE];
free ([[[data + LOCATION_ARRAY] + [data + LOCATION_CURRENT_LINE]] + MAP_VALUE]);
free ([[data + LOCATION_ARRAY] + [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 == [data + LOCATION_LENGTH])
return 0;
[[data + LOCATION_ARRAY] + i] = [[data + LOCATION_ARRAY] + i + 1];
i++;
}
}

View File

@ -2,10 +2,92 @@ enum LOCATION_LENGTH, LOCATION_CURRENT_LINE, LOCATION_ARRAY, LOCATION_MODE;
enum MODE_PRINT, MODE_NO_PRINT;
define DATA_SIZE=4;
parsing(data, cmd_ptr)
range(start, stop)
{
local i;
local sign;
local tab;
if (start > stop)
sign = 0 - 1;
else
sign = 0 + 1;
tab = galloc((stop - start) * sign + 1 + (start == stop));
if (tab == NULL)
return NULL;
i = 0;
loop
{
[tab + i + 1] = start + (i * sign);
if (start + (i * sign) == stop)
break;
i++;
}
[tab] = i + (start == stop);
return tab;
}
get_number(data, str)
{
local ranges;
local start = 0 -1;
local stop = 0 -1;
if ([[str]] == '%')
{
ranges = range(0, [data + LOCATION_LENGTH]);
[str] = [str] + 1;
}
else
{
loop
{
if ([[str]] == '$')
{
if (start == 0 - 1)
start = data + LOCATION_LENGTH;
else
stop = data + LOCATION_LENGTH;
[str] = [str] + 1;
}
else if (isdigit([[str]]))
{
if (start == 0 - 1)
start = aton([str]) - 1;
else
stop = aton([str]) - 1;
loop
{
if (isdigit([[str]]) == 0)
break;
[str] = [str] + 1;
}
}
else
break;
if ((start != 0 - 1 & stop != 0 - 1) | [[str]] == ',')
[str] = [str] + 1;
else
break;
}
if (start != 0 - 1)
{
if (stop != 0 - 1)
ranges = range(start, stop);
else
ranges = range(start, start);
}
else
ranges = range([data + LOCATION_CURRENT_LINE], [data + LOCATION_CURRENT_LINE]);
}
return ranges;
}
parsing(data, cmd_ptr, ranges_ptr)
{
local input;
local tmp;
local i;
local line_application;
input = get_input(data, "(default)", NULL);
@ -17,25 +99,8 @@ parsing(data, cmd_ptr)
[cmd_ptr] = strdup("p");
return [cmd_ptr] == NULL;
}
if (isdigit([input]) == 1)
{
line_application = aton(input);
if (line_application == 0 | line_application >= [data + LOCATION_LENGTH] + 1)
{
[cmd_ptr] = NULL;
putstr("invalid line\n");
free(input);
return 0;
}
[data + LOCATION_CURRENT_LINE] = line_application - 1;
}
tmp = input;
loop
{
if (isdigit([tmp]) == 0)
break;
tmp++;
}
[ranges_ptr] = get_number(data, &tmp);
if (strcmp(tmp, "") == 0)
[cmd_ptr] = strdup("p");
else
@ -43,6 +108,14 @@ parsing(data, cmd_ptr)
free(input);
if ([cmd_ptr] == NULL)
return 1;
i = 0;
loop
{
if ([[ranges_ptr]] == i)
break;
[[ranges_ptr] + i + 1] = [[[data + LOCATION_ARRAY] + [[ranges_ptr] + i + 1]] + MAP_KEY];
i++;
}
}
global id=0;
@ -51,6 +124,8 @@ main()
{
local data;
local cmd;
local ranges;
local i;
data = galloc(DATA_SIZE);
if (data == NULL)
@ -75,23 +150,32 @@ main()
}
loop
{
if (parsing(data, &cmd))
if (parsing(data, &cmd, &ranges))
return 1;
if (cmd != NULL)
i = 0;
loop
{
if (strcmp(cmd, "a") == 0)
cmd_add(data, NULL);
else if (strcmp(cmd, "p") == 0)
cmd_print(data, NULL);
else if (strcmp(cmd, "n") == 0)
cmd_numbered(data, NULL);
else if (strcmp(cmd, "d") == 0)
cmd_delete(data, NULL);
else if (strcmp(cmd, "h") == 0)
cmd_help(data, NULL);
else
error(data, "cmd not foud");
free(cmd);
if (i == [ranges])
break;
[data + LOCATION_CURRENT_LINE] = get_line_index_by_id(data, [ranges + 1 + i]);
if (cmd != NULL)
{
if (strcmp(cmd, "a") == 0)
cmd_add(data, NULL);
else if (strcmp(cmd, "p") == 0)
cmd_print(data, NULL);
else if (strcmp(cmd, "n") == 0)
cmd_numbered(data, NULL);
else if (strcmp(cmd, "d") == 0)
cmd_delete(data, NULL);
else if (strcmp(cmd, "h") == 0)
cmd_help(data, NULL);
else
error(data, "cmd not foud");
free(cmd);
}
i++;
}
free(ranges);
}
}

View File

@ -30,3 +30,18 @@ get_input(data, description, buffer)
else
return geadline2("", buffer);
}
get_line_index_by_id(data, id)
{
local i;
i = 0;
loop
{
if ([[[data + LOCATION_ARRAY] + i] + MAP_KEY] == id)
return i;
if ([data + LOCATION_LENGTH] == i)
return 0;
i++;
}
}