Skip to content

Commit 4a8eba4

Browse files
committed
Paraconf release 1.1.0
* Add a way to query the path to the parsed configuration file * Add a way to check version
1 parent 1c820c0 commit 4a8eba4

File tree

10 files changed

+212
-49
lines changed

10 files changed

+212
-49
lines changed

CMakeLists.txt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,24 @@ set(paraconf_VERSION_MAJOR "${CMAKE_MATCH_1}")
5151
set(paraconf_VERSION_MINOR "${CMAKE_MATCH_2}")
5252
set(paraconf_VERSION_PATCH "${CMAKE_MATCH_3}")
5353
set(paraconf_VERSION_MODIF "${CMAKE_MATCH_5}")
54-
if("git" STREQUAL "${paraconf_VERSION_MODIF}" OR "alpha" STREQUAL "${paraconf_VERSION_MODIF}")
54+
set(paraconf_VERSION_VARIANT 0)
55+
if("git" STREQUAL "${paraconf_VERSION_MODIF}" OR "${paraconf_VERSION_MODIF}" MATCHES "(alpha|beta|rc)([0-9]*)")
56+
if("git" STREQUAL "${paraconf_VERSION_MODIF}")
57+
execute_process(COMMAND "${paraconf_SOURCE_DIR}/cmake/version-uid" "--nbdays" WORKING_DIRECTORY "${paraconf_SOURCE_DIR}" OUTPUT_VARIABLE paraconf_VERSION_VARIANT OUTPUT_STRIP_TRAILING_WHITESPACE)
58+
else()
59+
if("alpha" STREQUAL "${CMAKE_MATCH_1}")
60+
set(paraconf_VERSION_VARIANT 65512)
61+
elseif("beta" STREQUAL "${CMAKE_MATCH_1}")
62+
set(paraconf_VERSION_VARIANT 65520)
63+
elseif("rc" STREQUAL "${CMAKE_MATCH_1}")
64+
set(paraconf_VERSION_VARIANT 65528)
65+
endif()
66+
math(EXPR paraconf_VERSION_VARIANT "${paraconf_VERSION_VARIANT} + 0${CMAKE_MATCH_2}")
67+
endif()
5568
execute_process(COMMAND "${paraconf_SOURCE_DIR}/cmake/version-uid" "${paraconf_VERSION_MODIF}" WORKING_DIRECTORY "${paraconf_SOURCE_DIR}" OUTPUT_VARIABLE paraconf_VERSION_MODIF OUTPUT_STRIP_TRAILING_WHITESPACE)
5669
endif()
5770
set(paraconf_VERSION "${paraconf_VERSION_MAJOR}.${paraconf_VERSION_MINOR}.${paraconf_VERSION_PATCH}${paraconf_VERSION_MODIF}")
71+
configure_file(include/paraconf_version.h.in paraconf_version.h @ONLY)
5872

5973

