Batch update libstorm-nvgt modules
This commit is contained in:
141
save_utils.nvgt
141
save_utils.nvgt
@@ -1,101 +1,102 @@
|
||||
// Generic save/file helpers.
|
||||
|
||||
bool save_utils_sort_string_case_insensitive(const string &in a, const string &in b) {
|
||||
return a.lower() < b.lower();
|
||||
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[] 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 dirPrefix = directory;
|
||||
if (dirPrefix != "" && dirPrefix.substr(dirPrefix.length() - 1) != "/") {
|
||||
dirPrefix += "/";
|
||||
}
|
||||
|
||||
string[]@ items = glob(dirPrefix + "*" + extension);
|
||||
if (@items is null) return result;
|
||||
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);
|
||||
}
|
||||
}
|
||||
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;
|
||||
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;
|
||||
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;
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
file outFile;
|
||||
if (!outFile.open(filename, "wb")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (outFile.write(data) < data.length()) {
|
||||
outFile.close();
|
||||
return false;
|
||||
}
|
||||
if (outFile.write(data) < data.length()) {
|
||||
outFile.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
outFile.close();
|
||||
return true;
|
||||
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;
|
||||
}
|
||||
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;
|
||||
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 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);
|
||||
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 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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
rawData = decrypt_string_aes(encryptedData, key);
|
||||
if (rawData.length() == 0 && allowPlaintextFallback) {
|
||||
rawData = encryptedData;
|
||||
}
|
||||
|
||||
return rawData.length() > 0;
|
||||
return rawData.length() > 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user