簡體   English   中英

在C中使用PCRE在文件內搜索和替換

[英]Search and replace within a file using PCRE in C

我想用C解析一個外殼樣式的鍵值配置文件,並根據需要替換值。 一個示例文件可能看起來像

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), %s\n", 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: %d\n", rc);
                break;
        }
        free(re);
        return -1;
    }

    for (i = 0; i < rc; i++) {
        printf("========\nlength of vector: %d\nvector[0..1]: %d %d\nchars at start/end: %c %c\n", ovector[2*i+1] - ovector[2*i], ovector[0], ovector[1], str[ovector[0]], str[ovector[1]]);
        printf("file content length is %d\n========\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("%s\n", newFile);
    return 0;
}

該代碼在某些情況下有效,但是如果我要替換FOOANOTHER_KEY有些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"

該代碼工作正常。 我不明白為什么代碼在這里表現不同。

您忘記了終止str ,因此隨后調用strlen(str)將產生不可預測的結果。 可以更改:

str = malloc(confSize);
fread(str, 1, confSize, conf);

至:

str = malloc(confSize + 1);     // note: extra char for '\0' terminator
fread(str, 1, confSize, conf);
str[confSize] = '\0';           // terminate string!

和/或將confSize而不是strlen(str)傳遞給pcre_exec

為字符串分配了confSize個內存字節。 以confSize為10為例。

 str = malloc(confSize);

因此,您的字符串的有效索引是0-9。 但是此行將“ \\ n”分配給第十個索引,即第十一個字節:

  str[confSize] = '\n';

如果您希望最后一個字符為“ \\ n”,則應為:

  str[confSize - 1] = '\n';

替換文本之前的多余字符來自未正確以null終止您的before字符串的情況。 (就像Paul R指出的那樣,您沒有對整個緩沖區str空終止。)

char *before = malloc(ovector[0] + 1);
memcpy(before, str, ovector[0]);
before[ovector[0]] = '\0';

無論如何,分配子字符串和復制內容的工作似乎不必要地復雜並且容易出錯。 例如, somethingLen變量是否計算終止的空字符? 有時他們這樣做,有時卻沒有。 我建議選擇一種表示形式並始終使用它。 (而且,在不再使用所有已分配的緩沖區之后,您應該真正free它們,並且可能還要清理編譯后的正則表達式。)

您可以使用“之前”部分中的%s格式說明符的precision字段,僅對目標緩沖區分配一次即可進行替換:

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來存儲目標文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM