aboutsummaryrefslogtreecommitdiff
path: root/c/common.cpp
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-07-16 01:53:49 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-07-16 01:53:49 +0200
commit2fed063bf167dcb8f900c4a1f11b0a02ae115d16 (patch)
tree99b41b649fb6a1b6683bd90cd38ab894dd526ce3 /c/common.cpp
parent84c3dd3587e219caa80adc2070f0e9fe004c27bc (diff)
downloadEpisodeBrowser-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.cpp18
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)