Repository navigation
Expand file tree
/
Copy pathFileIOInterface.cpp
More file actions
183 lines (158 loc) · 3.63 KB
/
Copy pathFileIOInterface.cpp
File metadata and controls
183 lines (158 loc) · 3.63 KB
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "stdafx.h"
#include "FileIOInterface.h"
#ifdef _WIN32
#include <io.h>
#else
#include<dirent.h>
#endif
#include <sys/stat.h>
FileIOInterface::~FileIOInterface()
{
}
FileHandle* FileIOInterface::Open(const char*szFilePath, OPENMODE mode)
{
std::string filepath;
if (!strstr(szFilePath,":") && szFilePath[0]!='/')
{//相对路径
filepath = JSVM::GetInstance()->m_szExternalPath + "/" + szFilePath;
}
else filepath = szFilePath;
FileHandle *f = new FileHandle;
const char*mod="";
if (mode == OP_READ)mod = "rb";
else if (mode == OP_READWRITE)mod = "wb+";
else if (mode == OP_READAPPEND)mod = "ab+";
if (mode == OP_READWRITE || mode == OP_READAPPEND)
{
MakeDir(Getdir(filepath.c_str()));
}
f->pFile = fopen(filepath.c_str(), mod);
if (!f->pFile)
{
int err = errno;
delete f;
return nullptr;
}
return f;
}
void FileIOInterface::Close(FileHandle*fHandle)
{
fclose(fHandle->pFile);
delete fHandle;
}
long long FileIOInterface::GetLength(FileHandle*fHandle)
{
long n=ftell(fHandle->pFile);
fseek(fHandle->pFile, 0, SEEK_END); //定位到文件末
long nFileLen = ftell(fHandle->pFile); //文件长度
fseek(fHandle->pFile, n,SEEK_SET);
return nFileLen;
}
long long FileIOInterface::Seek(FileHandle*fHandle, SEEKTYPE type, long long nOffset)
{
if (type == SE_BEGIN)return fseek(fHandle->pFile, nOffset, SEEK_SET);
else if (type == SE_CURRENT)return fseek(fHandle->pFile, nOffset, SEEK_CUR);
else if (type == SE_END)return fseek(fHandle->pFile, nOffset, SEEK_END);
else return 0;
}
long long FileIOInterface::Read(FileHandle*fHandle, void*pBuffer, int nLen)
{
return fread(pBuffer, 1, nLen, fHandle->pFile);
}
long long FileIOInterface::Write(FileHandle*fHandle, const void*pBuffer, int nLen)
{
return fwrite(pBuffer, 1, nLen, fHandle->pFile);
}
std::string FileIOInterface::Getdir(const char* path)
{
path = _strdup(path);
char*p = strrchr((char*)path, '/');
if (p)
{
*p = 0;
std::string ret = path;
free((void*)path);
return ret;
}
else
{
free((void*)path);
return "";
}
}
bool FileIOInterface::MakeDir(const std::string& szPath)
{
int re = 0;
if (_mkdir(szPath.c_str()))re = errno;
if (re == EEXIST)
{
return FALSE;
}
else if (re == ENOENT)
{
if (!MakeDir(Getdir(szPath.c_str())))return FALSE;
return _mkdir(szPath.c_str()) == 0;
}
else return TRUE;
}
void FileIOInterface::RmDir(const std::string&szPath)
{
if (!IsDirectory(szPath.c_str()))
{
remove(szPath.c_str());
return;
}
#ifdef _WIN32
long handle; // handle for search
struct _finddata_t fileinfo; // sturct of file info
handle = _findfirst((std::string(szPath)+"/*").c_str(), &fileinfo); // first search
if (-1 == handle)return ;
RmDir(fileinfo.name);
while (!_findnext(handle, &fileinfo)) // loop search until no more matching files
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
RmDir(fileinfo.name);
}
_findclose(handle); // close handle
#else
DIR*pDir;
dirent*dmsg;
if ((pDir = opendir(szPath.c_str())) != NULL)
{
// travesal dirs to delete file
while ((dmsg = readdir(pDir)) != NULL)
{
if (strcmp(dmsg->d_name, ".") != 0 && strcmp(dmsg->d_name, "..") != 0)
{
RmDir(szPath+"/"+dmsg->d_name);
}
}
}
if (pDir != NULL)
{
closedir(pDir);
}
#endif
rmdir(szPath.c_str());
}
bool FileIOInterface::IsDirectory(const char* szPath)
{
struct stat buf;
int result;
result = stat(szPath, &buf);
if (S_IFDIR & buf.st_mode){
return true;
}
else if (S_IFREG & buf.st_mode){
return false;
}
return false;
}
bool FileIOInterface::IsFileExist(const char*szPath)
{
struct stat buf;
int result;
result = stat(szPath, &buf);
if (result == -1)return false;
return true;
}