oxo_helper.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (C) =USTC= Fu Li
  3. *
  4. * Author : Fu Li
  5. * Create : 2004-6-26
  6. * Home : http://www.crazy-bit.com/
  7. * Mail : crazybit@263.net
  8. * History :
  9. */
  10. #ifndef __FOO_PHOXO_HELPER__2004_06_26__H__
  11. #define __FOO_PHOXO_HELPER__2004_06_26__H__
  12. #include "StdDefine.h"
  13. //=============================================================================
  14. /**
  15. * Some pure C++ helper funtion.
  16. */
  17. class FCOXOHelper
  18. {
  19. public:
  20. /**
  21. * @name Memory helper.
  22. */
  23. //@{
  24. /// Allocate memory with initialized to zero.
  25. static BYTE* ZeroMalloc (int nBytes) ;
  26. /// Free memory alloc by ZeroMalloc.
  27. static void ZeroFree (void* pPixel) ;
  28. //@}
  29. /**
  30. * @name File helper.
  31. */
  32. //@{
  33. /**
  34. * Save buffer to file.
  35. * if the destination file exist, this function will delete it.
  36. */
  37. static bool SaveBufferToFile (const char* szFilename, const void* pBuffer, int nLength)
  38. {
  39. #ifdef VC60
  40. FILE *pf = fopen (szFilename, "wb") ;
  41. if (!pf)
  42. {
  43. assert(false); return false;
  44. }
  45. #else
  46. FILE *pf = NULL;
  47. _tfopen_s(&pf, szFilename, _T("wb"));
  48. if ( !pf )
  49. {
  50. ASSERT(false);
  51. return false;
  52. }
  53. #endif
  54. fwrite (pBuffer, 1, nLength, pf) ;
  55. fclose (pf) ;
  56. return true ;
  57. }
  58. /// Load file to memory (you must use <B>delete[]</B> to free returned pBuffer).
  59. static void LoadFileToBuffer (const char* szFilename, char*& pBuffer, int& nLength)
  60. {
  61. pBuffer=0;
  62. nLength=0;
  63. #ifdef VC60
  64. FILE * pf = fopen (szFilename, "rb") ;
  65. if (!pf)
  66. {
  67. assert(false);
  68. return;
  69. }
  70. #else
  71. FILE *pf = NULL;
  72. _tfopen_s(&pf, szFilename, _T("rb"));
  73. if ( !pf )
  74. {
  75. ASSERT(false);
  76. return ;
  77. }
  78. #endif
  79. // get file length
  80. fseek (pf, 0, SEEK_END) ;
  81. nLength = (int)ftell(pf) ; assert(nLength > 0);
  82. if (nLength > 0)
  83. {
  84. // read file into memory
  85. pBuffer = new char[nLength + 8] ;
  86. memset (&pBuffer[nLength], 0, 8) ;
  87. fseek (pf, 0, SEEK_SET) ;
  88. fread (pBuffer, 1, nLength, pf) ;
  89. }
  90. fclose (pf) ;
  91. }
  92. //@}
  93. /**
  94. * @name String convert helper.
  95. */
  96. //@{
  97. /// int/long/float/double ==> ASCII
  98. template<class T> static std::string X2A (const T& tNumber, int nWidth=0)
  99. {
  100. std::ostringstream sOutStream ;
  101. sOutStream.width (nWidth) ;
  102. sOutStream.fill ('0') ;
  103. sOutStream << tNumber ;
  104. return sOutStream.str() ;
  105. }
  106. /// ASCII ==> int/long/float/double
  107. template<class T> static void A2X (const std::string& strNumber, T& tResult)
  108. {
  109. tResult = (T)0 ;
  110. std::stringstream sTranslation ;
  111. sTranslation << strNumber ;
  112. sTranslation >> tResult ;
  113. }
  114. /// URL encode.
  115. static std::string EncodeURL (const char* pBufDummy, int nLen)
  116. {
  117. const unsigned char * pBuf = (const unsigned char*)pBufDummy ;
  118. static std::string s_HexTable = "0123456789ABCDEF",
  119. s_UnsafeTable = "\"<>%\\^[]`+$,@:;/!#?=&" ;
  120. std::string s ;
  121. for (int i=0 ; i < nLen ; i++)
  122. {
  123. if ((pBuf[i] > 32) && (pBuf[i] < 123) && (s_UnsafeTable.find(pBuf[i]) == std::string::npos))
  124. {
  125. s += pBuf[i] ;
  126. }
  127. else
  128. {
  129. s += "%" ;
  130. s += s_HexTable[pBuf[i] / 16] ;
  131. s += s_HexTable[pBuf[i] % 16] ;
  132. }
  133. }
  134. return s ;
  135. }
  136. //@}
  137. /// Get filename's ext name.
  138. static std::string GetFileExt (const char* pFile)
  139. {
  140. if (!pFile)
  141. {assert(false); return "";}
  142. std::string strFile(pFile), strOut ;
  143. const size_t nPos = strFile.find_last_of (".") ;
  144. if (nPos != std::string::npos)
  145. strOut = strFile.substr (nPos + 1) ;
  146. return strOut ;
  147. }
  148. /// Output is from small to big.
  149. static void BubbleSort (int pArray[], int iNumElement)
  150. {
  151. if (!pArray)
  152. {assert(false); return;}
  153. for (int i = iNumElement-1 ; i > 0 ; i--)
  154. {
  155. bool bFlag = true ;
  156. for (int j = 0 ; j < i ; j++)
  157. if (pArray[j] > pArray[j + 1])
  158. {
  159. FSwap (pArray[j], pArray[j+1]) ;
  160. bFlag = false ;
  161. }
  162. if (bFlag)
  163. break ;
  164. }
  165. }
  166. };
  167. //=============================================================================
  168. // inline Implement
  169. //=============================================================================
  170. inline BYTE* FCOXOHelper::ZeroMalloc (int nBytes)
  171. {
  172. #ifdef WIN32
  173. // BoundChecker can's check the memory alloc by <VirtualAlloc>
  174. #ifdef _DEBUG
  175. BYTE * pByte = (BYTE*)malloc (nBytes) ;
  176. memset (pByte, 0, nBytes) ;
  177. return pByte ;
  178. #else
  179. return (BYTE*)VirtualAlloc (NULL, nBytes, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE) ;
  180. #endif
  181. #else
  182. BYTE * pByte = (BYTE*)malloc (nBytes) ;
  183. memset (pByte, 0, nBytes) ;
  184. return pByte ;
  185. #endif
  186. }
  187. //-----------------------------------------------------------------------------
  188. inline void FCOXOHelper::ZeroFree (void* pPixel)
  189. {
  190. if (!pPixel)
  191. return ;
  192. #ifdef WIN32
  193. #ifdef _DEBUG
  194. free (pPixel) ;
  195. #else
  196. ::VirtualFree (pPixel, 0, MEM_RELEASE) ;
  197. #endif
  198. #else
  199. free (pPixel) ;
  200. #endif
  201. }
  202. //-----------------------------------------------------------------------------
  203. #endif