myFile.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include "myFile.h"
  2. int myFile::read_file(char* path, node_t* nd)
  3. {
  4. #ifdef _WIN32
  5. char* pbuf;
  6. HANDLE hFile;
  7. DWORD flen,rlen;
  8. BOOL r;
  9. hFile = CreateFileA(path, //name
  10. GENERIC_READ, //以读方式打开
  11. FILE_SHARE_READ, //可共享读
  12. NULL, //默认安全设置
  13. OPEN_EXISTING, //只打开已经存在的文件
  14. FILE_ATTRIBUTE_NORMAL, //常规文件属性
  15. NULL); //无模板
  16. if (hFile== INVALID_HANDLE_VALUE) {
  17. printf("%s open failed, err: %d\n", path, GetLastError());
  18. return -1;
  19. }
  20. flen = GetFileSize(hFile, NULL);
  21. if (flen== INVALID_FILE_SIZE) {
  22. printf("%s, get invalid file size!\n", path);
  23. return -1;
  24. }
  25. pbuf = (char*)malloc(flen);
  26. if (!pbuf) {
  27. printf("malloc %d failed!\n", flen);
  28. CloseHandle(hFile);
  29. return -1;
  30. }
  31. r = ReadFile(hFile, pbuf, flen, &rlen, NULL);
  32. if (r==FALSE) {
  33. printf("ReadFile failed!\n");
  34. CloseHandle(hFile);
  35. return -1;
  36. }
  37. nd->pbuf = pbuf;
  38. nd->dlen = rlen;
  39. nd->blen = rlen;
  40. CloseHandle(hFile);
  41. #else
  42. struct stat st;
  43. char* pbuf;
  44. FILE* fp;
  45. int r,flen,rlen;
  46. r = stat((const char *)path, &st);
  47. if (r) {
  48. return -1;
  49. }
  50. flen = st.st_size;
  51. fp = fopen((const char *)path, "r");
  52. if(!fp) {
  53. printf("fopen %s failed!\n", path);
  54. return -1;
  55. }
  56. pbuf = (char*)malloc(flen);
  57. if (!pbuf) {
  58. printf("malloc %d failed!\n", flen);
  59. fclose(fp);
  60. return -1;
  61. }
  62. r = fread(pbuf, 1, flen, fp);
  63. if (r!=flen) {
  64. printf("fread %s failed!\n", path);
  65. fclose(fp);
  66. return -1;
  67. }
  68. nd->pbuf = pbuf;
  69. nd->dlen = rlen;
  70. nd->blen = rlen;
  71. fclose(fp);
  72. #endif
  73. return 0;
  74. }
  75. int myFile::get_time(char *path, char *time, int len)
  76. {
  77. #ifdef _WIN32
  78. FILETIME IpCreationTime; //文件的创建时间
  79. FILETIME IpLastAccessTime; //对文件的最近访问时间
  80. FILETIME IpLastWriteTime; //文件的最近修改时间
  81. FILETIME ltime;
  82. SYSTEMTIME rtime;
  83. HANDLE hFile = CreateFileA(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  84. if (hFile == INVALID_HANDLE_VALUE)
  85. {
  86. printf("Get the dictionary failed!\n");
  87. CloseHandle(hFile);
  88. return -1;
  89. }
  90. if (!GetFileTime(hFile, &IpCreationTime, &IpLastAccessTime, &IpLastWriteTime)) {
  91. return -1;
  92. }
  93. CloseHandle(hFile);
  94. FileTimeToLocalFileTime(&IpLastWriteTime, &ltime);
  95. FileTimeToSystemTime(&ltime, &rtime); //将文件时间转化为系统时间
  96. snprintf(time, len, "%04u%02u%02u %02u:%02u:%02u", rtime.wYear, rtime.wMonth, rtime.wDay, rtime.wHour, rtime.wMinute, rtime.wSecond);
  97. #else
  98. int r;
  99. struct stat st;
  100. r = stat((const char *)path, &st);
  101. if (r) {
  102. return -1;
  103. }
  104. struct tm* ptm=localtime(&st.st_atime);
  105. snprintf(time, len, "%04u%02u%02u %02u:%02u:%02u \n", ptm->tm_year, ptm->tm_mon, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
  106. #endif
  107. return 0;
  108. }
  109. int myFile::get_version(char* path, void* version, int len)
  110. {
  111. int r=-1;
  112. return r;
  113. }
  114. char* myFile::wchar_to_char(wchar_t* wchr)
  115. {
  116. #ifdef _WIN32
  117. int clen = WideCharToMultiByte(CP_ACP, 0, wchr, -1, NULL, 0, NULL, NULL);
  118. char* chr = new char[clen];
  119. WideCharToMultiByte(CP_ACP, 0, wchr, -1, chr, clen, NULL, NULL);
  120. #else
  121. char* chr;
  122. int r,len = wcslen(wchr) + 1;
  123. if(len <= 1)
  124. return 0;
  125. chr = (char*)malloc(len);
  126. if(!chr) {
  127. return NULL;
  128. }
  129. r = wcstombs(chr, wchr, len);
  130. if(r<=0) {
  131. free(chr);
  132. chr = NULL;
  133. }
  134. #endif
  135. return chr;
  136. }
  137. wchar_t* myFile::char_to_wchar(char* chr)
  138. {
  139. #ifdef _WIN32
  140. int clen = MultiByteToWideChar(CP_ACP, 0, chr, -1, NULL, 0);
  141. wchar_t* wchr = new wchar_t[clen];
  142. MultiByteToWideChar(CP_ACP, 0, chr, -1, wchr, clen);
  143. #else
  144. wchar_t *wchr;
  145. int r,len = strlen(chr) + 1;
  146. if(len <= 1)
  147. return 0;
  148. wchr = (wchar_t*)malloc(sizeof(wchar_t)*len);
  149. if(!wchr) {
  150. return NULL;
  151. }
  152. r = mbstowcs(wchr, chr, sizeof(wchar_t)*len);
  153. if(r<=0) {
  154. free(wchr);
  155. wchr = NULL;
  156. }
  157. #endif
  158. return wchr;
  159. }