#include "myFile.h" int myFile::read_file(char* path, node_t* nd) { #ifdef _WIN32 char* pbuf; HANDLE hFile; DWORD flen,rlen; BOOL r; hFile = CreateFileA(path, //name GENERIC_READ, //以读方式打开 FILE_SHARE_READ, //可共享读 NULL, //默认安全设置 OPEN_EXISTING, //只打开已经存在的文件 FILE_ATTRIBUTE_NORMAL, //常规文件属性 NULL); //无模板 if (hFile== INVALID_HANDLE_VALUE) { printf("%s open failed, err: %d\n", path, GetLastError()); return -1; } flen = GetFileSize(hFile, NULL); if (flen== INVALID_FILE_SIZE) { printf("%s, get invalid file size!\n", path); return -1; } pbuf = (char*)malloc(flen); if (!pbuf) { printf("malloc %d failed!\n", flen); CloseHandle(hFile); return -1; } r = ReadFile(hFile, pbuf, flen, &rlen, NULL); if (r==FALSE) { printf("ReadFile failed!\n"); CloseHandle(hFile); return -1; } nd->pbuf = pbuf; nd->dlen = rlen; nd->blen = rlen; CloseHandle(hFile); #else struct stat st; char* pbuf; FILE* fp; int r,flen,rlen; r = stat((const char *)path, &st); if (r) { return -1; } flen = st.st_size; fp = fopen((const char *)path, "r"); if(!fp) { printf("fopen %s failed!\n", path); return -1; } pbuf = (char*)malloc(flen); if (!pbuf) { printf("malloc %d failed!\n", flen); fclose(fp); return -1; } r = fread(pbuf, 1, flen, fp); if (r!=flen) { printf("fread %s failed!\n", path); fclose(fp); return -1; } nd->pbuf = pbuf; nd->dlen = rlen; nd->blen = rlen; fclose(fp); #endif return 0; } int myFile::get_time(char *path, char *time, int len) { #ifdef _WIN32 FILETIME IpCreationTime; //文件的创建时间 FILETIME IpLastAccessTime; //对文件的最近访问时间 FILETIME IpLastWriteTime; //文件的最近修改时间 FILETIME ltime; SYSTEMTIME rtime; HANDLE hFile = CreateFileA(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); if (hFile == INVALID_HANDLE_VALUE) { printf("Get the dictionary failed!\n"); CloseHandle(hFile); return -1; } if (!GetFileTime(hFile, &IpCreationTime, &IpLastAccessTime, &IpLastWriteTime)) { return -1; } CloseHandle(hFile); FileTimeToLocalFileTime(&IpLastWriteTime, <ime); FileTimeToSystemTime(<ime, &rtime); //将文件时间转化为系统时间 snprintf(time, len, "%04u%02u%02u %02u:%02u:%02u", rtime.wYear, rtime.wMonth, rtime.wDay, rtime.wHour, rtime.wMinute, rtime.wSecond); #else int r; struct stat st; r = stat((const char *)path, &st); if (r) { return -1; } struct tm* ptm=localtime(&st.st_atime); 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); #endif return 0; } int myFile::get_version(char* path, void* version, int len) { int r=-1; return r; } char* myFile::wchar_to_char(wchar_t* wchr) { #ifdef _WIN32 int clen = WideCharToMultiByte(CP_ACP, 0, wchr, -1, NULL, 0, NULL, NULL); char* chr = new char[clen]; WideCharToMultiByte(CP_ACP, 0, wchr, -1, chr, clen, NULL, NULL); #else char* chr; int r,len = wcslen(wchr) + 1; if(len <= 1) return 0; chr = (char*)malloc(len); if(!chr) { return NULL; } r = wcstombs(chr, wchr, len); if(r<=0) { free(chr); chr = NULL; } #endif return chr; } wchar_t* myFile::char_to_wchar(char* chr) { #ifdef _WIN32 int clen = MultiByteToWideChar(CP_ACP, 0, chr, -1, NULL, 0); wchar_t* wchr = new wchar_t[clen]; MultiByteToWideChar(CP_ACP, 0, chr, -1, wchr, clen); #else wchar_t *wchr; int r,len = strlen(chr) + 1; if(len <= 1) return 0; wchr = (wchar_t*)malloc(sizeof(wchar_t)*len); if(!wchr) { return NULL; } r = mbstowcs(wchr, chr, sizeof(wchar_t)*len); if(r<=0) { free(wchr); wchr = NULL; } #endif return wchr; }