Skip to content

Commit f171bfe

Browse files
committed
updated order of File Header in csv, changed path to filesystem
1 parent eaf8a0d commit f171bfe

File tree

4 files changed

+21
-23
lines changed

4 files changed

+21
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ vs.proj/.vs/
3535
/.vs/notepadpp-multireplace/v17/Solution.VC.db
3636
/.vs/notepadpp-multireplace/v17/DocumentLayout.backup.json
3737
/vs.proj/MultiReplace/x64/Debug/MultiReplace.vcxproj.FileListAbsolute.txt
38+
/.vs/slnx.sqlite

.vs/ProjectSettings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"CurrentProjectSetting": "No Configurations"
3+
}

src/IniFileCache.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <algorithm>
2424
#include <codecvt>
2525
#include <windows.h>
26+
#include <filesystem>
2627

2728
namespace {
2829

@@ -74,19 +75,11 @@ bool IniFileCache::load(const std::wstring& iniFile)
7475
// ------------------------------------------------------------------
7576
bool IniFileCache::parse(const std::wstring& iniFilePath)
7677
{
77-
// convert wide → UTF‑8 for std::ifstream
78-
int sz8 = WideCharToMultiByte(CP_UTF8, 0,
79-
iniFilePath.c_str(), (int)iniFilePath.size(),
80-
nullptr, 0, nullptr, nullptr);
81-
std::string filePath(sz8, 0);
82-
WideCharToMultiByte(CP_UTF8, 0,
83-
iniFilePath.c_str(), (int)iniFilePath.size(),
84-
&filePath[0], sz8, nullptr, nullptr);
85-
86-
std::ifstream ini(filePath, std::ios::binary);
78+
// Open via filesystem::path (wide) → WinAPI 'W' path, Unicode-safe
79+
std::ifstream ini(std::filesystem::path(iniFilePath), std::ios::binary);
8780
if (!ini.is_open()) return false;
8881

89-
// read whole file
82+
// Read whole file (unchanged)
9083
std::string raw((std::istreambuf_iterator<char>(ini)),
9184
std::istreambuf_iterator<char>());
9285
ini.close();

src/MultiReplacePanel.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4998,11 +4998,12 @@ bool MultiReplace::resolveLuaSyntax(std::string& inputString, const LuaVariables
49984998
int MultiReplace::safeLoadFileSandbox(lua_State* L) {
49994999
// Return signature: (bool success, table or errorMessage)
50005000

5001-
// 1) Get the file path argument
5001+
// Get the file path argument
50025002
const char* path = luaL_checkstring(L, 1);
50035003

5004-
// 2) Read raw bytes from disk
5005-
std::ifstream in(path, std::ios::binary);
5004+
// UTF-8 → Wide → filesystem::path → ifstream
5005+
std::wstring wpath = Encoding::utf8ToWString(path);
5006+
std::ifstream in(std::filesystem::path(wpath), std::ios::binary);
50065007
if (!in) {
50075008
lua_pushboolean(L, false);
50085009
lua_pushfstring(L, "Cannot open file: %s", path);
@@ -5012,10 +5013,10 @@ int MultiReplace::safeLoadFileSandbox(lua_State* L) {
50125013
std::istreambuf_iterator<char>());
50135014
in.close();
50145015

5015-
// 3) Detect UTF-8 vs ANSI
5016+
// Detect UTF-8 vs ANSI
50165017
bool rawIsUtf8 = Encoding::isValidUtf8(raw);
50175018

5018-
// 4) Convert to a true UTF-8 buffer if needed
5019+
// Convert to a true UTF-8 buffer if needed
50195020
std::string utf8_buf;
50205021
if (rawIsUtf8) {
50215022
utf8_buf = std::move(raw);
@@ -5032,7 +5033,7 @@ int MultiReplace::safeLoadFileSandbox(lua_State* L) {
50325033
WideCharToMultiByte(CP_UTF8, 0, wide.data(), wlen, &utf8_buf[0], u8len, nullptr, nullptr);
50335034
}
50345035

5035-
// 5) Strip UTF-8 BOM if present
5036+
// Strip UTF-8 BOM if present
50365037
if (utf8_buf.size() >= 3 &&
50375038
static_cast<unsigned char>(utf8_buf[0]) == 0xEF &&
50385039
static_cast<unsigned char>(utf8_buf[1]) == 0xBB &&
@@ -5041,22 +5042,22 @@ int MultiReplace::safeLoadFileSandbox(lua_State* L) {
50415042
utf8_buf.erase(0, 3);
50425043
}
50435044

5044-
// 6) Load the UTF-8 chunk into Lua
5045+
// Load the UTF-8 chunk into Lua
50455046
if (luaL_loadbuffer(L, utf8_buf.data(), utf8_buf.size(), path) != LUA_OK) {
50465047
lua_pushboolean(L, false);
50475048
lua_pushstring(L, lua_tostring(L, -1)); // error message
50485049
return 2;
50495050
}
50505051

5051-
// 7) Run the chunk: expect it to return a table of entries
5052+
// Run the chunk: expect it to return a table of entries
50525053
if (lua_pcall(L, 0, 1, 0) != LUA_OK) {
50535054
lua_pushboolean(L, false);
50545055
lua_pushstring(L, lua_tostring(L, -1));
50555056
return 2;
50565057
}
50575058
// Stack now: [ table ]
50585059

5059-
// 8) Prepend a 'true' for the success flag
5060+
// Prepend a 'true' for the success flag
50605061
lua_pushboolean(L, true);
50615062
lua_insert(L, -2); // now stack: [ true, table ]
50625063

@@ -9367,7 +9368,7 @@ std::wstring MultiReplace::promptSaveListToCsv() {
93679368
}
93689369

93699370
bool MultiReplace::saveListToCsvSilent(const std::wstring& filePath, const std::vector<ReplaceItemData>& list) {
9370-
std::ofstream outFile(filePath, std::ios::binary);
9371+
std::ofstream outFile(std::filesystem::path(filePath), std::ios::binary);
93719372

93729373
if (!outFile.is_open()) {
93739374
return false;
@@ -9377,7 +9378,7 @@ bool MultiReplace::saveListToCsvSilent(const std::wstring& filePath, const std::
93779378
outFile.write("\xEF\xBB\xBF", 3);
93789379

93799380
// Convert and Write CSV header
9380-
std::string utf8Header = Encoding::wstringToUtf8(L"Selected,Find,Replace,WholeWord,MatchCase,UseVariables,Regex,Extended,Comments\n");
9381+
std::string utf8Header = Encoding::wstringToUtf8(L"Selected,Find,Replace,WholeWord,MatchCase,UseVariables,Extended,Regex,Comments\n");
93819382
outFile << utf8Header;
93829383

93839384
// Write list items to CSV file
@@ -9470,7 +9471,7 @@ int MultiReplace::checkForUnsavedChanges() {
94709471

94719472
void MultiReplace::loadListFromCsvSilent(const std::wstring& filePath, std::vector<ReplaceItemData>& list) {
94729473
// Open the CSV file
9473-
std::ifstream inFile(filePath, std::ios::binary);
9474+
std::ifstream inFile(std::filesystem::path(filePath), std::ios::binary);
94749475
if (!inFile.is_open()) {
94759476
std::wstring shortenedFilePathW = getShortenedFilePath(filePath, 500);
94769477
throw CsvLoadException(Encoding::wstringToUtf8(LM.get(L"status_unable_to_open_file", { shortenedFilePathW })));

0 commit comments

Comments
 (0)