From: Quentin Glidic Date: Wed, 22 Aug 2012 15:02:02 +0000 (+0200) Subject: i3bar: Rename determine_json_version to parse_json_header X-Git-Tag: 4.4~135 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=13ecc79fcc1f91271617d531237780e7f4a06012;p=i3%2Fi3 i3bar: Rename determine_json_version to parse_json_header --- diff --git a/i3bar/include/common.h b/i3bar/include/common.h index 6f8a7b2d..0893e953 100644 --- a/i3bar/include/common.h +++ b/i3bar/include/common.h @@ -52,6 +52,6 @@ TAILQ_HEAD(statusline_head, status_block) statusline_head; #include "xcb.h" #include "config.h" #include "libi3.h" -#include "determine_json_version.h" +#include "parse_json_header.h" #endif diff --git a/i3bar/include/determine_json_version.h b/i3bar/include/determine_json_version.h deleted file mode 100644 index 52c6f5d2..00000000 --- a/i3bar/include/determine_json_version.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * vim:ts=4:sw=4:expandtab - * - * i3bar - an xcb-based status- and ws-bar for i3 - * © 2010-2012 Axel Wagner and contributors (see also: LICENSE) - * - * determine_json_version.c: Determines the JSON protocol version based on the - * first line of input from a child program. - * - */ -#ifndef DETERMINE_JSON_VERSION_H_ -#define DETERMINE_JSON_VERSION_H_ - -#include - -/* - * Determines the JSON i3bar protocol version from the given buffer. In case - * the buffer does not contain valid JSON, or no version field is found, this - * function returns -1. The amount of bytes consumed by parsing the header is - * returned in *consumed (if non-NULL). - * - * The return type is an int32_t to avoid machines with different sizes of - * 'int' to allow different values here. It’s highly unlikely we ever exceed - * even an int8_t, but still… - * - */ -int32_t determine_json_version(const unsigned char *buffer, int length, unsigned int *consumed); - -#endif diff --git a/i3bar/include/parse_json_header.h b/i3bar/include/parse_json_header.h new file mode 100644 index 00000000..52c6f5d2 --- /dev/null +++ b/i3bar/include/parse_json_header.h @@ -0,0 +1,29 @@ +/* + * vim:ts=4:sw=4:expandtab + * + * i3bar - an xcb-based status- and ws-bar for i3 + * © 2010-2012 Axel Wagner and contributors (see also: LICENSE) + * + * determine_json_version.c: Determines the JSON protocol version based on the + * first line of input from a child program. + * + */ +#ifndef DETERMINE_JSON_VERSION_H_ +#define DETERMINE_JSON_VERSION_H_ + +#include + +/* + * Determines the JSON i3bar protocol version from the given buffer. In case + * the buffer does not contain valid JSON, or no version field is found, this + * function returns -1. The amount of bytes consumed by parsing the header is + * returned in *consumed (if non-NULL). + * + * The return type is an int32_t to avoid machines with different sizes of + * 'int' to allow different values here. It’s highly unlikely we ever exceed + * even an int8_t, but still… + * + */ +int32_t determine_json_version(const unsigned char *buffer, int length, unsigned int *consumed); + +#endif diff --git a/i3bar/src/determine_json_version.c b/i3bar/src/determine_json_version.c deleted file mode 100644 index abd43038..00000000 --- a/i3bar/src/determine_json_version.c +++ /dev/null @@ -1,104 +0,0 @@ -/* - * vim:ts=4:sw=4:expandtab - * - * i3bar - an xcb-based status- and ws-bar for i3 - * © 2010-2012 Axel Wagner and contributors (see also: LICENSE) - * - * determine_json_version.c: Determines the JSON protocol version based on the - * first line of input from a child program. - * - */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -static bool version_key; -static int32_t version_number; - -#if YAJL_MAJOR >= 2 -static int version_integer(void *ctx, long long val) { -#else -static int version_integer(void *ctx, long val) { -#endif - if (version_key) - version_number = (uint32_t)val; - return 1; -} - -#if YAJL_MAJOR >= 2 -static int version_map_key(void *ctx, const unsigned char *stringval, size_t stringlen) { -#else -static int version_map_key(void *ctx, const unsigned char *stringval, unsigned int stringlen) { -#endif - version_key = (stringlen == strlen("version") && - strncmp((const char*)stringval, "version", strlen("version")) == 0); - return 1; -} - -static yajl_callbacks version_callbacks = { - NULL, /* null */ - NULL, /* boolean */ - &version_integer, - NULL, /* double */ - NULL, /* number */ - NULL, /* string */ - NULL, /* start_map */ - &version_map_key, - NULL, /* end_map */ - NULL, /* start_array */ - NULL /* end_array */ -}; - -/* - * Determines the JSON i3bar protocol version from the given buffer. In case - * the buffer does not contain valid JSON, or no version field is found, this - * function returns -1. The amount of bytes consumed by parsing the header is - * returned in *consumed (if non-NULL). - * - * The return type is an int32_t to avoid machines with different sizes of - * 'int' to allow different values here. It’s highly unlikely we ever exceed - * even an int8_t, but still… - * - */ -int32_t determine_json_version(const unsigned char *buffer, int length, unsigned int *consumed) { -#if YAJL_MAJOR >= 2 - yajl_handle handle = yajl_alloc(&version_callbacks, NULL, NULL); - /* Allow trailing garbage. yajl 1 always behaves that way anyways, but for - * yajl 2, we need to be explicit. */ - yajl_config(handle, yajl_allow_trailing_garbage, 1); -#else - yajl_parser_config parse_conf = { 0, 0 }; - - yajl_handle handle = yajl_alloc(&version_callbacks, &parse_conf, NULL, NULL); -#endif - - version_key = false; - version_number = -1; - - yajl_status state = yajl_parse(handle, buffer, length); - if (state != yajl_status_ok) { - version_number = -1; - if (consumed != NULL) - *consumed = 0; - } else { - if (consumed != NULL) - *consumed = yajl_get_bytes_consumed(handle); - } - - yajl_free(handle); - - return version_number; -} diff --git a/i3bar/src/parse_json_header.c b/i3bar/src/parse_json_header.c new file mode 100644 index 00000000..abd43038 --- /dev/null +++ b/i3bar/src/parse_json_header.c @@ -0,0 +1,104 @@ +/* + * vim:ts=4:sw=4:expandtab + * + * i3bar - an xcb-based status- and ws-bar for i3 + * © 2010-2012 Axel Wagner and contributors (see also: LICENSE) + * + * determine_json_version.c: Determines the JSON protocol version based on the + * first line of input from a child program. + * + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static bool version_key; +static int32_t version_number; + +#if YAJL_MAJOR >= 2 +static int version_integer(void *ctx, long long val) { +#else +static int version_integer(void *ctx, long val) { +#endif + if (version_key) + version_number = (uint32_t)val; + return 1; +} + +#if YAJL_MAJOR >= 2 +static int version_map_key(void *ctx, const unsigned char *stringval, size_t stringlen) { +#else +static int version_map_key(void *ctx, const unsigned char *stringval, unsigned int stringlen) { +#endif + version_key = (stringlen == strlen("version") && + strncmp((const char*)stringval, "version", strlen("version")) == 0); + return 1; +} + +static yajl_callbacks version_callbacks = { + NULL, /* null */ + NULL, /* boolean */ + &version_integer, + NULL, /* double */ + NULL, /* number */ + NULL, /* string */ + NULL, /* start_map */ + &version_map_key, + NULL, /* end_map */ + NULL, /* start_array */ + NULL /* end_array */ +}; + +/* + * Determines the JSON i3bar protocol version from the given buffer. In case + * the buffer does not contain valid JSON, or no version field is found, this + * function returns -1. The amount of bytes consumed by parsing the header is + * returned in *consumed (if non-NULL). + * + * The return type is an int32_t to avoid machines with different sizes of + * 'int' to allow different values here. It’s highly unlikely we ever exceed + * even an int8_t, but still… + * + */ +int32_t determine_json_version(const unsigned char *buffer, int length, unsigned int *consumed) { +#if YAJL_MAJOR >= 2 + yajl_handle handle = yajl_alloc(&version_callbacks, NULL, NULL); + /* Allow trailing garbage. yajl 1 always behaves that way anyways, but for + * yajl 2, we need to be explicit. */ + yajl_config(handle, yajl_allow_trailing_garbage, 1); +#else + yajl_parser_config parse_conf = { 0, 0 }; + + yajl_handle handle = yajl_alloc(&version_callbacks, &parse_conf, NULL, NULL); +#endif + + version_key = false; + version_number = -1; + + yajl_status state = yajl_parse(handle, buffer, length); + if (state != yajl_status_ok) { + version_number = -1; + if (consumed != NULL) + *consumed = 0; + } else { + if (consumed != NULL) + *consumed = yajl_get_bytes_consumed(handle); + } + + yajl_free(handle); + + return version_number; +}