mirror of
https://github.com/onyx-and-iris/vmrcli.git
synced 2024-11-15 17:40:56 +00:00
add utility function replace_multiple_space_with_one()
use it to parse the interactive input
This commit is contained in:
parent
2dda32ead9
commit
1d71f38d39
6
include/util.h
Normal file
6
include/util.h
Normal 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
38
src/util.c
Normal 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;
|
||||||
|
}
|
12
src/vmrcli.c
12
src/vmrcli.c
@ -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 */
|
|
||||||
|
|
||||||
|
if (command[0] != '\0')
|
||||||
parse_command(vmr, command);
|
parse_command(vmr, command);
|
||||||
|
|
||||||
|
p++;
|
||||||
}
|
}
|
||||||
|
|
||||||
p = input; /* reset pointer */
|
p = input; /* reset pointer */
|
||||||
|
Loading…
Reference in New Issue
Block a user