2018-07-13 05:17:08 +08:00
|
|
|
/* filesystem.h
|
|
|
|
|
|
|
|
Copyright (c) 2015, Nikolaj Schlej. All rights reserved.
|
|
|
|
This program and the accompanying materials
|
|
|
|
are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
which accompanies this distribution. The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php
|
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FILESYSTEM_H
|
|
|
|
#define FILESYSTEM_H
|
|
|
|
|
|
|
|
#include "basetypes.h"
|
|
|
|
#include "ustring.h"
|
2018-10-08 17:58:12 +08:00
|
|
|
#include "ubytearray.h"
|
2018-07-13 05:17:08 +08:00
|
|
|
#include <sys/stat.h>
|
2018-10-08 17:58:12 +08:00
|
|
|
#include <fstream>
|
2018-07-13 05:17:08 +08:00
|
|
|
|
2022-09-27 23:36:27 +08:00
|
|
|
#if defined(_WIN32) || defined(__MINGW32__)
|
2018-07-13 05:17:08 +08:00
|
|
|
#include <direct.h>
|
2018-10-10 22:20:00 +08:00
|
|
|
#include <stdlib.h>
|
2018-07-13 05:17:08 +08:00
|
|
|
static inline bool isExistOnFs(const UString & path) {
|
|
|
|
struct _stat buf;
|
2018-08-02 08:41:11 +08:00
|
|
|
return (_stat(path.toLocal8Bit(), &buf) == 0);
|
2018-07-13 05:17:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool makeDirectory(const UString & dir) {
|
2018-08-02 08:41:11 +08:00
|
|
|
return (_mkdir(dir.toLocal8Bit()) == 0);
|
2018-07-13 05:17:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool changeDirectory(const UString & dir) {
|
2018-08-02 08:41:11 +08:00
|
|
|
return (_chdir(dir.toLocal8Bit()) == 0);
|
2018-07-13 05:17:08 +08:00
|
|
|
}
|
2018-10-10 22:20:00 +08:00
|
|
|
|
2018-10-11 04:41:53 +08:00
|
|
|
static inline void removeDirectory(const UString & dir) {
|
2018-11-12 19:49:35 +08:00
|
|
|
int r = _rmdir(dir.toLocal8Bit());
|
|
|
|
// Hack: unlike *nix, Windows does not permit deleting current directories.
|
|
|
|
if (r < 0 && errno == EACCES && changeDirectory(dir + UString("/../"))) {
|
|
|
|
_rmdir(dir.toLocal8Bit());
|
|
|
|
}
|
2018-10-11 04:41:53 +08:00
|
|
|
}
|
|
|
|
|
2018-10-10 22:25:31 +08:00
|
|
|
static inline UString getAbsPath(const UString & path) {
|
2018-11-12 19:49:35 +08:00
|
|
|
char abs[_MAX_PATH] = {};
|
2018-10-10 22:20:00 +08:00
|
|
|
if (_fullpath(abs, path.toLocal8Bit(), sizeof(abs)))
|
2018-10-10 22:25:31 +08:00
|
|
|
return UString(abs);
|
2018-10-10 22:20:00 +08:00
|
|
|
return path;
|
|
|
|
}
|
2018-07-13 05:17:08 +08:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
2018-10-10 22:20:00 +08:00
|
|
|
#include <stdlib.h>
|
2018-07-13 05:17:08 +08:00
|
|
|
static inline bool isExistOnFs(const UString & path) {
|
|
|
|
struct stat buf;
|
2018-08-02 08:41:11 +08:00
|
|
|
return (stat(path.toLocal8Bit(), &buf) == 0);
|
2018-07-13 05:17:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool makeDirectory(const UString & dir) {
|
2018-08-02 08:41:11 +08:00
|
|
|
return (mkdir(dir.toLocal8Bit(), ACCESSPERMS) == 0);
|
2018-07-13 05:17:08 +08:00
|
|
|
}
|
|
|
|
|
2018-10-11 04:41:53 +08:00
|
|
|
static inline void removeDirectory(const UString & dir) {
|
|
|
|
rmdir(dir.toLocal8Bit());
|
|
|
|
}
|
|
|
|
|
2018-07-13 05:17:08 +08:00
|
|
|
static inline bool changeDirectory(const UString & dir) {
|
2018-08-02 08:41:11 +08:00
|
|
|
return (chdir(dir.toLocal8Bit()) == 0);
|
2018-07-13 05:17:08 +08:00
|
|
|
}
|
2018-10-10 22:20:00 +08:00
|
|
|
|
|
|
|
static inline UString getAbsPath(const UString & path) {
|
2018-10-10 22:23:45 +08:00
|
|
|
char abs[PATH_MAX] = {};
|
2018-10-10 22:20:00 +08:00
|
|
|
// Last is a non-standard extension for non-existent files.
|
|
|
|
if (realpath(path.toLocal8Bit(), abs) || abs[0] != '\0')
|
|
|
|
return UString(abs);
|
|
|
|
return path;
|
|
|
|
}
|
2018-07-13 05:17:08 +08:00
|
|
|
#endif
|
|
|
|
|
2018-10-08 17:58:12 +08:00
|
|
|
static inline USTATUS readFileIntoBuffer(const UString & inPath, UByteArray &buf) {
|
|
|
|
if (!isExistOnFs(inPath))
|
|
|
|
return U_FILE_OPEN;
|
|
|
|
|
|
|
|
std::ifstream inputFile(inPath.toLocal8Bit(), std::ios::in | std::ios::binary);
|
|
|
|
if (!inputFile)
|
|
|
|
return U_FILE_OPEN;
|
|
|
|
std::vector<char> buffer(std::istreambuf_iterator<char>(inputFile),
|
|
|
|
(std::istreambuf_iterator<char>()));
|
|
|
|
inputFile.close();
|
|
|
|
|
|
|
|
buf = buffer;
|
|
|
|
|
|
|
|
return U_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2018-07-13 05:17:08 +08:00
|
|
|
#endif
|