diff options
author | John Ankarström <john@ankarstrom.se> | 2022-07-26 19:41:11 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-07-26 19:41:11 +0200 |
commit | b966497200b47ca5efb3a5853891ea4590927371 (patch) | |
tree | 45670cff2c4682f0d0c1b1e33c1dddadfe8d58d7 | |
parent | 547cbe578dcdb5aa5cfdac1cdb327f92bad21219 (diff) | |
download | EpisodeBrowser-b966497200b47ca5efb3a5853891ea4590927371.tar.gz |
Use 's' instead of 'str' for as prefix for managed strings.
-rw-r--r-- | README | 11 | ||||
-rw-r--r-- | c/common.cpp | 8 | ||||
-rw-r--r-- | c/common.h | 2 | ||||
-rw-r--r-- | c/datalistview.cpp | 10 | ||||
-rw-r--r-- | c/episodelistview.cpp | 20 | ||||
-rw-r--r-- | c/main.cpp | 8 | ||||
-rw-r--r-- | c/pl.cpp | 4 | ||||
-rw-r--r-- | c/pl.h | 4 |
8 files changed, 32 insertions, 35 deletions
@@ -70,12 +70,9 @@ Following is a summary of some coding conventions used in the project. - dw = DWORD - lvi = LVITEM - sz = unmanaged, zero-terminated narrow string (char*) - - wsz = ... wide string (wchar_t*) - - tsz = ... tstring (TCHAR*) - - str = managed narrow string (std::string) - - wstr = ... (std::wstring) - - tstr = ... (std::basic_string<TCHAR>) - - bstr = ... (std::basic_string<T> of any type T) + - wsz = unmanaged, zero-terminated wide string (wchar_t*) + - s = managed narrow string (std::string) + - ws = managed wide string (std::wstring) The list above is non-exhaustive. Variables whose type is unknown (in templates) do not need prefixes. Some very common self-explanatory @@ -99,7 +96,7 @@ For example, prefer... - LVITEM* over LPLVITEM - int over INT - DWORD over unsigned long - (when interacting with the Windows API) + (but only when interacting with Windows) Note... diff --git a/c/common.cpp b/c/common.cpp index ca8dfac..bc2d4af 100644 --- a/c/common.cpp +++ b/c/common.cpp @@ -56,14 +56,14 @@ Library::~Library() } /* Convert narrow unmanaged string to wide managed string. */ -std::wstring WstrFromSz(const char* sz, int iCp) +std::wstring WsFromSz(const char* sz, int iCp) { int cbMultiByte = strlen(sz)+1; int cchWideChar = MultiByteToWideChar(iCp, 0, sz, cbMultiByte, NULL, 0); - std::wstring wstr(cchWideChar, 0); - if (!MultiByteToWideChar(iCp, 0, sz, cbMultiByte, wstr.data(), cchWideChar)) + std::wstring ws(cchWideChar, 0); + if (!MultiByteToWideChar(iCp, 0, sz, cbMultiByte, ws.data(), cchWideChar)) throw Win32Error(GetLastError()); - return wstr; + return ws; } /* Move message box to center of main window. */ @@ -11,7 +11,7 @@ #define WA "A" #endif -std::wstring WstrFromSz(const char* sz, int iCp = CP_UTF8); +std::wstring WsFromSz(const char* sz, int iCp = CP_UTF8); int EBMessageBox(const wchar_t* wszText, const wchar_t* wszCaption, unsigned uType); struct Win32Error : public std::exception diff --git a/c/datalistview.cpp b/c/datalistview.cpp index e64f89a..a091ace 100644 --- a/c/datalistview.cpp +++ b/c/datalistview.cpp @@ -56,21 +56,21 @@ void DataListView::ShowEpisode(const int iEpisode) Query q (NULL, PL_predicate("episode_datum", iArity, "episode_data"), t); for (int i = 0; q.NextSolution(); i++) { - std::wstring wstrKey; - std::wstring wstrValue; + std::wstring wsKey; + std::wstring wsValue; - if (!(PlGet(t+1, &wstrKey) && PlGet(t+2, &wstrValue))) + if (!(PlGet(t+1, &wsKey) && PlGet(t+2, &wsValue))) continue; lviKey.mask = LVIF_TEXT; lviKey.iItem = i; lviKey.iSubItem = 0; - lviKey.pszText = wstrKey.data(); + lviKey.pszText = wsKey.data(); ListView_InsertItem(hWnd, &lviKey); lviValue.iItem = i; lviValue.iSubItem = 1; - lviValue.pszText = wstrValue.data(); + lviValue.pszText = wsValue.data(); ListView_SetItem(hWnd, &lviValue); } diff --git a/c/episodelistview.cpp b/c/episodelistview.cpp index 49e59d1..5aa8850 100644 --- a/c/episodelistview.cpp +++ b/c/episodelistview.cpp @@ -281,16 +281,16 @@ int CALLBACK EpisodeListView::SortProc(const LPARAM iItem1, const LPARAM iItem2, case ELVSITITLE: { Mark m; - char* sz1,* sz2; + std::wstring ws1, ws2; int cch, cch1, cch2; - if (!Pl("episode_data","episode_title",lvi1.lParam,&sz1)) + if (!Pl("episode_data","episode_title",lvi1.lParam,&ws1)) return 0; - if (!Pl("episode_data","episode_title",lvi2.lParam,&sz2)) + if (!Pl("episode_data","episode_title",lvi2.lParam,&ws2)) return 0; - cch1 = strlen(sz1); - cch2 = strlen(sz2); + cch1 = wcslen(ws1.c_str()); + cch2 = wcslen(ws2.c_str()); cch = cch1 > cch2? cch2: cch1; - return iOrder*_strnicmp(sz1, sz2, cch); + return iOrder*_wcsnicmp(ws1.c_str(), ws2.c_str(), cch); } default: return 0; @@ -429,12 +429,12 @@ void EpisodeListView::Update() /* Update episode name and rating. */ void EpisodeListView::UpdateItem(const LVITEM* const pLvi) { - std::wstring wstrName; - if (!Pl("episode_data","episode_title",pLvi->lParam,&wstrName)) { + std::wstring wsName; + if (!Pl("episode_data","episode_title",pLvi->lParam,&wsName)) { if (!Pl("episode_data","update_episode_data")) goto r; - if (!Pl("episode_data","episode_title",pLvi->lParam,&wstrName)) goto r; + if (!Pl("episode_data","episode_title",pLvi->lParam,&wsName)) goto r; } - ListView_SetItemText(hWnd, pLvi->iItem, ELVSITITLE, wstrName.data()); + ListView_SetItemText(hWnd, pLvi->iItem, ELVSITITLE, wsName.data()); int iRating; r: if (!Pl("episode_data","episode_rating",pLvi->lParam,&iRating)) { @@ -65,15 +65,15 @@ void OnTerminate() noexcept try { std::rethrow_exception(std::current_exception()); } catch (const term_t& t) { - std::wstring wstr; - if (PlString(t, &wstr)) - TerminateMsg(L"a Prolog exception", wstr.c_str()); + std::wstring ws; + if (PlString(t, &ws)) + TerminateMsg(L"a Prolog exception", ws.c_str()); else TerminateMsg(L"a Prolog exception", NULL); } catch (const Win32Error& e) { TerminateMsg(L"a Windows error", e.WhatW()); } catch (const std::exception& e) { - TerminateMsg(L"an exception", WstrFromSz(e.what()).c_str()); + TerminateMsg(L"an exception", WsFromSz(e.what()).c_str()); } catch (...) { TerminateMsg(L"an exception", NULL); } @@ -78,10 +78,10 @@ int Query::NextSolution() } /* Convert Prolog term to wide characters. */ -int PlString(const term_t t, std::wstring* const pWstr, const int iFlags) +int PlString(const term_t t, std::wstring* const pWs, const int iFlags) { char* sz; int r = PL_get_chars(t, &sz, iFlags); - if (r) *pWstr = WstrFromSz(sz); + if (r) *pWs = WsFromSz(sz); return r; } @@ -7,7 +7,7 @@ #include "common.h" -int PlString(term_t t, std::wstring* pWstr, int iFlags = CVT_WRITE); +int PlString(term_t t, std::wstring* pWs, int iFlags = CVT_WRITE); struct Frame { @@ -76,7 +76,7 @@ inline int PlGet(term_t t, std::wstring* x) { Mark m; char* sz; if (!PlGet(t, &sz)) return 0; - *x = WstrFromSz(sz); + *x = WsFromSz(sz); return 1; /* or catch potential exception from BstrFromSz? */ } |