#include "public.h" //逐层建立目录 bool CreatePath(const char* sPathIn) { //建立目录 char sPath[MAX_PATH]; strcpy(sPath, sPathIn); int i, len; len = strlen(sPath); for(i = 0;i < len; i++) { if(sPath[i] == '\\' || sPath[i] == '/') { sPath[i] = '\0'; MakeDir(sPath); sPath[i] = '/'; } } return true; } //获取当前程序目录,安卓无效 int GetCurLibsPath(char *psPath, char* sFile) { char path[MAX_PATH]; #ifdef WIN32 int cnt = GetModuleFileNameA(NULL,path,MAX_PATH); #else Dl_info dInfo; int cnt = dladdr("GetCurLibsPath" , &dInfo); if (cnt < 0 || cnt >= MAX_PATH) return -1; strcpy(path, dInfo.dli_fname); cnt = strlen(path); #endif if (cnt < 0 || cnt >= MAX_PATH) return -1; for(int i = cnt; i >= 0; --i) { if(path[i] == '/' || path[i]=='\\') { if (sFile) strcpy(sFile,path + i + 1); path[i]='\0'; break; } } strcpy(psPath,path); return strlen(path); } //获取当前程序目录,安卓无效 int GetCurProcPath(char *psPath, char* sFile) { char path[MAX_PATH]; #ifdef WIN32 int cnt = GetModuleFileNameA(NULL,path,MAX_PATH); #else int cnt = readlink("/proc/self/exe", path, MAX_PATH); #endif if (cnt < 0 || cnt >= MAX_PATH) return -1; for(int i = cnt; i >= 0; --i) { if(path[i] == '/' || path[i]=='\\') { if (sFile) strcpy(sFile,path + i + 1); path[i]='\0'; break; } } strcpy(psPath,path); return strlen(path); } //获取当前程序目录 int GetCurWorkPath(char *psPath) { char path[MAX_PATH]; #ifdef WIN32 int cnt = GetCurrentDirectoryA(MAX_PATH,path); if (cnt < 0 || cnt >= MAX_PATH) return -1; int iPos = cnt - 1; if (path[iPos] != '/' && path[iPos] != '\\') strcpy(path + cnt, "\\"); #else // char* pWd = getcwd(path, MAX_PATH); char* pWd = realpath("./", path); if (pWd == NULL) return -1; int cnt = strlen(path); for(int i = cnt; i >= 0; --i) { if(path[i] == '/' || path[i]=='\\') { path[i]='\0'; break; } } #endif strcpy(psPath,path); return strlen(path); } //16进制字符串转换成内存数据ANSI码,如"41"转换成字符"A" int HexStrToANSI(const char* sAsc,unsigned char* sBin, int iDataLen) { if (iDataLen == 0) iDataLen = strlen((char*)sAsc); char sTemp[4], cTempCh; sTemp[2] = 0; int ia = 0, iPos = 0; for (ia = 0; ia < iDataLen; ia++) { cTempCh=sAsc[ia]; if ((cTempCh >= 'A' && cTempCh <= 'F') || (cTempCh >= 'a' && cTempCh <= 'f') || (cTempCh >= '0' && cTempCh <= '9')) { sTemp[iPos % 2] = cTempCh; iPos ++; if ((iPos % 2) == 0) sBin[(iPos / 2) -1] = strtol(sTemp, NULL, 16); } else { if ((iPos % 2) == 1) return 0; break; } } return iPos / 2; } //内存数据ANSI码转换成16进制字符串,如字符"A"转换成"41" char* ANSIToHexStr(const unsigned char * sData, int iLen,char*sOut) { int iPos; for (iPos = 0; iPos < iLen; iPos++) { sprintf(sOut + (iPos * 2), "%02X", sData[iPos]); } *(sOut + (iPos*2))=0; return sOut; } //内存数据ANSI码转换成2进制字符串,如字符"A""41"转换成"01000001" char* ANSIToBitStr(const unsigned char * sData, int iLen, char*sOut) { int iPos, iBit, iLeng = 0; for (iPos = 0; iPos < iLen; iPos++) { for (iBit = 7; iBit >= 0; iBit--) { if ((sData[iPos] & (1 << iBit)) > 0) sOut[iLeng++] = '1'; else sOut[iLeng++] = '0'; } } sOut[iLeng] = 0; return sOut; } void ToUpper(char* sIn, int iLen) { if (iLen <= 0) iLen = strlen(sIn); for (int i = 0; i< iLen && *sIn; i++){ if (*sIn > 0x60 && *sIn < 0x7b) *sIn -= 0x20; } } void ToLower(char* sIn, int iLen) { if (iLen <= 0) iLen = strlen(sIn); for (int i = 0; i< iLen && *sIn; i++){ if (*sIn > 0x40 && *sIn < 0x5b) *sIn += 0x20; } } //获取系统时间,毫秒 INT64 GetSystemTime() { INT64 lRet=0; time_t tNow; time(&tNow); lRet = (INT64)tNow * 1000; #ifdef _WIN32 SYSTEMTIME sysTime; GetLocalTime(&sysTime); lRet += sysTime.wMilliseconds; #else struct timeval tmv; struct timezone tmz; gettimeofday(&tmv, &tmz); lRet += tmv.tv_usec / 1000; #endif return lRet; } //BASE64格式字符串解码成文件数据 int Base64Decode(const char* sDataIn, int iInDataLen, unsigned char* sOutBuf, int iOutBufLen) { int iRetLen = iInDataLen * 3 / 4; if (iOutBufLen <= iRetLen) return 0; //解码表 const char DecodeTable[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, // '+' 0, 0, 0, 63, // '/' 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9' 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z' 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z' }; int nValue; iRetLen = 0; for (int ia=0; ia < iInDataLen; ) { if (*sDataIn == '\r' || *sDataIn == '\n') { ia++; sDataIn++; continue; } nValue = DecodeTable[*sDataIn++] << 18; nValue += DecodeTable[*sDataIn++] << 12; sOutBuf[iRetLen++] = (nValue & 0x00FF0000) >> 16; if (*sDataIn != '=') { nValue += DecodeTable[*sDataIn++] << 6; sOutBuf[iRetLen++] = (nValue & 0x0000FF00) >> 8; if (*sDataIn != '=') { nValue += DecodeTable[*sDataIn++]; sOutBuf[iRetLen++] = nValue & 0x000000FF; } } ia += 4; } return iRetLen; }