6074
# Libraries
@@ -85,7 +99,7 @@ install(TARGETS paraconf EXPORT PC_export
8599
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT Runtime NAMELINK_COMPONENT Development
86100
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
87101
)
88-
install(FILES include/paraconf.h "${paraconf_BINARY_DIR}/paraconf_export.h"
102+
install(FILES include/paraconf.h "${paraconf_BINARY_DIR}/paraconf_export.h" "${paraconf_BINARY_DIR}/paraconf_version.h"
89103
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
90104
COMPONENT Development
91105
)

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.4
1+
1.1.0

cmake/version-uid

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,30 @@
77
set -e
88
export LC_ALL=C
99

10-
PRE_RELEASE="${1-alpha}"
11-
WORK_DIR="${2-.}"
1210
GIT_EXE="${GIT_EXE-"$(which git 2> /dev/null || true)"}"
1311

12+
WORK_DIR="${2-.}"
13+
cd "${WORK_DIR}"
14+
15+
if [ "x${1}" = "x--nbdays" ]
16+
then
17+
if [ -x "${GIT_EXE}" ]
18+
then
19+
echo "$(( ( $(date +%s -d $(git show -s --format=%cs HEAD | sed 's/ .*//') ) - $(date +%s -u -d 20150101) ) / 86400 ))"
20+
else
21+
echo "$(( ( $(date +%s -u) - $(date +%s -u -d 20150101) ) / 86400 ))"
22+
fi
23+
exit
24+
fi
25+
26+
PRE_RELEASE="${1-alpha}"
1427
if [ git = "${PRE_RELEASE}" ]
1528
then
1629
PRE_RELEASE=alpha
1730
fi
1831

1932
if [ -x "${GIT_EXE}" ]
2033
then
21-
cd "${WORK_DIR}"
2234
GIT_DATE="$("${GIT_EXE}" show -s --format=%ci HEAD | sed 's/ .*//')"
2335
GIT_SHA1="$("${GIT_EXE}" rev-parse --verify --short=7 HEAD)"
2436
echo "-${PRE_RELEASE}.${GIT_DATE}.${GIT_SHA1}"

include/paraconf.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <yaml.h>
1313

1414
#include "paraconf_export.h"
15+
#include "paraconf_version.h"
1516

1617
#ifdef __cplusplus
1718
extern "C" {
@@ -55,14 +56,18 @@ typedef struct PC_errhandler_s {
5556

5657
} PC_errhandler_t;
5758

59+
/** An opaque type describing a yaml document
60+
*/
61+
typedef struct PC_document_s PC_document_t;
62+
5863
/** An opaque type describing a yaml tree (possibly a leaf node)
5964
*/
6065
typedef struct PC_tree_s {
6166
/// The tree status
6267
PC_status_t status;
6368

6469
/// The document containing the tree
65-
yaml_document_t* document;
70+
PC_document_t* pcdoc;
6671

6772
/// the node inside the tree
6873
yaml_node_t* node;
@@ -87,9 +92,28 @@ static inline PC_status_t PC_status(PC_tree_t tree)
8792
}
8893

8994
/** Return a human-readabe message describing the last error that occured in paraconf
95+
*
96+
* \return a human-readabe message describing the last error that occured in paraconf
9097
*/
9198
char PARACONF_EXPORT* PC_errmsg();
9299

100+
/** Return the version of paraconf
101+
*
102+
* The version is returned as a 64 bit integer where:
103+
* - the 16 upper bits represent the major revision number
104+
* - the 16 following bits represent the minor revision number
105+
* - the 16 following bits represent the patch revision number
106+
* - the 16 lower bits can represent a variant such as pre-release
107+
* - 0 for a normal release
108+
* - number of days since Jan. 01 2015 for a git revision
109+
* - 65532 (2^16-24) + alpha number for an alpha release
110+
* - 65532 (2^16-16) + beta number for a beta release
111+
* - 65532 (2^16-8) + rc-number for a release candidate
112+
*
113+
* \return the version of paraconf
114+
*/
115+
uint64_t PARACONF_EXPORT PC_version();
116+
93117
/** Sets the error handler to use
94118
*
95119
* PC_assert is the default handler before this function is called
@@ -142,6 +166,11 @@ PC_tree_t PARACONF_EXPORT PC_parse_string(const char* document);
142166
*/
143167
PC_tree_t PARACONF_EXPORT PC_root(yaml_document_t* document);
144168

169+
/** Returns the path of the file from which the document was loaded
170+
* (or `\<string\>' if this was not loaded from a file)
171+
*/
172+
const char PARACONF_EXPORT* PC_path(PC_tree_t tree);
173+
145174
/** Looks for a node in a yaml document given a ypath index
146175
*
147176
* Does nothing if the provided tree is in error and returns the input tree.

include/paraconf_version.h.in

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* Copyright (C) The Paraconf development team, see COPYRIGHT.md file at the
2+
* root of the project or at https://github.com/pdidev/paraconf
3+
*
4+
* SPDX-License-Identifier: MIT
5+
*/
6+
7+
#ifndef PARACONF_VERSION_H__
8+
#define PARACONF_VERSION_H__
9+
10+
#include <stdint.h>
11+
12+
// clang-format off
13+
/// paraconf major revision number
14+
#define PARACONF_VERSION_MAJOR @paraconf_VERSION_MAJOR@
15+
/// paraconf minor revision number
16+
#define PARACONF_VERSION_MINOR @paraconf_VERSION_MINOR@
17+
/// paraconf patch revision number
18+
#define PARACONF_VERSION_PATCH @paraconf_VERSION_PATCH@
19+
/** paraconf version variant, such as pre-release
20+
* - 0 for a normal release
21+
* - number of days since Jan. 01 2015 for a git revision
22+
* - 65532 (2^16-24) + alpha number for an alpha release
23+
* - 65532 (2^16-16) + beta number for a beta release
24+
* - 65532 (2^16-8) + rc-number for a release candidate
25+
*/
26+
#define PARACONF_VERSION_VARIANT @paraconf_VERSION_VARIANT@
27+
// clang-format on
28+
/** The version of paraconf as a 64 bit integer where:
29+
* - the 16 upper bits represent the major revision number
30+
* - the 16 following bits represent the minor revision number
31+
* - the 16 following bits represent the patch revision number
32+
* - the 16 lower bits can represent a variant such as pre-release
33+
* - 0 for a normal release
34+
* - number of days since Jan. 01 2015 for a git revision
35+
* - 65512 (2^16-24) + alpha number for an alpha release
36+
* - 65520 (2^16-16) + beta number for a beta release
37+
* - 65528 (2^16-8) + rc-number for a release candidate
38+
*/
39+
#define PARACONF_VERSION \
40+
((((uint64_t)(PARACONF_VERSION_MAJOR)) << 48) | (((uint64_t)(PARACONF_VERSION_MINOR)) << 32) | (((uint64_t)(PARACONF_VERSION_PATCH)) << 16) \
41+
| ((uint64_t)(PARACONF_VERSION_VARIANT)))
42+
43+
#endif // PARACONF_VERSION_H__

src/api.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,27 @@
2222

2323
static const char* nodetype[4] = {"none", "scalar", "sequence", "mapping"};
2424

25+
static const char* PC_NO_PATH = "<string>";
26+
27+
static inline void pc_path_free(const char* path)
28+
{
29+
if (path != PC_NO_PATH) free((void*)path);
30+
}
31+
32+
static inline void pc_set_path(PC_tree_t tree, const char* path)
33+
{
34+
pc_path_free(tree.pcdoc->path);
35+
size_t pathlen = strlen(path);
36+
char* pathcpy = malloc((pathlen + 1) * sizeof(char));
37+
strncpy(pathcpy, path, pathlen);
38+
tree.pcdoc->path = pathcpy;
39+
}
40+
41+
uint64_t PC_version()
42+
{
43+
return PARACONF_VERSION;
44+
}
45+
2546
PC_tree_t PC_parse_path(const char* path)
2647
{
2748
PC_tree_t restree = {PC_OK, NULL, NULL};
@@ -41,6 +62,7 @@ PC_tree_t PC_parse_path(const char* path)
4162
}
4263

4364
fclose(conf_file);
65+
pc_set_path(restree, path);
4466
return restree;
4567

4668
err1:
@@ -169,10 +191,17 @@ PC_tree_t PC_parse_file(FILE* conf_file)
169191

170192
PC_tree_t PC_root(yaml_document_t* document)
171193
{
172-
PC_tree_t restree = {PC_OK, document, yaml_document_get_root_node(document)};
194+
PC_tree_t restree = {PC_OK, malloc(sizeof(PC_document_t)), yaml_document_get_root_node(document)};
195+
PC_document_t pcdoc = {*document, PC_NO_PATH};
196+
*restree.pcdoc = pcdoc;
173197
return restree;
174198
}
175199

200+
const char* PC_path(PC_tree_t tree)
201+
{
202+
return tree.pcdoc->path;
203+
}
204+
176205
PC_tree_t PC_get(const PC_tree_t tree, const char* index_fmt, ...)
177206
{
178207
va_list ap;
@@ -364,8 +393,11 @@ PC_status_t PC_bool(const PC_tree_t tree, int* res)
364393

365394
PC_status_t PC_tree_destroy(PC_tree_t* tree)
366395
{
367-
yaml_document_delete(tree->document);
368-
free(tree->document);
396+
yaml_document_delete(&tree->pcdoc->document);
397+
pc_path_free(tree->pcdoc->path);
398+
tree->pcdoc->path = NULL;
399+
free(tree->pcdoc);
400+
tree->pcdoc = NULL;
369401
tree->node = NULL;
370402
return tree->status;
371403
}

src/tools.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
#include <string.h>
1313

1414
#include "paraconf.h"
15+
#include "ypath.h"
1516

1617
static inline PC_tree_t subtree(PC_tree_t tree, int key)
1718
{
18-
tree.node = yaml_document_get_node(tree.document, key);
19+
tree.node = yaml_document_get_node(&tree.pcdoc->document, key);
1920
assert(tree.node);
2021
return tree;
2122
}

src/ypath.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static PC_tree_t get_seq_idx(const PC_tree_t tree, const char** req_index, const
9999
err0
100100
);
101101
}
102-
restree.node = yaml_document_get_node(tree.document, *(tree.node->data.sequence.items.start + seq_idx));
102+
restree.node = yaml_document_get_node(&tree.pcdoc->document, *(tree.node->data.sequence.items.start + seq_idx));
103103
assert(tree.node);
104104

105105
*req_index = index;
@@ -177,7 +177,7 @@ static PC_tree_t get_map_key_val(const PC_tree_t tree, const char** req_index, c
177177
err0
178178
);
179179
}
180-
restree.node = yaml_document_get_node(tree.document, pair->value);
180+
restree.node = yaml_document_get_node(&tree.pcdoc->document, pair->value);
181181
assert(tree.node);
182182

183183
*req_index = index;
@@ -291,7 +291,7 @@ static PC_tree_t get_map_idx_key(const PC_tree_t tree, const char** req_index, c
291291
++index;
292292

293293
// handle pair
294-
restree.node = yaml_document_get_node(tree.document, pair->key);
294+
restree.node = yaml_document_get_node(&tree.pcdoc->document, pair->key);
295295
assert(tree.node);
296296

297297
*req_index = index;
@@ -343,7 +343,7 @@ static PC_tree_t get_map_idx_val(const PC_tree_t tree, const char** req_index, c
343343
++index;
344344

345345
// handle pair
346-
restree.node = yaml_document_get_node(tree.document, pair->value);
346+
restree.node = yaml_document_get_node(&tree.pcdoc->document, pair->value);
347347
assert(tree.node);
348348

349349
*req_index = index;

src/ypath.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99

1010
#include "paraconf.h"
1111

12+
struct PC_document_s {
13+
/// The underlying YAML document
14+
yaml_document_t document;
15+
/// The path to the file from which the document was parsed
16+
const char* path;
17+
};
18+
1219
PC_tree_t PARACONF_EXPORT PC_sget(PC_tree_t tree, const char* index);
1320

1421
#endif // YPATH_H__

0 commit comments

Comments
 (0)