我想用C解析shell样式的键值配置文件,并根据需要替换值。一个示例文件可能看起来像
FOO="test"
SOME_KEY="some value here"
ANOTHER_KEY="here.we.go"
SOMETHING="0"
FOO_BAR_BAZ="2"
要查找值,我想使用正则表达式。我是一个初学者与PCRE库,所以我创建了一些代码来测试。这个应用程序有两个参数:第一个是要搜索的键。第二个是要填入双引号的值。
#include <pcre.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define OVECCOUNT 30
int main(int argc, char **argv){
const char *error;
int erroffset;
pcre *re;
int rc;
int i;
int ovector[OVECCOUNT];
char regex[64];
sprintf(regex,"(?<=^%s=\").+(?<!\")", argv[1]);
char *str;
FILE *conf;
conf = fopen("test.conf", "rw");
fseek(conf, 0, SEEK_END);
int confSize = ftell(conf)+1;
rewind(conf);
str = malloc(confSize);
fread(str, 1, confSize, conf);
fclose(conf);
str[confSize-1] = 'n';
re = pcre_compile (
regex, /* the pattern */
PCRE_CASELESS | PCRE_MULTILINE, /* default options */
&error, /* for error message */
&erroffset, /* for error offset */
0); /* use default character tables */
if (!re) {
printf("pcre_compile failed (offset: %d), %sn", erroffset, error);
return -1;
}
rc = pcre_exec (
re, /* the compiled pattern */
0, /* no extra data - pattern was not studied */
str, /* the string to match */
confSize, /* the length of the string */
0, /* start at offset 0 in the subject */
0, /* default options */
ovector, /* output vector for substring information */
OVECCOUNT); /* number of elements in the output vector */
if (rc < 0) {
switch (rc) {
case PCRE_ERROR_NOMATCH:
printf("String didn't match");
break;
default:
printf("Error while matching: %dn", rc);
break;
}
free(re);
return -1;
}
for (i = 0; i < rc; i++) {
printf("========nlength of vector: %dnvector[0..1]: %d %dnchars at start/end: %c %cn", ovector[2*i+1] - ovector[2*i], ovector[0], ovector[1], str[ovector[0]], str[ovector[1]]);
printf("file content length is %dn========n", strlen(str));
}
int newContentLen = strlen(argv[2])+1;
char *newContent = calloc(newContentLen,1);
memcpy(newContent, argv[2], newContentLen);
char *before = malloc(ovector[0]);
memcpy(before, str, ovector[0]);
int afterLen = confSize-ovector[1];
char *after = malloc(afterLen);
memcpy(after, str+ovector[1],afterLen);
int newFileLen = newContentLen+ovector[0]+afterLen;
char *newFile = calloc(newFileLen,1);
sprintf(newFile,"%s%s%s", before,newContent, after);
printf("%sn", newFile);
return 0;
}
这段代码在某些情况下是可以工作的,但是如果我想替换FOO
或ANOTHER_KEY
,就有问题了。
$ ./search_replace.out FOO baz
========
length of vector: 5
vector[0..1]: 5 10
chars at start/end: b "
file content length is 94
========
FOO="9@baz"
SOME_KEY="some value here"
ANOTHER_KEY="here.we.go"
SOMETHING="0"
FOO_BAR_BAZ="2"
$ ./search_replace.out ANOTHER_KEY insert
========
length of vector: 10
vector[0..1]: 52 62
chars at start/end: h "
file content length is 94
========
FOO="baaar"
SOME_KEY="some value here"
ANOTHER_KEY=")insert"
SOMETHING="0"
FOO_BAR_BAZ="2"
现在如果我稍微改变一下输入文件的格式为
TEST="new inserted"
FOO="test"
SOME_KEY="some value here"
ANOTHER_KEY="here.we.go"
SOMETHING="0"
FOO_BAR_BAZ="2"
代码工作正常。我不明白为什么代码在这里的行为不同
在被替换的文本之前的额外字符来自没有正确地null终止您的before
字符串。(正如Paul R指出的那样,您没有将整个缓冲区str
终止为空。)所以:
char *before = malloc(ovector[0] + 1);
memcpy(before, str, ovector[0]);
before[ovector[0]] = ' ';
无论如何,分配子字符串和复制内容似乎没有必要那么复杂,而且容易出错。例如,somethingLen
变量是否计算终止null字符?有时会,有时不会。我建议选择一种表示并始终使用它。(并且您应该在不再使用它们之后真正地free
所有分配的缓冲区,并且可能还清理编译的regex。)
您可以通过在"before"部分使用%s
格式说明符的精度字段来为目标缓冲区分配一次替换:
int cutLen = ovector[1] - ovector[0];
int newFileLen = confSize + strlen(argv[2]) - cutLen;
char *newFile = malloc(newFileLen + 1);
snprintf(newFile, newFileLen + 1, "%.*s%s%s",
ovector[0], str, argv[2], str + ovector[1]);
fprintf
来保存目标文件
您忘记终止str
,因此随后调用strlen(str)
将产生不可预测的结果。要么改变:
str = malloc(confSize);
fread(str, 1, confSize, conf);
:
str = malloc(confSize + 1); // note: extra char for ' ' terminator
fread(str, 1, confSize, conf);
str[confSize] = ' '; // terminate string!
和/或传递confSize
而不是strlen(str)
给pcre_exec
。
您的字符串分配了confSize字节的内存。以confSize为10为例。
str = malloc(confSize);
所以字符串的有效索引是0-9。但是这一行将'n'赋值给第10个索引,也就是第11个字节:
str[confSize] = 'n';
如果你想让最后一个字符是'n',它应该是:
str[confSize - 1] = 'n';