Roa*_*-EX 6 c++ regex command-line command-line-arguments
我一直在开发一个模仿 shell 终端的程序,并且遇到了一个比我预期更难的实现问题。基本上,我试图分割参数,就像 shell 传递给其可执行文件的方式一样。因此,想象一下这样的输入:
$> ./foo some arguments
人们期望传递给程序的参数是一个数组,如下所示(假设是 C/C++):
char ** argv = {"foo", "some" "arguments"}
但是,如果参数是:
$> ./foo "My name is foo" bar
该数组将是:
char ** argv = {"foo", "My name is foo", "bar"}
任何人都可以建议一种有效的方法来实现这一点,例如界面如下:
vector<string> splitArgs(string allArgs);或者string[] splitArgs(string allArgs);
当然,我可以简单地在“阅读单词”/“阅读引用的文本”状态之间迭代和切换,但我觉得这并不那么有效。我也考虑过正则表达式的想法,但我不太熟悉 C++ 中如何实现这一点。对于这个项目,我也安装了 boost 库,如果有帮助的话。
谢谢!RR
小智 0
有时我仍然使用这个简单的 C 实用函数来实现此目的。我主要在标准库非常有限的嵌入式系统上使用它,因此可以使用标准库控件更改大多数代码以提高效率,但基本技术应该保持不变,标记字符串的引用部分在解析之前,然后通过分割标记将字符串分解为单独的标记,最后消除各个部分中的引号。
/**
* Split a line into separate words.
*/
static void splitLine(char *pLine, char **pArgs) {
char *pTmp = strchr(pLine, ' ');
if (pTmp) {
*pTmp = '\0';
pTmp++;
while ((*pTmp) && (*pTmp == ' ')) {
pTmp++;
}
if (*pTmp == '\0') {
pTmp = NULL;
}
}
*pArgs = pTmp;
}
/**
* Breaks up a line into multiple arguments.
*
* @param io_pLine Line to be broken up.
* @param o_pArgc Number of components found.
* @param io_pargc Array of individual components
*/
static void parseArguments(char *io_pLine, int *o_pArgc, char **o_pArgv) {
char *pNext = io_pLine;
size_t i;
int j;
int quoted = 0;
size_t len = strlen(io_pLine);
// Protect spaces inside quotes, but lose the quotes
for(i = 0; i < len; i++) {
if ((!quoted) && ('"' == io_pLine[i])) {
quoted = 1;
io_pLine[i] = ' ';
} else if ((quoted) && ('"' == io_pLine[i])) {
quoted = 0;
io_pLine[i] = ' ';
} else if ((quoted) && (' ' == io_pLine[i])) {
io_pLine[i] = '\1';
}
}
// init
MY_memset(o_pArgv, 0x00, sizeof(char*) * C_MAXARGS);
*o_pArgc = 1;
o_pArgv[0] = io_pLine;
while ((NULL != pNext) && (*o_pArgc < C_MAXARGS)) {
splitLine(pNext, &(o_pArgv[*o_pArgc]));
pNext = o_pArgv[*o_pArgc];
if (NULL != o_pArgv[*o_pArgc]) {
*o_pArgc += 1;
}
}
for(j = 0; j < *o_pArgc; j++) {
len = strlen(o_pArgv[j]);
for(i = 0; i < len; i++) {
if('\1' == o_pArgv[j][i]) {
o_pArgv[j][i] = ' ';
}
}
}
}
Run Code Online (Sandbox Code Playgroud)