123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- /*
- * Copyright (C) =USTC= Fu Li
- *
- * Author : Fu Li
- * Create : 2004-6-26
- * Home : http://www.crazy-bit.com/
- * Mail : crazybit@263.net
- * History :
- */
- #ifndef __FOO_PHOXO_HELPER__2004_06_26__H__
- #define __FOO_PHOXO_HELPER__2004_06_26__H__
- #include "StdDefine.h"
- //=============================================================================
- /**
- * Some pure C++ helper funtion.
- */
- class FCOXOHelper
- {
- public:
- /**
- * @name Memory helper.
- */
- //@{
- /// Allocate memory with initialized to zero.
- static BYTE* ZeroMalloc (int nBytes) ;
- /// Free memory alloc by ZeroMalloc.
- static void ZeroFree (void* pPixel) ;
- //@}
- /**
- * @name File helper.
- */
- //@{
- /**
- * Save buffer to file.
- * if the destination file exist, this function will delete it.
- */
- static bool SaveBufferToFile (const char* szFilename, const void* pBuffer, int nLength)
- {
- #ifdef VC60
- FILE *pf = fopen (szFilename, "wb") ;
- if (!pf)
- {
- assert(false); return false;
- }
- #else
- FILE *pf = NULL;
- _tfopen_s(&pf, szFilename, _T("wb"));
- if ( !pf )
- {
- ASSERT(false);
- return false;
- }
- #endif
- fwrite (pBuffer, 1, nLength, pf) ;
- fclose (pf) ;
- return true ;
- }
- /// Load file to memory (you must use <B>delete[]</B> to free returned pBuffer).
- static void LoadFileToBuffer (const char* szFilename, char*& pBuffer, int& nLength)
- {
- pBuffer=0;
- nLength=0;
- #ifdef VC60
- FILE * pf = fopen (szFilename, "rb") ;
- if (!pf)
- {
- assert(false);
- return;
- }
- #else
- FILE *pf = NULL;
- _tfopen_s(&pf, szFilename, _T("rb"));
- if ( !pf )
- {
- ASSERT(false);
- return ;
- }
- #endif
- // get file length
- fseek (pf, 0, SEEK_END) ;
- nLength = (int)ftell(pf) ; assert(nLength > 0);
- if (nLength > 0)
- {
- // read file into memory
- pBuffer = new char[nLength + 8] ;
- memset (&pBuffer[nLength], 0, 8) ;
- fseek (pf, 0, SEEK_SET) ;
- fread (pBuffer, 1, nLength, pf) ;
- }
- fclose (pf) ;
- }
- //@}
- /**
- * @name String convert helper.
- */
- //@{
- /// int/long/float/double ==> ASCII
- template<class T> static std::string X2A (const T& tNumber, int nWidth=0)
- {
- std::ostringstream sOutStream ;
- sOutStream.width (nWidth) ;
- sOutStream.fill ('0') ;
- sOutStream << tNumber ;
- return sOutStream.str() ;
- }
- /// ASCII ==> int/long/float/double
- template<class T> static void A2X (const std::string& strNumber, T& tResult)
- {
- tResult = (T)0 ;
- std::stringstream sTranslation ;
- sTranslation << strNumber ;
- sTranslation >> tResult ;
- }
- /// URL encode.
- static std::string EncodeURL (const char* pBufDummy, int nLen)
- {
- const unsigned char * pBuf = (const unsigned char*)pBufDummy ;
- static std::string s_HexTable = "0123456789ABCDEF",
- s_UnsafeTable = "\"<>%\\^[]`+$,@:;/!#?=&" ;
- std::string s ;
- for (int i=0 ; i < nLen ; i++)
- {
- if ((pBuf[i] > 32) && (pBuf[i] < 123) && (s_UnsafeTable.find(pBuf[i]) == std::string::npos))
- {
- s += pBuf[i] ;
- }
- else
- {
- s += "%" ;
- s += s_HexTable[pBuf[i] / 16] ;
- s += s_HexTable[pBuf[i] % 16] ;
- }
- }
- return s ;
- }
- //@}
- /// Get filename's ext name.
- static std::string GetFileExt (const char* pFile)
- {
- if (!pFile)
- {assert(false); return "";}
- std::string strFile(pFile), strOut ;
- const size_t nPos = strFile.find_last_of (".") ;
- if (nPos != std::string::npos)
- strOut = strFile.substr (nPos + 1) ;
- return strOut ;
- }
- /// Output is from small to big.
- static void BubbleSort (int pArray[], int iNumElement)
- {
- if (!pArray)
- {assert(false); return;}
- for (int i = iNumElement-1 ; i > 0 ; i--)
- {
- bool bFlag = true ;
- for (int j = 0 ; j < i ; j++)
- if (pArray[j] > pArray[j + 1])
- {
- FSwap (pArray[j], pArray[j+1]) ;
- bFlag = false ;
- }
- if (bFlag)
- break ;
- }
- }
- };
- //=============================================================================
- // inline Implement
- //=============================================================================
- inline BYTE* FCOXOHelper::ZeroMalloc (int nBytes)
- {
- #ifdef WIN32
- // BoundChecker can's check the memory alloc by <VirtualAlloc>
- #ifdef _DEBUG
- BYTE * pByte = (BYTE*)malloc (nBytes) ;
- memset (pByte, 0, nBytes) ;
- return pByte ;
- #else
- return (BYTE*)VirtualAlloc (NULL, nBytes, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE) ;
- #endif
- #else
- BYTE * pByte = (BYTE*)malloc (nBytes) ;
- memset (pByte, 0, nBytes) ;
- return pByte ;
- #endif
- }
- //-----------------------------------------------------------------------------
- inline void FCOXOHelper::ZeroFree (void* pPixel)
- {
- if (!pPixel)
- return ;
- #ifdef WIN32
- #ifdef _DEBUG
- free (pPixel) ;
- #else
- ::VirtualFree (pPixel, 0, MEM_RELEASE) ;
- #endif
- #else
- free (pPixel) ;
- #endif
- }
- //-----------------------------------------------------------------------------
- #endif
|