Skip to content

Commit

Permalink
New loader and dumper
Browse files Browse the repository at this point in the history
  • Loading branch information
MickaelBlet committed Apr 21, 2024
1 parent c6fc5b7 commit 85c5cc4
Show file tree
Hide file tree
Showing 16 changed files with 2,838 additions and 2,769 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.5)

add_subdirectory("third/Dict")

project(blet_conf VERSION 1.0.0 LANGUAGES CXX)
project(blet_conf VERSION 2.0.0 LANGUAGES CXX)

# OPTIONS
option(BUILD_EXAMPLE "Build example binaries" OFF)
Expand All @@ -23,7 +23,9 @@ if(NOT CMAKE_INSTALL_INCLUDEDIR)
endif()

add_library("${PROJECT_NAME}"
"${CMAKE_CURRENT_SOURCE_DIR}/src/conf.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/dump.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/exception.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/load.cpp"
)

set_target_properties("${PROJECT_NAME}"
Expand Down
32 changes: 20 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ parents[] = {
}
]
}
# json
parents[] = {
name = toto
age = 0x2A
gender = M
childs = []
"name": "toto",
"age": 0x2A,
"gender": "M",
"childs": []
}
; pseudo json one line
parents[] = { name = clara, age = 42, gender = F, childs = [], emptyObj = {} }
Expand Down Expand Up @@ -216,14 +217,21 @@ std::cout << blet::conf::dump(conf, 2, ' ', blet::conf::JSON_STYLE) << std::endl

## Build
```bash
# static build
mkdir build ; pushd build && cmake .. && make -j ; popd
# shared build
mkdir build ; pushd build && cmake -DBUILD_SHARED_LIBS=1 .. && make -j ; popd
# static build and launch test
mkdir build ; pushd build && cmake -DBUILD_TESTING=1 .. && make -j && make test ; popd
# shared build and launch test
mkdir build ; pushd build && cmake -DBUILD_SHARED_LIBS=1 -DBUILD_TESTING=1 .. && make -j && make test ; popd
# Static Release
mkdir build; pushd build; cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=0 .. && make -j && make install; popd
# Dynamic Release
mkdir build; pushd build; cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=1 .. && make -j && make install; popd

# Static Release C++98
mkdir build; pushd build; cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=98 -DBUILD_SHARED_LIBS=0 .. && make -j && make install; popd
# Dynamic Release C++98
mkdir build; pushd build; cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=98 -DBUILD_SHARED_LIBS=1 .. && make -j && make install; popd

# Install with custom directory
mkdir build; pushd build; cmake -DCMAKE_INSTALL_PREFIX="YOUR_INSTALL_PATH" .. && make -j && make install; popd

# Example + Tests + Coverage
mkdir build; pushd build; cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_EXAMPLE=1 -DBUILD_TESTING=1 -DBUILD_COVERAGE=1 -DCMAKE_CXX_STANDARD=98 .. && make -j && make test -j; popd
```

## Load Functions
Expand Down
42 changes: 42 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,48 @@ std::cout << conf["test"]["42 space "]["key[]"] << std::endl;
// value space
```

## Json

```conf
$ cat ./test6.conf
# comment
{
"test": {
"key": 42, # comment line
"42": "value"
}
}
```
```cpp
blet::Dict conf = blet::conf::loadFile("./test6.conf");
std::cout << conf["test"]["key"] << std::endl;
std::cout << conf["test"]["42"] << std::endl;
// output:
// 42
// value
```

## Pseudo Json

```conf
$ cat ./test7.conf
# comment
{
test = {
key = 42 # comment line
42 = "value"
}
}
```
```cpp
blet::Dict conf = blet::conf::loadFile("./test7.conf");
std::cout << conf["test"]["key"] << std::endl;
std::cout << conf["test"]["42"] << std::endl;
// output:
// 42
// value
```

## loadFile

```conf
Expand Down
2 changes: 1 addition & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ foreach(file ${example_files})
NO_SYSTEM_FROM_IMPORTED ON
COMPILE_FLAGS "-pedantic -Wall -Wextra -Werror"
)
target_link_libraries("${filenamewe}.${library_project_name}.example" PUBLIC "${library_project_name}_single_include")
target_link_libraries("${filenamewe}.${library_project_name}.example" PUBLIC "${library_project_name}")
endforeach()
9 changes: 5 additions & 4 deletions example/quickstart.conf
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ parents[] = {
}
]
}
# json
parents[] = {
name = toto
age = 0x2A
gender = M
childs = []
"name": "toto",
"age": 0x2A,
"gender": "M",
"childs": []
}
; pseudo json one line
parents[] = { name = clara, age = 42, gender = F, childs = [], emptyObj = {} }
12 changes: 6 additions & 6 deletions include/blet/conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ namespace blet {
namespace conf {

/**
* @brief Parse exception from std::exception
* @brief Load exception from std::exception
*/
class LoadException : public std::exception {
public:
LoadException(const std::string& filename, const std::string& message);
LoadException(const std::string& filename, std::size_t line, std::size_t column, const std::string& message);
virtual ~LoadException() throw();
~LoadException() throw();
const char* what() const throw();
const std::string& filename() const throw();
const std::string& message() const throw();
Expand Down Expand Up @@ -88,31 +88,31 @@ std::string dump(const blet::Dict& dict, std::size_t indent = 0, char indentChar
enum EDumpStyle style = CONF_STYLE);

/**
* @brief Parse and load a config from filename.
* @brief Load a config from filename.
*
* @param filename A filename.
* @return blet::Dict Dictionnary of config.
*/
blet::Dict loadFile(const char* filename);

/**
* @brief Parse and load a config from stream.
* @brief Load a config from stream.
*
* @param stream A stream.
* @return blet::Dict Dictionnary of config.
*/
blet::Dict loadStream(std::istream& stream);

/**
* @brief Parse and load a config from string.
* @brief Load a config from string.
*
* @param str A string.
* @return blet::Dict Dictionnary of config.
*/
blet::Dict loadString(const std::string& str);

/**
* @brief Parse and load a config from data.
* @brief Load a config from data.
*
* @param data A data.
* @param size Size of data.
Expand Down
4 changes: 3 additions & 1 deletion single_include/amalgamate_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
],
"sources": [
"include/blet/conf.h",
"src/conf.cpp"
"src/dump.cpp",
"src/exception.cpp",
"src/load.cpp"
]
}
Loading

0 comments on commit 85c5cc4

Please sign in to comment.