/* * 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) { FILE * pf = fopen (szFilename, "wb") ; if (!pf) {assert(false); return false;} fwrite (pBuffer, 1, nLength, pf) ; fclose (pf) ; return true ; } /// Load file to memory (you must use delete[] to free returned pBuffer). static void LoadFileToBuffer (const char* szFilename, char*& pBuffer, int& nLength) { pBuffer=0; nLength=0; FILE * pf = fopen (szFilename, "rb") ; if (!pf) {assert(false); return;} // 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 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 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 #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