From 1d71f38d390ae8ba5bc27094341542b92d7406fe Mon Sep 17 00:00:00 2001 From: onyx-and-iris Date: Thu, 27 Jun 2024 19:18:28 +0100 Subject: [PATCH] add utility function replace_multiple_space_with_one() use it to parse the interactive input --- include/util.h | 6 ++++++ src/util.c | 38 ++++++++++++++++++++++++++++++++++++++ src/vmrcli.c | 14 ++++++++++---- 3 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 include/util.h create mode 100644 src/util.c diff --git a/include/util.h b/include/util.h new file mode 100644 index 0000000..e44c962 --- /dev/null +++ b/include/util.h @@ -0,0 +1,6 @@ +#ifndef __UTIL_H__ +#define __UTIL_H__ + +void replace_multiple_space_with_one(char *s); + +#endif /* __UTIL_H__ */ \ No newline at end of file diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..9eecf87 --- /dev/null +++ b/src/util.c @@ -0,0 +1,38 @@ +#include + +void replace_multiple_space_with_one(char *s) +{ + int j = 0; + int count = 0; + int len = strlen(s); + + if (len == 1 && (s[0] == ' ' || s[0] == '\t')) + { + s[0] = '\0'; + return; + } + + if (len < 2) + return; + + for (int i = 0; s[i] != '\0'; i++) + { + if (s[i] == ' ' || s[i] == '\t') + { + count++; + } + + if (s[i] != ' ' && s[i] != '\t') + { + if (count >= 1) + { + count = 0; + s[j++] = ' '; + } + s[j++] = s[i]; + } + } + s[j] = '\0'; + + return; +} \ No newline at end of file diff --git a/src/vmrcli.c b/src/vmrcli.c index 38ea811..19d1841 100644 --- a/src/vmrcli.c +++ b/src/vmrcli.c @@ -5,6 +5,7 @@ #include "cdll.h" #include "vmr.h" #include "log.h" +#include "util.h" #define MAX_LINE 512 @@ -172,6 +173,8 @@ void interactive(T_VBVMR_INTERFACE *vmr) { char input[MAX_LINE]; char *p = input; + char command[MAX_LINE]; + int i; while (fgets(input, MAX_LINE, stdin) != NULL) { @@ -179,17 +182,20 @@ void interactive(T_VBVMR_INTERFACE *vmr) if (strlen(input) == 1 && (strncmp(input, "Q", 1) == 0 || strncmp(input, "q", 1) == 0)) break; + replace_multiple_space_with_one(input); while (*p) { - char command[MAX_LINE]; - int i = 0; + memset(command, '\0', sizeof(command)); + i = 0; while (!isspace(*p)) command[i++] = *p++; command[i] = '\0'; - p++; /* shift to next char */ - parse_command(vmr, command); + if (command[0] != '\0') + parse_command(vmr, command); + + p++; } p = input; /* reset pointer */