diff options
author | John Ankarström <john@ankarstrom.se> | 2022-07-16 01:53:49 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-07-16 01:53:49 +0200 |
commit | 2fed063bf167dcb8f900c4a1f11b0a02ae115d16 (patch) | |
tree | 99b41b649fb6a1b6683bd90cd38ab894dd526ce3 /c/common.cpp | |
parent | 84c3dd3587e219caa80adc2070f0e9fe004c27bc (diff) | |
download | EpisodeBrowser-2fed063bf167dcb8f900c4a1f11b0a02ae115d16.tar.gz |
Rewrite TszFromSz as TsmFromSz.
I.e. using std::basic_string<TCHAR> instead of TCHAR *. This removes
all unmanaged frees.
Diffstat (limited to 'c/common.cpp')
-rw-r--r-- | c/common.cpp | 18 |
1 files changed, 6 insertions, 12 deletions
diff --git a/c/common.cpp b/c/common.cpp index 1fcff31..c7ea873 100644 --- a/c/common.cpp +++ b/c/common.cpp @@ -6,26 +6,20 @@ #include "common.h" /* Convert normal string to TSTR using given codepage. */ -TCHAR *TszFromSz(const char *sz, int iCp) +std::basic_string<TCHAR> TsmFromSz(const char *sz, int iCp) { - TCHAR *tsz; - #ifdef UNICODE int cbMultiByte, cchWideChar; cbMultiByte = strlen(sz)+1; cchWideChar = MultiByteToWideChar(iCp, 0, sz, cbMultiByte, NULL, 0); - tsz = (TCHAR *)malloc(cchWideChar*sizeof(WCHAR)); - if (!tsz) return NULL; - if (!MultiByteToWideChar(iCp, 0, sz, cbMultiByte, tsz, cchWideChar)) - return NULL; + std::wstring wsm(cchWideChar, 0); + if (!MultiByteToWideChar(iCp, 0, sz, cbMultiByte, wsm.data(), cchWideChar)) + throw Win32Error(GetLastError()); + return wsm; #else - tsz = malloc(strlen(sz)+1); - if (!tsz) return NULL; - strcpy(tsz, sz); + return std::string(sz); #endif - - return tsz; } Win32Error::Win32Error(DWORD dwErr) |