PublicFunction.h
Upload User: oadesign
Upload Date: 2013-12-25
Package Size: 265k
Code Size: 8k
Category:

Process-Thread

Development Platform:

Visual C++

  1. // PublicFunction.h: interface for the CPublicFunction class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #if !defined(AFX_PUBLICFUNCTION_H__654F176C_F11F_4843_AEA0_F53676D52341__INCLUDED_)
  5. #define AFX_PUBLICFUNCTION_H__654F176C_F11F_4843_AEA0_F53676D52341__INCLUDED_
  6. #if _MSC_VER > 1000
  7. #pragma once
  8. #endif // _MSC_VER > 1000
  9. // 日志级别
  10. #define L_VERBOSE 0x00000000 // 详细,一般是调试信息
  11. #define L_NORMAL 0x00000001 // 正常,程序运行时显示的一般信息,纪录运行过程
  12. #define L_WARNING 0x00000002 // 警告,程序运行有错误,但不是致命错误,程序仍然可以继续运行下去
  13. #define L_ERROR 0x00000003 // 错误,程序运行出现了严重错误,程序必须立即终止执行
  14. #define L_DEBUG 0x00000004 // 调试,程序在调试版里输出的日志,在Publish版里就没有了
  15. #define L_LOGNUM 5 // 日志级别的数量
  16. // 输出位置
  17. #define L_OUT_CONSOLE 0x00000100 // 输出到控制台
  18. #define L_OUT_DLG 0x00000200 // 输出到对话框
  19. #define L_ONLY_LOGFILE 0x00000400 // 只保存到日志文件,不提示任何信息
  20. #define L_NOTCLOSEPREV 0x00000800 // 当开启对话框的时候不关闭原来的对话框
  21. #define SLEEP_RETURN(x)
  22. {
  23. if ( ::WaitForSingleObject ( m_hEvtEndModule, x ) == WAIT_OBJECT_0 )
  24. return FALSE;
  25. }
  26. #define SLEEP_BREAK(x)
  27. {
  28. if ( ::WaitForSingleObject ( m_hEvtEndModule, x ) == WAIT_OBJECT_0 )
  29. break;
  30. }
  31. // 进入临间区
  32. #define EnterCRIT(pCRIT,CRITName,FileName,Line) 
  33. {
  34. (pCRIT)->Lock ();
  35. TRACE ( "--------> %s , EnterCriticalSection ,%s ( %d )n", CRITName, FileName, Line );
  36. }
  37. // 离开临间区
  38. #define LeaveCRIT(pCRIT,CRITName,FileName,Line) 
  39. {
  40. TRACE ( "<-------- %s , LeaveCriticalSection ,%s ( %d )n", CRITName, FileName, Line );
  41. (pCRIT)->Unlock();
  42. }
  43. class CHwDir  
  44. {
  45. public:
  46. CHwDir(
  47. LPCTSTR lpszBasePathOrFile,
  48. BOOL bSerachSubDir = TRUE, // 搜索子目录
  49. BOOL bAbsolutePath =TRUE, // 是绝对路径
  50. CHwDir **ppHwDir=NULL // 将这个类的指针传出去给调用者
  51. );
  52. virtual ~CHwDir();
  53. public:
  54. ULONGLONG GetAmountBytes();
  55. CStringArray *m_pStrAryResFile, *m_pStrArySubDirectory;
  56. char m_szBasePathFile[MAX_PATH];
  57. void Cancel();
  58. private:
  59. DWORD FindAllFileUnderOneDir(LPCTSTR lpszFileName,LPCTSTR lpszDirectory,BOOL bFindDir);
  60. DWORD Dir();
  61. int HandleOneFile(LPCTSTR lpszDirectory, WIN32_FIND_DATA* pFindData,BOOL bFindDir);
  62. DWORD FindDirAndFile(LPCTSTR lpszDirectory);
  63. private:
  64. BOOL m_bCancel;
  65. CStringArray *m_pStrArySubDirectory_Private;
  66. DWORD m_dwDirNum;
  67. ULONGLONG m_AmountBytes;
  68. BOOL m_bSerachSubDir;
  69. BOOL m_bAbsolutePath; //是否要保存绝对路径
  70. DWORD m_dwRelativePathStartPos; //相对路径的起始位置
  71. char m_strFilter[MAX_PATH/2];
  72. };
  73. class CHwDirEx  
  74. {
  75. public:
  76. CHwDirEx(
  77. LPCTSTR lpszMultiFindPath, // 要搜索的路径,如“E:\winnt\;d:temp\;”
  78. LPCTSTR lpszMultiFindFilter, // 要搜索的过滤条件,如“*.bmp;*.exe;”
  79. LPCTSTR lpszMultiExcludeFilter=NULL, // 要排除的过滤条件,如“*.bmp;*.exe;”
  80. BOOL bSerachSubDir = TRUE, // 搜索子目录
  81. BOOL bAbsolutePath =TRUE, // 是绝对路径
  82. CHwDirEx **ppHwDirEx=NULL // 将这个类的指针传出去给调用者
  83. );
  84. ULONGLONG GetAmountBytes();
  85. virtual ~CHwDirEx();
  86. void Cancel();
  87. CStringArray *m_pStrAryResFile, *m_pStrArySubDirectory;
  88. private:
  89. CHwDir* m_pHwDir;
  90. BOOL m_bCancel;
  91. void DirAll(
  92. LPCTSTR lpszFindPath, // 要搜索的路径,如“E:\winnt\”
  93. LPCTSTR lpszMultiFindFilter, // 要搜索的过滤条件,如“*.bmp;*.exe;”
  94. LPCTSTR lpszMultiExcludeFilter=NULL, // 要排除的过滤条件,如“*.bmp;*.exe;”
  95. BOOL bSerachSubDir = TRUE, // 搜索子目录
  96. BOOL bAbsolutePath =TRUE // 是绝对路径
  97. );
  98. void Dir(
  99. LPCTSTR lpszFindPath, // 要搜索的路径,如“E:\winnt\”
  100. LPCTSTR lpszMultiFindFilter, // 要搜索的过滤条件,如“*.bmp;*.exe;”
  101. CStringArray &StrAryResFile, // 搜索的文件保存到此
  102. CStringArray &StrArySubDirectory, // 搜索的子目录保存到此
  103. DWORD &dwAmountBytes, // 总字节数保存到此
  104. BOOL bSerachSubDir = TRUE, // 搜索子目录
  105. BOOL bAbsolutePath =TRUE // 是绝对路径
  106. );
  107. DWORD m_dwDirNum;
  108. ULONGLONG m_AmountBytes;
  109. };
  110. //==========================================================================
  111. // 常用操作宏
  112. //==========================================================================
  113. #define GET_VALID_STRING_FROM_TOW(cs1,cs2) ( (cs1.GetLength()>0)?cs1:cs2 )
  114. #define GET_SAFE_STRING(str) ( (str)?(str):"" )
  115. #define NULL_STRING_FOR_DB ""
  116. #define GET_VALID_CSTRING(cs) ( (cs).GetLength()>0?(cs):((cs),(cs)=NULL_STRING_FOR_DB) )
  117. #define GET_VALID_CSTRING_P(csp) ( (csp)?(*(csp)):"" )
  118. #define STRNCPY_CS(sz,cs) strncpy((char*)(sz),(cs).GetBuffer(0),sizeof(sz))
  119. #define STRNCPY_SZ(sz1,sz2) strncpy(((char*)(sz1)),(sz2)?((char*)(sz2)):"",sizeof(sz1))
  120. #define STRNCPY(sz1,sz2,size) 
  121. {
  122. strncpy(((char*)(sz1)),(sz2)?((char*)(sz2)):"",(size));
  123. ((char*)(sz1))[(size)-1] = '';
  124. }
  125. // 时间类型的数据长度
  126. #define DATETIME_TYPE_LENGTH 20
  127. #define STRCPY(sz1,sz2) strcpy ( (char*)(sz1), (char*)((sz2)?(sz2):"") )
  128. #define STRLEN_SZ(sz) ((sz)?strlen((char*)(sz)):0)
  129. #define COPMNC_CS_SZ(cs,sz) ( (sz) && ((cs).CompareNoCase(sz)==0) )
  130. #define STRCMP_SAFE(sz1,sz2) (strcmp((char*)GET_SAFE_STRING(sz1),(char*)GET_SAFE_STRING(sz2)))
  131. #define STRLEN_SAFE(sz) ((sz)?strlen((char*)(sz)):0)
  132. #define ATOI_SAFE(sz) (atoi((const char*)(GET_SAFE_STRING((char*)(sz)))))
  133. #define ASSERT_ADDRESS(p,size) ASSERT((p)!=NULL && AfxIsValidAddress((p),(size),TRUE))
  134. #define VALID_IP_PORT(ip,port) ((STRLEN_SAFE(ip)>0) && (port)>1000)
  135. #define LENGTH(x) sizeof(x)/sizeof(x[0])
  136. #define MIN(x,y) (((DWORD)(x)<(DWORD)(y))?(x):(y))
  137. #define MAX(x,y) (((DWORD)(x)>(DWORD)(y))?(x):(y))
  138. // 有效的句柄
  139. #define HANDLE_IS_VALID(h) ((HANDLE)(h) && ((HANDLE)(h)!=INVALID_HANDLE_VALUE))
  140. // 关闭句柄
  141. #define CLOSE_HANDLE(h)
  142. {
  143. if ( HANDLE_IS_VALID(h) )
  144. {
  145. ::CloseHandle ( h );
  146. (h) = NULL;
  147. }
  148. }
  149. int ConvertStrToCTime(char *chtime, CTime &cTime );
  150. CString GetOneLine ( CString &str );
  151. void Log ( UINT nLevel, LPCTSTR lpszFormat, ... );
  152. int GetMouthByShortStr ( LPCTSTR lpszShortMonth );
  153. CString hwFormatMessage ( DWORD dwErrorCode );
  154. int WriteDataToFile(LPCTSTR filename,char* data,long size,LPCTSTR mode, int nStartPos=-1 );
  155. CString Data2HexString ( int nOffset, char *data, int size );
  156. void DbgLog ( LPCTSTR lpszFormat, ... );
  157. int ReadDataFromFile(LPCTSTR filename,char* data,long size, int nStartPos=-1);
  158. char* MakeSureDirectory(LPCTSTR lpszDirName);
  159. int hwSnprintf ( char *buffer, int count, const char *format, ... );
  160. BOOL PartPathAndFileAndExtensionName (
  161. IN LPCTSTR lpszFilePath, // 全路径名(包含文件名)
  162. OUT CString *pcsOnlyPath, // 输出光路径(没有文件名)
  163. OUT CString *pcsOnlyFileName, // 输出光文件名(没有路径)
  164. OUT CString *pcsExtensionName // 输出扩展名
  165. );
  166. BOOL PartFileAndPathByFullPath (
  167. LPCTSTR lpszFilePath,
  168. char *szOnlyFileName,
  169. int nFileNameSize,
  170. char *szOnlyPath =NULL,
  171. int nPathSize=0
  172. );
  173. template<class T1, class T2>
  174. int FindFromArray ( IN T1 &Ary, IN T2 Find )
  175. {
  176. int nCount = Ary.GetSize();
  177. for ( int i=0; i<nCount; i++ )
  178. {
  179. if ( Ary.GetAt(i) == Find )
  180. return i;
  181. }
  182. return -1;
  183. }
  184. template<class T1, class T2, class T3, class T4>
  185. int FindFromArray ( IN T1 &Ary, IN T2 Find, IN T3 &AppAry, IN T4 AppFind )
  186. {
  187. int nCount = Ary.GetSize();
  188. for ( int i=0; i<nCount; i++ )
  189. {
  190. if ( Ary.GetAt(i) == Find && 
  191. AppAry.GetAt(i) == AppFind )
  192. {
  193. return i;
  194. }
  195. }
  196. return -1;
  197. }
  198. void StandardizationPathBuffer ( char *szPath, int nSize, char cFlagChar='\' );
  199. CString StandardizationFileForPathName ( LPCTSTR lpszFileOrPathName, BOOL bIsFileName, char cReplaceChar='_' );
  200. char *hwStrrChr ( const char *string, int c );
  201. char *hwStrChr ( const char *string, int c );
  202. BOOL PartFileAndExtensionName (
  203. IN LPCTSTR lpszFileName,
  204. OUT char *szFileName,
  205. IN int nFileNameSize,
  206. OUT char *szExtensionName=NULL,
  207. IN int nExtensionNameSize=0 );
  208. BOOL hwDeleteFolder ( LPCTSTR lpszFolder );
  209. BOOL CopyFileAppend ( LPCTSTR lpszFileName_Src, LPCTSTR lpszFileName_Dst, int nOffset );
  210. BOOL CreateNullFile ( LPCTSTR lpszFileName, int nFileSize );
  211. BOOL WaitForThreadEnd ( HANDLE hThread, DWORD dwWaitTime=5000 );
  212. DWORD SelectPathByCommonDlg(LPSTR lpszPathName,HWND hwndOwner=NULL);
  213. BOOL SelectPathByCommonDlg ( CWnd *pDlg, UINT nEditID );
  214. #endif // !defined(AFX_PUBLICFUNCTION_H__654F176C_F11F_4843_AEA0_F53676D52341__INCLUDED_)