summaryrefslogtreecommitdiff
path: root/core/os/file_access.cpp
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <rverschelde@gmail.com>2020-05-14 23:09:03 +0200
committerGitHub <noreply@github.com>2020-05-14 23:09:03 +0200
commit00949f0c5fcc6a4f8382a4a97d5591fd9ec380f8 (patch)
tree2b1c31f45add24085b64425ce440f577424c16a1 /core/os/file_access.cpp
parent5046f666a1181675b39f156c38346525dc1c444e (diff)
parent0ee0fa42e6639b6fa474b7cf6afc6b1a78142185 (diff)
Merge pull request #38738 from akien-mga/cause-we-never-go-out-of-style
Style: Remove new line at block start, enforce line between functions, enforce braces in if and loop blocks
Diffstat (limited to 'core/os/file_access.cpp')
-rw-r--r--core/os/file_access.cpp140
1 files changed, 46 insertions, 94 deletions
diff --git a/core/os/file_access.cpp b/core/os/file_access.cpp
index cb8705f706..f9ba8ff2d2 100644
--- a/core/os/file_access.cpp
+++ b/core/os/file_access.cpp
@@ -43,7 +43,6 @@ FileAccess::FileCloseFailNotify FileAccess::close_fail_notify = nullptr;
bool FileAccess::backup_save = false;
FileAccess *FileAccess::create(AccessType p_access) {
-
ERR_FAIL_INDEX_V(p_access, ACCESS_MAX, nullptr);
FileAccess *ret = create_func[p_access]();
@@ -52,34 +51,30 @@ FileAccess *FileAccess::create(AccessType p_access) {
}
bool FileAccess::exists(const String &p_name) {
-
- if (PackedData::get_singleton() && PackedData::get_singleton()->has_path(p_name))
+ if (PackedData::get_singleton() && PackedData::get_singleton()->has_path(p_name)) {
return true;
+ }
FileAccess *f = open(p_name, READ);
- if (!f)
+ if (!f) {
return false;
+ }
memdelete(f);
return true;
}
void FileAccess::_set_access_type(AccessType p_access) {
-
_access_type = p_access;
};
FileAccess *FileAccess::create_for_path(const String &p_path) {
-
FileAccess *ret = nullptr;
if (p_path.begins_with("res://")) {
-
ret = create(ACCESS_RESOURCES);
} else if (p_path.begins_with("user://")) {
-
ret = create(ACCESS_USERDATA);
} else {
-
ret = create(ACCESS_FILESYSTEM);
}
@@ -87,20 +82,19 @@ FileAccess *FileAccess::create_for_path(const String &p_path) {
}
Error FileAccess::reopen(const String &p_path, int p_mode_flags) {
-
return _open(p_path, p_mode_flags);
};
FileAccess *FileAccess::open(const String &p_path, int p_mode_flags, Error *r_error) {
-
//try packed data first
FileAccess *ret = nullptr;
if (!(p_mode_flags & WRITE) && PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled()) {
ret = PackedData::get_singleton()->try_open_path(p_path);
if (ret) {
- if (r_error)
+ if (r_error) {
*r_error = OK;
+ }
return ret;
}
}
@@ -108,10 +102,10 @@ FileAccess *FileAccess::open(const String &p_path, int p_mode_flags, Error *r_er
ret = create_for_path(p_path);
Error err = ret->_open(p_path, p_mode_flags);
- if (r_error)
+ if (r_error) {
*r_error = err;
+ }
if (err != OK) {
-
memdelete(ret);
ret = nullptr;
}
@@ -120,7 +114,6 @@ FileAccess *FileAccess::open(const String &p_path, int p_mode_flags, Error *r_er
}
FileAccess::CreateFunc FileAccess::get_create_func(AccessType p_access) {
-
return create_func[p_access];
};
@@ -130,15 +123,11 @@ String FileAccess::fix_path(const String &p_path) const {
String r_path = p_path.replace("\\", "/");
switch (_access_type) {
-
case ACCESS_RESOURCES: {
-
if (ProjectSettings::get_singleton()) {
if (r_path.begins_with("res://")) {
-
String resource_path = ProjectSettings::get_singleton()->get_resource_path();
if (resource_path != "") {
-
return r_path.replace("res:/", resource_path);
};
return r_path.replace("res://", "");
@@ -147,12 +136,9 @@ String FileAccess::fix_path(const String &p_path) const {
} break;
case ACCESS_USERDATA: {
-
if (r_path.begins_with("user://")) {
-
String data_dir = OS::get_singleton()->get_user_data_dir();
if (data_dir != "") {
-
return r_path.replace("user:/", data_dir);
};
return r_path.replace("user://", "");
@@ -160,7 +146,6 @@ String FileAccess::fix_path(const String &p_path) const {
} break;
case ACCESS_FILESYSTEM: {
-
return r_path;
} break;
case ACCESS_MAX:
@@ -173,7 +158,6 @@ String FileAccess::fix_path(const String &p_path) const {
/* these are all implemented for ease of porting, then can later be optimized */
uint16_t FileAccess::get_16() const {
-
uint16_t res;
uint8_t a, b;
@@ -181,7 +165,6 @@ uint16_t FileAccess::get_16() const {
b = get_8();
if (endian_swap) {
-
SWAP(a, b);
}
@@ -191,8 +174,8 @@ uint16_t FileAccess::get_16() const {
return res;
}
-uint32_t FileAccess::get_32() const {
+uint32_t FileAccess::get_32() const {
uint32_t res;
uint16_t a, b;
@@ -200,7 +183,6 @@ uint32_t FileAccess::get_32() const {
b = get_16();
if (endian_swap) {
-
SWAP(a, b);
}
@@ -210,8 +192,8 @@ uint32_t FileAccess::get_32() const {
return res;
}
-uint64_t FileAccess::get_64() const {
+uint64_t FileAccess::get_64() const {
uint64_t res;
uint32_t a, b;
@@ -219,7 +201,6 @@ uint64_t FileAccess::get_64() const {
b = get_32();
if (endian_swap) {
-
SWAP(a, b);
}
@@ -231,38 +212,35 @@ uint64_t FileAccess::get_64() const {
}
float FileAccess::get_float() const {
-
MarshallFloat m;
m.i = get_32();
return m.f;
};
real_t FileAccess::get_real() const {
-
- if (real_is_double)
+ if (real_is_double) {
return get_double();
- else
+ } else {
return get_float();
+ }
}
double FileAccess::get_double() const {
-
MarshallDouble m;
m.l = get_64();
return m.d;
};
String FileAccess::get_token() const {
-
CharString token;
CharType c = get_8();
while (!eof_reached()) {
-
if (c <= ' ') {
- if (token.length())
+ if (token.length()) {
break;
+ }
} else {
token += c;
}
@@ -281,16 +259,13 @@ class CharBuffer {
int written = 0;
bool grow() {
-
if (vector.resize(next_power_of_2(1 + written)) != OK) {
-
return false;
}
if (buffer == stack_buffer) { // first chunk?
for (int i = 0; i < written; i++) {
-
vector.write[i] = stack_buffer[i];
}
}
@@ -309,9 +284,7 @@ public:
}
_FORCE_INLINE_ void push_back(char c) {
-
if (written >= capacity) {
-
ERR_FAIL_COND(!grow());
}
@@ -319,24 +292,22 @@ public:
}
_FORCE_INLINE_ const char *get_data() const {
-
return buffer;
}
};
String FileAccess::get_line() const {
-
CharBuffer line;
CharType c = get_8();
while (!eof_reached()) {
-
if (c == '\n' || c == '\0') {
line.push_back(0);
return String::utf8(line.get_data());
- } else if (c != '\r')
+ } else if (c != '\r') {
line.push_back(c);
+ }
c = get_8();
}
@@ -345,21 +316,21 @@ String FileAccess::get_line() const {
}
Vector<String> FileAccess::get_csv_line(const String &p_delim) const {
-
ERR_FAIL_COND_V(p_delim.length() != 1, Vector<String>());
String l;
int qc = 0;
do {
- if (eof_reached())
+ if (eof_reached()) {
break;
+ }
l += get_line() + "\n";
qc = 0;
for (int i = 0; i < l.length(); i++) {
-
- if (l[i] == '"')
+ if (l[i] == '"') {
qc++;
+ }
}
} while (qc % 2);
@@ -371,7 +342,6 @@ Vector<String> FileAccess::get_csv_line(const String &p_delim) const {
bool in_quote = false;
String current;
for (int i = 0; i < l.length(); i++) {
-
CharType c = l[i];
CharType s[2] = { 0, 0 };
@@ -384,7 +354,6 @@ Vector<String> FileAccess::get_csv_line(const String &p_delim) const {
current += s;
i++;
} else {
-
in_quote = !in_quote;
}
} else {
@@ -399,10 +368,10 @@ Vector<String> FileAccess::get_csv_line(const String &p_delim) const {
}
int FileAccess::get_buffer(uint8_t *p_dst, int p_length) const {
-
int i = 0;
- for (i = 0; i < p_length && !eof_reached(); i++)
+ for (i = 0; i < p_length && !eof_reached(); i++) {
p_dst[i] = get_8();
+ }
return i;
}
@@ -425,44 +394,40 @@ String FileAccess::get_as_utf8_string() const {
}
void FileAccess::store_16(uint16_t p_dest) {
-
uint8_t a, b;
a = p_dest & 0xFF;
b = p_dest >> 8;
if (endian_swap) {
-
SWAP(a, b);
}
store_8(a);
store_8(b);
}
-void FileAccess::store_32(uint32_t p_dest) {
+void FileAccess::store_32(uint32_t p_dest) {
uint16_t a, b;
a = p_dest & 0xFFFF;
b = p_dest >> 16;
if (endian_swap) {
-
SWAP(a, b);
}
store_16(a);
store_16(b);
}
-void FileAccess::store_64(uint64_t p_dest) {
+void FileAccess::store_64(uint64_t p_dest) {
uint32_t a, b;
a = p_dest & 0xFFFFFFFF;
b = p_dest >> 32;
if (endian_swap) {
-
SWAP(a, b);
}
@@ -471,31 +436,29 @@ void FileAccess::store_64(uint64_t p_dest) {
}
void FileAccess::store_real(real_t p_real) {
-
- if (sizeof(real_t) == 4)
+ if (sizeof(real_t) == 4) {
store_float(p_real);
- else
+ } else {
store_double(p_real);
+ }
}
void FileAccess::store_float(float p_dest) {
-
MarshallFloat m;
m.f = p_dest;
store_32(m.i);
};
void FileAccess::store_double(double p_dest) {
-
MarshallDouble m;
m.d = p_dest;
store_64(m.l);
};
uint64_t FileAccess::get_modified_time(const String &p_file) {
-
- if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_file))
+ if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_file)) {
return 0;
+ }
FileAccess *fa = create_for_path(p_file);
ERR_FAIL_COND_V_MSG(!fa, 0, "Cannot create FileAccess for path '" + p_file + "'.");
@@ -506,9 +469,9 @@ uint64_t FileAccess::get_modified_time(const String &p_file) {
}
uint32_t FileAccess::get_unix_permissions(const String &p_file) {
-
- if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_file))
+ if (PackedData::get_singleton() && !PackedData::get_singleton()->is_disabled() && PackedData::get_singleton()->has_path(p_file)) {
return 0;
+ }
FileAccess *fa = create_for_path(p_file);
ERR_FAIL_COND_V_MSG(!fa, 0, "Cannot create FileAccess for path '" + p_file + "'.");
@@ -519,7 +482,6 @@ uint32_t FileAccess::get_unix_permissions(const String &p_file) {
}
Error FileAccess::set_unix_permissions(const String &p_file, uint32_t p_permissions) {
-
FileAccess *fa = create_for_path(p_file);
ERR_FAIL_COND_V_MSG(!fa, ERR_CANT_CREATE, "Cannot create FileAccess for path '" + p_file + "'.");
@@ -529,23 +491,21 @@ Error FileAccess::set_unix_permissions(const String &p_file, uint32_t p_permissi
}
void FileAccess::store_string(const String &p_string) {
-
- if (p_string.length() == 0)
+ if (p_string.length() == 0) {
return;
+ }
CharString cs = p_string.utf8();
store_buffer((uint8_t *)&cs[0], cs.length());
}
void FileAccess::store_pascal_string(const String &p_string) {
-
CharString cs = p_string.utf8();
store_32(cs.length());
store_buffer((uint8_t *)&cs[0], cs.length());
};
String FileAccess::get_pascal_string() {
-
uint32_t sl = get_32();
CharString cs;
cs.resize(sl + 1);
@@ -559,13 +519,11 @@ String FileAccess::get_pascal_string() {
};
void FileAccess::store_line(const String &p_line) {
-
store_string(p_line);
store_8('\n');
}
void FileAccess::store_csv_line(const Vector<String> &p_values, const String &p_delim) {
-
ERR_FAIL_COND(p_delim.length() != 1);
String line = "";
@@ -587,13 +545,12 @@ void FileAccess::store_csv_line(const Vector<String> &p_values, const String &p_
}
void FileAccess::store_buffer(const uint8_t *p_src, int p_length) {
-
- for (int i = 0; i < p_length; i++)
+ for (int i = 0; i < p_length; i++) {
store_8(p_src[i]);
+ }
}
Vector<uint8_t> FileAccess::get_file_as_array(const String &p_path, Error *r_error) {
-
FileAccess *f = FileAccess::open(p_path, READ, r_error);
if (!f) {
if (r_error) { // if error requested, do not throw error
@@ -609,7 +566,6 @@ Vector<uint8_t> FileAccess::get_file_as_array(const String &p_path, Error *r_err
}
String FileAccess::get_file_as_string(const String &p_path, Error *r_error) {
-
Error err;
Vector<uint8_t> array = get_file_as_array(p_path, &err);
if (r_error) {
@@ -628,10 +584,10 @@ String FileAccess::get_file_as_string(const String &p_path, Error *r_error) {
}
String FileAccess::get_md5(const String &p_file) {
-
FileAccess *f = FileAccess::open(p_file, READ);
- if (!f)
+ if (!f) {
return String();
+ }
CryptoCore::MD5Context ctx;
ctx.start();
@@ -639,14 +595,13 @@ String FileAccess::get_md5(const String &p_file) {
unsigned char step[32768];
while (true) {
-
int br = f->get_buffer(step, 32768);
if (br > 0) {
-
ctx.update(step, br);
}
- if (br < 4096)
+ if (br < 4096) {
break;
+ }
}
unsigned char hash[16];
@@ -658,7 +613,6 @@ String FileAccess::get_md5(const String &p_file) {
}
String FileAccess::get_multiple_md5(const Vector<String> &p_file) {
-
CryptoCore::MD5Context ctx;
ctx.start();
@@ -669,14 +623,13 @@ String FileAccess::get_multiple_md5(const Vector<String> &p_file) {
unsigned char step[32768];
while (true) {
-
int br = f->get_buffer(step, 32768);
if (br > 0) {
-
ctx.update(step, br);
}
- if (br < 4096)
+ if (br < 4096) {
break;
+ }
}
memdelete(f);
}
@@ -688,10 +641,10 @@ String FileAccess::get_multiple_md5(const Vector<String> &p_file) {
}
String FileAccess::get_sha256(const String &p_file) {
-
FileAccess *f = FileAccess::open(p_file, READ);
- if (!f)
+ if (!f) {
return String();
+ }
CryptoCore::SHA256Context ctx;
ctx.start();
@@ -699,14 +652,13 @@ String FileAccess::get_sha256(const String &p_file) {
unsigned char step[32768];
while (true) {
-
int br = f->get_buffer(step, 32768);
if (br > 0) {
-
ctx.update(step, br);
}
- if (br < 4096)
+ if (br < 4096) {
break;
+ }
}
unsigned char hash[32];