diff options
author | George Hazan <george.hazan@gmail.com> | 2023-08-10 17:12:19 +0300 |
---|---|---|
committer | George Hazan <george.hazan@gmail.com> | 2023-08-10 17:12:19 +0300 |
commit | 271ae145aa8515edb34fb9b5407ada242928dd66 (patch) | |
tree | a6ce64d145a1fcce3ff17158b70bfecfa8e1a797 | |
parent | c6cfee9f2c096278dae31d241cc814d10fee806e (diff) |
NS: another memleak fixed
-rw-r--r-- | plugins/NewStory/src/templates.cpp | 117 | ||||
-rw-r--r-- | plugins/NewStory/src/templates.h | 9 |
2 files changed, 117 insertions, 9 deletions
diff --git a/plugins/NewStory/src/templates.cpp b/plugins/NewStory/src/templates.cpp index 87c3faf9de..cd32c5b9c6 100644 --- a/plugins/NewStory/src/templates.cpp +++ b/plugins/NewStory/src/templates.cpp @@ -10,13 +10,15 @@ wchar_t *months[12] = LPGENW("July"), LPGENW("August"), LPGENW("September"), LPGENW("October"), LPGENW("November"), LPGENW("December") }; +/////////////////////////////////////////////////////////////////////////////// +// Template formatting for options dialog + wchar_t *TplFormatStringEx(int tpl, wchar_t *sztpl, MCONTACT hContact, ItemData *item) { if (tpl < 0 || tpl >= TPL_COUNT || !sztpl) return mir_wstrdup(L""); TemplateVars vars; - memset(&vars, 0, sizeof(vars)); auto &T = templates[tpl]; for (int i = 0; i < TemplateInfo::VF_COUNT; i++) @@ -40,6 +42,109 @@ wchar_t *TplFormatStringEx(int tpl, wchar_t *sztpl, MCONTACT hContact, ItemData return buf; } +/////////////////////////////////////////////////////////////////////////////// +// Template formatting for the control + +static void AppendUnicodeToBuffer(CMStringA &buf, const wchar_t *p, TemplateVars *vars) +{ + for (; *p; p++) { + if (*p == '\r' && p[1] == '\n') { + buf.Append("\\p "); + p++; + } + else if (*p == '\n') { + buf.Append("\\p "); + } + else if (*p == '\t') { + buf.Append("\\tab "); + } + else if (*p == '\\' || *p == '{' || *p == '}') { + buf.AppendChar('\\'); + buf.AppendChar((char)*p); + } + else if (*p == '[') { + if (p[1] == 'c' && p[3] == ']') { + buf.AppendFormat("\\cf%c ", p[2]); + p += 3; + continue; + } + + char *pEnd = ""; + if (p[1] == '/') { + pEnd = "0"; + p++; + } + if (p[1] == 'b' && p[2] == ']') { + buf.AppendFormat("\\b%s ", pEnd); + p += 2; + } + else if (p[1] == 'i' && p[2] == ']') { + buf.AppendFormat("\\i%s ", pEnd); + p += 2; + } + else if (p[1] == 'u' && p[2] == ']') { + buf.AppendFormat("\\ul%s ", pEnd); + p += 2; + } + else if (p[1] == 's' && p[2] == ']') { + buf.AppendFormat("\\strike%s ", pEnd); + p += 2; + } + else if (p[1] == 'c' && p[2] == ']') { + buf.Append("\\cf1 "); + p += 2; + } + else buf.AppendChar('['); + } + else if (*p == '%' && vars) { + wchar_t *var = vars->GetVar((p[1] & 0xff)); + if (var) + AppendUnicodeToBuffer(buf, var, 0); + p++; + } + else if (*p < 128) { + buf.AppendChar((char)*p); + } + else { + buf.AppendFormat("\\u%d ?", *p); + } + } +} + +CMStringA TplFormatRtf(int tpl, MCONTACT hContact, ItemData *item) +{ + if (tpl < 0 || tpl >= TPL_COUNT) + return CMStringA(""); + + auto &T = templates[tpl]; + + TemplateVars vars; + + for (int i = 0; i < TemplateInfo::VF_COUNT; i++) + if (T.vf[i]) + T.vf[i](VFM_VARS, &vars, hContact, item); + + CMStringA buf; + buf.Append("{\\rtf1\\ansi\\deff0"); + + COLORREF cr = GetSysColor(COLOR_WINDOWTEXT); + buf.AppendFormat("{\\colortbl \\red%u\\green%u\\blue%u;", GetRValue(cr), GetGValue(cr), GetBValue(cr)); + cr = g_colorTable[(item->dbe.flags & DBEF_SENT) ? COLOR_OUTNICK : COLOR_INNICK].cl; + buf.AppendFormat("\\colortbl \\red%u\\green%u\\blue%u;}", GetRValue(cr), GetGValue(cr), GetBValue(cr)); + + wchar_t tmp[2] = { 0, 0 }; + + buf.Append("\\par "); + + buf.Append("{\\uc1 "); + AppendUnicodeToBuffer(buf, (T.value != nullptr) ? T.value : T.defvalue, &vars); + buf.Append("}}"); + return buf; +} + +/////////////////////////////////////////////////////////////////////////////// +// Template formatting for copying text + wchar_t *TplFormatString(int tpl, MCONTACT hContact, ItemData *item) { if ((tpl < 0) || (tpl >= TPL_COUNT)) @@ -50,7 +155,6 @@ wchar_t *TplFormatString(int tpl, MCONTACT hContact, ItemData *item) T.value = mir_wstrdup(T.defvalue); TemplateVars vars; - memset(&vars, 0, sizeof(vars)); for (int i = 0; i < TemplateInfo::VF_COUNT; i++) if (T.vf[i]) @@ -58,6 +162,7 @@ wchar_t *TplFormatString(int tpl, MCONTACT hContact, ItemData *item) wchar_t *buf = (wchar_t *)mir_alloc(sizeof(wchar_t) * (TplMeasureVars(&vars, T.value) + 1)); wchar_t *bufptr = buf; + for (wchar_t *p = T.value; *p; p++) { if (*p == '%') { wchar_t *var = vars.GetVar((p[1] & 0xff)); @@ -74,18 +179,16 @@ wchar_t *TplFormatString(int tpl, MCONTACT hContact, ItemData *item) } // Variable management -void TplInitVars(TemplateVars *vars) +TemplateVars::TemplateVars() { memset(&vars, 0, sizeof(vars)); } -void TplCleanVars(TemplateVars *vars) +TemplateVars::~TemplateVars() { - for (auto &V : vars->vars) + for (auto &V : vars) if (V.val && V.del) mir_free(V.val); - - memset(&vars, 0, sizeof(vars)); } int TplMeasureVars(TemplateVars *vars, wchar_t *str) diff --git a/plugins/NewStory/src/templates.h b/plugins/NewStory/src/templates.h index 7cee810fb1..c85f570562 100644 --- a/plugins/NewStory/src/templates.h +++ b/plugins/NewStory/src/templates.h @@ -9,6 +9,9 @@ enum struct TemplateVars { + TemplateVars(); + ~TemplateVars(); + struct { wchar_t *val; bool del; @@ -76,7 +79,9 @@ extern TemplateInfo templates[TPL_COUNT]; void LoadTemplates(); void SaveTemplates(); -wchar_t* TplFormatString(int tpl, MCONTACT hContact, ItemData *args); -wchar_t* TplFormatStringEx(int tpl, wchar_t *sztpl, MCONTACT hContact, ItemData *args); +CMStringA TplFormatRtf(int tpl, MCONTACT hContact, ItemData *args); +wchar_t* TplFormatString(int tpl, MCONTACT hContact, ItemData * item); +wchar_t* TplFormatStringEx(int tpl, wchar_t *sztpl, MCONTACT hContact, ItemData *args); + #endif // __templates_h__
\ No newline at end of file |