aboutsummaryrefslogtreecommitdiff
path: root/c
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
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')
-rw-r--r--c/common.cpp18
-rw-r--r--c/common.h2
-rw-r--r--c/datalistview.cpp14
-rw-r--r--c/episodelistview.cpp21
-rw-r--r--c/main.cpp5
-rw-r--r--c/pl.cpp15
-rw-r--r--c/pl.h2
7 files changed, 37 insertions, 40 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)
diff --git a/c/common.h b/c/common.h
index a6bba23..738368f 100644
--- a/c/common.h
+++ b/c/common.h
@@ -6,7 +6,7 @@
#include <stdexcept>
#include <windows.h>
-TCHAR *TszFromSz(const char *, int);
+std::basic_string<TCHAR> TsmFromSz(const char *, int);
struct Win32Error : public std::exception
{
Win32Error(DWORD);
diff --git a/c/datalistview.cpp b/c/datalistview.cpp
index 2920c0c..2f184dc 100644
--- a/c/datalistview.cpp
+++ b/c/datalistview.cpp
@@ -46,29 +46,23 @@ void DataListView::ShowEpisode(int iEpisode)
for (int i = 0; PL_next_solution(q); i++) {
char *szKey;
char *szValue;
- TCHAR *tszKey, *tszValue;
if (!(PL_get_atom_chars(t+1,&szKey) && PL_get_atom_chars(t+2,&szValue)))
continue;
- tszKey = TszFromSz(szKey, CP_UTF8);
- if (!tszKey) continue;
- tszValue = TszFromSz(szValue, CP_UTF8);
- if (!tszValue) goto c;
+ std::basic_string<TCHAR> tsmKey = TsmFromSz(szKey, CP_UTF8);
+ std::basic_string<TCHAR> tsmValue = TsmFromSz(szValue, CP_UTF8);
lviKey.mask = LVIF_TEXT;
lviKey.iItem = i;
lviKey.iSubItem = 0;
- lviKey.pszText = tszKey;
+ lviKey.pszText = tsmKey.data();
ListView_InsertItem(m_hWnd, &lviKey);
lviValue.iItem = i;
lviValue.iSubItem = 1;
- lviValue.pszText = tszValue;
+ lviValue.pszText = tsmValue.data();
ListView_SetItem(m_hWnd, &lviValue);
-
- free(tszValue);
- c: free(tszKey);
}
PL_cut_query(q);
diff --git a/c/episodelistview.cpp b/c/episodelistview.cpp
index 1a41986..a802f90 100644
--- a/c/episodelistview.cpp
+++ b/c/episodelistview.cpp
@@ -287,11 +287,10 @@ void EpisodeListView::Update()
extern char g_szLimitScreenwriter[];
extern int g_bViewTVOriginal, g_bViewWatched;
- if (g_szLimitScreenwriter[0])
- if (!Pl("episode_data","episode_datum",
- iEp,"Screenwriter",g_szLimitScreenwriter))
- continue;
-
+ if (g_szLimitScreenwriter[0]
+ && !Pl("episode_data","episode_datum",iEp,"Screenwriter",g_szLimitScreenwriter))
+ continue;
+
if (!g_bViewWatched)
if (Pl("track_episodes","watched",iEp)) continue;
@@ -356,30 +355,26 @@ void EpisodeListView::Update()
/* Update episode name and rating. */
void EpisodeListView::UpdateItem(LPLVITEM lpLvi)
{
+ std::basic_string<TCHAR> tsmName;
char *szName;
int iRating;
- TCHAR *tszName;
static TCHAR tszRating[3];
- tszName = NULL;
if (!Pl("episode_data","episode_title",lpLvi->lParam,&szName)) {
if (!Pl("episode_data","update_episode_data")) goto r;
if (!Pl("episode_data","episode_title",lpLvi->lParam,&szName))
goto r;
}
- tszName = TszFromSz(szName, CP_UTF8);
- if (tszName)
- ListView_SetItemText(m_hWnd, lpLvi->iItem, ELVSITITLE, tszName);
+ tsmName = TsmFromSz(szName, CP_UTF8);
+ ListView_SetItemText(m_hWnd, lpLvi->iItem, ELVSITITLE, tsmName.data());
r: if (!Pl("episode_data","episode_rating",lpLvi->lParam,&iRating)) {
ListView_SetItemText(m_hWnd, lpLvi->iItem, ELVSIRATING, TEXT(""));
- goto f;
+ return;
}
_stprintf_s(tszRating, sizeof(tszRating), TEXT("%d"), iRating);
ListView_SetItemText(m_hWnd, lpLvi->iItem, ELVSIRATING, tszRating);
-
-f: if (tszName) free(tszName);
}
LRESULT CALLBACK EpisodeListView::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
diff --git a/c/main.cpp b/c/main.cpp
index 6ed7ac1..c1d05a6 100644
--- a/c/main.cpp
+++ b/c/main.cpp
@@ -134,12 +134,9 @@ void OnTerminate()
try {
std::rethrow_exception(std::current_exception());
} catch (term_t &t) {
- char *sz;
TCHAR *tsz;
- /* TODO: PL_get_wchars */
- if (PL_get_chars(t, &sz, CVT_WRITE) && (tsz = TszFromSz(sz, CP_UTF8))) {
+ if (PL_get_tchars(t, &tsz, CVT_WRITE)) {
MessageBox(NULL, tsz, TEXT("Fatal Error"), MB_ICONERROR);
- free(tsz);
} else
MessageBoxA(NULL, "The program was terminated due to a Prolog exception.",
"Fatal Error", MB_ICONERROR);
diff --git a/c/pl.cpp b/c/pl.cpp
index ace83e7..c944547 100644
--- a/c/pl.cpp
+++ b/c/pl.cpp
@@ -1,3 +1,4 @@
+#include <windows.h>
#include <SWI-Prolog.h>
#include "pl.h"
@@ -33,6 +34,20 @@ int Query::NextSolution()
return 0;
}
+int PL_get_tchars(term_t t, TCHAR **lpTsz, int iFlags)
+{
+#ifdef UNICODE
+ size_t sizLen;
+ if (!PL_get_wchars(t, &sizLen, lpTsz, iFlags))
+ return 0;
+ return sizLen;
+#else
+ if (!PL_get_chars(t, lpTsz, iFlags))
+ return 0;
+ return -1;
+#endif
+}
+
int Plx(const char *szMod, const char *szPred)
{
term_t t = PL_new_term_refs(0);
diff --git a/c/pl.h b/c/pl.h
index f0edfb8..ad13dcc 100644
--- a/c/pl.h
+++ b/c/pl.h
@@ -1,8 +1,10 @@
#ifndef PL_H
#define PL_H
+#include <windows.h>
#include <SWI-Prolog.h>
+int PL_get_tchars(term_t, TCHAR **, int);
int Plx(const char *, const char *);
struct Query
{