A property set library.
Usage
Saving and Loading Settings
C++
#include <cassert>
#include <cstdlib>
#include <filesystem>
#include <memory>
#include <optional>
#include <string>
namespace usage_tetengo::property
{
const std::filesystem::path& setting_file_path();
void save_load()
{
assert(!std::filesystem::exists(setting_file_path()));
"tetengo_property_sample" };
"tetengo_property_sample" };
const auto loaded_value1 = props_for_getting.get_string("foo");
assert(!loaded_value1);
props_for_setting.commit();
assert(std::filesystem::exists(setting_file_path()));
const auto loaded_value2 = props_for_getting.get_string("foo");
assert(!loaded_value2);
props_for_getting.update();
const auto loaded_value3 = props_for_getting.get_string("foo");
assert(*loaded_value3 == "bar");
std::filesystem::remove(setting_file_path());
}
const std::filesystem::path& setting_file_path()
{
#if defined(_WIN32)
static const std::filesystem::path path_{ std::filesystem::path{ std::getenv("APPDATA") } /
"tetengo_property_sample" / "tetengo_property_sample.json" };
#else
static const std::filesystem::path path_{ std::filesystem::path{ std::getenv("HOME") } /
".tetengo_property_sample.json" };
#endif
return path_;
}
}
C
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
static const char* setting_file_path();
static int file_exists(const char* path);
void usage_tetengo_property_saveLoad()
{
assert(!file_exists(setting_file_path()));
(void)value_length1;
assert(value_length1 == (size_t)-1);
assert(file_exists(setting_file_path()));
(void)value_length2;
assert(value_length2 == (size_t)-1);
assert(value_length3 != (size_t)-1);
char* const value = (char*)malloc((value_length3 + 1) * sizeof(char));
if (!value)
{
assert(0);
return;
}
value[value_length3] = '\0';
assert(strcmp(value, "bar") == 0);
free((void*)value);
remove(setting_file_path());
}
static const char* setting_file_path()
{
static char path[4096] = { '\0' };
if (path[0] == '\0')
{
#if defined(_WIN32)
strcat(path, getenv("APPDATA"));
strcat(path, "\\tetengo_property_sample\\tetengo_property_sample.json");
#else
strcat(path, getenv("HOME"));
strcat(path, "/.tetengo_property_sample.json");
#endif
}
return path;
}
static int file_exists(const char* const path)
{
#if defined(_WIN32)
struct _stat buffer;
return _stat(path, &buffer) == 0;
#else
struct stat buffer;
return stat(path, &buffer) == 0;
#endif
}