tetengo 1.8.1
A multipurpose library set
Loading...
Searching...
No Matches
tetengo::property Namespace Reference

A property set library. More...

Classes

class  file_storage
 A file storage. More...
 
class  file_storage_loader
 A file storage loader. More...
 
class  memory_storage
 A memory storage. More...
 
class  memory_storage_loader
 A memory storage loader. More...
 
class  property_set
 A property set. More...
 
class  storage
 A storage. More...
 
class  storage_loader
 A storage loader. More...
 
class  storage_loader_proxy
 A storage loader proxy. More...
 
class  storage_proxy
 A storage proxy. More...
 
class  windows_registry_storage
 A Windows registry storage. More...
 
class  windows_registry_storage_loader
 A Windows registry storage loader. More...
 

Detailed Description

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()));
// Creates separate property sets with the same path for illustration purpose.
// Uses the one for setting and the other for getting.
tetengo::property::property_set props_for_setting{ std::make_unique<tetengo::property::file_storage_loader>(),
"tetengo_property_sample" };
tetengo::property::property_set props_for_getting{ std::make_unique<tetengo::property::file_storage_loader>(),
"tetengo_property_sample" };
// On Windows, by using tetengo::property::windows_registry_storage_loader, the values will be stored into the
// Windows registry.
// tetengo::property::property_set props_for_setting{
// std::make_unique<tetengo::property::windows_registry_storage_loader>(), "tetengo_property_sample"
//};
// tetengo::property::property_set props_for_getting{
// std::make_unique<tetengo::property::windows_registry_storage_loader>(), "tetengo_property_sample"
//};
// Sets a string value "bar" for the key "foo".
props_for_setting.set_string("foo", "bar");
// The value cannot be yet obtained from the property set for getting.
const auto loaded_value1 = props_for_getting.get_string("foo");
assert(!loaded_value1);
// Saves the values of the property set for setting to the file.
props_for_setting.commit();
assert(std::filesystem::exists(setting_file_path()));
// The value cannot be yet obtained from the property set for getting.
const auto loaded_value2 = props_for_getting.get_string("foo");
assert(!loaded_value2);
// Loads the property set values from the file.
props_for_getting.update();
// The value can now be obtained from the property set for getting.
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()
{
// When specifying the path "tetengo_property_sample" (with no path delimiter),
#if defined(_WIN32)
// in the usual case on Windows, the path of the setting file will be
// "C:\Users\USER_NAME\AppData\Roaming\tetengo_property_sample\tetengo_property_sample.json".
static const std::filesystem::path path_{ std::filesystem::path{ std::getenv("APPDATA") } /
"tetengo_property_sample" / "tetengo_property_sample.json" };
#else
// in the usual case on UNIX, the path of the setting file will be
// "/home/USER_NAME/.tetengo_property_sample.json".
static const std::filesystem::path path_{ std::filesystem::path{ std::getenv("HOME") } /
".tetengo_property_sample.json" };
#endif
return path_;
}
}
A property set.
Definition property_set.hpp:28
void set_string(const std::filesystem::path &key, const std::string &value)
Sets a value in a string.
A file storage.
A storage.
A property set.

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()));
// Creates separate property sets with the same path for illustration purpose.
// Uses the one for setting and the other for getting.
tetengo_property_propertySet_t* const p_props_for_setting = tetengo_property_propertySet_create(
tetengo_property_propertySet_t* const p_props_for_getting = tetengo_property_propertySet_create(
/*
On Windows, by using tetengo_property_storageLoader_createWindowsRegistoryStorageLoader(), the values will
be stored into the Windows registry.
*/
/*
tetengo_property_propertySet_t* const p_props_for_setting = tetengo_property_propertySet_create(
tetengo_property_storageLoader_createWindowsRegistoryStorageLoader(), "tetengo_property_sample");
tetengo_property_propertySet_t* const p_props_for_getting = tetengo_property_propertySet_create(
tetengo_property_storageLoader_createWindowsRegistoryStorageLoader(), "tetengo_property_sample");
*/
// Sets a string value "bar" for the key "foo".
tetengo_property_propertySet_setString(p_props_for_setting, "foo", "bar");
// The value cannot be yet obtained from the property set for getting.
const size_t value_length1 = tetengo_property_propertySet_getString(p_props_for_getting, "foo", NULL, 0);
(void)value_length1;
assert(value_length1 == (size_t)-1);
// Saves the values of the property set for setting to the file.
assert(file_exists(setting_file_path()));
// The value cannot be yet obtained from the property set for getting.
const size_t value_length2 = tetengo_property_propertySet_getString(p_props_for_getting, "foo", NULL, 0);
(void)value_length2;
assert(value_length2 == (size_t)-1);
// Loads the property set values from the file.
// The value can now be obtained from the property set for getting.
const size_t value_length3 = tetengo_property_propertySet_getString(p_props_for_getting, "foo", NULL, 0);
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';
tetengo_property_propertySet_getString(p_props_for_getting, "foo", value, value_length3 + 1);
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')
{
/*
When specifying the path "tetengo_property_sample" (with no path delimiter),
*/
#if defined(_WIN32)
/*
in the usual case on Windows, the path of the setting file will be
"C:\Users\USER_NAME\AppData\Roaming\tetengo_property_sample\tetengo_property_sample.json".
*/
strcat(path, getenv("APPDATA"));
strcat(path, "\\tetengo_property_sample\\tetengo_property_sample.json");
#else
/*
in the usual case on UNIX, the path of the setting file will be
"/home/USER_NAME/.tetengo_property_sample.json".
*/
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
}
A property set.
size_t tetengo_property_propertySet_getString(const tetengo_property_propertySet_t *p_property_set, const char *key, char *p_value, size_t value_capacity)
Returns the value in a string.
void tetengo_property_propertySet_setString(tetengo_property_propertySet_t *p_property_set, const char *key, const char *value)
Sets a value in a string.
void tetengo_property_propertySet_update(tetengo_property_propertySet_t *p_property_set)
Updates the property values to the latest state in the storage.
void tetengo_property_propertySet_destroy(const tetengo_property_propertySet_t *p_property_set)
Destroys a property set.
tetengo_property_propertySet_t * tetengo_property_propertySet_create(tetengo_property_storageLoader_t *p_storage_loader, const char *path)
Creates a property set.
void tetengo_property_propertySet_commit(const tetengo_property_propertySet_t *p_property_set)
Commits the property value changes to the storage.
A storage.
tetengo_property_storageLoader_t * tetengo_property_storageLoader_createFileStorageLoader()
Creates a file storage loader.