Skip to content

Commit

Permalink
less WCHAR
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed May 21, 2024
1 parent ed6a30a commit 1695022
Show file tree
Hide file tree
Showing 18 changed files with 82 additions and 70 deletions.
2 changes: 1 addition & 1 deletion src/CrashHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ static DWORD WINAPI CrashDumpThread(LPVOID) {
// set the SUMATRAPDF_FULLDUMP environment variable for more complete dumps
DWORD n = GetEnvironmentVariableA("SUMATRAPDF_FULLDUMP", nullptr, 0);
bool fullDump = (0 != n);
WCHAR* ws = ToWStrTemp(gCrashDumpPath);
TempWStr ws = ToWStrTemp(gCrashDumpPath);
dbghelp::WriteMiniDump(ws, &gMei, fullDump);
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Favorites.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ static void AppendFavMenuItems(HMENU m, FileState* f, int& idx, bool combined, b
s = FavReadableNameTemp(fn);
}
auto safeStr = MenuToSafeStringTemp(s);
WCHAR* ws = ToWStrTemp(safeStr);
TempWStr ws = ToWStrTemp(safeStr);
AppendMenuW(m, MF_STRING, (UINT_PTR)fn->menuId, ws);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,7 @@ HMENU BuildMenuFromMenuDef(MenuDef* menuDef, HMENU menu, BuildMenuCtx* ctx) {
if (subMenuDef == menuDefFile) {
DynamicPartOfFileMenu(subMenu, ctx);
}
WCHAR* ws = ToWStrTemp(title);
TempWStr ws = ToWStrTemp(title);
AppendMenuW(menu, flags, (UINT_PTR)subMenu, ws);
} else {
str::Str title2 = title;
Expand All @@ -1408,7 +1408,7 @@ HMENU BuildMenuFromMenuDef(MenuDef* menuDef, HMENU menu, BuildMenuCtx* ctx) {
}
}
UINT flags = MF_STRING | (disableMenu ? MF_DISABLED : MF_ENABLED);
WCHAR* ws = ToWStrTemp(title2.Get());
TempWStr ws = ToWStrTemp(title2.Get());
AppendMenuW(menu, flags, md.idOrSubmenu, ws);
}

