[parser] add multiple occurrence support to replace_in_token_data()

* Closes #1333
This commit is contained in:
Pete Batard 2020-03-23 17:49:40 +00:00
parent 29bc207c35
commit 590b89a56a
No known key found for this signature in database
GPG Key ID: 38E0CF5E69EDD671
2 changed files with 31 additions and 15 deletions

View File

@ -1,7 +1,7 @@
/*
* Rufus: The Reliable USB Formatting Utility
* Elementary Unicode compliant find/replace parser
* Copyright © 2012-2018 Pete Batard <pete@akeo.ie>
* Copyright © 2012-2020 Pete Batard <pete@akeo.ie>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -1087,13 +1087,14 @@ out:
* Returns a pointer to rep if replacement occurred, NULL otherwise
* TODO: We might have to end up with a regexp engine, so that we can do stuff like: "foo*" -> "bar\1"
*/
#define MAX_OCCURRENCES 4
char* replace_in_token_data(const char* filename, const char* token, const char* src, const char* rep, BOOL dos2unix)
{
const wchar_t* outmode[] = { L"w", L"w, ccs=UTF-8", L"w, ccs=UTF-16LE" };
wchar_t *wtoken = NULL, *wfilename = NULL, *wtmpname = NULL, *wsrc = NULL, *wrep = NULL, bom = 0;
wchar_t buf[1024], *torep;
wchar_t buf[1024], *torep[MAX_OCCURRENCES + 1] = { NULL };
FILE *fd_in = NULL, *fd_out = NULL;
size_t i, ns, size;
size_t i, j, p[MAX_OCCURRENCES + 1] = { 0 }, ns, size;
int mode = 0;
char *ret = NULL, tmp[2];
@ -1151,7 +1152,6 @@ char* replace_in_token_data(const char* filename, const char* token, const char*
// duprintf("'%s' was detected as %s\n", filename,
// (mode==0)?"ANSI/UTF8 (no BOM)":((mode==1)?"UTF8 (with BOM)":"UTF16 (with BOM"));
wtmpname = (wchar_t*)calloc(wcslen(wfilename)+2, sizeof(wchar_t));
if (wtmpname == NULL) {
uprintf("Could not allocate space for temporary output name\n");
@ -1189,16 +1189,32 @@ char* replace_in_token_data(const char* filename, const char* token, const char*
continue;
i += ns;
torep = wcsstr(&buf[i], wsrc);
if (torep == NULL) {
// p[x] = starting position of the fragment with the replaceable string
p[0] = 0;
for (j = 0; j < MAX_OCCURRENCES; j++) {
torep[j] = wcsstr(&buf[i], wsrc);
if (torep[j] == NULL)
break;
// Next fragment will start after current + replaced string
i = (torep[j] - buf) + wcslen(wsrc);
p[j + 1] = i;
// Truncate each fragment to before the replaced string
*torep[j] = 0;
}
// No replaceable string found => output as is
if (torep[0] == NULL) {
fputws(buf, fd_out);
continue;
}
i = (torep-buf) + wcslen(wsrc);
*torep = 0;
// coverity[invalid_type]
fwprintf_s(fd_out, L"%s%s%s", buf, wrep, &buf[i]);
// Output all the truncated fragments + replaced strings
for (j = 0; torep[j] != NULL; j++)
fwprintf_s(fd_out, L"%s%s", &buf[p[j]], wrep);
// Ouput the last fragment
fwprintf_s(fd_out, L"%s", &buf[p[j]]);
ret = (char*)rep;
}

View File

@ -33,7 +33,7 @@ LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG DIALOGEX 12, 12, 232, 326
STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_ACCEPTFILES
CAPTION "Rufus 3.10.1628"
CAPTION "Rufus 3.10.1629"
FONT 9, "Segoe UI Symbol", 400, 0, 0x0
BEGIN
LTEXT "Drive Properties",IDS_DRIVE_PROPERTIES_TXT,8,6,53,12,NOT WS_GROUP
@ -395,8 +395,8 @@ END
//
VS_VERSION_INFO VERSIONINFO
FILEVERSION 3,10,1628,0
PRODUCTVERSION 3,10,1628,0
FILEVERSION 3,10,1629,0
PRODUCTVERSION 3,10,1629,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
@ -414,13 +414,13 @@ BEGIN
VALUE "Comments", "https://rufus.ie"
VALUE "CompanyName", "Akeo Consulting"
VALUE "FileDescription", "Rufus"
VALUE "FileVersion", "3.10.1628"
VALUE "FileVersion", "3.10.1629"
VALUE "InternalName", "Rufus"
VALUE "LegalCopyright", "© 2011-2020 Pete Batard (GPL v3)"
VALUE "LegalTrademarks", "https://www.gnu.org/licenses/gpl-3.0.html"
VALUE "OriginalFilename", "rufus-3.10.exe"
VALUE "ProductName", "Rufus"
VALUE "ProductVersion", "3.10.1628"
VALUE "ProductVersion", "3.10.1629"
END
END
BLOCK "VarFileInfo"