forked from Mdashdotdashn/LittleGPTracker
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathUnixFileSystem.cpp
More file actions
200 lines (164 loc) · 4.1 KB
/
UnixFileSystem.cpp
File metadata and controls
200 lines (164 loc) · 4.1 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
#include "UnixFileSystem.h"
#include "System/Console/Trace.h"
#include <string.h>
#include <stdarg.h>
#include <unistd.h>
#if defined(_64BIT) || defined(__ANDROID__)
#include <dirent.h>
#else
#include <sys/dir.h>
#endif
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include "Application/Utils/wildcard.h"
UnixDir::UnixDir(const char *path):I_Dir(path) {
} ;
void UnixDir::GetContent(char *mask) {
Empty() ;
DIR* directory;
struct dirent* entry;
directory = opendir (path_);
if (directory == NULL) {
Trace::Error("Failed to open %s",path_) ;
return ;
}
while ((entry = readdir (directory)) != NULL) {
char current[128] ;
strcpy(current,entry->d_name) ;
char *c=current ;
while(*c) {
*c=tolower(*c) ;
c++ ;
}
// Trace::Dump("testing mask") ;
if (wildcardfit (mask,current)) {
strcpy(current,entry->d_name) ;
// Trace::Dump("Inserting %s/%s",path_,current) ;
std::string fullpath=path_ ;
if (path_[strlen(path_)-1]!='/') {
fullpath+="/" ;
}
fullpath+=current ;
Path *path=new Path(fullpath.c_str()) ;
Insert(path) ;
} else {
// Trace::Dump("skipping %s",current) ;
}
} ;
closedir (directory);
};
void UnixDir::GetProjectContent() {
Empty() ;
const char* mask = (const char *) "*";
DIR* directory;
struct dirent* entry;
directory = opendir (path_);
if (directory == NULL) {
Trace::Error("Failed to open %s",path_) ;
return ;
}
while ((entry = readdir (directory)) != NULL) {
char current[128] ;
strcpy(current,entry->d_name) ;
char *c=current ;
while(*c) {
*c=tolower(*c) ;
c++ ;
}
// Trace::Dump("testing mask") ;
if (wildcardfit (mask,current)) {
strcpy(current,entry->d_name) ;
// Trace::Dump("Inserting %s/%s",path_,current) ;
//we dont want any dot files but we do want ..
if(current[0] == '.' && current[1] != '.'){
//de nada
} else {
std::string fullpath=path_ ;
if (path_[strlen(path_)-1]!='/') {
fullpath+="/" ;
}
fullpath+=current ;
Path *path=new Path(fullpath.c_str()) ;
Insert(path) ;
}
} else {
// Trace::Dump("skipping %s",current) ;
}
} ;
closedir (directory);
} ;
UnixFile::UnixFile(FILE *file) {
file_=file ;
} ;
int UnixFile::Read(void *ptr,int size, int nmemb) {
return fread(ptr,size,nmemb,file_) ;
} ;
int UnixFile::Write(const void *ptr,int size, int nmemb) {
return fwrite(ptr,size,nmemb,file_) ;
} ;
void UnixFile::Printf(const char *fmt, ...) {
va_list args;
va_start(args,fmt);
vfprintf(file_,fmt,args );
va_end(args);
} ;
void UnixFile::Seek(long offset,int whence) {
fseek(file_,offset,whence) ;
} ;
long UnixFile::Tell() {
return ftell(file_) ;
} ;
void UnixFile::Close() {
fflush(file_) ;
#ifndef _64BIT
fsync(fileno(file_)) ;
#endif
fclose(file_) ;
} ;
UnixFileSystem::UnixFileSystem() {
}
I_File *UnixFileSystem::Open(const char *path,char *mode) {
char *rmode ;
switch(*mode) {
case 'r':
rmode=(char *)"rb" ;
break ;
case 'w':
rmode=(char *)"wb" ;
break ;
default:
Trace::Error("Invalid mode: %s",mode) ;
return 0 ;
}
FILE *file=fopen(path,rmode) ;
UnixFile *wFile=0 ;
if (file) {
wFile=new UnixFile(file) ;
}
return wFile ;
} ;
I_Dir *UnixFileSystem::Open(const char *path) {
return new UnixDir(path) ;
} ;
FileType UnixFileSystem::GetFileType(const char* path) {
struct stat attributes ;
if (stat(path,&attributes)==0) {
if (attributes.st_mode&S_IFDIR) return FT_DIR ;
if (attributes.st_mode&S_IFREG) return FT_FILE ;
}
return FT_UNKNOWN ;
} ;
void UnixFileSystem::Delete(const char *path) {
remove(path) ;
} ;
Result UnixFileSystem::MakeDir(const char *path) {
int success = mkdir(path,S_IRWXU) ;
if (success <0)
{
std::ostringstream oss;
oss << "Could not create path " << path << " (errno:" << errno << ")";
return Result(oss.str());
}
return Result::NoError;
} ;