public.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "public.h"
  2. //逐层建立目录
  3. bool CreatePath(const char* sPathIn)
  4. {
  5. //建立目录
  6. char sPath[MAX_PATH];
  7. strcpy(sPath, sPathIn);
  8. int i, len;
  9. len = strlen(sPath);
  10. for(i = 0;i < len; i++)
  11. {
  12. if(sPath[i] == '\\' || sPath[i] == '/')
  13. {
  14. sPath[i] = '\0';
  15. MakeDir(sPath);
  16. sPath[i] = '/';
  17. }
  18. }
  19. return true;
  20. }
  21. //获取当前程序目录,安卓无效
  22. int GetCurLibsPath(char *psPath, char* sFile)
  23. {
  24. char path[MAX_PATH];
  25. #ifdef WIN32
  26. int cnt = GetModuleFileNameA(NULL,path,MAX_PATH);
  27. #else
  28. Dl_info dInfo;
  29. int cnt = dladdr("GetCurLibsPath" , &dInfo);
  30. if (cnt < 0 || cnt >= MAX_PATH) return -1;
  31. strcpy(path, dInfo.dli_fname);
  32. cnt = strlen(path);
  33. #endif
  34. if (cnt < 0 || cnt >= MAX_PATH) return -1;
  35. for(int i = cnt; i >= 0; --i)
  36. {
  37. if(path[i] == '/' || path[i]=='\\')
  38. {
  39. if (sFile) strcpy(sFile,path + i + 1);
  40. path[i]='\0';
  41. break;
  42. }
  43. }
  44. strcpy(psPath,path);
  45. return strlen(path);
  46. }
  47. //获取当前程序目录,安卓无效
  48. int GetCurProcPath(char *psPath, char* sFile)
  49. {
  50. char path[MAX_PATH];
  51. #ifdef WIN32
  52. int cnt = GetModuleFileNameA(NULL,path,MAX_PATH);
  53. #else
  54. int cnt = readlink("/proc/self/exe", path, MAX_PATH);
  55. #endif
  56. if (cnt < 0 || cnt >= MAX_PATH) return -1;
  57. for(int i = cnt; i >= 0; --i)
  58. {
  59. if(path[i] == '/' || path[i]=='\\')
  60. {
  61. if (sFile) strcpy(sFile,path + i + 1);
  62. path[i]='\0';
  63. break;
  64. }
  65. }
  66. strcpy(psPath,path);
  67. return strlen(path);
  68. }
  69. //获取当前程序目录
  70. int GetCurWorkPath(char *psPath)
  71. {
  72. char path[MAX_PATH];
  73. #ifdef WIN32
  74. int cnt = GetCurrentDirectoryA(MAX_PATH,path);
  75. if (cnt < 0 || cnt >= MAX_PATH) return -1;
  76. int iPos = cnt - 1;
  77. if (path[iPos] != '/' && path[iPos] != '\\')
  78. strcpy(path + cnt, "\\");
  79. #else
  80. // char* pWd = getcwd(path, MAX_PATH);
  81. char* pWd = realpath("./", path);
  82. if (pWd == NULL) return -1;
  83. int cnt = strlen(path);
  84. for(int i = cnt; i >= 0; --i)
  85. {
  86. if(path[i] == '/' || path[i]=='\\')
  87. {
  88. path[i]='\0';
  89. break;
  90. }
  91. }
  92. #endif
  93. strcpy(psPath,path);
  94. return strlen(path);
  95. }
  96. //16进制字符串转换成内存数据ANSI码,如"41"转换成字符"A"
  97. int HexStrToANSI(const char* sAsc,unsigned char* sBin, int iDataLen)
  98. {
  99. if (iDataLen == 0) iDataLen = strlen((char*)sAsc);
  100. char sTemp[4], cTempCh;
  101. sTemp[2] = 0;
  102. int ia = 0, iPos = 0;
  103. for (ia = 0; ia < iDataLen; ia++)
  104. {
  105. cTempCh=sAsc[ia];
  106. if ((cTempCh >= 'A' && cTempCh <= 'F') ||
  107. (cTempCh >= 'a' && cTempCh <= 'f') ||
  108. (cTempCh >= '0' && cTempCh <= '9'))
  109. {
  110. sTemp[iPos % 2] = cTempCh;
  111. iPos ++;
  112. if ((iPos % 2) == 0)
  113. sBin[(iPos / 2) -1] = strtol(sTemp, NULL, 16);
  114. }
  115. else
  116. {
  117. if ((iPos % 2) == 1) return 0;
  118. break;
  119. }
  120. }
  121. return iPos / 2;
  122. }
  123. //内存数据ANSI码转换成16进制字符串,如字符"A"转换成"41"
  124. char* ANSIToHexStr(const unsigned char * sData, int iLen,char*sOut)
  125. {
  126. int iPos;
  127. for (iPos = 0; iPos < iLen; iPos++)
  128. {
  129. sprintf(sOut + (iPos * 2), "%02X", sData[iPos]);
  130. }
  131. *(sOut + (iPos*2))=0;
  132. return sOut;
  133. }
  134. //内存数据ANSI码转换成2进制字符串,如字符"A""41"转换成"01000001"
  135. char* ANSIToBitStr(const unsigned char * sData, int iLen, char*sOut)
  136. {
  137. int iPos, iBit, iLeng = 0;
  138. for (iPos = 0; iPos < iLen; iPos++)
  139. {
  140. for (iBit = 7; iBit >= 0; iBit--)
  141. {
  142. if ((sData[iPos] & (1 << iBit)) > 0)
  143. sOut[iLeng++] = '1';
  144. else
  145. sOut[iLeng++] = '0';
  146. }
  147. }
  148. sOut[iLeng] = 0;
  149. return sOut;
  150. }
  151. void ToUpper(char* sIn, int iLen)
  152. {
  153. if (iLen <= 0) iLen = strlen(sIn);
  154. for (int i = 0; i< iLen && *sIn; i++){
  155. if (*sIn > 0x60 && *sIn < 0x7b) *sIn -= 0x20;
  156. }
  157. }
  158. void ToLower(char* sIn, int iLen)
  159. {
  160. if (iLen <= 0) iLen = strlen(sIn);
  161. for (int i = 0; i< iLen && *sIn; i++){
  162. if (*sIn > 0x40 && *sIn < 0x5b) *sIn += 0x20;
  163. }
  164. }
  165. //获取系统时间,毫秒
  166. INT64 GetSystemTime()
  167. {
  168. INT64 lRet=0;
  169. time_t tNow;
  170. time(&tNow);
  171. lRet = (INT64)tNow * 1000;
  172. #ifdef _WIN32
  173. SYSTEMTIME sysTime;
  174. GetLocalTime(&sysTime);
  175. lRet += sysTime.wMilliseconds;
  176. #else
  177. struct timeval tmv;
  178. struct timezone tmz;
  179. gettimeofday(&tmv, &tmz);
  180. lRet += tmv.tv_usec / 1000;
  181. #endif
  182. return lRet;
  183. }
  184. //BASE64格式字符串解码成文件数据
  185. int Base64Decode(const char* sDataIn, int iInDataLen, unsigned char* sOutBuf, int iOutBufLen)
  186. {
  187. int iRetLen = iInDataLen * 3 / 4;
  188. if (iOutBufLen <= iRetLen) return 0;
  189. //解码表
  190. const char DecodeTable[] =
  191. {
  192. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  193. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  194. 62, // '+'
  195. 0, 0, 0,
  196. 63, // '/'
  197. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // '0'-'9'
  198. 0, 0, 0, 0, 0, 0, 0,
  199. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
  200. 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, // 'A'-'Z'
  201. 0, 0, 0, 0, 0, 0,
  202. 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
  203. 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, // 'a'-'z'
  204. };
  205. int nValue;
  206. iRetLen = 0;
  207. for (int ia=0; ia < iInDataLen; )
  208. {
  209. if (*sDataIn == '\r' || *sDataIn == '\n')
  210. {
  211. ia++;
  212. sDataIn++;
  213. continue;
  214. }
  215. nValue = DecodeTable[*sDataIn++] << 18;
  216. nValue += DecodeTable[*sDataIn++] << 12;
  217. sOutBuf[iRetLen++] = (nValue & 0x00FF0000) >> 16;
  218. if (*sDataIn != '=')
  219. {
  220. nValue += DecodeTable[*sDataIn++] << 6;
  221. sOutBuf[iRetLen++] = (nValue & 0x0000FF00) >> 8;
  222. if (*sDataIn != '=')
  223. {
  224. nValue += DecodeTable[*sDataIn++];
  225. sOutBuf[iRetLen++] = nValue & 0x000000FF;
  226. }
  227. }
  228. ia += 4;
  229. }
  230. return iRetLen;
  231. }