Skip to content

Commit

Permalink
Rename flacon to cracon to avoid conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisTM committed Jun 5, 2024
1 parent eecec97 commit be67ed6
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 77 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.14)
project(flacon LANGUAGES CXX)
project(cracon LANGUAGES CXX)

if (NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
Expand All @@ -17,7 +17,7 @@ option(BUILD_EXAMPLES "Build the examples" ON)

include(cmake/CPM.cmake)

add_library(${PROJECT_NAME} src/flacon.cpp)
add_library(${PROJECT_NAME} src/cracon.cpp)
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})

target_include_directories(${PROJECT_NAME} PUBLIC
Expand Down
62 changes: 52 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# Flacon C++ Library
# Cracon C++ Library

**Flacon** is a lightweight C++ library for handling _JSON_ configuration files. It provides a simple and intuitive API for reading, writing, and managing configuration data. Whether you're building a command-line tool, a desktop application, or a server application, Flacon can simplify your configuration management.
Cracon stands for Craft Config.

**Cracon** is a lightweight C++ library for handling _JSON_ configuration files. It provides a simple and intuitive API for reading, writing configurations. It outputs two files, one `default` with all defaults parameters set in the code, and one for changed parameters.

It supports (tested for):
- booleans
- enums
- (u)int(00)_t
- float/double
- std::vector
- std::string
- std::array

Most of the heavy lifting is done by [Niels Lohmann's awesome JSON library](https://json.nlohmann.me).

Expand All @@ -18,8 +29,8 @@ Most of the heavy lifting is done by [Niels Lohmann's awesome JSON library](http

```bash
# For Linux
git clone https://github.com/AlexisTM/flacon
mkdir flacon/build && cd flacon/build
git clone https://github.com/AlexisTM/cracon
mkdir cracon/build && cd cracon/build
cmake .. -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON
cmake --build .
# For Windows
Expand All @@ -28,12 +39,43 @@ cmake --build . --config Release
ctest
```

## Integration in your project

This library uses [CPM.cmake](https://github.com/cpm-cmake/CPM.cmake) for dependency management. This permits many other usage, see [examples/cmake...](examples/) for different integrations.

```cmake
include(cmake/CPM.cmake)
CPMAddPackage(
NAME cracon
GITHUB_REPOSITORY alexistm/cracon
GIT_TAG main
VERSION 0.0.1
OPTIONS "BUILD_EXAMPLES OFF" "BUILD_TESTING OFF"
)
add_executable(${PROJECT_NAME} main.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_link_libraries(${PROJECT_NAME} cracon)
install(
TARGETS ${PROJECT_NAME}
EXPORT export_${PROJECT_NAME}
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)
```

## API

### Basic usage

```cpp
flacon::File config;
cracon::File config;
bool success = config.init("config.json", "defaults.json");
if (!success) {
// The file couldn't be opened/read/written/created
Expand Down Expand Up @@ -70,7 +112,7 @@ Will result in:
Avoid typos by using a parameter group.

```c++
flacon::SharedFile config;
cracon::SharedFile config;
bool success = config.init("config.json", "defaults.json");
if (!success) {
fprintf(stderr, "We failed to open, read or write the configuration files.");
Expand Down Expand Up @@ -101,9 +143,9 @@ config.write();
Live parameters is a wrapper around a value. It wraps a value to use a single line to read/write a parameter and avoid typos in the configuration path.
```c++
flacon::SharedFile config;
cracon::SharedFile config;
bool success = config.init("config.json", "defaults.json");
auto some_int = config.get_param("int", 42); // flacon::File::Param<int>
auto some_int = config.get_param("int", 42); // cracon::File::Param<int>
int value = some_int.get(); // 42
int other_value = some_int.set(50); // Write down 50 & updates internals
```
Expand All @@ -115,8 +157,8 @@ int other_value = some_int.set(50); // Write down 50 & updates internals

## Contributing

Contributions to Flacon are welcome! Feel free to open issues, submit pull requests, or provide feedback. Especially if you are a CMake Guru and knows how to ensure this is as easy to integrate as possible.
Contributions to Cracon are welcome! Feel free to open issues, submit pull requests, or provide feedback. Especially if you are a CMake Guru and knows how to ensure this is as easy to integrate as possible.

## License

Flacon is released under the MIT License. See the [LICENSE](LICENSE) file for details.
Cracon is released under the MIT License. See the [LICENSE](LICENSE) file for details.
4 changes: 2 additions & 2 deletions examples/basic_usage.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <flacon/flacon.hpp>
#include <cracon/cracon.hpp>

int main() {
flacon::File config;
cracon::File config;
bool success = config.init("config.json", "defaults.json");
if (!success) {
fprintf(stderr, "We failed to open, read or write the configuration files.");
Expand Down
4 changes: 2 additions & 2 deletions examples/cmake_add_subfolder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ endif()

set(BUILD_TESTING OFF CACHE BOOL "")
set(BUILD_EXAMPLES OFF CACHE BOOL "")
add_subdirectory(../../ flacon-dep)
add_subdirectory(../../ cracon-dep)

add_executable(${PROJECT_NAME} main.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)

target_link_libraries(${PROJECT_NAME} flacon)
target_link_libraries(${PROJECT_NAME} cracon)

install(
TARGETS ${PROJECT_NAME}
Expand Down
4 changes: 2 additions & 2 deletions examples/cmake_add_subfolder/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <flacon/flacon.hpp>
#include <cracon/cracon.hpp>

int main() {
flacon::File config;
cracon::File config;
bool success = config.init("config.json", "defaults.json");
if (!success) {
fprintf(stderr, "We failed to open, read or write the configuration files.");
Expand Down
8 changes: 4 additions & 4 deletions examples/cmake_cpm_example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ endif()

include(cmake/CPM.cmake)
CPMAddPackage(
NAME flacon
GITHUB_REPOSITORY alexistm/flacon
NAME cracon
GITHUB_REPOSITORY alexistm/cracon
GIT_TAG main
VERSION 0.0.1
OPTIONS "BUILD_EXAMPLES OFF"
OPTIONS "BUILD_EXAMPLES OFF" "BUILD_TESTING OFF"
)

add_executable(${PROJECT_NAME} main.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)

target_link_libraries(${PROJECT_NAME} flacon)
target_link_libraries(${PROJECT_NAME} cracon)

install(
TARGETS ${PROJECT_NAME}
Expand Down
4 changes: 2 additions & 2 deletions examples/cmake_cpm_example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <flacon/flacon.hpp>
#include <cracon/cracon.hpp>

int main(int argc, char** argv) {
flacon::File config;
cracon::File config;
bool success = config.init("config.json", "defaults.json");
if (!success) {
fprintf(stderr, "We failed to open, read or write the configuration files.");
Expand Down
12 changes: 6 additions & 6 deletions examples/group_param_usage.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#include <cstdio>
#include <flacon/flacon.hpp>
#include <cracon/cracon.hpp>

class Car {
public:
Car(flacon::SharedFile::Group config) {
Car(cracon::SharedFile::Group config) {
speed = config.get_param<int64_t>("speed", 9000);
horsepower = config.get_param<int64_t>("horsepower", 120);
motor_curve = config.get_param<std::array<int, 24>>("motor_curve", {});
}

flacon::SharedFile::Param<int64_t> speed;
flacon::SharedFile::Param<int64_t> horsepower;
flacon::SharedFile::Param<std::array<int, 24>> motor_curve;
cracon::SharedFile::Param<int64_t> speed;
cracon::SharedFile::Param<int64_t> horsepower;
cracon::SharedFile::Param<std::array<int, 24>> motor_curve;
};

int main() {
// The SharedFile allows to use the Group and Param features
flacon::SharedFile config;
cracon::SharedFile config;
bool success = config.init("config.json", "defaults.json");
if (!success) {
fprintf(stderr, "We failed to open, read or write the configuration files.");
Expand Down
22 changes: 11 additions & 11 deletions include/flacon/flacon.hpp → include/cracon/cracon.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#ifndef FLACON_FLACON_HPP
#define FLACON_FLACON_HPP
#ifndef CRACON_CRACON_HPP
#define CRACON_CRACON_HPP

#include <cassert>
#include <flacon/similarity_traits.hpp>
#include <cracon/similarity_traits.hpp>
#include <fstream>
#include <memory>
#include <mutex>
#include <nlohmann/json.hpp>
#include <string>

namespace flacon {
namespace cracon {
class File {
public:
/**
Expand Down Expand Up @@ -48,11 +48,11 @@ class File {
should_write_config_ = true;
val = new_value;
if (val.is_null()) {
printf("[flacon] [WARN] The key didn't exist for %s\n", accessor.c_str());
printf("[cracon] [WARN] The key didn't exist for %s\n", accessor.c_str());
} else {
if (!is_similar<T>(val)) {
printf(
"[flacon] [WARN] The new key is not a similar type to the "
"[cracon] [WARN] The new key is not a similar type to the "
"precedent configuration: %s replaced by %s\n",
accessor.c_str(), val.dump().c_str());
}
Expand Down Expand Up @@ -85,7 +85,7 @@ class File {
auto &val = config_.at(pointer);
if (val.is_null()) {
printf(
"[flacon] [INFO] The requested key doesn't exist for %s defaulted "
"[cracon] [INFO] The requested key doesn't exist for %s defaulted "
"to \n",
default_[pointer].dump().c_str());
return default_val;
Expand All @@ -94,7 +94,7 @@ class File {
// using the wrong type; It is considered the code is right;
if (!is_similar<T>(val)) {
fprintf(stderr,
"[flacon] [ERROR] The read value %s is not a similar type to "
"[cracon] [ERROR] The read value %s is not a similar type to "
"%s at %s defaulted to %s\n",
val.dump().c_str(), typeid(T).name(), accessor.c_str(),
default_[pointer].dump().c_str());
Expand All @@ -103,7 +103,7 @@ class File {
return val.get<T>();
} catch (std::exception const &ex) {
printf(
"[flacon] [INFO] The requested key doesn't exist for %s defaulted "
"[cracon] [INFO] The requested key doesn't exist for %s defaulted "
"to %s. Error: %s\n",
accessor.c_str(), default_[pointer].dump().c_str(), ex.what());
return default_val;
Expand Down Expand Up @@ -321,6 +321,6 @@ class SharedFile : public File {
};

std::string get_package_share_directory(const std::string &package_name);
} // namespace flacon
} // namespace cracon

#endif // FLACON_FLACON_HPP
#endif // CRACON_CRACON_HPP
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef FLACON_SIMILARITY_TRAIT
#define FLACON_SIMILARITY_TRAIT
#ifndef CRACON_SIMILARITY_TRAIT
#define CRACON_SIMILARITY_TRAIT

#include <cassert>
#include <cstdint>
Expand All @@ -10,7 +10,7 @@
#include <string_view>
#include <type_traits>

namespace flacon {
namespace cracon {

template <typename T>
struct is_vector : public std::false_type {};
Expand Down Expand Up @@ -51,11 +51,11 @@ bool is_similar(nlohmann::json const &value) {
{
int64_t parsed_value = value.get<int64_t>();
if (parsed_value < std::numeric_limits<T>::lowest()) {
fprintf(stderr, "[flacon] [WARN] Out of bounds (min)\n");
fprintf(stderr, "[cracon] [WARN] Out of bounds (min)\n");

return false;
} else if (parsed_value > std::numeric_limits<T>::max()) {
fprintf(stderr, "[flacon] [WARN] Out of bounds (max)\n");
fprintf(stderr, "[cracon] [WARN] Out of bounds (max)\n");
return false;
}
}
Expand All @@ -66,10 +66,10 @@ bool is_similar(nlohmann::json const &value) {
{
int64_t parsed_value = value.get<int64_t>();
if (parsed_value < 0) {
fprintf(stderr, "[flacon] [WARN] Out of bounds (min) \n");
fprintf(stderr, "[cracon] [WARN] Out of bounds (min) \n");
return false;
} else if (parsed_value > std::numeric_limits<T>::max()) {
fprintf(stderr, "[flacon] [WARN] Out of bounds (max)\n");
fprintf(stderr, "[cracon] [WARN] Out of bounds (max)\n");
return false;
}
}
Expand All @@ -80,10 +80,10 @@ bool is_similar(nlohmann::json const &value) {
{
double high_limit = value.get<double>();
if (high_limit < std::numeric_limits<T>::lowest()) {
fprintf(stderr, "[flacon] [WARN] Out of bounds (min) \n");
fprintf(stderr, "[cracon] [WARN] Out of bounds (min) \n");
return false;
} else if (high_limit > std::numeric_limits<T>::max()) {
fprintf(stderr, "[flacon] [WARN] Out of bounds (max)\n");
fprintf(stderr, "[cracon] [WARN] Out of bounds (max)\n");
return false;
}
}
Expand Down Expand Up @@ -116,21 +116,21 @@ bool is_similar(nlohmann::json const &value) {
if (value.is_number_integer()) {
int high_limit = value.get<int>();
if (high_limit < 0) {
fprintf(stderr, "[flacon] [WARN] Enums have to be >0\n");
fprintf(stderr, "[cracon] [WARN] Enums have to be >0\n");
return false;
} else if (high_limit > std::pow(2, sizeof(T) * 8 - 1)) {
// The value has to be representable in the enum
fprintf(stderr, "[flacon] [WARN] Out of bounds (max)\n");
fprintf(stderr, "[cracon] [WARN] Out of bounds (max)\n");
return false;
}
return true;
}
return false;
}

fprintf(stderr, "[flacon] [ERROR] type is not supported\n");
fprintf(stderr, "[cracon] [ERROR] type is not supported\n");
return false;
}
} // namespace flacon
} // namespace cracon

#endif // FLACON_SIMILARITY_TRAIT
#endif // CRACON_SIMILARITY_TRAIT
Loading

0 comments on commit be67ed6

Please sign in to comment.