From 69141249476e89aa29b31ea8e81450eea0f67e11 Mon Sep 17 00:00:00 2001 From: Maykel Arias Torres Date: Thu, 5 Dec 2019 19:06:07 -0500 Subject: [PATCH] [include] refs #5 Refactor `make lint-libc` --- include/base64.h | 24 +++-- include/fcassert.h | 2 +- include/fcstring.h | 24 ++--- include/fctest.h | 40 ++++---- include/fctypes.h | 1 - include/json.h | 241 ++++++++++++++++++++------------------------- 6 files changed, 154 insertions(+), 178 deletions(-) diff --git a/include/base64.h b/include/base64.h index fb1fd74..6157741 100644 --- a/include/base64.h +++ b/include/base64.h @@ -10,11 +10,12 @@ Thank you for inspiration: #include -//Base64 char table function - used internally for decoding +// Base64 char table function - used internally for decoding int b64_int(unsigned int ch); // in_size : the number bytes to be encoded. -// Returns the recommended memory size to be allocated for the output buffer excluding the null byte +// Returns the recommended memory size to be allocated for the output buffer +// excluding the null byte unsigned int b64e_size(unsigned int in_size); // in_size : the number bytes to be decoded. @@ -23,22 +24,25 @@ unsigned int b64d_size(unsigned int in_size); // in : buffer of "raw" binary to be encoded. // in_len : number of bytes to be encoded. -// out : pointer to buffer with enough memory, user is responsible for memory allocation, receives null-terminated string -// returns size of output including null byte -unsigned int b64_encode(const unsigned char* in, unsigned int in_len, unsigned char* out); +// out : pointer to buffer with enough memory, user is responsible for memory +// allocation, receives null-terminated string returns size of output including +// null byte +unsigned int b64_encode(const unsigned char *in, unsigned int in_len, + unsigned char *out); // in : buffer of base64 string to be decoded. // in_len : number of bytes to be decoded. -// out : pointer to buffer with enough memory, user is responsible for memory allocation, receives "raw" binary -// returns size of output excluding null byte -int b64_decode(const unsigned char* in, unsigned int in_len, unsigned char* out); +// out : pointer to buffer with enough memory, user is responsible for memory +// allocation, receives "raw" binary returns size of output excluding null byte +int b64_decode(const unsigned char *in, unsigned int in_len, + unsigned char *out); // file-version b64_encode // Input : filenames // returns size of output -unsigned int b64_encodef(char* InFile, char* OutFile); +unsigned int b64_encodef(char *InFile, char *OutFile); // file-version b64_decode // Input : filenames // returns size of output -int b64_decodef(char* InFile, char* OutFile); +int b64_decodef(char *InFile, char *OutFile); diff --git a/include/fcassert.h b/include/fcassert.h index 095f598..230911d 100644 --- a/include/fcassert.h +++ b/include/fcassert.h @@ -1,8 +1,8 @@ #ifndef LIBFC_ASSERT_H #define LIBFC_ASSERT_H -#include "libfibercrypto.h" #include "fcerrors.h" +#include "libfibercrypto.h" extern GoInt isGoStringEq(GoString string1, GoString string2); diff --git a/include/fcstring.h b/include/fcstring.h index 0cb1ef2..8e579c0 100644 --- a/include/fcstring.h +++ b/include/fcstring.h @@ -5,26 +5,26 @@ #include #include -extern void randBytes(GoSlice* bytes, size_t n); +extern void randBytes(GoSlice *bytes, size_t n); -extern void bytesnhex(unsigned char* buf, char* str, int n); +extern void bytesnhex(unsigned char *buf, char *str, int n); -extern void strnhex(unsigned char* buf, char* str, int n); +extern void strnhex(unsigned char *buf, char *str, int n); -extern void strnhexlower(unsigned char* buf, char* str, int n); +extern void strnhexlower(unsigned char *buf, char *str, int n); -extern int hexnstr(const char* hex, unsigned char* str, int n); +extern int hexnstr(const char *hex, unsigned char *str, int n); -extern void bin2hex(unsigned char* buf, char* str, int n); +extern void bin2hex(unsigned char *buf, char *str, int n); -extern int cmpGoSlice_GoSlice(GoSlice* slice1, GoSlice_* slice2); +extern int cmpGoSlice_GoSlice(GoSlice *slice1, GoSlice_ *slice2); -extern void bin2hex(unsigned char* buf, char* str, int n); +extern void bin2hex(unsigned char *buf, char *str, int n); -extern int string_has_suffix(const char* str, const char* suffix); +extern int string_has_suffix(const char *str, const char *suffix); -extern int string_has_prefix(const char* str, const char* prefix); +extern int string_has_prefix(const char *str, const char *prefix); -extern int count_words(const char* str, int length); +extern int count_words(const char *str, int length); -#endif //LIBFC_STRING_H +#endif // LIBFC_STRING_H diff --git a/include/fctest.h b/include/fctest.h index fa4372a..d65f5eb 100644 --- a/include/fctest.h +++ b/include/fctest.h @@ -1,8 +1,8 @@ -#include "json.h" -#include "libfibercrypto.h" #include "fcassert.h" #include "fctypes.h" +#include "json.h" +#include "libfibercrypto.h" #ifndef LIBFC_TEST_H #define LIBFC_TEST_H @@ -19,45 +19,49 @@ *---------------------------------------------------------------------- */ -void* registerMemCleanup(void* p); +void *registerMemCleanup(void *p); -int registerJsonFree(void* p); +int registerJsonFree(void *p); -void freeRegisteredJson(void* p); +void freeRegisteredJson(void *p); int registerHandleClose(Handle handle); void closeRegisteredHandle(Handle handle); -void freeRegisteredMemCleanup(void* p); +void freeRegisteredMemCleanup(void *p); -int copySlice(GoSlice_* pdest, GoSlice_* psource, int elem_size); -int copyGoSlice_toGoSlice(GoSlice* pdest, GoSlice_* psource, int elem_size); +int copySlice(GoSlice_ *pdest, GoSlice_ *psource, int elem_size); +int copyGoSlice_toGoSlice(GoSlice *pdest, GoSlice_ *psource, int elem_size); -int cutSlice(GoSlice_* slice, int start, int end, int elem_size, GoSlice_* result); +int cutSlice(GoSlice_ *slice, int start, int end, int elem_size, + GoSlice_ *result); -int concatSlices(GoSlice_* slice1, GoSlice_* slice2, int elem_size, GoSlice_* result); +int concatSlices(GoSlice_ *slice1, GoSlice_ *slice2, int elem_size, + GoSlice_ *result); /*---------------------------------------------------------------------- * JSON helpers *---------------------------------------------------------------------- */ -json_value* loadJsonFile(const char* filename); +json_value *loadJsonFile(const char *filename); -json_value* json_get_string(json_value* value, const char* key); +json_value *json_get_string(json_value *value, const char *key); -int json_set_string(json_value* value, const char* new_string_value); +int json_set_string(json_value *value, const char *new_string_value); -int compareJsonValues(json_value* value1, json_value* value2); +int compareJsonValues(json_value *value1, json_value *value2); -json_value* get_json_value(json_value* node, const char* path, json_type type); +json_value *get_json_value(json_value *node, const char *path, json_type type); -json_value* get_json_value_not_strict(json_value* node, const char* path, json_type type, int allow_null); +json_value *get_json_value_not_strict(json_value *node, const char *path, + json_type type, int allow_null); -int compareJsonValuesWithIgnoreList(json_value* value1, json_value* value2, const char* ignoreList); +int compareJsonValuesWithIgnoreList(json_value *value1, json_value *value2, + const char *ignoreList); -int parseBoolean(const char* str, int length); +int parseBoolean(const char *str, int length); /*---------------------------------------------------------------------- * Test infrastructure diff --git a/include/fctypes.h b/include/fctypes.h index 58bfc8e..e6b3a20 100644 --- a/include/fctypes.h +++ b/include/fctypes.h @@ -133,7 +133,6 @@ typedef struct { ///< size. } GoSlice_; - #include "fctypes.gen.h" /** diff --git a/include/json.h b/include/json.h index 467aa3a..34d1990 100644 --- a/include/json.h +++ b/include/json.h @@ -54,218 +54,187 @@ extern "C" { #endif -typedef struct -{ - unsigned long max_memory; - int settings; +typedef struct { + unsigned long max_memory; + int settings; - /* Custom allocator support (leave null to use malloc/free) - */ + /* Custom allocator support (leave null to use malloc/free) + */ - void* (*mem_alloc)(size_t, int zero, void* user_data); - void (*mem_free)(void*, void* user_data); + void *(*mem_alloc)(size_t, int zero, void *user_data); + void (*mem_free)(void *, void *user_data); - void* user_data; /* will be passed to mem_alloc and mem_free */ + void *user_data; /* will be passed to mem_alloc and mem_free */ - size_t value_extra; /* how much extra space to allocate for values? */ + size_t value_extra; /* how much extra space to allocate for values? */ } json_settings; #define json_enable_comments 0x01 typedef enum { - json_none, - json_object, - json_array, - json_integer, - json_double, - json_string, - json_boolean, - json_null + json_none, + json_object, + json_array, + json_integer, + json_double, + json_string, + json_boolean, + json_null } json_type; extern const struct _json_value json_value_none; typedef struct _json_object_entry { - json_char* name; - unsigned int name_length; + json_char *name; + unsigned int name_length; - struct _json_value* value; + struct _json_value *value; } json_object_entry; typedef struct _json_value { - struct _json_value* parent; + struct _json_value *parent; - json_type type; + json_type type; - union { - int boolean; - json_int_t integer; - double dbl; + union { + int boolean; + json_int_t integer; + double dbl; - struct - { - unsigned int length; - json_char* ptr; /* null terminated */ + struct { + unsigned int length; + json_char *ptr; /* null terminated */ - } string; + } string; - struct - { - unsigned int length; + struct { + unsigned int length; - json_object_entry* values; + json_object_entry *values; #if defined(__cplusplus) && __cplusplus >= 201103L - decltype(values) begin() const - { - return values; - } - decltype(values) end() const - { - return values + length; - } + decltype(values) begin() const { return values; } + decltype(values) end() const { return values + length; } #endif - } object; + } object; - struct - { - unsigned int length; - struct _json_value** values; + struct { + unsigned int length; + struct _json_value **values; #if defined(__cplusplus) && __cplusplus >= 201103L - decltype(values) begin() const - { - return values; - } - decltype(values) end() const - { - return values + length; - } + decltype(values) begin() const { return values; } + decltype(values) end() const { return values + length; } #endif - } array; + } array; - } u; + } u; - union { - struct _json_value* next_alloc; - void* object_mem; + union { + struct _json_value *next_alloc; + void *object_mem; - } _reserved; + } _reserved; #ifdef JSON_TRACK_SOURCE - /* Location of the value in the source JSON - */ - unsigned int line, col; + /* Location of the value in the source JSON + */ + unsigned int line, col; #endif - - /* Some C++ operator sugar */ + /* Some C++ operator sugar */ #ifdef __cplusplus public: - inline _json_value() - { - memset(this, 0, sizeof(_json_value)); - } - - inline const struct _json_value& operator[](int index) const - { - if (type != json_array || index < 0 || ((unsigned int)index) >= u.array.length) { - return json_value_none; - } + inline _json_value() { memset(this, 0, sizeof(_json_value)); } - return *u.array.values[index]; + inline const struct _json_value &operator[](int index) const { + if (type != json_array || index < 0 || + ((unsigned int)index) >= u.array.length) { + return json_value_none; } - inline const struct _json_value& operator[](const char* index) const - { - if (type != json_object) - return json_value_none; + return *u.array.values[index]; + } - for (unsigned int i = 0; i < u.object.length; ++i) - if (!strcmp(u.object.values[i].name, index)) - return *u.object.values[i].value; + inline const struct _json_value &operator[](const char *index) const { + if (type != json_object) + return json_value_none; - return json_value_none; - } + for (unsigned int i = 0; i < u.object.length; ++i) + if (!strcmp(u.object.values[i].name, index)) + return *u.object.values[i].value; - inline operator const char*() const - { - switch (type) { - case json_string: - return u.string.ptr; + return json_value_none; + } - default: - return ""; - }; - } + inline operator const char *() const { + switch (type) { + case json_string: + return u.string.ptr; - inline operator json_int_t() const - { - switch (type) { - case json_integer: - return u.integer; + default: + return ""; + }; + } - case json_double: - return (json_int_t)u.dbl; + inline operator json_int_t() const { + switch (type) { + case json_integer: + return u.integer; - default: - return 0; - }; - } + case json_double: + return (json_int_t)u.dbl; - inline operator BOOL() const - { - if (type != json_boolean) - return false; + default: + return 0; + }; + } - return u.boolean != 0; - } + inline operator BOOL() const { + if (type != json_boolean) + return false; - inline operator double() const - { - switch (type) { - case json_integer: - return (double)u.integer; + return u.boolean != 0; + } - case json_double: - return u.dbl; + inline operator double() const { + switch (type) { + case json_integer: + return (double)u.integer; - default: - return 0; - }; - } + case json_double: + return u.dbl; + + default: + return 0; + }; + } #endif } json_value; -json_value* json_parse(const json_char* json, - size_t length); +json_value *json_parse(const json_char *json, size_t length); #define json_error_max 128 -json_value* json_parse_ex(json_settings* settings, - const json_char* json, - size_t length, - char* error); - -void json_value_free(json_value*); +json_value *json_parse_ex(json_settings *settings, const json_char *json, + size_t length, char *error); +void json_value_free(json_value *); /* Not usually necessary, unless you used a custom mem_alloc and now want to * use a custom mem_free. */ -void json_value_free_ex(json_settings* settings, - json_value*); - +void json_value_free_ex(json_settings *settings, json_value *); #ifdef __cplusplus } /* extern "C" */