-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFile.cpp
294 lines (261 loc) · 7.09 KB
/
File.cpp
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* File: File.cpp
* Author: User
*
* Created on 14 Январь 2012 г., 23:35
*/
#include "File.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <libgen.h>
#include <stdlib.h>
#include <io.h>
#include <iostream>
#include <direct.h>
#include <dirent.h>
#include <string.h>
#include <winbase.h>
#include "IOError.h"
File::File(const String& name) {
this->name.assign(name);
}
File::File(const File& orig) {
name.assign(orig.name);
}
File::File(const File& parent,const String& childName){
name.assign(parent.name);
name = name.trimEnd("/");
name = name.trimEnd("\\");
String cn(childName.c_str());
cn.assign(cn.trimEnd("/"));
cn.assign(cn.trimEnd("\\"));
cn.assign(cn.trimStart("/"));
cn.assign(cn.trimStart("\\"));
name = name.append("\\");
name = name.append(cn);
}
File::~File() {
}
bool File::isExists(){
struct stat s;
if( stat(name.c_str(),&s)==0 ){
return true;
}
return false;
}
bool File::isFile(){
struct stat s;
if( stat(name.c_str(),&s)!=0 ){
return false;
}
return S_ISREG(s.st_mode);
}
bool File::isDir(){
struct stat s;
if( stat(name.c_str(),&s)!=0 ){
return false;
}
return S_ISDIR(s.st_mode);
}
String File::getPath(){
return String(name);
}
String File::getName(){
char* res = (char *)malloc(strlen(name.c_str())+1);
char* res2 = 0;
strcpy(res,name.c_str());
res2 = basename(res);
String res3 = String(res2);
free(res);
return String(res3);
}
File File::getParent(){
char* res = (char *)malloc(strlen(name.c_str())+1);
char* res2 = 0;
strcpy(res,name.c_str());
res2 = dirname(res);
String res3 = String(res2);
free(res);
return File(res3);
}
long File::getSize(){
struct stat s;
if( stat(name.c_str(),&s)!=0 ){
return -1;
}
return s.st_size;
}
//using namespace std;
bool File::mkdir(bool withParents){
// cout << "mkdir " << name << endl;
if( isDir() ){
// cout << "is already created" << endl;
// cout << "success" << endl;
return true;
}
File fParent = getParent();
if( fParent.getPath().equals(".") ){
// cout << name << " is root" << endl;
// cout << "failed" << endl;
return false;
}
if( fParent.isDir() ){
// cout << "parent exists" << endl;
if( _mkdir( name.c_str() )==0 ){
// cout << "must be created" << endl;
return true;
}
// cout << "failed" << endl;
return false;
}else{
// cout << "parent not exists" << endl;
if( withParents ){
if( fParent.mkdir(true) ){
if( _mkdir( name.c_str() )==0 ){
// cout << "must be created" << endl;
// cout << "success" << endl;
return true;
}
}
}else{
// cout << "failed" << endl;
return false;
}
}
// cout << "failed" << endl;
return false;
}
RandomAccessFile File::open(const String& mode) throw (IOError) {
//RandomAccessFile rafile(name.replaceAll("\\","/").c_str());
RandomAccessFile rafile(name.c_str());
rafile.open(mode);
return rafile;
}
void File::copy(const File& target){
File trgt(target);
// std::cout << "copy " << name.c_str() << " to " << trgt.getPath() << std::endl;
if( isFile() ){
File tarDir = trgt.getParent();
if( !tarDir.isExists() ){
if( !tarDir.mkdir(true) ){
String mess("can't create dir: ");
mess = mess.append(tarDir.getPath());
throw new IOError(-1,mess);
}
}
RandomAccessFile rSrc = open("r");
RandomAccessFile rTarget = trgt.open("w+");
char buff[4096];
while(true){
bool eof = rSrc.isEOF();
if( eof ){
break;
}
size_t rd = rSrc.read(buff,sizeof(buff));
// std::cout << "readed " << rd << " bytes" << std::endl;
if( rd>0 ){
rTarget.write(buff,rd);
}
if( rd<0 )break;
}
rTarget.flush();
rTarget.close();
rSrc.close();
return;
}
if( isDir() ){
if( !trgt.isExists() ){
if( !trgt.mkdir(true) ){
String mess("can't create dir: ");
mess = mess.append(trgt.getPath());
throw new IOError(-1,mess);
}
}
if( !trgt.isDir() ){
String mess("is not dir: ");
mess = mess.append(trgt.getPath());
throw new IOError(-1,mess);
}
std::vector<File> files = readdir();
for( int idx=0; idx<files.size(); idx++ ){
File srcFile = files[idx];
File tFile = File(*this,srcFile.getName());
srcFile.copy(tFile);
}
}
}
std::vector<File> File::readdir(){
std::vector<File> files;
DIR *dir=0;
dir = opendir(name.c_str());
if( !dir ){
String mess("can't open dir");
mess.append(name.c_str());
throw new IOError(-1,mess);
}
dirent* dirEntry;
while(true){
dirEntry = ::readdir(dir);
if( dirEntry==NULL )break;
char* _entryName = (char *)malloc(dirEntry->d_namlen+1);
for( int is=0; is<dirEntry->d_namlen; is++ ){
_entryName[is] = dirEntry->d_name[is];
}
_entryName[dirEntry->d_namlen] = 0;
String fileName(_entryName);
free( _entryName );
if( fileName.equals(".") )continue;
if( fileName.equals("..") )continue;
File fDir(name.c_str());
File f(fDir,fileName);
files.push_back(f);
}
closedir(dir);
return files;
}
File File::getTempDir(){
String tmpDirString( getenv("TEMP") );
File tmpDir(tmpDirString);
return tmpDir;
}
File File::getCurrentDir(){
char currentDir[16384];
if( getcwd( currentDir,16384 )==0 ){
String mess("can't read current dir (getcwd)");
throw new IOError(-1,mess);
}
String curDirString( currentDir );
File curDir(curDirString);
return curDir;
}
bool File::setCurrentDir(File& file){
String p = file.getPath();
return chdir( p.c_str() )==0;
}
bool File::remove(){
if( isFile() ){
if( DeleteFile( name.c_str() )==0 ){
std::cout << "error delete file " << name << std::endl;
std::cout << "GetLastError()=" << GetLastError() << std::endl;
return false;
}
return true;
}
if( isDir() ){
std::vector<File> content = readdir();
bool succDel = true;
for( int i=0; i<content.size(); i++ ){
if( !content[i].remove() )succDel = false;
}
if( succDel ){
if( RemoveDirectory( name.c_str() )==0 ){
std::cout << "error delete dir " << name << std::endl;
std::cout << "GetLastError()=" << GetLastError() << std::endl;
return false;
}
}
return true;
}
std::cout << "can't delete " << name << std::endl;
return false;
}