oxo_helper.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. FILE * pf = fopen (szFilename, "wb") ;
  40. if (!pf)
  41. {assert(false); return false;}
  42. fwrite (pBuffer, 1, nLength, pf) ;
  43. fclose (pf) ;
  44. return true ;
  45. }
  46. /// Load file to memory (you must use <B>delete[]</B> to free returned pBuffer).
  47. static void LoadFileToBuffer (const char* szFilename, char*& pBuffer, int& nLength)
  48. {
  49. pBuffer=0; nLength=0;
  50. FILE * pf = fopen (szFilename, "rb") ;
  51. if (!pf)
  52. {assert(false); return;}
  53. // get file length
  54. fseek (pf, 0, SEEK_END) ;
  55. nLength = (int)ftell(pf) ; assert(nLength > 0);
  56. if (nLength > 0)
  57. {
  58. // read file into memory
  59. pBuffer = new char[nLength + 8] ;
  60. memset (&pBuffer[nLength], 0, 8) ;
  61. fseek (pf, 0, SEEK_SET) ;
  62. fread (pBuffer, 1, nLength, pf) ;
  63. }
  64. fclose (pf) ;
  65. }
  66. //@}
  67. /**
  68. * @name String convert helper.
  69. */
  70. //@{
  71. /// int/long/float/double ==> ASCII
  72. template<class T> static std::string X2A (const T& tNumber, int nWidth=0)
  73. {
  74. std::ostringstream sOutStream ;
  75. sOutStream.width (nWidth) ;
  76. sOutStream.fill ('0') ;
  77. sOutStream << tNumber ;
  78. return sOutStream.str() ;
  79. }
  80. /// ASCII ==> int/long/float/double
  81. template<class T> static void A2X (const std::string& strNumber, T& tResult)
  82. {
  83. tResult = (T)0 ;
  84. std::stringstream sTranslation ;
  85. sTranslation << strNumber ;
  86. sTranslation >> tResult ;
  87. }
  88. /// URL encode.
  89. static std::string EncodeURL (const char* pBufDummy, int nLen)
  90. {
  91. const unsigned char * pBuf = (const unsigned char*)pBufDummy ;
  92. static std::string s_HexTable = "0123456789ABCDEF",
  93. s_UnsafeTable = "\"<>%\\^[]`+$,@:;/!#?=&" ;
  94. std::string s ;
  95. for (int i=0 ; i < nLen ; i++)
  96. {
  97. if ((pBuf[i] > 32) && (pBuf[i] < 123) && (s_UnsafeTable.find(pBuf[i]) == std::string::npos))
  98. {
  99. s += pBuf[i] ;
  100. }
  101. else
  102. {
  103. s += "%" ;
  104. s += s_HexTable[pBuf[i] / 16] ;
  105. s += s_HexTable[pBuf[i] % 16] ;
  106. }
  107. }
  108. return s ;
  109. }
  110. //@}
  111. /// Get filename's ext name.
  112. static std::string GetFileExt (const char* pFile)
  113. {
  114. if (!pFile)
  115. {assert(false); return "";}
  116. std::string strFile(pFile), strOut ;
  117. const size_t nPos = strFile.find_last_of (".") ;
  118. if (nPos != std::string::npos)
  119. strOut = strFile.substr (nPos + 1) ;
  120. return strOut ;
  121. }
  122. /// Output is from small to big.
  123. static void BubbleSort (int pArray[], int iNumElement)
  124. {
  125. if (!pArray)
  126. {assert(false); return;}
  127. for (int i = iNumElement-1 ; i > 0 ; i--)
  128. {
  129. bool bFlag = true ;
  130. for (int j = 0 ; j < i ; j++)
  131. if (pArray[j] > pArray[j + 1])
  132. {
  133. FSwap (pArray[j], pArray[j+1]) ;
  134. bFlag = false ;
  135. }
  136. if (bFlag)
  137. break ;
  138. }
  139. }
  140. };
  141. //=============================================================================
  142. // inline Implement
  143. //=============================================================================
  144. inline BYTE* FCOXOHelper::ZeroMalloc (int nBytes)
  145. {
  146. #ifdef WIN32
  147. // BoundChecker can's check the memory alloc by <VirtualAlloc>
  148. #ifdef _DEBUG
  149. BYTE * pByte = (BYTE*)malloc (nBytes) ;
  150. memset (pByte, 0, nBytes) ;
  151. return pByte ;
  152. #else
  153. return (BYTE*)VirtualAlloc (NULL, nBytes, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE) ;
  154. #endif
  155. #else
  156. BYTE * pByte = (BYTE*)malloc (nBytes) ;
  157. memset (pByte, 0, nBytes) ;
  158. return pByte ;
  159. #endif
  160. }
  161. //-----------------------------------------------------------------------------
  162. inline void FCOXOHelper::ZeroFree (void* pPixel)
  163. {
  164. if (!pPixel)
  165. return ;
  166. #ifdef WIN32
  167. #ifdef _DEBUG
  168. free (pPixel) ;
  169. #else
  170. ::VirtualFree (pPixel, 0, MEM_RELEASE) ;
  171. #endif
  172. #else
  173. free (pPixel) ;
  174. #endif
  175. }
  176. //-----------------------------------------------------------------------------
  177. #endif