Util.cpp
资源名称:FindMail.zip [点击查看]
上传用户:feituo2008
上传日期:2013-02-02
资源大小:493k
文件大小:5k
源码类别:
Email客户端
开发平台:
Visual C++
- #include "stdafx.h"
- #include <stdio.h>
- #include "resource.h"
- CRITICAL_SECTION g_cs_WriteLog, g_cs_WriteStat, g_cs_WriteExistEmail, g_cs_WriteNonexistEmail;
- int g_f_all_cs_inited =0, g_f_all_cs_deleted =0;
- void InitAllUtilCS()
- {
- if(g_f_all_cs_inited) return;
- InitializeCriticalSection(&g_cs_WriteLog);
- InitializeCriticalSection(&g_cs_WriteStat);
- InitializeCriticalSection(&g_cs_WriteExistEmail);
- InitializeCriticalSection(&g_cs_WriteNonexistEmail);
- g_f_all_cs_inited =true;
- g_f_all_cs_deleted =false;
- }
- void DeleteAllUtilCS()
- {
- if(g_f_all_cs_deleted || !g_f_all_cs_inited) return;
- DeleteCriticalSection(&g_cs_WriteLog);
- DeleteCriticalSection(&g_cs_WriteStat);
- DeleteCriticalSection(&g_cs_WriteExistEmail);
- DeleteCriticalSection(&g_cs_WriteNonexistEmail);
- g_f_all_cs_deleted =true;
- g_f_all_cs_inited =false;
- }
- /*class CUtilInit
- {
- public:
- CUtilInit()
- {
- InitAllUtilCS();
- }
- ~CUtilInit()
- {
- DeleteAllUtilCS();
- }
- };
- CUtilInit g_util_init;
- */
- HWND g_hWndNotify =NULL;
- int g_show_stat =true;
- int WriteStat(char *format, ...)
- {
- if(!g_show_stat) return 0;
- if(g_hWndNotify ==NULL) return 0;
- if(g_f_all_cs_inited) EnterCriticalSection(&g_cs_WriteStat);
- va_list args;
- char temp[4096];
- va_start(args, format);
- int len =vsprintf(temp, format, args);
- va_end(args);
- temp[len] =0;
- CString str;
- CWnd *pdlg =CWnd::FromHandle(g_hWndNotify);
- pdlg->GetDlgItemText(IDE_STAT, str);
- if(str.GetLength() >30000) str ="";
- str +=*(new CString(temp))+"rn";
- pdlg->SetDlgItemText(IDE_STAT, str);
- pdlg->SendDlgItemMessage(IDE_STAT, WM_VSCROLL, SB_BOTTOM, 0L);
- if(g_f_all_cs_inited) LeaveCriticalSection(&g_cs_WriteStat);
- return 0;
- }
- int WriteError(char *format, ...)
- {
- if(g_hWndNotify ==NULL) return 0;
- if(g_f_all_cs_inited) EnterCriticalSection(&g_cs_WriteStat);
- va_list args;
- char temp[4096];
- va_start(args, format);
- int len =vsprintf(temp, format, args);
- va_end(args);
- temp[len] =0;
- CString str;
- CWnd *pdlg =CWnd::FromHandle(g_hWndNotify);
- pdlg->GetDlgItemText(IDE_STAT, str);
- if(str.GetLength() >30000) str ="";
- str +=*(new CString(temp))+"rn";
- pdlg->SetDlgItemText(IDE_STAT, str);
- pdlg->SendDlgItemMessage(IDE_STAT, WM_VSCROLL, SB_BOTTOM, 0L);
- if(g_f_all_cs_inited) LeaveCriticalSection(&g_cs_WriteStat);
- return 0;
- }
- void WriteLog(char *fmt,...)
- {
- if(!g_show_stat) return;
- FILE *fp;
- va_list args;
- char modname[128];
- if(g_f_all_cs_inited) EnterCriticalSection(&g_cs_WriteLog);
- if((fp =fopen("c:\findmail.log", "a")) !=NULL)
- {
- va_start(args,fmt);
- GetModuleFileName(NULL, modname, sizeof(modname));
- //fprintf(fp, "n%s:n", modname);
- vfprintf(fp, fmt, args);
- fprintf(fp, "n");
- fclose(fp);
- va_end(args);
- }
- if(g_f_all_cs_inited) LeaveCriticalSection(&g_cs_WriteLog);
- }
- // output exist email to file and the edit control
- int WriteExistEmail(char *outfile, char *addr)
- {
- static int count =0;
- if(g_f_all_cs_inited) EnterCriticalSection(&g_cs_WriteExistEmail);
- FILE *fp;
- if((fp =fopen(outfile, "a")) !=NULL)
- {
- fprintf(fp, "%srn", addr);
- fclose(fp);
- }
- else return -1;
- CString str;
- CWnd *pdlg =CWnd::FromHandle(g_hWndNotify);
- pdlg->GetDlgItemText(IDE_EMAIL_EXIST, str);
- if(str.GetLength() >30000) str ="";
- str +=*(new CString(addr))+"rn";
- pdlg->SetDlgItemText(IDE_EMAIL_EXIST, str);
- pdlg->SendDlgItemMessage(IDE_EMAIL_EXIST, WM_VSCROLL, SB_BOTTOM, 0L);
- SetDlgItemInt(g_hWndNotify, IDC_EXIST_COUNT, ++count, 0);
- if(g_f_all_cs_inited) LeaveCriticalSection(&g_cs_WriteExistEmail);
- return 0;
- }
- // output nonexist email to file and the edit control
- int g_show_nonexist =true;
- int WriteNonexistEmail(char *outfile, char *addr)
- {
- if(!g_show_nonexist) return 0;
- static int count =0;
- if(g_f_all_cs_inited) EnterCriticalSection(&g_cs_WriteNonexistEmail);
- FILE *fp;
- if((fp =fopen(outfile, "a")) !=NULL)
- {
- fprintf(fp, "%srn", addr);
- fclose(fp);
- }
- else return -1;
- CString str;
- CWnd *pdlg =CWnd::FromHandle(g_hWndNotify);
- pdlg->GetDlgItemText(IDE_EMAIL_NONEXIST, str);
- if(str.GetLength() >30000) str ="";
- str +=*(new CString(addr))+"rn";
- pdlg->SetDlgItemText(IDE_EMAIL_NONEXIST, str);
- pdlg->SendDlgItemMessage(IDE_EMAIL_NONEXIST, WM_VSCROLL, SB_BOTTOM, 0L);
- SetDlgItemInt(g_hWndNotify, IDC_NONEXIST_COUNT, ++count, 0);
- if(g_f_all_cs_inited) LeaveCriticalSection(&g_cs_WriteNonexistEmail);
- return 0;
- }
- void WriteBinData(char *buf, int len)
- {
- FILE *fp;
- if((fp =fopen("c:\findmail.log", "a")) !=NULL)
- {
- for(int i =0; i<len; i++)
- fprintf(fp, "%d: %d, %crn", i, buf[i]&0xFF, buf[i]&0xFF);
- fprintf(fp, "nn");
- fclose(fp);
- }
- }
- char * GetErrString(char *str, DWORD errcode)
- {
- LPVOID lpbuf;
- if(FormatMessage(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- errcode,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR) &lpbuf,
- 0,
- NULL
- ))
- {
- lstrcpy(str, (char *)lpbuf);
- LocalFree(lpbuf);
- }
- return str;
- }