diff options
Diffstat (limited to 'plugins/Scriver/src/utils.cpp')
-rw-r--r-- | plugins/Scriver/src/utils.cpp | 166 |
1 files changed, 83 insertions, 83 deletions
diff --git a/plugins/Scriver/src/utils.cpp b/plugins/Scriver/src/utils.cpp index 2d17cbef67..96d8e1a2ad 100644 --- a/plugins/Scriver/src/utils.cpp +++ b/plugins/Scriver/src/utils.cpp @@ -27,23 +27,15 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define TTI_NONE 0
#endif
-int safe_wcslen(wchar_t *msg, int maxLen) {
- int i;
- for (i = 0; i < maxLen; i++) {
- if (msg[i] == (wchar_t)0)
- return i;
- }
- return 0;
-}
-
-wchar_t *a2w(const char *src, int len) {
+wchar_t *a2w(const char *src, int len)
+{
wchar_t *wline;
int i;
- if (len <0) {
+ if (len < 0) {
len = (int)strlen(src);
}
wline = (wchar_t*)mir_alloc(2 * (len + 1));
- for (i = 0; i < len; i ++) {
+ for (i = 0; i < len; i++) {
wline[i] = src[i];
}
wline[i] = 0;
@@ -57,7 +49,8 @@ enum MIMFLAGS { MIM_UNICODE = 2
};
-int IsUnicodeMIM() {
+int IsUnicodeMIM()
+{
if (!(mimFlags & MIM_CHECKED))
mimFlags = MIM_CHECKED | MIM_UNICODE;
@@ -66,57 +59,62 @@ int IsUnicodeMIM() { const char *filename = "scriver.log";
-void logInfo(const char *fmt, ...) {
+void logInfo(const char *fmt, ...)
+{
SYSTEMTIME time;
char *str;
va_list vararg;
int strsize;
- FILE *flog=fopen(filename,"at");
- if (flog!=NULL) {
+ FILE *flog = fopen(filename, "at");
+ if (flog != NULL) {
GetLocalTime(&time);
- va_start(vararg, fmt);
- str = (char*) malloc(strsize=2048);
- while (mir_vsnprintf(str, strsize, fmt, vararg) == -1)
- str = (char*) realloc(str, strsize+=2048);
- va_end(vararg);
- fprintf(flog,"%04d-%02d-%02d %02d:%02d:%02d,%03d [%s]",time.wYear,time.wMonth,time.wDay,time.wHour,time.wMinute,time.wSecond,time.wMilliseconds, "INFO");
- fprintf(flog," %s\n",str);
- free(str);
+ va_start(vararg, fmt);
+ str = (char*)malloc(strsize = 2048);
+ while (mir_vsnprintf(str, strsize, fmt, vararg) == -1)
+ str = (char*)realloc(str, strsize += 2048);
+ va_end(vararg);
+ fprintf(flog, "%04d-%02d-%02d %02d:%02d:%02d,%03d [%s]", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds, "INFO");
+ fprintf(flog, " %s\n", str);
+ free(str);
fclose(flog);
}
}
-int GetRichTextLength(HWND hwnd, int codepage, BOOL inBytes) {
+int GetRichTextLength(HWND hwnd, int codepage, BOOL inBytes)
+{
GETTEXTLENGTHEX gtl;
gtl.codepage = codepage;
if (inBytes) {
gtl.flags = GTL_NUMBYTES;
- } else {
+ }
+ else {
gtl.flags = GTL_NUMCHARS;
}
gtl.flags |= GTL_PRECISE | GTL_USECRLF;
- return (int) SendMessage(hwnd, EM_GETTEXTLENGTHEX, (WPARAM)>l, 0);
+ return (int)SendMessage(hwnd, EM_GETTEXTLENGTHEX, (WPARAM)>l, 0);
}
-TCHAR *GetRichText(HWND hwnd, int codepage) {
- GETTEXTEX gt = {0};
+TCHAR *GetRichText(HWND hwnd, int codepage)
+{
+ GETTEXTEX gt = { 0 };
TCHAR *textBuffer = NULL;
int textBufferSize;
codepage = 1200;
textBufferSize = GetRichTextLength(hwnd, codepage, TRUE);
if (textBufferSize > 0) {
textBufferSize += sizeof(TCHAR);
- textBuffer = (TCHAR *) mir_alloc(textBufferSize);
+ textBuffer = (TCHAR*)mir_alloc(textBufferSize);
gt.cb = textBufferSize;
gt.flags = GT_USECRLF;
gt.codepage = codepage;
- SendMessage(hwnd, EM_GETTEXTEX, (WPARAM) >, (LPARAM)textBuffer);
+ SendMessage(hwnd, EM_GETTEXTEX, (WPARAM)>, (LPARAM)textBuffer);
}
return textBuffer;
}
-char *GetRichTextEncoded(HWND hwnd, int codepage) {
+char *GetRichTextEncoded(HWND hwnd, int codepage)
+{
TCHAR *textBuffer = GetRichText(hwnd, codepage);
char *textUtf = NULL;
if (textBuffer != NULL) {
@@ -126,29 +124,31 @@ char *GetRichTextEncoded(HWND hwnd, int codepage) { return textUtf;
}
-int SetRichTextEncoded(HWND hwnd, const char *text, int codepage) {
+int SetRichTextEncoded(HWND hwnd, const char *text, int codepage)
+{
TCHAR *textToSet;
SETTEXTEX st;
st.flags = ST_DEFAULT;
st.codepage = 1200;
textToSet = mir_utf8decodeW(text);
- SendMessage(hwnd, EM_SETTEXTEX, (WPARAM) &st, (LPARAM)textToSet);
+ SendMessage(hwnd, EM_SETTEXTEX, (WPARAM)&st, (LPARAM)textToSet);
mir_free(textToSet);
return GetRichTextLength(hwnd, st.codepage, FALSE);
}
-int SetRichTextRTF(HWND hwnd, const char *text) {
+int SetRichTextRTF(HWND hwnd, const char *text)
+{
SETTEXTEX st;
st.flags = ST_DEFAULT;
st.codepage = CP_ACP;
- SendMessage(hwnd, EM_SETTEXTEX, (WPARAM) &st, (LPARAM)text);
+ SendMessage(hwnd, EM_SETTEXTEX, (WPARAM)&st, (LPARAM)text);
return GetRichTextLength(hwnd, st.codepage, FALSE);
}
static DWORD CALLBACK RichTextStreamCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG * pcb)
{
static DWORD dwRead;
- char ** ppText = (char **) dwCookie;
+ char **ppText = (char**)dwCookie;
if (*ppText == NULL) {
*ppText = (char*)mir_alloc(cb + 1);
@@ -160,7 +160,7 @@ static DWORD CALLBACK RichTextStreamCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, else {
char *p = (char*)mir_alloc(dwRead + cb + 1);
memcpy(p, *ppText, dwRead);
- memcpy(p+dwRead, pbBuff, cb);
+ memcpy(p + dwRead, pbBuff, cb);
p[dwRead + cb] = 0;
mir_free(*ppText);
*ppText = p;
@@ -176,7 +176,7 @@ char* GetRichTextRTF(HWND hwnd) if (hwnd == 0)
return NULL;
- char* pszText = NULL;
+ char *pszText = NULL;
EDITSTREAM stream = { 0 };
stream.pfnCallback = RichTextStreamCallback;
stream.dwCookie = (DWORD_PTR)&pszText; // pass pointer to pointer
@@ -187,57 +187,52 @@ char* GetRichTextRTF(HWND hwnd) void rtrimText(TCHAR *text)
{
static TCHAR szTrimString[] = _T(":;,.!?\'\"><()[]- \r\n");
- int iLen = mir_tstrlen(text)-1;
- while(iLen >= 0 && _tcschr(szTrimString, text[iLen])) {
+ size_t iLen = mir_tstrlen(text) - 1;
+ while (iLen >= 0 && _tcschr(szTrimString, text[iLen])) {
text[iLen] = _T('\0');
iLen--;
}
}
-TCHAR *limitText(TCHAR *text, int limit)
+TCHAR* limitText(TCHAR *text, int limit)
{
- int len = mir_tstrlen(text);
- if (len > g_dat.limitNamesLength)
- {
- TCHAR *ptszTemp = (TCHAR *)mir_alloc(sizeof(TCHAR) * (limit + 4));
+ size_t len = mir_tstrlen(text);
+ if (len > g_dat.limitNamesLength) {
+ TCHAR *ptszTemp = (TCHAR*)mir_alloc(sizeof(TCHAR) * (limit + 4));
_tcsncpy(ptszTemp, text, limit + 1);
_tcsncpy(ptszTemp + limit, _T("..."), 4);
return ptszTemp;
}
return text;
}
-TCHAR *GetRichTextWord(HWND hwnd, POINTL *ptl)
+TCHAR* GetRichTextWord(HWND hwnd, POINTL *ptl)
{
TCHAR* pszWord = NULL;
long iCharIndex, start, end, iRes;
pszWord = GetRichEditSelection(hwnd);
if (pszWord == NULL) {
iCharIndex = SendMessage(hwnd, EM_CHARFROMPOS, 0, (LPARAM)ptl);
- if (iCharIndex >= 0)
- {
+ if (iCharIndex >= 0) {
start = SendMessage(hwnd, EM_FINDWORDBREAK, WB_LEFT, iCharIndex);//-iChars;
end = SendMessage(hwnd, EM_FINDWORDBREAK, WB_RIGHT, iCharIndex);//-iChars;
- if (end - start > 0)
- {
+ if (end - start > 0) {
TEXTRANGE tr;
CHARRANGE cr;
memset(&tr, 0, sizeof(TEXTRANGE));
- pszWord = (TCHAR *)mir_alloc(sizeof(TCHAR) * (end - start + 1));
+ pszWord = (TCHAR*)mir_alloc(sizeof(TCHAR) * (end - start + 1));
cr.cpMin = start;
cr.cpMax = end;
tr.chrg = cr;
tr.lpstrText = pszWord;
iRes = SendMessage(hwnd, EM_GETTEXTRANGE, 0, (LPARAM)&tr);
- if (iRes <= 0)
- {
+ if (iRes <= 0) {
mir_free(pszWord);
pszWord = NULL;
}
}
}
}
- if (pszWord != NULL)
- {
+ if (pszWord != NULL) {
rtrimText(pszWord);
}
return pszWord;
@@ -245,27 +240,27 @@ TCHAR *GetRichTextWord(HWND hwnd, POINTL *ptl) static DWORD CALLBACK StreamOutCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG * pcb)
{
- MessageSendQueueItem *msi = (MessageSendQueueItem *) dwCookie;
+ MessageSendQueueItem *msi = (MessageSendQueueItem *)dwCookie;
msi->sendBuffer = (char*)mir_realloc(msi->sendBuffer, msi->sendBufferSize + cb + 2);
- memcpy (msi->sendBuffer + msi->sendBufferSize, pbBuff, cb);
+ memcpy(msi->sendBuffer + msi->sendBufferSize, pbBuff, cb);
msi->sendBufferSize += cb;
- *((TCHAR *)(msi->sendBuffer+msi->sendBufferSize)) = '\0';
+ *((TCHAR*)(msi->sendBuffer + msi->sendBufferSize)) = '\0';
*pcb = cb;
- return 0;
+ return 0;
}
TCHAR *GetRichEditSelection(HWND hwnd)
{
CHARRANGE sel;
SendMessage(hwnd, EM_EXGETSEL, 0, (LPARAM)&sel);
- if (sel.cpMin!=sel.cpMax) {
+ if (sel.cpMin != sel.cpMax) {
MessageSendQueueItem msi;
EDITSTREAM stream;
DWORD dwFlags = 0;
memset(&stream, 0, sizeof(stream));
stream.pfnCallback = StreamOutCallback;
- stream.dwCookie = (DWORD_PTR) &msi;
- dwFlags = SF_TEXT|SF_UNICODE|SFF_SELECTION;
+ stream.dwCookie = (DWORD_PTR)&msi;
+ dwFlags = SF_TEXT | SF_UNICODE | SFF_SELECTION;
msi.sendBuffer = NULL;
msi.sendBufferSize = 0;
SendMessage(hwnd, EM_STREAMOUT, (WPARAM)dwFlags, (LPARAM)&stream);
@@ -285,7 +280,7 @@ void AppendToBuffer(char **buffer, int *cbBufferEnd, int *cbBufferAlloced, const if (charsDone >= 0)
break;
*cbBufferAlloced += 1024;
- *buffer = (char*) mir_realloc(*buffer, *cbBufferAlloced);
+ *buffer = (char*)mir_realloc(*buffer, *cbBufferAlloced);
}
va_end(va);
*cbBufferEnd += charsDone;
@@ -294,8 +289,8 @@ void AppendToBuffer(char **buffer, int *cbBufferEnd, int *cbBufferAlloced, const int MeasureMenuItem(WPARAM wParam, LPARAM lParam)
{
- LPMEASUREITEMSTRUCT mis = (LPMEASUREITEMSTRUCT) lParam;
- if (mis->itemData != (ULONG_PTR) g_dat.hButtonIconList && mis->itemData != (ULONG_PTR) g_dat.hSearchEngineIconList && mis->itemData != (ULONG_PTR) g_dat.hChatButtonIconList) {
+ LPMEASUREITEMSTRUCT mis = (LPMEASUREITEMSTRUCT)lParam;
+ if (mis->itemData != (ULONG_PTR)g_dat.hButtonIconList && mis->itemData != (ULONG_PTR)g_dat.hSearchEngineIconList && mis->itemData != (ULONG_PTR)g_dat.hChatButtonIconList) {
return FALSE;
}
mis->itemWidth = max(0, GetSystemMetrics(SM_CXSMICON) - GetSystemMetrics(SM_CXMENUCHECK) + 4);
@@ -307,8 +302,8 @@ int DrawMenuItem(WPARAM wParam, LPARAM lParam) {
int y;
int id;
- LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT) lParam;
- if (dis->itemData != (ULONG_PTR) g_dat.hButtonIconList && dis->itemData != (ULONG_PTR) g_dat.hSearchEngineIconList && dis->itemData != (ULONG_PTR) g_dat.hChatButtonIconList) {
+ LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT)lParam;
+ if (dis->itemData != (ULONG_PTR)g_dat.hButtonIconList && dis->itemData != (ULONG_PTR)g_dat.hSearchEngineIconList && dis->itemData != (ULONG_PTR)g_dat.hChatButtonIconList) {
return FALSE;
}
id = dis->itemID;
@@ -325,9 +320,11 @@ int DrawMenuItem(WPARAM wParam, LPARAM lParam) rc.bottom = rc.top + GetSystemMetrics(SM_CYSMICON) + 2;
FillRect(dis->hDC, &rc, GetSysColorBrush(COLOR_HIGHLIGHT));
ImageList_DrawEx((HIMAGELIST)dis->itemData, id, dis->hDC, 2, y, 0, 0, CLR_NONE, CLR_DEFAULT, ILD_SELECTED);
- } else
+ }
+ else
ImageList_DrawEx((HIMAGELIST)dis->itemData, id, dis->hDC, 2, y, 0, 0, CLR_NONE, CLR_DEFAULT, ILD_FOCUS);
- } else {
+ }
+ else {
if (dis->itemState & ODS_CHECKED) {
HBRUSH hBrush;
RECT rc;
@@ -346,7 +343,8 @@ int DrawMenuItem(WPARAM wParam, LPARAM lParam) FillRect(dis->hDC, &rc, hBrush);
DeleteObject(hBrush);
ImageList_DrawEx((HIMAGELIST)dis->itemData, id, dis->hDC, 2, y, 0, 0, CLR_NONE, GetSysColor(COLOR_MENU), ILD_BLEND25);
- } else
+ }
+ else
ImageList_DrawEx((HIMAGELIST)dis->itemData, id, dis->hDC, 2, y, 0, 0, CLR_NONE, CLR_NONE, ILD_NORMAL);
}
return TRUE;
@@ -355,21 +353,23 @@ int DrawMenuItem(WPARAM wParam, LPARAM lParam) // Code taken from http://www.geekhideout.com/urlcode.shtml
/* Converts an integer value to its hex character*/
-char to_hex(char code) {
+char to_hex(char code)
+{
static char hex[] = "0123456789abcdef";
return hex[code & 15];
}
/* Returns a url-encoded version of str */
/* IMPORTANT: be sure to free() the returned string after use */
-char *url_encode(char *str) {
+char *url_encode(char *str)
+{
char *pstr = str, *buf = (char*)mir_alloc(strlen(str) * 3 + 1), *pbuf = buf;
while (*pstr) {
- if ( (48 <= *pstr && *pstr <= 57) ||//0-9
- (65 <= *pstr && *pstr <= 90) ||//ABC...XYZ
- (97 <= *pstr && *pstr <= 122) ||//abc...xyz
+ if ((48 <= *pstr && *pstr <= 57) ||//0-9
+ (65 <= *pstr && *pstr <= 90) ||//ABC...XYZ
+ (97 <= *pstr && *pstr <= 122) ||//abc...xyz
*pstr == '-' || *pstr == '_' || *pstr == '.')
- *pbuf++ = *pstr;
+ *pbuf++ = *pstr;
else if (*pstr == ' ')
*pbuf++ = '+';
else
@@ -384,8 +384,8 @@ void SearchWord(TCHAR * word, int engine) {
char szURL[4096];
if (word && word[0]) {
- ptrA wordUTF( mir_utf8encodeT(word));
- ptrA wordURL( mir_urlEncode(wordUTF));
+ ptrA wordUTF(mir_utf8encodeT(word));
+ ptrA wordURL(mir_urlEncode(wordUTF));
switch (engine) {
case SEARCHENGINE_WIKIPEDIA:
mir_snprintf(szURL, SIZEOF(szURL), "http://en.wikipedia.org/wiki/%s", wordURL);
@@ -420,11 +420,11 @@ void SearchWord(TCHAR * word, int engine) void SetSearchEngineIcons(HMENU hMenu, HIMAGELIST hImageList)
{
- for (int i=0; i < IDI_LASTICON - IDI_GOOGLE; i++) {
+ for (int i = 0; i < IDI_LASTICON - IDI_GOOGLE; i++) {
MENUITEMINFO minfo = { sizeof(minfo) };
minfo.fMask = MIIM_BITMAP | MIIM_DATA;
minfo.hbmpItem = HBMMENU_CALLBACK;
- minfo.dwItemData = (ULONG_PTR) hImageList;
+ minfo.dwItemData = (ULONG_PTR)hImageList;
SetMenuItemInfo(hMenu, IDM_SEARCH_GOOGLE + i, FALSE, &minfo);
}
}
@@ -500,14 +500,14 @@ HDWP ResizeToolbar(HWND hwnd, HDWP hdwp, int width, int vPos, int height, int cC int i;
int lPos = 0;
int rPos = width;
- for (i = 0; i < cControls ; i++) {
+ for (i = 0; i < cControls; i++) {
if (!buttons[i].alignment && (controlVisibility & (1 << i))) {
lPos += buttons[i].spacing;
hdwp = DeferWindowPos(hdwp, GetDlgItem(hwnd, buttons[i].controlId), 0, lPos, vPos, buttons[i].width, height, SWP_NOZORDER);
lPos += buttons[i].width;
}
}
- for (i = cControls - 1; i >=0; i--) {
+ for (i = cControls - 1; i >= 0; i--) {
if (buttons[i].alignment && (controlVisibility & (1 << i))) {
rPos -= buttons[i].spacing + buttons[i].width;
hdwp = DeferWindowPos(hdwp, GetDlgItem(hwnd, buttons[i].controlId), 0, rPos, vPos, buttons[i].width, height, SWP_NOZORDER);
|