-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathccfilewin32.cc
More file actions
264 lines (246 loc) · 5.41 KB
/
ccfilewin32.cc
File metadata and controls
264 lines (246 loc) · 5.41 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "platformconfig.h"
#if CCTARGET_PLATFORM == CCPLATFORM_WIN32
#include "ccfilewin32.h"
#include <time.h>
#include <assert.h>
CC_file_strategy* CC_file_strategy::GetInstance()
{
if (CC_file_strategy::f_instance == nullptr)
{
f_instance = new CC_file_win32();
}
return f_instance;
}
void CC_file_win32::Create(std::string path)
{
CC_file_strategy::Create(path);
}
intptr_t CC_file_win32::Copy(std::string sourcePath, std::string destPath)
{
return CC_file_strategy::Copy(sourcePath, destPath);
}
void CC_file_win32::Delete(std::string path)
{
bool exist = Exists(path);
if (!exist)
{
fprintf(stderr, "File does not exists\n");
return;
}
int stat = ::remove(path.c_str());
if (stat != 0)
{
fprintf(stderr, "Remove file %s failed\n", path.c_str());
return;
}
}
bool CC_file_win32::Exists(std::string path)
{
int exist = _access(path.c_str(), FileAccess::EXISTENCEONLY);
if (exist)
{
return false;
}
return true;
}
bool CC_file_win32::CanRead(std::string path)
{
int exist = _access(path.c_str(), FileAccess::READPERMISSION);
if (exist)
{
return false;
}
return true;
}
bool CC_file_win32::CanExecute(std::string path)
{
if (!Exists(path))
return false;
auto index = path.rfind('.');
std::string str = std::string(path.begin() + index + 1, path.end());
if (str == "exe" || str == "bat")
return true;
return false;
}
bool CC_file_win32::CanWrite(std::string path)
{
int exist = _access(path.c_str(), FileAccess::WRITEPERMISSION);
if (exist)
{
return false;
}
return true;
}
FileAccess CC_file_win32::GetAccessControl(std::string path)
{
int exist = _access(path.c_str(), FileAccess::WRITEPERMISSION);
if (exist)
{
return FileAccess::READPERMISSION;
}
else
return FileAccess::READANDWRITEPERMISSION;
}
FileAttributes CC_file_win32::GetAttributes(std::string path)
{
bool exist = Exists(path);
_finddata_t fileInfo;
if (exist)
{
intptr_t handle = _findfirst(path.c_str(), &fileInfo);
if (handle == -1)
{
fprintf(stderr, "No matching Unknown attributes %s %d\n", __FUNCTION__, __LINE__);
return FileAttributes::UNKNOWN;
}
auto value = (int)fileInfo.attrib;
_findclose(handle);
return (FileAttributes)value;
}
return FileAttributes::UNKNOWN;
}
DateTime CC_file_win32::GetCreateTime(std::string path)
{
bool exist = Exists(path);
_finddata_t fileInfo;
if (exist)
{
intptr_t handle = _findfirst(path.c_str(), &fileInfo);
if (handle == -1)
{
fprintf(stderr, "No matching files at %s %d\n", __FUNCTION__, __LINE__);
return DateTime();
}
else
{
time_t value = fileInfo.time_create;
struct tm* tim = localtime(&value);
DateTime date;
date.year = tim->tm_yday + 1900;
date.month = tim->tm_mon + 1;
date.day = tim->tm_mday;
date.hour = tim->tm_hour;
date.minute = tim->tm_min;
date.sec = tim->tm_sec;
_findclose(handle);
return date;
}
}
return DateTime();
}
DateTime CC_file_win32::GetLastAccessTime(std::string path)
{
bool exist = Exists(path);
_finddata_t fileInfo;
if (exist)
{
intptr_t handle = _findfirst(path.c_str(), &fileInfo);
if (handle == -1)
{
fprintf(stderr, "No matching %s %d\n", __FUNCTION__, __LINE__);
return DateTime();
}
else
{
time_t value = fileInfo.time_access;
struct tm* tim = localtime(&value);
DateTime date;
date.year = tim->tm_yday + 1900;
date.month = tim->tm_mon + 1;
date.day = tim->tm_mday;
date.hour = tim->tm_hour;
date.minute = tim->tm_min;
date.sec = tim->tm_sec;
_findclose(handle);
return date;
}
}
return DateTime();
}
DateTime CC_file_win32::GetLastModifyTime(std::string path)
{
bool exist = Exists(path);
_finddata_t fileInfo;
if (exist)
{
intptr_t handle = _findfirst(path.c_str(), &fileInfo);
if (handle == -1)
{
return DateTime();
}
else
{
time_t value = fileInfo.time_write;
struct tm* tim = localtime(&value);
DateTime date;
date.year = tim->tm_yday + 1900;
date.month = tim->tm_mon + 1;
date.day = tim->tm_mday;
date.hour = tim->tm_hour;
date.minute = tim->tm_min;
date.sec = tim->tm_sec;
_findclose(handle);
return date;
}
}
return DateTime();
}
long CC_file_win32::CopyFileData(FileStream* src, FileStream* dest)
{
if (src == NULL || dest == NULL) {
return -1;
}
// move to the end of file
fseek(src->Getfd(), 0, SEEK_END);
// get file size
long file_size = ftell(src->Getfd());
// back to the beginning of file
rewind(src->Getfd());
int res = 0;
intptr_t count = 0; // percentage。
const int LEN_DATA = 128 * 1024;
unsigned char data[LEN_DATA] = { 0 };
// printf("size: %lu \n", sizeof(data));
while (1) {
// current position
long current = ftell(src->Getfd());
// remainder
long leave = file_size - current;
int readSize = LEN_DATA;
if (leave == 0) {
// done
printf("leave == 0 feof.\n");
break;
}
else if (leave < LEN_DATA) {
// almost done
readSize = (int)leave;
}
size_t res_t = fread(&data, 1, readSize, src->Getfd());
if (res_t == readSize) {
// 正常读取
size_t w_res = fwrite(&data, 1, res_t, dest->Getfd());
count += w_res;
if (res_t == w_res) {
//printf("write=%ld res_t=%u \n", count, res_t);
}
else {
printf("fwrite fail. \n");
res = -1;
break;
}
}
else if (feof(src->Getfd()) != 0){
printf("feof res_t=%u \n", res_t);
break;
}
else {
printf("fread fail...\n");
res = -1;
break;
}
}
printf("一共写字节数为: %d\n", count);
return (res == -1) ? -1 : count;
}
#endif