102 lines
2.7 KiB
Plaintext
102 lines
2.7 KiB
Plaintext
// Generic save/file helpers.
|
|
|
|
bool save_utils_sort_string_case_insensitive(const string &in a, const string &in b) {
|
|
return a.lower() < b.lower();
|
|
}
|
|
|
|
string[] list_files_with_extension(const string&in extension, const string&in directory = "") {
|
|
string[] result;
|
|
if (extension == "") return result;
|
|
|
|
string dirPrefix = directory;
|
|
if (dirPrefix != "" && dirPrefix.substr(dirPrefix.length() - 1) != "/") {
|
|
dirPrefix += "/";
|
|
}
|
|
|
|
string[]@ items = glob(dirPrefix + "*" + extension);
|
|
if (@items is null) return result;
|
|
|
|
for (uint itemIndex = 0; itemIndex < items.length(); itemIndex++) {
|
|
string item = items[itemIndex];
|
|
if (item.length() >= extension.length() &&
|
|
item.substr(item.length() - extension.length()) == extension) {
|
|
result.insert_last(item);
|
|
}
|
|
}
|
|
|
|
if (result.length() > 1) {
|
|
result.sort(save_utils_sort_string_case_insensitive);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
bool has_files_with_extension(const string&in extension, const string&in directory = "") {
|
|
return list_files_with_extension(extension, directory).length() > 0;
|
|
}
|
|
|
|
string strip_file_extension(const string&in filename, const string&in extension) {
|
|
if (extension != "" &&
|
|
filename.length() >= extension.length() &&
|
|
filename.substr(filename.length() - extension.length()) == extension) {
|
|
return filename.substr(0, filename.length() - extension.length());
|
|
}
|
|
return filename;
|
|
}
|
|
|
|
bool save_string_file(const string&in filename, const string&in data) {
|
|
if (data.length() == 0) {
|
|
return false;
|
|
}
|
|
|
|
file outFile;
|
|
if (!outFile.open(filename, "wb")) {
|
|
return false;
|
|
}
|
|
|
|
if (outFile.write(data) < data.length()) {
|
|
outFile.close();
|
|
return false;
|
|
}
|
|
|
|
outFile.close();
|
|
return true;
|
|
}
|
|
|
|
bool read_string_file(const string&in filename, string&out data, const bool allowEmpty = false) {
|
|
file inFile;
|
|
if (!inFile.open(filename, "rb")) {
|
|
return false;
|
|
}
|
|
|
|
data = inFile.read();
|
|
inFile.close();
|
|
return allowEmpty || data.length() > 0;
|
|
}
|
|
|
|
string encrypt_string_aes(const string&in rawData, const string&in key) {
|
|
return string_aes_encrypt(rawData, key);
|
|
}
|
|
|
|
string decrypt_string_aes(const string&in encryptedData, const string&in key) {
|
|
return string_aes_decrypt(encryptedData, key);
|
|
}
|
|
|
|
bool save_encrypted_file(const string&in filename, const string&in rawData, const string&in key) {
|
|
string encryptedData = encrypt_string_aes(rawData, key);
|
|
return save_string_file(filename, encryptedData);
|
|
}
|
|
|
|
bool read_encrypted_file(const string&in filename, const string&in key, string&out rawData, const bool allowPlaintextFallback = true) {
|
|
string encryptedData = "";
|
|
if (!read_string_file(filename, encryptedData, false)) {
|
|
return false;
|
|
}
|
|
|
|
rawData = decrypt_string_aes(encryptedData, key);
|
|
if (rawData.length() == 0 && allowPlaintextFallback) {
|
|
rawData = encryptedData;
|
|
}
|
|
|
|
return rawData.length() > 0;
|
|
}
|