From 9f7bf8ddf255f06a3c27c13dad87e34a70356d58 Mon Sep 17 00:00:00 2001 From: Stefan Zabka Date: Wed, 20 May 2020 18:07:43 +0200 Subject: First implementation of prompt --- 02_exercise/prompt_utils.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 02_exercise/prompt_utils.c (limited to '02_exercise/prompt_utils.c') diff --git a/02_exercise/prompt_utils.c b/02_exercise/prompt_utils.c new file mode 100644 index 0000000..819509d --- /dev/null +++ b/02_exercise/prompt_utils.c @@ -0,0 +1,90 @@ +#include "prompt_utils.h" + +#include "array.h" +#include +#include +#include +#include + +char const *const relative_path(char const *const from_dir, char const *const to_dir) { + // easiest cases first + { + int result = strcmp(from_dir, to_dir); + if (result == 0) { + char *return_value = malloc(2); + strcpy(return_value, "."); + return return_value; + } + } + { + if (strcmp("/", from_dir) == 0) { + char *return_value = malloc(strlen(to_dir) + 2); + return_value[0] = '.'; + memcpy(return_value + 1, to_dir, strlen(to_dir) + 1); + return return_value; + } + } + // Splitting path into pieces so we can do strcmp on each of them + size_t const *const from_dir_indeces = get_seperator_indeces(from_dir, '/'); + size_t const *const to_dir_indeces = get_seperator_indeces(to_dir, '/'); + + size_t from_dir_len = arrayLen(from_dir_indeces); + size_t to_dir_len = arrayLen(to_dir_indeces); + + // finding the longest common substring + size_t array_len = from_dir_len < to_dir_len ? from_dir_len : to_dir_len; + size_t i = 0; + size_t common_position = 0; + for (; i < array_len - 1; i++) { + if (from_dir_indeces[i + 1] != to_dir_indeces[i + 1]) { + break; + } + size_t index = from_dir_indeces[i]; + size_t count = from_dir_indeces[i + 1] - from_dir_indeces[i]; + if (strncmp(from_dir + index, to_dir + index, count) != 0) { + break; + } + common_position = from_dir_indeces[i + 1]; + } + + size_t levels_up = from_dir_len - i - 1; + char *return_value; + if (levels_up == 0) { + // Equal dirs for whole length of path and not equal => subdir + size_t length = strlen(to_dir + common_position) + strlen("./") + 1; + return_value = malloc(length * sizeof(char)); + strcpy(return_value, "."); + strcat(return_value, to_dir + common_position); + } else { + char const *const go_up = "/.."; + size_t length = strlen(to_dir + common_position) + strlen("..") + + strlen(go_up) * (levels_up - 1); + return_value = malloc(length * sizeof(char)); + strcpy(return_value, ".."); + for (size_t j = 0; j < levels_up - 1; j++) { + strcat(return_value, go_up); + } + strcat(return_value, to_dir + common_position); + } + + arrayRelease((void *)from_dir_indeces); + arrayRelease((void *)to_dir_indeces); + + return return_value; +} + +size_t *get_seperator_indeces(char const *const text, char seperator) { + size_t *indeces; + arrayInit(indeces); + char const *current = text; + if (strchr(current, seperator) == NULL) { + arrayRelease(indeces); + return NULL; + } + while ((current = strchr(current, seperator)) != NULL) { + arrayPush(indeces) = current - text; + ++current; + } + arrayPush(indeces) = strlen(text); + return indeces; +} -- cgit v1.2.3-54-g00ecf