mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-02-19 03:13:14 +00:00
Create PropertiesFile functionality and tests
This commit is contained in:
parent
28f11a2cf3
commit
470e65e90f
@ -1,8 +1,13 @@
|
||||
//
|
||||
// Created by ken on 18/08/2025.
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#ifndef TACTILITY_PROPERTIESFILE_H
|
||||
#define TACTILITY_PROPERTIESFILE_H
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#endif //TACTILITY_PROPERTIESFILE_H
|
||||
namespace tt::file {
|
||||
|
||||
bool loadPropertiesFile(const std::string& filePath, std::map<std::string, std::string>& outProperties);
|
||||
|
||||
bool loadPropertiesFile(const std::string& filePath, std::function<void(const std::string& key, const std::string& value)> callback);
|
||||
|
||||
}
|
||||
|
||||
@ -1,3 +1,56 @@
|
||||
//
|
||||
// Created by ken on 18/08/2025.
|
||||
//
|
||||
#include "Tactility/file/PropertiesFile.h"
|
||||
|
||||
#include <Tactility/StringUtils.h>
|
||||
#include <Tactility/file/File.h>
|
||||
|
||||
namespace tt::file {
|
||||
|
||||
static auto TAG = "PropertiesFile";
|
||||
|
||||
bool getKeyValuePair(const std::string& input, std::string& key, std::string& value) {
|
||||
auto index = input.find('=');
|
||||
if (index == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
key = input.substr(0, index);
|
||||
value = input.substr(index + 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool loadPropertiesFile(const std::string& filePath, std::function<void(const std::string& key, const std::string& value)> callback) {
|
||||
auto input = readString(filePath);
|
||||
if (input == nullptr) {
|
||||
TT_LOG_E(TAG, "Failed to read file contents of %s", filePath.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* input_start = reinterpret_cast<const char*>(input.get());
|
||||
std::string input_string;
|
||||
input_string.assign(input_start);
|
||||
|
||||
uint16_t line_count = 0;
|
||||
string::split(input_string, "\n", [&line_count, &filePath, &callback](auto token) {
|
||||
line_count++;
|
||||
std::string key, value;
|
||||
auto trimmed_token = string::trim(token, " \t");
|
||||
if (!trimmed_token.starts_with("#") ) {
|
||||
if (getKeyValuePair(token, key, value)) {
|
||||
std::string trimmed_key = string::trim(key, " \t");
|
||||
std::string trimmed_value = string::trim(value, " \t");
|
||||
callback(trimmed_key, trimmed_value);
|
||||
} else {
|
||||
TT_LOG_E(TAG, "Failed to parse line %d of %s", line_count, filePath.c_str());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool loadPropertiesFile(const std::string& filePath, std::map<std::string, std::string>& outProperties) {
|
||||
return loadPropertiesFile(filePath, [&outProperties](const std::string& key, const std::string& value) {
|
||||
outProperties[key] = value;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,3 +1,30 @@
|
||||
//
|
||||
// Created by ken on 18/08/2025.
|
||||
//
|
||||
#include "TestFile.h"
|
||||
#include "doctest.h"
|
||||
|
||||
#include <Tactility/file/PropertiesFile.h>
|
||||
|
||||
using namespace tt;
|
||||
|
||||
TEST_CASE("loadPropertiesFile() should return false when the file does not exist") {
|
||||
std::map<std::string, std::string> properties;
|
||||
CHECK_EQ(file::loadPropertiesFile("does_not_exist.properties", properties), false);
|
||||
}
|
||||
|
||||
TEST_CASE("PropertiesFile should parse a valid file properly") {
|
||||
TestFile file("test.properties");
|
||||
file.writeData(
|
||||
"# Comment\n" // Regular comment
|
||||
" \t# Comment\n" // Prefixed comment
|
||||
"key1=value1\n" // Regular property
|
||||
" \tkey 2\t = \tvalue 2\t " // Property with empty space
|
||||
);
|
||||
|
||||
std::map<std::string, std::string> properties;
|
||||
|
||||
// Load data
|
||||
CHECK_EQ(file::loadPropertiesFile(file.getPath(), properties), true);
|
||||
|
||||
CHECK_EQ(properties.size(), 2);
|
||||
CHECK_EQ(properties["key1"], "value1");
|
||||
CHECK_EQ(properties["key 2"], "value 2");
|
||||
}
|
||||
|
||||
@ -1,8 +1,42 @@
|
||||
//
|
||||
// Created by ken on 18/08/2025.
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#ifndef TACTILITY_TESTFILE_H
|
||||
#define TACTILITY_TESTFILE_H
|
||||
#include "doctest.h"
|
||||
#include <unistd.h>
|
||||
|
||||
#endif //TACTILITY_TESTFILE_H
|
||||
#include <Tactility/file/File.h>
|
||||
|
||||
/**
|
||||
* A class for creating test files that can automatically clean themselves up.
|
||||
*/
|
||||
class TestFile {
|
||||
const char* path;
|
||||
bool autoClean;
|
||||
|
||||
public:
|
||||
|
||||
TestFile(const char* path, bool autoClean = true) : path(path), autoClean(autoClean) {
|
||||
if (autoClean && exists()) {
|
||||
remove();
|
||||
}
|
||||
}
|
||||
|
||||
~TestFile() {
|
||||
if (autoClean && exists()) {
|
||||
remove();
|
||||
}
|
||||
}
|
||||
|
||||
const char* getPath() const { return path; }
|
||||
|
||||
void writeData(const char* data) const {
|
||||
CHECK_EQ(tt::file::writeString(path, data), true);
|
||||
}
|
||||
|
||||
bool exists() const {
|
||||
return access(path, F_OK) == 0;
|
||||
}
|
||||
|
||||
void remove() const {
|
||||
::remove(path);
|
||||
}
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user