add utility function replace_multiple_space_with_one()

use it to parse the interactive input
This commit is contained in:
onyx-and-iris 2024-06-27 19:18:28 +01:00
parent 2dda32ead9
commit 1d71f38d39
3 changed files with 54 additions and 4 deletions

6
include/util.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef __UTIL_H__
#define __UTIL_H__
void replace_multiple_space_with_one(char *s);
#endif /* __UTIL_H__ */

38
src/util.c Normal file
View File

@ -0,0 +1,38 @@
#include <string.h>
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;
}

View File

@ -5,6 +5,7 @@
#include "cdll.h" #include "cdll.h"
#include "vmr.h" #include "vmr.h"
#include "log.h" #include "log.h"
#include "util.h"
#define MAX_LINE 512 #define MAX_LINE 512
@ -172,6 +173,8 @@ void interactive(T_VBVMR_INTERFACE *vmr)
{ {
char input[MAX_LINE]; char input[MAX_LINE];
char *p = input; char *p = input;
char command[MAX_LINE];
int i;
while (fgets(input, MAX_LINE, stdin) != NULL) 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)) if (strlen(input) == 1 && (strncmp(input, "Q", 1) == 0 || strncmp(input, "q", 1) == 0))
break; break;
replace_multiple_space_with_one(input);
while (*p) while (*p)
{ {
char command[MAX_LINE]; memset(command, '\0', sizeof(command));
int i = 0; i = 0;
while (!isspace(*p)) while (!isspace(*p))
command[i++] = *p++; command[i++] = *p++;
command[i] = '\0'; 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 */ p = input; /* reset pointer */