Expand Down Expand Up @@ -1514,7 +1514,7 @@ void MenuUpdatePrintItem(MainWindow* win, HMENU menu, bool disableOnly = false)
}
}
if (!filePrintAllowed || !disableOnly) {
WCHAR* ws = ToWStrTemp(printItem.Get());
TempWStr ws = ToWStrTemp(printItem.Get());
ModifyMenuW(menu, CmdPrint, MF_BYCOMMAND | MF_STRING, (UINT_PTR)CmdPrint, ws);
}
MenuSetEnabled(menu, CmdPrint, filePrintEnabled && filePrintAllowed);
Expand Down Expand Up @@ -2214,7 +2214,7 @@ void MenuCustomDrawItem(HWND hwnd, DRAWITEMSTRUCT* dis) {
// DrawTextEx handles & => underscore drawing
rc.top += padY;
rc.left += cxCheckMark;
WCHAR* ws = ToWStrTemp(menuText);
TempWStr ws = ToWStrTemp(menuText);
DrawTextExW(hdc, ws, -1, &rc, DT_LEFT, nullptr);
if (shortcutText != nullptr) {
ws = ToWStrTemp(shortcutText);
Expand Down
2 changes: 1 addition & 1 deletion src/PdfSync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ int SyncTex::RebuildIndexIfNeeded() {

TempStr syncPathTemp = str::DupTemp(syncFilePath.Get());
Repeat:
WCHAR* ws = ToWStrTemp(syncPathTemp);
TempWStr ws = ToWStrTemp(syncPathTemp);
AutoFreeStr pathAnsi = strconv::WstrToAnsi(ws);
scanner = synctex_scanner_new_with_output_file(pathAnsi, nullptr, 1);
if (scanner) {
Expand Down
2 changes: 1 addition & 1 deletion src/SearchAndDDE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ void FindTextOnThread(MainWindow* win, TextSearchDirection direction, const char

// TODO: for https://github.com/sumatrapdfreader/sumatrapdf/issues/2655
char* ReverseTextTemp(char* s) {
WCHAR* ws = ToWStrTemp(s);
TempWStr ws = ToWStrTemp(s);
int n = str::Leni(ws);
for (int i = 0; i < n / 2; i++) {
WCHAR c1 = ws[i];
Expand Down
2 changes: 1 addition & 1 deletion src/SumatraDialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ static INT_PTR CALLBACK Dialog_Settings_Proc(HWND hDlg, UINT msg, WPARAM wp, LPA
cmdLine = detected[0];
}
for (char* s : detected) {
WCHAR* ws = ToWStrTemp(s);
TempWStr ws = ToWStrTemp(s);
// if no existing command was selected then set the user custom command in the combo
ComboBox_AddString(hwndComboBox, ws);
}
Expand Down
69 changes: 35 additions & 34 deletions src/SumatraPDF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2753,7 +2753,7 @@ void CloseWindow(MainWindow* win, bool quitIfLast, bool forceClose) {
}

// returns false if no filter has been appended
static bool AppendFileFilterForDoc(DocController* ctrl, str::WStr& fileFilter) {
static bool AppendFileFilterForDoc(DocController* ctrl, str::Str& fileFilter) {
// TODO: use ctrl->GetDefaultFileExt()
Kind type = nullptr;
if (ctrl->AsFixed()) {
Expand All @@ -2764,32 +2764,32 @@ static bool AppendFileFilterForDoc(DocController* ctrl, str::WStr& fileFilter) {

auto ext = ctrl->GetDefaultFileExt();
if (str::EqI(ext, ".xps")) {
fileFilter.Append(_TR("XPS documents"));
fileFilter.Append(_TRA("XPS documents"));
} else if (type == kindEngineDjVu) {
fileFilter.Append(_TR("DjVu documents"));
fileFilter.Append(_TRA("DjVu documents"));
} else if (type == kindEngineComicBooks) {
fileFilter.Append(_TR("Comic books"));
fileFilter.Append(_TRA("Comic books"));
} else if (type == kindEngineImage) {
WCHAR* extW = ToWStrTemp(ctrl->GetDefaultFileExt() + 1);
fileFilter.AppendFmt(_TR("Image files (*.%s)"), extW);
fileFilter.AppendFmt(_TRA("Image files (*.%s)"), extW);
} else if (type == kindEngineImageDir) {
return false; // only show "All files"
} else if (type == kindEnginePostScript) {
fileFilter.Append(_TR("Postscript documents"));
fileFilter.Append(_TRA("Postscript documents"));
} else if (type == kindEngineChm) {
fileFilter.Append(_TR("CHM documents"));
fileFilter.Append(_TRA("CHM documents"));
} else if (type == kindEngineEpub) {
fileFilter.Append(_TR("EPUB ebooks"));
fileFilter.Append(_TRA("EPUB ebooks"));
} else if (type == kindEngineMobi) {
fileFilter.Append(_TR("Mobi documents"));
fileFilter.Append(_TRA("Mobi documents"));
} else if (type == kindEngineFb2) {
fileFilter.Append(_TR("FictionBook documents"));
fileFilter.Append(_TRA("FictionBook documents"));
} else if (type == kindEnginePdb) {
fileFilter.Append(_TR("PalmDoc documents"));
fileFilter.Append(_TRA("PalmDoc documents"));
} else if (type == kindEngineTxt) {
fileFilter.Append(_TR("Text documents"));
fileFilter.Append(_TRA("Text documents"));
} else {
fileFilter.Append(_TR("PDF documents"));
fileFilter.Append(_TRA("PDF documents"));
}
return true;
}
Expand Down Expand Up @@ -2825,20 +2825,20 @@ static void SaveCurrentFileAs(MainWindow* win) {
return;
}

TempWStr defExt = ToWStrTemp(ctrl->GetDefaultFileExt());
auto defExt = ctrl->GetDefaultFileExt();
// Prepare the file filters (use \1 instead of \0 so that the
// double-zero terminated string isn't cut by the string handling
// methods too early on)
str::WStr fileFilter(256);
str::Str fileFilter(256);
if (AppendFileFilterForDoc(ctrl, fileFilter)) {
fileFilter.AppendFmt(L"\1*%s\1", defExt);
fileFilter.AppendFmt("\1*%s\1", defExt);
}
fileFilter.Append(_TR("All files"));
fileFilter.Append(L"\1*.*\1");
str::TransCharsInPlace(fileFilter.Get(), L"\1", L"\0");
fileFilter.Append(_TRA("All files"));
fileFilter.Append("\1*.*\1");
str::TransCharsInPlace(fileFilter.CStr(), "\1", "\0");

WCHAR dstFileName[MAX_PATH];
auto baseName = path::GetBaseNameTemp(srcFileName);
TempStr baseName = path::GetBaseNameTemp(srcFileName);
str::BufSet(dstFileName, dimof(dstFileName), baseName);
if (str::FindChar(dstFileName, ':')) {
// handle embed-marks (for embedded PDF documents):
Expand All @@ -2854,7 +2854,7 @@ static void SaveCurrentFileAs(MainWindow* win) {
ext = colon;
}
memmove(ext, colon, (str::Len(colon) + 1) * sizeof(WCHAR));
} else if (str::EndsWithI(dstFileName, defExt)) {
} else if (str::EndsWithI(dstFileName, ToWStrTemp(defExt))) {
// Remove the extension so that it can be re-added depending on the chosen filter
int idx = str::Leni(dstFileName) - str::Leni(defExt);
dstFileName[idx] = '\0';
Expand All @@ -2865,13 +2865,13 @@ static void SaveCurrentFileAs(MainWindow* win) {
ofn.hwndOwner = win->hwndFrame;
ofn.lpstrFile = dstFileName;
ofn.nMaxFile = dimof(dstFileName);
ofn.lpstrFilter = fileFilter.Get();
ofn.lpstrFilter = ToWStrTemp(fileFilter);
ofn.nFilterIndex = 1;
// defExt can be null, we want to skip '.'
if (str::Leni(defExt) > 0 && defExt[0] == L'.') {
defExt++;
}
ofn.lpstrDefExt = defExt;
ofn.lpstrDefExt = ToWStrTemp(defExt);
ofn.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;
// note: explicitly not setting lpstrInitialDir so that the OS
// picks a reasonable default (in particular, we don't want this
Expand All @@ -2885,9 +2885,8 @@ static void SaveCurrentFileAs(MainWindow* win) {
TempStr realDstFileName = ToUtf8Temp(dstFileName);

// Make sure that the file has a valid extension
if (!str::EndsWithI(dstFileName, defExt)) {
TempWStr s = str::JoinTemp(dstFileName, defExt);
realDstFileName = ToUtf8(dstFileName);
if (!str::EndsWithI(realDstFileName, defExt)) {
realDstFileName = str::JoinTemp(realDstFileName, defExt);
}

logf("Saving '%s' to '%s'\n", srcFileName, realDstFileName);
Expand Down Expand Up @@ -2984,19 +2983,20 @@ static void RenameCurrentFile(MainWindow* win) {
// Prepare the file filters (use \1 instead of \0 so that the
// double-zero terminated string isn't cut by the string handling
// methods too early on)
const WCHAR* defExt = ToWStrTemp(ctrl->GetDefaultFileExt());
str::WStr fileFilter(256);
const char* defExt = ctrl->GetDefaultFileExt();
TempWStr defExtW = ToWStrTemp(defExt);
str::Str fileFilter(256);
bool ok = AppendFileFilterForDoc(ctrl, fileFilter);
CrashIf(!ok);
fileFilter.AppendFmt(L"\1*%s\1", defExt);
str::TransCharsInPlace(fileFilter.Get(), L"\1", L"\0");
fileFilter.AppendFmt("\1*%s\1", defExt);
str::TransCharsInPlace(fileFilter.Get(), "\1", "\0");

WCHAR dstFileName[MAX_PATH];
auto baseName = path::GetBaseNameTemp(srcPath);
str::BufSet(dstFileName, dimof(dstFileName), baseName);
// Remove the extension so that it can be re-added depending on the chosen filter
if (str::EndsWithI(dstFileName, defExt)) {
int idx = str::Leni(dstFileName) - str::Leni(defExt);
if (str::EndsWithI(dstFileName, defExtW)) {
int idx = str::Leni(dstFileName) - str::Leni(defExtW);
dstFileName[idx] = '\0';
}

Expand All @@ -3008,12 +3008,13 @@ static void RenameCurrentFile(MainWindow* win) {
ofn.hwndOwner = win->hwndFrame;
ofn.lpstrFile = dstFileName;
ofn.nMaxFile = dimof(dstFileName);
ofn.lpstrFilter = fileFilter.Get();
ofn.lpstrFilter = ToWStrTemp(fileFilter);
;
ofn.nFilterIndex = 1;
// note: the other two dialogs are named "Open" and "Save As"
ofn.lpstrTitle = _TR("Rename To");
ofn.lpstrInitialDir = initDir;
ofn.lpstrDefExt = defExt + 1;
ofn.lpstrDefExt = defExtW + 1;
ofn.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY;

ok = GetSaveFileNameW(&ofn);
Expand Down
2 changes: 1 addition & 1 deletion src/SumatraProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ static void GetProps(DocController* ctrl, PropertiesLayout* layoutData, bool ext
if (IsUIRightToLeft() && IsWindowsVistaOrGreater()) {
// ensure that the size remains ungarbled left-to-right
// (note: XP doesn't know about \u202A...\u202C)
WCHAR* ws = ToWStrTemp(strTemp);
TempWStr ws = ToWStrTemp(strTemp);
ws = str::Format(L"\u202A%s\u202C", ws);
strTemp = ToUtf8Temp(ws);
str::Free(ws);
Expand Down
2 changes: 1 addition & 1 deletion src/TableOfContents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ static void GetLeftRightCounts(TocItem* node, int& l2r, int& r2l) {
return;
}
if (node->title) {
WCHAR* ws = ToWStrTemp(node->title);
TempWStr ws = ToWStrTemp(node->title);
for (const WCHAR* c = ws; *c; c++) {
if (isLeftToRightChar(*c)) {
l2r++;
Expand Down
2 changes: 1 addition & 1 deletion src/scratch.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Ideas for refactorings:

- change all size_t => int
- remove all use of _TR( : 61 matches (was 74, 94, 141)
- remove all use of _TR( : 48 matches (was 61, 74, 94, 141)
- change use of Str::Get() => Str::CStr() (or maybe make it Str() as in StrSpan?)

------------------------------------------------------------------
Expand Down
6 changes: 5 additions & 1 deletion src/utils/BaseUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -667,10 +667,14 @@ defer { fclose(f); };
defer { instance->Release(); };
*/

// exists just to mark the intent, needed by both StrUtil.h and TempAllocator.h
using TempStr = char*;
using TempWStr = WCHAR*;

#include "GeomUtil.h"
#include "Vec.h"
#include "TempAllocator.h"
#include "StrUtil.h"
#include "TempAllocator.h"
#include "StrVec.h"
#include "StrconvUtil.h"
#include "Scoped.h"
Expand Down
2 changes: 1 addition & 1 deletion src/utils/FileUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ bool Match(const char* path, const char* filter) {
}

bool IsAbsolute(const char* path) {
WCHAR* ws = ToWStrTemp(path);
TempWStr ws = ToWStrTemp(path);
return !PathIsRelativeW(ws);
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/StrconvUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ char* WstrToAnsi(const WCHAR* src) {
}

char* Utf8ToAnsi(const char* s) {
WCHAR* ws = ToWStrTemp(s);
TempWStr ws = ToWStrTemp(s);
return WstrToAnsi(ws);
}

Expand Down
10 changes: 10 additions & 0 deletions src/utils/TempAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,13 @@ TempWStr ToWStrTemp(const char* s, size_t cb) {
}
return strconv::Utf8ToWstr(s, cb, GetTempAllocator());
}

// handles embedded 0 in the string
TempWStr ToWStrTemp(const str::Str& str) {
if (str.IsEmpty()) {
return nullptr;
}
char* s = str.CStr();
size_t cb = str.Size();
return strconv::Utf8ToWstr(s, cb, GetTempAllocator());
}
5 changes: 1 addition & 4 deletions src/utils/TempAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ FORCEINLINE T* AllocArrayTemp(size_t n) {
return (T*)Allocator::AllocZero(a, n * sizeof(T));
}

// exists just to mark the intentA
using TempStr = char*;
using TempWStr = WCHAR*;

namespace str {
TempStr DupTemp(const char* s, size_t cb = (size_t)-1);
TempWStr DupTemp(const WCHAR* s, size_t cch = (size_t)-1);
Expand All @@ -33,3 +29,4 @@ TempStr FormatTemp(const char* fmt, ...);

TempStr ToUtf8Temp(const WCHAR* s, size_t cch = (size_t)-1);
TempWStr ToWStrTemp(const char* s, size_t cb = (size_t)-1);
TempWStr ToWStrTemp(const str::Str& s);
2 changes: 1 addition & 1 deletion src/utils/ThreadUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ typedef struct tagTHREADNAME_INFO {
#pragma warning(disable : 6322) // silence /analyze: Empty _except block
void SetThreadName(const char* threadName, DWORD threadId) {
if (DynSetThreadDescription && threadId == 0) {
WCHAR* ws = ToWStrTemp(threadName);
TempWStr ws = ToWStrTemp(threadName);
DynSetThreadDescription(GetCurrentThread(), ws);
return;
}
Expand Down
12 changes: 6 additions & 6 deletions src/utils/WinUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ bool LoggedDeleteRegValue(HKEY hkey, const char* keyName, const char* valName) {
}

HRESULT CLSIDFromString(const char* lpsz, LPCLSID pclsid) {
WCHAR* ws = ToWStrTemp(lpsz);
TempWStr ws = ToWStrTemp(lpsz);
return CLSIDFromString(ws, pclsid);
}

Expand Down Expand Up @@ -1340,7 +1340,7 @@ static bool CopyOrAppendTextToClipboard(const WCHAR* text, bool appendOnly) {
}

static bool CopyOrAppendTextToClipboard(const char* s, bool appendOnly) {
WCHAR* ws = ToWStrTemp(s);
TempWStr ws = ToWStrTemp(s);
return CopyOrAppendTextToClipboard(ws, appendOnly);
}

Expand Down Expand Up @@ -1730,7 +1730,7 @@ void MenuSetText(HMENU m, int id, const WCHAR* s) {
}

void MenuSetText(HMENU m, int id, const char* s) {
WCHAR* ws = ToWStrTemp(s);
TempWStr ws = ToWStrTemp(s);
MenuSetText(m, id, ws);
}

Expand Down Expand Up @@ -2670,7 +2670,7 @@ void HwndSetText(HWND hwnd, const char* sv) {
SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)L"");
return;
}
WCHAR* ws = ToWStrTemp(sv);
TempWStr ws = ToWStrTemp(sv);
SendMessageW(hwnd, WM_SETTEXT, 0, (LPARAM)ws);
}

Expand Down Expand Up @@ -2837,7 +2837,7 @@ int HdcDrawText(HDC hdc, const char* s, RECT* r, uint fmt, HFONT font) {
if (!s) {
return 0;
}
WCHAR* ws = ToWStrTemp(s);
TempWStr ws = ToWStrTemp(s);
if (!ws) {
return 0;
}
Expand All @@ -2860,7 +2860,7 @@ int HdcDrawText(HDC hdc, const char* s, const Point& pos, uint fmt, HFONT font)
// uses the same logic as HdcDrawText
Size HdcMeasureText(HDC hdc, const char* s, uint fmt, HFONT font) {
fmt |= DT_CALCRECT;
WCHAR* ws = ToWStrTemp(s);
TempWStr ws = ToWStrTemp(s);
if (!ws) {
return {};
}
Expand Down

0 comments on commit 1695022

Please sign in to comment.