From d37efa1cd5832d99b1929c1e27448ebd997cc232 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Sun, 20 Nov 2011 09:50:00 +0100 Subject: [PATCH] code cleanups, made naming more consisten, etc. --- ChangeLog | 8 ++- include/token/cclass.h | 81 ++++++++++++++------------- include/token/crypt.h | 14 ++--- include/token/dyntype.h | 20 +++---- include/token/dyntype/hash.h | 8 +-- include/token/packet.h | 12 ++-- src/cclass.c | 41 +++++++------- src/crypt.c | 80 +++++++++++++-------------- src/dyntype.c | 82 +++++++++++++-------------- src/dyntype/hash.c | 104 +++++++++++++++++------------------ src/packet.c | 48 ++++++++-------- tests/mock/class.c | 16 +++--- tests/mock/class.h | 8 +-- tests/runtest.c | 6 +- 14 files changed, 270 insertions(+), 258 deletions(-) diff --git a/ChangeLog b/ChangeLog index 485ba57..2ca731c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,10 +1,14 @@ +2011-11-20 09:50:00 +0100 Georg Hopp + + * code cleanups, made naming more consisten, etc. (HEAD, master) + 2011-11-20 05:49:12 +0100 Georg Hopp - * added a doxygen config as well as the patch for doxygen to handle my own class definitions. Changed all copyright to file scope (HEAD, master) + * added a doxygen config as well as the patch for doxygen to handle my own class definitions. Changed all copyright to file scope (origin/master, origin/HEAD) 2011-11-20 02:39:21 +0100 Georg Hopp - * finished tests for packet (origin/master, origin/HEAD) + * finished tests for packet 2011-11-20 00:41:23 +0100 Georg Hopp diff --git a/include/token/cclass.h b/include/token/cclass.h index caf600c..e2103da 100644 --- a/include/token/cclass.h +++ b/include/token/cclass.h @@ -23,15 +23,8 @@ #include #include -#define SWAP(a, b) a ^= b; b ^= a; a ^= b; - #define CCLASS_MAGIC 0xFEFE -#define CLASS(_class) \ - struct _##_class; \ - typedef struct _##_class * _class; \ - extern const _CCLASS const __##_class; \ - struct _##_class typedef void (* ctor)(void *, va_list *); typedef void (* clr)(void *); @@ -39,51 +32,65 @@ typedef void (* dtor)(void *); typedef void (* jCtor)(void *, struct json_object *); typedef void (* jTo)(void *, struct json_object **); -typedef struct CCLASS { +typedef struct _CCLASS { const int magic; size_t size; - void (* __construct)(void * _this, va_list * params); - void (* __clear)(void * _this); - void (* __destruct)(void * _this); - void (* __jsonConst)(void * _this, struct json_object * json); - void (* __toJson)(void * _this, struct json_object ** json); -} * _CCLASS; -#define _CCLASS_SIZE sizeof(_CCLASS) -#define CCLASS_SIZE sizeof(struct CCLASS) + ctor __construct; + clr __clear; + dtor __destruct; + jCtor __jsonConst; + jTo __toJson; +} * CCLASS; + +#define CCLASS_SIZE sizeof(struct _CCLASS) +#define CCLASS_PTR_SIZE sizeof(CCLASS) -#define __construct(class) static void __construct(class _this, va_list * params) -#define __clear(class) static void __clear(class _this) -#define __destruct(class) static void __destruct(class _this) -#define __jsonConst(class) static void __jsonConst(class _this, struct json_object * json) -#define __toJson(class) static void __toJson(class _this, struct json_object ** json) +#define __construct(_class) \ + static void __construct(_class this, va_list * params) +#define __clear(_class) \ + static void __clear(_class this) +#define __destruct(_class) \ + static void __destruct(_class this) +#define __jsonConst(_class) \ + static void __jsonConst(_class this, struct json_object * json) +#define __toJson(_class) \ + static void __toJson(_class this, struct json_object ** json) -#define INIT_CCLASS(class) \ - __construct(class); \ - __clear(class); \ - __destruct(class); \ - __jsonConst(class); \ - __toJson(class); \ - static const struct CCLASS _class = { \ +#define CLASS(_class) \ + struct _##_class; \ + typedef struct _##_class * _class; \ + extern const CCLASS const __##_class; \ + struct _##_class + +#define INIT_CLASS(_class) \ + __construct(_class); \ + __clear(_class); \ + __destruct(_class); \ + __jsonConst(_class); \ + __toJson(_class); \ + static const struct _CCLASS _cclass = { \ CCLASS_MAGIC, \ - sizeof(struct _##class), \ + sizeof(struct _##_class), \ (ctor)__construct, \ (clr)__clear, \ (dtor)__destruct, \ (jCtor)__jsonConst, \ (jTo)__toJson \ - }; const _CCLASS __##class = (const _CCLASS)&_class + }; const CCLASS __##_class = (const CCLASS)&_cclass + +void * _new(const CCLASS _class, ...); +#define new(_class, ...) _new((__##_class), __VA_ARGS__) + +void * _newFromJson(const CCLASS _class, struct json_object * json); +#define newFromJson(_class, json) _newFromJson((__##_class), (json)) + +int _instanceOf(const CCLASS _class, void * _object); +#define instanceOf(_class, object) _instanceOf((__##_class), (object)) -void * _new(const _CCLASS _class, ...); -void * _newFromJson(const _CCLASS _class, struct json_object * json); void clear(void * _object); void delete(void * _object); void toJson(void * _object, struct json_object ** json); int isObject(void * _object); -int _instanceOf(const _CCLASS _class, void * _object); - -#define new(class, ...) _new((__##class), __VA_ARGS__) -#define newFromJson(class, json) _newFromJson((__##class), (json)) -#define instanceOf(class, object) _instanceOf((__##class), (object)) #endif//__CCLASS_H__ diff --git a/include/token/crypt.h b/include/token/crypt.h index 9b41886..73e22bc 100644 --- a/include/token/crypt.h +++ b/include/token/crypt.h @@ -25,7 +25,7 @@ CLASS(CRYPT) { - const _CCLASS const class; + const CCLASS const class; const char * algorithm; const char * mode; MCRYPT mcrypt; @@ -35,21 +35,21 @@ CLASS(CRYPT) { }; void * crypt_encrypt( - CRYPT _this, + CRYPT this, const void * const data, const char * const password, size_t * length); void * crypt_decrypt( - CRYPT _this, + CRYPT this, const void * const data, const char * const password, size_t * length); -void * crypt_createIv(CRYPT _this); -void crypt_setIv(CRYPT _this, const void * const iv); -void crypt_setIvsize(CRYPT _this, const size_t size); -void crypt_setKeysize(CRYPT _this, const size_t size); +void * crypt_createIv(CRYPT this); +void crypt_setIv(CRYPT this, const void * const iv); +void crypt_setIvsize(CRYPT this, const size_t size); +void crypt_setKeysize(CRYPT this, const size_t size); #endif//__CRYPT_H__ // vim: set et ts=4 sw=4: diff --git a/include/token/dyntype.h b/include/token/dyntype.h index 454cc3c..ad123cd 100644 --- a/include/token/dyntype.h +++ b/include/token/dyntype.h @@ -39,9 +39,9 @@ enum DYNTYPE_TYPES { CLASS(DYNTYPE) { - const _CCLASS const class; + const CCLASS const class; enum DYNTYPE_TYPES type; - union _data { + union { unsigned char _boolean; int _int; double _double; @@ -60,14 +60,14 @@ CLASS(DYNTYPE) { #define dyntype_newArray(value) new(DYNTYPE, DYNTYPE_TYPE_ARRAY, value) #define dyntype_newHash(value) new(DYNTYPE, DYNTYPE_TYPE_HASH, value) -enum DYNTYPE_TYPES dyntype_getType(DYNTYPE _this); -size_t dyntype_getSize(DYNTYPE _this); -unsigned char dyntype_getBoolean(DYNTYPE _this); -int dyntype_getInt(DYNTYPE _this); -double dyntype_getDouble(DYNTYPE _this); -char * dyntype_getString(DYNTYPE _this); -DYNTYPE * dyntype_getArray(DYNTYPE _this); -DYNTYPE_HASH dyntype_getHash(DYNTYPE _this); +enum DYNTYPE_TYPES dyntype_getType(DYNTYPE this); +size_t dyntype_getSize(DYNTYPE this); +unsigned char dyntype_getBoolean(DYNTYPE this); +int dyntype_getInt(DYNTYPE this); +double dyntype_getDouble(DYNTYPE this); +char * dyntype_getString(DYNTYPE this); +DYNTYPE * dyntype_getArray(DYNTYPE this); +DYNTYPE_HASH dyntype_getHash(DYNTYPE this); #endif//__DYNTYPE_H__ diff --git a/include/token/dyntype/hash.h b/include/token/dyntype/hash.h index f3549b6..1a7a929 100644 --- a/include/token/dyntype/hash.h +++ b/include/token/dyntype/hash.h @@ -32,7 +32,7 @@ struct _DYNTYPE_HASH; #define __DYNTYPE_HASH_H__ CLASS(DYNTYPE_HASH) { - const _CCLASS const class; + const CCLASS const class; char ** keys; DYNTYPE * values; size_t size; @@ -40,9 +40,9 @@ CLASS(DYNTYPE_HASH) { }; -void dyntype_hash_set(DYNTYPE_HASH _this, const char * key, struct _DYNTYPE * value); -DYNTYPE dyntype_hash_get(DYNTYPE_HASH _this, const char * key); -void dyntype_hash_del(DYNTYPE_HASH _this, const char * key); +void dyntype_hash_set(DYNTYPE_HASH this, const char * key, struct _DYNTYPE * value); +DYNTYPE dyntype_hash_get(DYNTYPE_HASH this, const char * key); +void dyntype_hash_del(DYNTYPE_HASH this, const char * key); #endif diff --git a/include/token/packet.h b/include/token/packet.h index a69ba2f..5161d2a 100644 --- a/include/token/packet.h +++ b/include/token/packet.h @@ -30,16 +30,16 @@ enum PACKET_CONTENT_KEYS { CLASS(PACKET) { - const _CCLASS const class; + const CCLASS const class; DYNTYPE content[2]; }; -DYNTYPE packet_getHeader(PACKET _this); -DYNTYPE packet_getData(PACKET _this); -void packet_setHeader(PACKET _this, DYNTYPE header); -void packet_setData(PACKET _this, DYNTYPE data); -void packet_set_default_content(PACKET _this); +DYNTYPE packet_getHeader(PACKET this); +DYNTYPE packet_getData(PACKET this); +void packet_setHeader(PACKET this, DYNTYPE header); +void packet_setData(PACKET this, DYNTYPE data); +void packet_set_default_content(PACKET this); #endif//__PACKET_H__ diff --git a/src/cclass.c b/src/cclass.c index 0af3c85..8427aaa 100644 --- a/src/cclass.c +++ b/src/cclass.c @@ -30,12 +30,12 @@ #undef __toJson void * -_new(const _CCLASS _class, ...) +_new(const CCLASS _class, ...) { - const _CCLASS class = _class; - void * object = calloc(1, class->size); + const CCLASS class = _class; + void * object = calloc(1, class->size); - * (const struct CCLASS **) object = class; + * (const struct _CCLASS **) object = class; if (class->__construct) { va_list params; @@ -49,12 +49,12 @@ _new(const _CCLASS _class, ...) } void * -_newFromJson(const _CCLASS _class, struct json_object * json) +_newFromJson(const CCLASS _class, struct json_object * json) { - const struct CCLASS * class = _class; - void * object = calloc(1, class->size); + const CCLASS class = _class; + void * object = calloc(1, class->size); - * (const struct CCLASS **) object = class; + * (const struct _CCLASS **) object = class; if (class->__jsonConst && json) { class->__jsonConst(object, json); @@ -63,10 +63,19 @@ _newFromJson(const _CCLASS _class, struct json_object * json) return object; } +int +_instanceOf(const CCLASS _class, void * _object) +{ + const CCLASS * class = _object; + + return (class && _class == *class); +} + + void clear(void * _object) { - const struct CCLASS ** class = _object; + const CCLASS * class = _object; if (_object && *class && (*class)->__clear) { (*class)->__clear(_object); @@ -76,7 +85,7 @@ clear(void * _object) void delete(void * _object) { - const struct CCLASS ** class = *(void**)_object; + const CCLASS * class = *(void**)_object; if (*(void**)_object && *class && (*class)->__destruct) { (*class)->__destruct(*(void**)_object); @@ -89,7 +98,7 @@ delete(void * _object) void toJson(void * _object, struct json_object ** json) { - const struct CCLASS ** class = _object; + const CCLASS * class = _object; if (_object && *class && (*class)->__toJson) { (*class)->__toJson(_object, json); @@ -99,17 +108,9 @@ toJson(void * _object, struct json_object ** json) int isObject(void * _object) { - const struct CCLASS ** class = _object; + const CCLASS * class = _object; return (_object && (*class) && CCLASS_MAGIC == (*class)->magic); } -int -_instanceOf(const _CCLASS _class, void * _object) -{ - const struct CCLASS ** class = _object; - - return (class && _class == *class); -} - // vim: set et ts=4 sw=4: diff --git a/src/crypt.c b/src/crypt.c index a2738f1..16a4dfc 100644 --- a/src/crypt.c +++ b/src/crypt.c @@ -29,30 +29,30 @@ #include "token/crypt.h" -INIT_CCLASS(CRYPT); +INIT_CLASS(CRYPT); __construct(CRYPT) { - _this->algorithm = va_arg(* params, const char * const); - _this->mode = va_arg(* params, const char * const); + this->algorithm = va_arg(* params, const char * const); + this->mode = va_arg(* params, const char * const); - _this->mcrypt = mcrypt_module_open( - (char *)_this->algorithm, + this->mcrypt = mcrypt_module_open( + (char *)this->algorithm, NULL, - (char *)_this->mode, + (char *)this->mode, NULL); - _this->ivsize = mcrypt_enc_get_iv_size(_this->mcrypt); - _this->keysize = mcrypt_enc_get_key_size(_this->mcrypt); + this->ivsize = mcrypt_enc_get_iv_size(this->mcrypt); + this->keysize = mcrypt_enc_get_key_size(this->mcrypt); } __destruct(CRYPT) { - if (_this->iv) { - free(_this->iv); + if (this->iv) { + free(this->iv); } - mcrypt_module_close(_this->mcrypt); + mcrypt_module_close(this->mcrypt); } __jsonConst(CRYPT) {} @@ -60,18 +60,18 @@ __toJson(CRYPT) {} __clear(CRYPT) {} void * -crypt_createIv(CRYPT _this) +crypt_createIv(CRYPT this) { int urandom; size_t rsize = 0; void * iv = NULL; - iv = calloc(_this->ivsize, sizeof(char)); + iv = calloc(this->ivsize, sizeof(char)); urandom = open("/dev/urandom", O_RDONLY); - rsize = read(urandom, iv, _this->ivsize); + rsize = read(urandom, iv, this->ivsize); - if (_this->ivsize != rsize) { + if (this->ivsize != rsize) { free(iv); iv = NULL; } @@ -81,18 +81,18 @@ crypt_createIv(CRYPT _this) static void * -createKey(CRYPT _this, const char * const password) +createKey(CRYPT this, const char * const password) { void * key = NULL; - key = calloc(_this->keysize, sizeof(char)); + key = calloc(this->keysize, sizeof(char)); mhash_keygen( KEYGEN_MCRYPT, MHASH_SHA256, mhash_keygen_count(), key, - _this->keysize, + this->keysize, NULL, 0, (mutils_word8 *)password, // @TODO: bad karma...this might change password. @@ -103,7 +103,7 @@ createKey(CRYPT _this, const char * const password) void * crypt_encrypt( - CRYPT _this, + CRYPT this, const void * const data, const char * const password, size_t * length) @@ -112,25 +112,25 @@ crypt_encrypt( void * iv; void * key; - key = createKey(_this, password); - if(_this->iv) { - iv = _this->iv; + key = createKey(this, password); + if(this->iv) { + iv = this->iv; } else { - iv = crypt_createIv(_this); + iv = crypt_createIv(this); } - mcrypt_generic_init(_this->mcrypt, key, _this->keysize, iv); + mcrypt_generic_init(this->mcrypt, key, this->keysize, iv); - encrypted = calloc(_this->ivsize + *length, sizeof(char)); - memcpy(encrypted, iv, _this->ivsize); - memcpy(encrypted + _this->ivsize, data, *length); + encrypted = calloc(this->ivsize + *length, sizeof(char)); + memcpy(encrypted, iv, this->ivsize); + memcpy(encrypted + this->ivsize, data, *length); - mcrypt_generic(_this->mcrypt, encrypted + _this->ivsize, *length); - mcrypt_generic_deinit(_this->mcrypt); - *length += _this->ivsize; + mcrypt_generic(this->mcrypt, encrypted + this->ivsize, *length); + mcrypt_generic_deinit(this->mcrypt); + *length += this->ivsize; free(key); - if (_this->iv != iv) { + if (this->iv != iv) { free(iv); } @@ -139,7 +139,7 @@ crypt_encrypt( void * crypt_decrypt( - CRYPT _this, + CRYPT this, const void * const data, const char * const password, size_t * length) @@ -148,18 +148,18 @@ crypt_decrypt( void * iv; void * key; - key = createKey(_this, password); - iv = calloc(_this->ivsize, sizeof(char)); - memcpy(iv, data, _this->ivsize); + key = createKey(this, password); + iv = calloc(this->ivsize, sizeof(char)); + memcpy(iv, data, this->ivsize); - mcrypt_generic_init(_this->mcrypt, key, _this->keysize, iv); + mcrypt_generic_init(this->mcrypt, key, this->keysize, iv); - *length -= _this->ivsize; + *length -= this->ivsize; decrypted = calloc(*length, sizeof(char)); - memcpy(decrypted, data + _this->ivsize, *length); + memcpy(decrypted, data + this->ivsize, *length); - mdecrypt_generic(_this->mcrypt, decrypted, *length); - mcrypt_generic_deinit(_this->mcrypt); + mdecrypt_generic(this->mcrypt, decrypted, *length); + mcrypt_generic_deinit(this->mcrypt); free(key); free(iv); diff --git a/src/dyntype.c b/src/dyntype.c index 858d0a9..aa7ce68 100644 --- a/src/dyntype.c +++ b/src/dyntype.c @@ -35,15 +35,15 @@ size_t _dyntype_sizes[] = { sizeof(DYNTYPE_HASH) }; -INIT_CCLASS(DYNTYPE); +INIT_CLASS(DYNTYPE); __construct(DYNTYPE) { - _this->type = va_arg(* params, enum DYNTYPE_TYPES); + this->type = va_arg(* params, enum DYNTYPE_TYPES); - switch(_this->type) { + switch(this->type) { case DYNTYPE_TYPE_INT: - (_this->data)._int = va_arg(* params, int); + (this->data)._int = va_arg(* params, int); break; case DYNTYPE_TYPE_STRING: @@ -51,17 +51,17 @@ __construct(DYNTYPE) const char * string = va_arg(* params, const char *); const size_t length = strlen(string); - (_this->data)._string = calloc(length + 1, sizeof(char)); - memcpy((_this->data)._string, string, length); + (this->data)._string = calloc(length + 1, sizeof(char)); + memcpy((this->data)._string, string, length); } break; case DYNTYPE_TYPE_HASH: - (_this->data)._hash = va_arg(* params, DYNTYPE_HASH); + (this->data)._hash = va_arg(* params, DYNTYPE_HASH); break; default: - (_this->data)._hash = NULL; + (this->data)._hash = NULL; } } @@ -69,8 +69,8 @@ __jsonConst(DYNTYPE) { switch (json_object_get_type(json)) { case json_type_int: - _this->type = DYNTYPE_TYPE_INT; - (_this->data)._int = (int)json_object_get_int(json); + this->type = DYNTYPE_TYPE_INT; + (this->data)._int = (int)json_object_get_int(json); break; case json_type_string: @@ -78,21 +78,21 @@ __jsonConst(DYNTYPE) const char * string = json_object_get_string(json); const size_t length = strlen(string); - _this->type = DYNTYPE_TYPE_STRING; - (_this->data)._string = calloc(length + 1, sizeof(char)); - memcpy((_this->data)._string, string, length); + this->type = DYNTYPE_TYPE_STRING; + (this->data)._string = calloc(length + 1, sizeof(char)); + memcpy((this->data)._string, string, length); // the json object handles the memory for string.... } break; case json_type_object: - _this->type = DYNTYPE_TYPE_HASH; - (_this->data)._hash = newFromJson(DYNTYPE_HASH, json); + this->type = DYNTYPE_TYPE_HASH; + (this->data)._hash = newFromJson(DYNTYPE_HASH, json); break; default: - _this->type = DYNTYPE_TYPE_NULL; - (_this->data)._hash = NULL; + this->type = DYNTYPE_TYPE_NULL; + (this->data)._hash = NULL; } } @@ -100,14 +100,14 @@ __clear(DYNTYPE) {} __destruct(DYNTYPE) { - if (_this) { - switch(_this->type) { + if (this) { + switch(this->type) { case DYNTYPE_TYPE_STRING: - free((_this->data)._string); + free((this->data)._string); break; case DYNTYPE_TYPE_HASH: - delete(&((_this->data)._hash)); + delete(&((this->data)._hash)); break; default: @@ -118,17 +118,17 @@ __destruct(DYNTYPE) __toJson(DYNTYPE) { - switch(_this->type) { + switch(this->type) { case DYNTYPE_TYPE_INT: - *json = json_object_new_int((_this->data)._int); + *json = json_object_new_int((this->data)._int); break; case DYNTYPE_TYPE_STRING: - *json = json_object_new_string((_this->data)._string); + *json = json_object_new_string((this->data)._string); break; case DYNTYPE_TYPE_HASH: - toJson((_this->data)._hash, json); + toJson((this->data)._hash, json); break; default: @@ -137,51 +137,51 @@ __toJson(DYNTYPE) } enum DYNTYPE_TYPES -dyntype_getType(DYNTYPE _this) +dyntype_getType(DYNTYPE this) { - return _this->type; + return this->type; } size_t -dyntype_getSize(DYNTYPE _this) +dyntype_getSize(DYNTYPE this) { - return _dyntype_sizes[_this->type]; + return _dyntype_sizes[this->type]; } unsigned char -dyntype_getBoolean(DYNTYPE _this) +dyntype_getBoolean(DYNTYPE this) { - return (_this->data)._boolean; + return (this->data)._boolean; } int -dyntype_getInt(DYNTYPE _this) +dyntype_getInt(DYNTYPE this) { - return (_this->data)._int; + return (this->data)._int; } double -dyntype_getDouble(DYNTYPE _this) +dyntype_getDouble(DYNTYPE this) { - return (_this->data)._double; + return (this->data)._double; } char * -dyntype_getString(DYNTYPE _this) +dyntype_getString(DYNTYPE this) { - return (_this->data)._string; + return (this->data)._string; } DYNTYPE * -dyntype_getArray(DYNTYPE _this) +dyntype_getArray(DYNTYPE this) { - return (_this->data)._array; + return (this->data)._array; } DYNTYPE_HASH -dyntype_getHash(DYNTYPE _this) +dyntype_getHash(DYNTYPE this) { - return (_this->data)._hash; + return (this->data)._hash; } diff --git a/src/dyntype/hash.c b/src/dyntype/hash.c index 4cbda05..b6622f4 100644 --- a/src/dyntype/hash.c +++ b/src/dyntype/hash.c @@ -28,29 +28,29 @@ #define HASH_ENTRY_CHUNK_SIZE 128 -static void _updateHashSize(DYNTYPE_HASH _this); +static void _updateHashSize(DYNTYPE_HASH this); -INIT_CCLASS(DYNTYPE_HASH); +INIT_CLASS(DYNTYPE_HASH); __construct(DYNTYPE_HASH) { - _this->size = 0; - _this->used = 0; - _updateHashSize(_this); + this->size = 0; + this->used = 0; + _updateHashSize(this); } #undef __construct __jsonConst(DYNTYPE_HASH) { - __construct(_this, NULL); + __construct(this, NULL); if (json_type_object != json_object_get_type(json)) { return; } json_object_object_foreach(json, key, value) { - dyntype_hash_set(_this, key, newFromJson(DYNTYPE, value)); + dyntype_hash_set(this, key, newFromJson(DYNTYPE, value)); } } @@ -60,16 +60,16 @@ __destruct(DYNTYPE_HASH) { size_t index; - for (index = 0; index < _this->used; index ++) { - free(_this->keys[index]); - delete(_this->values[index]); + for (index = 0; index < this->used; index ++) { + free(this->keys[index]); + delete(this->values[index]); } - free(_this->keys); - free(_this->values); + free(this->keys); + free(this->values); - _this->size = _this->used = 0; - _updateHashSize(_this); + this->size = this->used = 0; + _updateHashSize(this); } __toJson(DYNTYPE_HASH) @@ -77,31 +77,31 @@ __toJson(DYNTYPE_HASH) size_t index; *json = json_object_new_object(); - for (index = 0; index < _this->used; index ++) { + for (index = 0; index < this->used; index ++) { struct json_object * values; - toJson(_this->values[index], &values); + toJson(this->values[index], &values); - json_object_object_add(*json, _this->keys[index], values); + json_object_object_add(*json, this->keys[index], values); } } static void -_updateHashSize(DYNTYPE_HASH _this) +_updateHashSize(DYNTYPE_HASH this) { - if (_this->used == _this->size) { - _this->size += HASH_ENTRY_CHUNK_SIZE; + if (this->used == this->size) { + this->size += HASH_ENTRY_CHUNK_SIZE; - _this->keys = realloc(_this->keys, sizeof(char*) * _this->size); - memset(_this->keys + (_this->used * sizeof(char*)), + this->keys = realloc(this->keys, sizeof(char*) * this->size); + memset(this->keys + (this->used * sizeof(char*)), 0, HASH_ENTRY_CHUNK_SIZE * sizeof(char*)); - _this->values = realloc( - _this->values, - sizeof(DYNTYPE) * _this->size); - memset(_this->values + (_this->used * sizeof(DYNTYPE)), + this->values = realloc( + this->values, + sizeof(DYNTYPE) * this->size); + memset(this->values + (this->used * sizeof(DYNTYPE)), 0, HASH_ENTRY_CHUNK_SIZE * sizeof(DYNTYPE)); } @@ -109,66 +109,66 @@ _updateHashSize(DYNTYPE_HASH _this) static size_t -_getHashIdx(DYNTYPE_HASH _this, const char * key) +_getHashIdx(DYNTYPE_HASH this, const char * key) { size_t index; for (index = 0; - index < _this->used && strcmp(_this->keys[index], key); + index < this->used && strcmp(this->keys[index], key); index++); return index; } void -dyntype_hash_set(DYNTYPE_HASH _this, const char * key, DYNTYPE value) +dyntype_hash_set(DYNTYPE_HASH this, const char * key, DYNTYPE value) { - size_t index = _getHashIdx(_this, key); + size_t index = _getHashIdx(this, key); - _this->keys[index] = calloc(strlen(key) + 1, sizeof(char)); - memcpy(_this->keys[index], key, strlen(key)); + this->keys[index] = calloc(strlen(key) + 1, sizeof(char)); + memcpy(this->keys[index], key, strlen(key)); - _this->values[index] = value; + this->values[index] = value; - if (index == _this->used) { - _this->used += 1; + if (index == this->used) { + this->used += 1; } - _updateHashSize(_this); + _updateHashSize(this); } DYNTYPE -dyntype_hash_get(DYNTYPE_HASH _this, const char * key) +dyntype_hash_get(DYNTYPE_HASH this, const char * key) { - size_t index = _getHashIdx(_this, key); + size_t index = _getHashIdx(this, key); - if (index == _this->used) { + if (index == this->used) { return NULL; } - return _this->values[index]; + return this->values[index]; } void -dyntype_hash_del(DYNTYPE_HASH _this, const char * key) +dyntype_hash_del(DYNTYPE_HASH this, const char * key) { - size_t index = _getHashIdx(_this, key); + size_t index = _getHashIdx(this, key); - if (index == _this->used) { - return NULL; + if (index == this->used) { + return; } - free(_this->keys[index]); + free(this->keys[index]); - memmove(_this->keys + index + 1, - _this->keys + index, - (_this->size - index) * sizeof(char*)); + memmove(this->keys + index + 1, + this->keys + index, + (this->size - index) * sizeof(char*)); - memmove(_this->values + index + 1, - _this->values + index, - (_this->size - index) * sizeof(DYNTYPE)); + memmove(this->values + index + 1, + this->values + index, + (this->size - index) * sizeof(DYNTYPE)); - _this->used -= 1; + this->used -= 1; } // vim: set et ts=4 sw=4: diff --git a/src/packet.c b/src/packet.c index f202fca..eed51f1 100644 --- a/src/packet.c +++ b/src/packet.c @@ -21,7 +21,7 @@ #include "token/packet.h" -INIT_CCLASS(PACKET); +INIT_CLASS(PACKET); __construct(PACKET) { @@ -34,12 +34,12 @@ __construct(PACKET) if (instanceOf(DYNTYPE, data)) { - packet_setHeader(_this, header); - packet_setData(_this, data); + packet_setHeader(this, header); + packet_setData(this, data); } else { - packet_set_default_content(_this); + packet_set_default_content(this); } } @@ -50,12 +50,12 @@ __jsonConst(PACKET) struct json_object * data = NULL; if (json_type_array != json_object_get_type(json)) { - packet_set_default_content(_this); + packet_set_default_content(this); return; } if (2 != json_object_array_length(json)) { - packet_set_default_content(_this); + packet_set_default_content(this); return; } @@ -63,20 +63,20 @@ __jsonConst(PACKET) data = json_object_array_get_idx(json, 1); if (! (header && data)) { - packet_set_default_content(_this); + packet_set_default_content(this); return; } - packet_setHeader(_this, newFromJson(DYNTYPE, header)); - packet_setData(_this, newFromJson(DYNTYPE, data)); + packet_setHeader(this, newFromJson(DYNTYPE, header)); + packet_setData(this, newFromJson(DYNTYPE, data)); } __clear(PACKET) {} __destruct(PACKET) { - DYNTYPE header = packet_getHeader(_this); - DYNTYPE data = packet_getData(_this); + DYNTYPE header = packet_getHeader(this); + DYNTYPE data = packet_getData(this); if (NULL != header) { delete(&header); @@ -93,40 +93,40 @@ __toJson(PACKET) *json = json_object_new_array(); - toJson(packet_getHeader(_this), &value); + toJson(packet_getHeader(this), &value); json_object_array_add(*json, value); - toJson(packet_getData(_this), &value); + toJson(packet_getData(this), &value); json_object_array_add(*json, value); } -DYNTYPE packet_getHeader(PACKET _this) +DYNTYPE packet_getHeader(PACKET this) { - return _this->content[PACKET_HEADER]; + return this->content[PACKET_HEADER]; } -DYNTYPE packet_getData(PACKET _this) +DYNTYPE packet_getData(PACKET this) { - return _this->content[PACKET_DATA]; + return this->content[PACKET_DATA]; } void -packet_setHeader(PACKET _this, DYNTYPE header) +packet_setHeader(PACKET this, DYNTYPE header) { - _this->content[PACKET_HEADER] = header; + this->content[PACKET_HEADER] = header; } void -packet_setData(PACKET _this, DYNTYPE data) +packet_setData(PACKET this, DYNTYPE data) { - _this->content[PACKET_DATA] = data; + this->content[PACKET_DATA] = data; } void -packet_set_default_content(PACKET _this) +packet_set_default_content(PACKET this) { - _this->content[PACKET_HEADER] = NULL; - _this->content[PACKET_DATA] = NULL; + this->content[PACKET_HEADER] = NULL; + this->content[PACKET_DATA] = NULL; } // vim: set et ts=4 sw=4: diff --git a/tests/mock/class.c b/tests/mock/class.c index 4d22a0f..66681d4 100644 --- a/tests/mock/class.c +++ b/tests/mock/class.c @@ -24,12 +24,12 @@ char _called; -INIT_CCLASS(MOCK_CLASS); +INIT_CLASS(MOCK_CLASS); __construct(MOCK_CLASS) { _called = 1; - _this->value = va_arg(* params, int); + this->value = va_arg(* params, int); } __jsonConst(MOCK_CLASS) @@ -37,7 +37,7 @@ __jsonConst(MOCK_CLASS) _called = 1; assert(json_type_int == json_object_get_type(json)); - _this->value = json_object_get_int(json); + this->value = json_object_get_int(json); } __clear(MOCK_CLASS) {} @@ -49,7 +49,7 @@ __destruct(MOCK_CLASS) __toJson(MOCK_CLASS) { - *json = json_object_new_int(_this->value); + *json = json_object_new_int(this->value); _called = 1; } @@ -58,15 +58,15 @@ __toJson(MOCK_CLASS) */ int -mock_class_getValue(MOCK_CLASS _this) +mock_class_getValue(MOCK_CLASS this) { - return _this->value; + return this->value; } void -mock_class_setValue(MOCK_CLASS _this, int value) +mock_class_setValue(MOCK_CLASS this, int value) { - _this->value = value; + this->value = value; } /** diff --git a/tests/mock/class.h b/tests/mock/class.h index e9ee9ed..a328aad 100644 --- a/tests/mock/class.h +++ b/tests/mock/class.h @@ -35,18 +35,16 @@ _reset() CLASS(MOCK_CLASS) { - const _CCLASS const class; + const CCLASS const class; int value; }; -extern const _CCLASS const __MOCK_CLASS; - /** * ~~~ method declarations ~~~~~~~~ */ -int mock_class_getValue(MOCK_CLASS _this); -void mock_class_setValue(MOCK_CLASS _this, int value); +int mock_class_getValue(MOCK_CLASS this); +void mock_class_setValue(MOCK_CLASS this, int value); #endif//__MOCK_CLASS_H__ // vim: set et ts=4 sw=4: diff --git a/tests/runtest.c b/tests/runtest.c index 58953e2..c67b57d 100644 --- a/tests/runtest.c +++ b/tests/runtest.c @@ -39,13 +39,15 @@ const char results[3] = { int isObjectNull(void * _object) { - const struct CCLASS ** class = _object; + const CCLASS * class = _object; if (! isObject(_object)) { return 0; } - return isMemNull(_object + _CCLASS_SIZE, (*class)->size - _CCLASS_SIZE); + return isMemNull( + _object + CCLASS_PTR_SIZE, + (*class)->size - CCLASS_PTR_SIZE); } int