1
1

otherapi.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 
  2. #include "otherapi.h"
  3. // 检查一个文件是否存在
  4. BOOL IsFileExist(LPCTSTR lpFileName)
  5. {
  6. WIN32_FIND_DATA fd = {0};
  7. HANDLE hFind = FindFirstFile(lpFileName, &fd);
  8. if (hFind != INVALID_HANDLE_VALUE) {
  9. FindClose(hFind);
  10. }
  11. return ((hFind != INVALID_HANDLE_VALUE) && !(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
  12. }
  13. // 获得文件大小
  14. size_t get_fileSize(const char* filename)
  15. {
  16. FILE* pfile = fopen(filename, "rb");
  17. fseek(pfile, 0 , SEEK_END);
  18. size_t size = ftell(pfile);
  19. fclose(pfile);
  20. return size;
  21. }
  22. // 功能 获得当前路径
  23. char* GetAppDir(char* szPath)
  24. {
  25. char* ret = szPath;
  26. GetModuleFileName(NULL, szPath, MAX_PATH); // 得到当前执行文件的文件名(包含路径)
  27. *(strrchr(szPath , '\\')) = '\0'; // 删除文件名,只留下目录
  28. return ret;
  29. }
  30. // 得到全路径文件的文件名
  31. const char* GetFileBaseName(const char* szPath)
  32. {
  33. const char* ret = szPath + strlen(szPath);
  34. while ((*ret != '\\') && (ret != (szPath - 1))) // 得到文件名
  35. ret--;
  36. ret++;
  37. return ret;
  38. }
  39. // 内存匹配函数memfind
  40. char* memfind(const char* buf, const char* tofind, size_t len)
  41. {
  42. size_t findlen = strlen(tofind);
  43. if (findlen > len) {
  44. return ((char*)NULL);
  45. }
  46. if (len < 1) {
  47. return ((char*)buf);
  48. }
  49. {
  50. const char* bufend = &buf[len - findlen + 1];
  51. const char* c = buf;
  52. for (; c < bufend; c++) {
  53. if (*c == *tofind) { // first letter matches
  54. if (!memcmp(c + 1, tofind + 1, findlen - 1)) { // found
  55. return ((char*)c);
  56. }
  57. }
  58. }
  59. }
  60. return ((char*)NULL);
  61. }
  62. // 路径转宽字节
  63. wchar_t* charToWCHAR(wchar_t* wch, const char* czs)
  64. {
  65. MultiByteToWideChar(CP_ACP, 0, czs, -1, wch, MAX_PATH); // czs 转换到宽字节wch
  66. return wch;
  67. }
  68. char* WCHARTochar(char* czs , const wchar_t* wch)
  69. {
  70. WideCharToMultiByte(CP_ACP, 0, wch, -1, czs, MAX_PATH , NULL, NULL);
  71. return czs;
  72. }