31 #include <sys/types.h>
35 #if !defined(__GNUC__) && !defined(_WIN32_WCE)
62 #define S_IREAD _S_IREAD
63 #define S_IWRITE _S_IWRITE
116 return ::write(fd, buf, count);
125 stdstr(&this->path, path);
144 return lseek(
getFd(), offset, whence);
149 return ::read(
getFd(), buf, count);
183 for (tmp = &
files; *tmp; tmp = &((*tmp)->next)) {
188 tmp2 =
new FileDesc(
this, path, mode, perms, tryDowngrade);
199 for (loop = &
files; *loop; loop = &((*loop)->next)) {
201 *loop = (*loop)->
next;
213 for (loop = &
files; *loop; loop = &((*loop)->next)) {
215 if ((*loop)->fd > 0) {
224 if (*loop !=
files) {
225 *loop = (*loop)->
next;
230 char tries = (((file->
mode & O_RDWR) == O_RDWR) && (file->
tryDowngrade)) ? 2 : 1;
231 for (
int i = 0; i < tries; i++) {
233 file->
mode = (file->
mode & ~O_RDWR);
234 file->
mode = (file->
mode | O_RDONLY);
258 static const char *writeTest =
"x";
262 char nibble [ 32767 ];
263 bool writable = file->
write(writeTest, 1);
268 char *buf =
new char [ strlen(file->
path) + 10 ];
270 for (i = 0; i < 9999; i++) {
271 sprintf(buf,
"%stmp%.4d", file->
path, i);
279 if (!fd || fd->
getFd() < 0)
284 bytes = (int)file->
read(nibble, 32767);
285 bytes = (bytes <
size)?bytes:(
int)
size;
286 if (fd->
write(nibble, bytes) != bytes) {
break; }
298 bytes = fd->
read(nibble, 32767);
299 file->
write(nibble, bytes);
300 }
while (bytes == 32767);
321 getenv(variableName);
330 !_waccess((
const wchar_t *)
utf8ToWChar(path).getRawData(), mode);
339 int len = (int)strlen(ipath) + ((ifileName)?strlen(ifileName):0) + 3;
341 char *path =
new char [ len ];
344 if ((path[strlen(path)-1] ==
'\\') || (path[strlen(path)-1] ==
'/'))
345 path[strlen(path)-1] = 0;
348 ch = path + strlen(path);
349 sprintf(ch,
"/%s", ifileName);
351 signed char retVal =
hasAccess(path, 04) ? 1 : 0;
360 int len = (int)strlen(ipath) + ((idirName)?strlen(idirName):0) + 1;
362 len += strlen(idirName);
363 char *path =
new char [ len ];
366 if ((path[strlen(path)-1] ==
'\\') || (path[strlen(path)-1] ==
'/'))
367 path[strlen(path)-1] = 0;
370 ch = path + strlen(path);
371 sprintf(ch,
"/%s", idirName);
373 signed char retVal =
hasAccess(path, 04) ? 1 : 0;
379 std::vector<struct DirEntry>
FileMgr::getDirList(
const char *dirPath,
bool includeSize,
bool includeIsDirectory) {
380 std::vector<struct DirEntry> dirList;
381 SWBuf basePath = dirPath;
387 if ((dir =
opendir(dirPath))) {
390 if ((strcmp(ent->
d_name,
".")) && (strcmp(ent->
d_name,
".."))) {
395 dirList.push_back(i);
403 WIN32_FIND_DATAW fileData;
405 const wchar_t *wcharPath = (
const wchar_t *)wcharBuf.
getRawData();
406 HANDLE findIterator = FindFirstFileW(wcharPath, &fileData);
407 if (findIterator != INVALID_HANDLE_VALUE) {
410 if (dirEntName !=
"." && dirEntName !=
"..") {
413 i.
isDirectory = fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
415 dirList.push_back(i);
417 }
while (FindNextFileW(findIterator, &fileData) != 0);
418 FindClose(findIterator);
427 char *buf =
new char [ strlen(pName) + 1 ];
431 int end = (int)strlen(buf) - 1;
433 if ((buf[end] ==
'/') || (buf[end] ==
'\\'))
444 _wmkdir((
const wchar_t *)
utf8ToWChar(buf).getRawData());
452 _wmkdir((
const wchar_t *)
utf8ToWChar(buf).getRawData());
466 ::open(fName, mode, perms);
468 ::_wopen((
const wchar_t *)
utf8ToWChar(fName).getRawData(), mode, perms);
506 len = (int)read(sfd, buf, 4096);
507 if (
write(dfd, buf, len) != len)
break;
522 ::_wremove((
const wchar_t *)
utf8ToWChar(fName).getRawData());
535 if (fDesc->
getFd() < 1)
541 len = (int)fDesc->
read(chunk, 254);
550 for (;start < len; start++) {
551 if ((chunk[start] != 13) && (chunk[start] !=
' ') && (chunk[start] !=
'\t'))
558 for (end = start; ((end < (len-1)) && (chunk[end] != 10)); end++);
560 if ((chunk[end] != 10) && (len == 254)) {
570 for (; end > start; end--) {
571 if ((chunk[end] != 10) && (chunk[end] != 13) && (chunk[end] !=
' ') && (chunk[end] !=
'\t')) {
572 if (chunk[end] ==
'\\') {
581 int size = (end - start) + 1;
585 line.
append(chunk+start, size);
588 return ((len > 0) || line.
length());
595 int error = stat(path, &stats);
598 int error = _wstat((
const wchar_t *)
utf8ToWChar(path).getRawData(), &stats);
601 return ((stats.st_mode & S_IFDIR) == S_IFDIR);
608 int error = stat(path, &stats);
611 int error = _wstat((
const wchar_t *)
utf8ToWChar(path).getRawData(), &stats);
614 return stats.st_size;
619 SWBuf baseSrcPath = srcDir;
620 if (!baseSrcPath.
endsWith(
"/") && !baseSrcPath.
endsWith(
"\\")) baseSrcPath +=
"/";
621 SWBuf baseDestPath = destDir;
622 if (!baseDestPath.
endsWith(
"/") && !baseDestPath.
endsWith(
"\\")) baseDestPath +=
"/";
624 std::vector<DirEntry> dirList =
getDirList(srcDir);
625 for (
unsigned int i = 0; i < dirList.size() && !retVal; ++i) {
626 SWBuf srcPath = baseSrcPath + dirList[i].name;
627 SWBuf destPath = baseDestPath + dirList[i].name;
640 SWBuf basePath = targetDir;
642 std::vector<DirEntry> dirList =
getDirList(targetDir);
643 for (
unsigned int i = 0; i < dirList.size(); ++i) {
644 SWBuf targetPath = basePath + dirList[i].name;
660 for (loop = &
files; *loop; loop = &((*loop)->next)) {
661 if ((*loop)->fd > 0) {
672 for (loop = &
files; *loop; loop = &((*loop)->next)) {
673 if ((*loop)->fd > 0) {
690 if ((homeDir[homeDir.
length()-1] !=
'\\') && (homeDir[homeDir.
length()-1] !=
'/')) {
static signed char existsFile(const char *ipath, const char *ifileName=0)
#define SWORD_NAMESPACE_START
long seek(long offset, int whence)
static int removeDir(const char *targetDir)
FileDesc * open(const char *path, int mode, bool tryDowngrade)
unsigned long length() const
static signed char existsDir(const char *ipath, const char *idirName=0)
static unsigned int RDONLY
static SWBuf getEnvValue(const char *variableName)
static int openFileReadOnly(const char *fName)
SWBuf wcharToUTF8(const wchar_t *buf)
static unsigned int TRUNC
static void closeFile(int fd)
SWBuf utf8ToWChar(const char *buf)
FileDesc(FileMgr *parent, const char *path, int mode, int perms, bool tryDowngrade)
bool endsWith(const SWBuf &postfix) const
long write(const void *buf, long count)
signed char trunc(FileDesc *file)
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
void close(FileDesc *file)
const char * c_str() const
SWBuf & append(const char *str, long max=-1)
static int removeFile(const char *fName)
SWDLLEXPORT void rewinddir(DIR *dir)
static int copyFile(const char *srcFile, const char *destFile)
static bool hasAccess(const char *path, int mode)
static unsigned int APPEND
SWDLLEXPORT DIR * opendir(const char *name)
SWDLLEXPORT struct dirent * readdir(DIR *dir)
int sysOpen(FileDesc *file)
static int createParent(const char *pName)
SWDLLEXPORT int closedir(DIR *dir)
static std::vector< struct DirEntry > getDirList(const char *dirPath, bool includeSize=false, bool includeIsDirectory=true)
static long write(int fd, const void *buf, long count)
static long getFileSize(const char *path)
static unsigned int CREAT
virtual long resourceConsumption()
static void setSystemFileMgr(FileMgr *newFileMgr)
static FileMgr * systemFileMgr
static int createPathAndFile(const char *fName)
static int copyDir(const char *srcDir, const char *destDir)
static unsigned int IWRITE
static unsigned int WRONLY
long read(void *buf, long count)
static char getLine(FileDesc *fDesc, SWBuf &line)
static unsigned int IREAD
static int openFile(const char *fName, int mode, int perms)
#define SWORD_NAMESPACE_END
class __staticsystemFileMgr _staticsystemFileMgr
static FileMgr * getSystemFileMgr()
static char isDirectory(const char *path)