From 1b7bb91cf19231d65a061a4a6d0a03030943de00 Mon Sep 17 00:00:00 2001 From: starnakin Date: Sat, 29 Jul 2023 20:17:09 +0200 Subject: [PATCH] add: cmd range --- src/commands/delete.🗿 | 5 +- src/main.🗿 | 152 ++++++++++++++++++++++++++++++++--------- src/utils.🗿 | 15 ++++ 3 files changed, 136 insertions(+), 36 deletions(-) diff --git a/src/commands/delete.🗿 b/src/commands/delete.🗿 index 40dee42..ff4c3b3 100644 --- a/src/commands/delete.🗿 +++ b/src/commands/delete.🗿 @@ -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++; } } diff --git a/src/main.🗿 b/src/main.🗿 index 525995b..faafbaf 100644 --- a/src/main.🗿 +++ b/src/main.🗿 @@ -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); } } diff --git a/src/utils.🗿 b/src/utils.🗿 index 1cd07d8..2ef6616 100644 --- a/src/utils.🗿 +++ b/src/utils.🗿 @@ -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++; + } +}