summaryrefslogtreecommitdiff
path: root/tipper/subst.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tipper/subst.cpp')
-rw-r--r--tipper/subst.cpp93
1 files changed, 29 insertions, 64 deletions
diff --git a/tipper/subst.cpp b/tipper/subst.cpp
index 7f43b51..b07f567 100644
--- a/tipper/subst.cpp
+++ b/tipper/subst.cpp
@@ -1,29 +1,32 @@
#include "common.h"
#include "subst.h"
#include <time.h>
+#include "str_utils.h"
-void StripBBCodesInPlace(wchar_t *text) {
+void StripBBCodesInPlace(TCHAR *text) {
if(!DBGetContactSettingByte(0, MODULE, "StripBBCodes", 1))
return;
+ if(text == 0) return;
+
int read = 0, write = 0;
- int len = wcslen(text);
+ int len = _tcslen(text);
while(read <= len) { // copy terminating null too
- while(read <= len && text[read] != L'[') {
+ while(read <= len && text[read] != _T('[')) {
if(text[read] != text[write]) text[write] = text[read];
read++; write++;
}
if(read > len) break;
- if(len - read >= 3 && (wcsnicmp(text + read, L"[b]", 3) == 0 || wcsnicmp(text + read, L"[i]", 3) == 0))
+ if(len - read >= 3 && (_tcsnicmp(text + read, _T("[b]"), 3) == 0 || _tcsnicmp(text + read, _T("[i]"), 3) == 0))
read += 3;
- else if(len - read >= 4 && (wcsnicmp(text + read, L"[/b]", 4) == 0 || wcsnicmp(text + read, L"[/i]", 4) == 0))
+ else if(len - read >= 4 && (_tcsnicmp(text + read, _T("[/b]"), 4) == 0 || _tcsnicmp(text + read, _T("[/i]"), 4) == 0))
read += 4;
- else if(len - read >= 6 && (wcsnicmp(text + read, L"[color", 6) == 0)) {
+ else if(len - read >= 6 && (_tcsnicmp(text + read, _T("[color"), 6) == 0)) {
while(read < len && text[read] != L']') read++;
read++;// skip the ']'
- } else if(len - read >= 8 && (wcsnicmp(text + read, L"[/color]", 8) == 0))
+ } else if(len - read >= 8 && (_tcsnicmp(text + read, _T("[/color]"), 8) == 0))
read += 8;
else {
if(text[read] != text[write]) text[write] = text[read];
@@ -56,7 +59,7 @@ void format_timestamp(DWORD ts, char *format, TCHAR *buff, int bufflen) {
DBTIMETOSTRINGT dbt = {0};
dbt.cbDest = bufflen;
dbt.szDest = buff;
- MultiByteToWideChar(code_page, 0, format, -1, form, 16);
+ a2t(format, form, 16);
dbt.szFormat = form;
CallService(MS_DB_TIME_TIMESTAMPTOSTRINGT, (WPARAM)ts, (LPARAM)&dbt);
} else {
@@ -66,7 +69,7 @@ void format_timestamp(DWORD ts, char *format, TCHAR *buff, int bufflen) {
dbt.szDest = buffA;
dbt.szFormat = format;
CallService(MS_DB_TIME_TIMESTAMPTOSTRING, (WPARAM)ts, (LPARAM)&dbt);
- MultiByteToWideChar(code_page, 0, buffA, -1, buff, bufflen);
+ a2t(buffA, buff, bufflen);
}
}
@@ -88,8 +91,7 @@ bool uid(HANDLE hContact, TCHAR *buff, int bufflen) {
_ltot(ci.dVal, buff, 10);
break;
case CNFT_ASCIIZ:
- if(unicode_system) _tcsncpy(buff, ci.pszVal, bufflen);
- else MultiByteToWideChar(code_page, 0, (char *)ci.pszVal, -1, buff, bufflen);
+ a2t(ci.pszVal, buff, bufflen);
break;
default:
return false;
@@ -119,40 +121,18 @@ TCHAR *GetLastMessageText(HANDLE hContact) {
if(dbei.cbBlob == 0 || dbei.pBlob == 0) return 0;
- wchar_t *msg;
+ TCHAR *msg = 0;
unsigned int msglen = strlen((char *)dbei.pBlob) + 1;
- // here we detect the double-zero wide char zero terminator - in case of messages that are not unicode but have
- // other data after the message (e.g. metacontact copied messages with source identifier)
- bool dz = false;
- for(unsigned int i = msglen; i < dbei.cbBlob; i++) {
- if(dbei.pBlob[i] == 0 && dbei.pBlob[i - 1] == 0) { // safe since msglen + 1 above
- dz = true;
- break;
- }
- }
-
// does blob contain unicode message?
- if(msglen < dbei.cbBlob && dz && wcslen((wchar_t *)(&dbei.pBlob[msglen]))) {
- // yes
- msg = (wchar_t *)(&dbei.pBlob[msglen]);
- wchar_t *ret = wcsdup(msg);
- StripBBCodesInPlace(ret);
- return ret;
+ if(msglen < dbei.cbBlob) {
+ msg = w2t((wchar_t *)(&dbei.pBlob[msglen]));
} else {
- // no, convert to unciode (allocate stack memory);
- int size = MultiByteToWideChar(code_page, 0, (char *) dbei.pBlob, -1, 0, 0);
- if(size > 0) {
- msg = (wchar_t *) malloc(sizeof(wchar_t) * size);
- MultiByteToWideChar(code_page, 0, (char *) dbei.pBlob, -1, msg, size);
- } else {
- msg = (wchar_t *) malloc(sizeof(wchar_t) * (wcslen(TranslateT("Empty message")) + 1));
- wcscpy(msg, TranslateT("Empty message"));
- }
- StripBBCodesInPlace(msg);
- return msg;
+ msg = a2t((char *)dbei.pBlob);
}
+ StripBBCodesInPlace(msg);
+ return msg;
}
return 0;
@@ -186,7 +166,7 @@ bool GetSysSubstText(HANDLE hContact, TCHAR *raw_spec, TCHAR *buff, int bufflen)
} else if (!_tcscmp(raw_spec, _T("proto"))) {
char *szProto = (char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0);
if (szProto){
- MultiByteToWideChar(code_page, 0, szProto, -1, buff, bufflen);
+ a2t(szProto, buff, bufflen);
return true;
}
} else if (!_tcscmp(raw_spec, _T("uidname"))) {
@@ -194,7 +174,7 @@ bool GetSysSubstText(HANDLE hContact, TCHAR *raw_spec, TCHAR *buff, int bufflen)
if (szProto){
char *szUniqueId = (char*)CallProtoService(szProto, PS_GETCAPS, PFLAG_UNIQUEIDTEXT, 0);
if(szUniqueId) {
- MultiByteToWideChar(code_page, 0, szUniqueId, -1, buff, bufflen);
+ a2t(szUniqueId, buff, bufflen);
return true;
}
}
@@ -216,24 +196,9 @@ bool GetSysSubstText(HANDLE hContact, TCHAR *raw_spec, TCHAR *buff, int bufflen)
HANDLE hSubContact = (HANDLE)CallService(MS_MC_GETMOSTONLINECONTACT, (WPARAM)hContact, 0);
if(!hSubContact) return false;
- if(unicode_system) { // get unicode name if possible, else get ascii and convert
- wchar_t *swzCDN = (wchar_t *) CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hSubContact, GCDNF_UNICODE);
- if(swzCDN) {
- wcsncpy(buff, swzCDN, bufflen);
- return true;
- }
- } else {
- char *szCDN = (char *)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hSubContact, 0);
- if(szCDN) {
- int size = MultiByteToWideChar(code_page, 0, (char *) szCDN, -1, 0, 0);
- if(size > 0) {
- MultiByteToWideChar(code_page, 0, (char *) szCDN, -1, buff, bufflen);
- return true;
- }
- }
- }
- return false;
-
+ TCHAR *stzCDN = (TCHAR *) CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hSubContact, GCDNF_TCHAR);
+ if(stzCDN) _tcsncpy(buff, stzCDN, bufflen);
+ return true;
} else if (!_tcscmp(raw_spec, _T("meta_subuid"))){
HANDLE hSubContact = (HANDLE)CallService(MS_MC_GETMOSTONLINECONTACT, (WPARAM)hContact, 0);
if(!hSubContact) return false;
@@ -245,7 +210,7 @@ bool GetSysSubstText(HANDLE hContact, TCHAR *raw_spec, TCHAR *buff, int bufflen)
char *szProto = (char*)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hSubContact, 0);
if (szProto){
- MultiByteToWideChar(code_page, 0, szProto, -1, buff, bufflen);
+ a2t(szProto, buff, bufflen);
return true;
}
} else if (!_tcscmp(raw_spec, _T("last_msg_time"))) {
@@ -373,7 +338,7 @@ bool ApplySubst(HANDLE hContact, const TCHAR *source, TCHAR *dest, int dest_len)
}
char sproto[256], *cp;
- WideCharToMultiByte(code_page, 0, p, -1, sproto, 256, 0, 0);
+ t2a(p, sproto, 256);
cp = (char *)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0);
if(cp == 0 || (negate ? stricmp(cp, sproto) == 0 : stricmp(cp, sproto) != 0))
goto empty;
@@ -395,7 +360,7 @@ bool ApplySubst(HANDLE hContact, const TCHAR *source, TCHAR *dest, int dest_len)
// get subst text
if(v > 4 && _tcsncmp(vname, _T("raw:"), 4) == 0) { // raw db substitution
char raw_spec[LABEL_LEN];
- WideCharToMultiByte(code_page, 0, &vname[4], -1, raw_spec, LABEL_LEN, 0, 0);
+ t2a(&vname[4], raw_spec, LABEL_LEN);
subst = GetRawSubstText(hContact, raw_spec, rep, VALUE_LEN);
} else if(v > 4 && _tcsncmp(vname, _T("sys:"), 4) == 0) { // 'system' substitution
subst = GetSysSubstText(hContact, &vname[4], rep, VALUE_LEN);
@@ -415,11 +380,11 @@ bool ApplySubst(HANDLE hContact, const TCHAR *source, TCHAR *dest, int dest_len)
if(subst) {
int rep_len = _tcslen(rep);
- wcsncpy(&dest[di], rep, min(rep_len, dest_len - di));
+ _tcsncpy(&dest[di], rep, min(rep_len, dest_len - di));
di += rep_len - 1; // -1 because we inc at bottom of loop
} else if(alt_subst) {
int alt_len = _tcslen(alt);
- wcsncpy(&dest[di], alt, min(alt_len, dest_len - di));
+ _tcsncpy(&dest[di], alt, min(alt_len, dest_len - di));
di += alt_len - 1; // -1 because we inc at bottom of loop
} else
goto empty; // empty value