From c49ffbfba82bb1404bc9bab46922c6c169da2322 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Thu, 12 Jan 2017 20:37:29 +0100 Subject: [PATCH] cJSON_Version: returns a version string This is useful to programmatically find out the version of cJSON that has been used (useful in case of scripting language bindings for example). --- cJSON.c | 8 ++++++++ cJSON.h | 4 ++++ test.c | 3 +++ 3 files changed, 15 insertions(+) diff --git a/cJSON.c b/cJSON.c index b7de473..80c0973 100644 --- a/cJSON.c +++ b/cJSON.c @@ -58,6 +58,14 @@ const char *cJSON_GetErrorPtr(void) return global_ep; } +extern const char* cJSON_Version(void) +{ + static char version[15]; + sprintf(version, "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH); + + return version; +} + /* case insensitive strcmp */ static int cJSON_strcasecmp(const char *s1, const char *s2) { diff --git a/cJSON.h b/cJSON.h index 6537062..4ef0396 100644 --- a/cJSON.h +++ b/cJSON.h @@ -32,6 +32,10 @@ extern "C" #define CJSON_VERSION_MAJOR 1 #define CJSON_VERSION_MINOR 2 #define CJSON_VERSION_PATCH 0 + +/* returns the version of cJSON as a string */ +extern const char* cJSON_Version(void); + #include /* cJSON Types: */ diff --git a/test.c b/test.c index e39b933..2af8a26 100644 --- a/test.c +++ b/test.c @@ -382,6 +382,9 @@ int main(void) "\n" "\n"; + /* print the version */ + printf("Version: %s\n", cJSON_Version()); + /* Process each json textblock by parsing, then rebuilding: */ doit(text1); doit(text2);