From 25428a5f92592bf2846376cd4bf3a9312f48f96b Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Mon, 14 Nov 2011 13:51:52 +0100 Subject: [PATCH] dyntype string stuff ready --- bigpoint_dyntype.c | 34 +++++++++++++++++++++++++++++++--- test4.c | 21 +++++++++++++++++++-- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/bigpoint_dyntype.c b/bigpoint_dyntype.c index 05f2462..9fa2a73 100644 --- a/bigpoint_dyntype.c +++ b/bigpoint_dyntype.c @@ -15,7 +15,12 @@ __construct(struct BIGPOINT_DYNTYPE * _this, va_list * params) switch(_this->type) { case BIGPOINT_DYNTYPE_INT: - (_this->data)._int = va_arg(* params, long); + (_this->data)._int = va_arg(* params, int); + break; + + case BIGPOINT_DYNTYPE_STRING: + (_this->data)._string = calloc(_this->size + 1, sizeof(char)); + memcpy((_this->data)._string, va_arg(* params, const char *), _this->size); break; default: @@ -34,6 +39,18 @@ __jsonConst(struct BIGPOINT_DYNTYPE * _this, struct json_object * json) (_this->data)._int = (int)json_object_get_int(json); break; + case json_type_string: + { + const char * string = json_object_get_string(json); + + _this->type = BIGPOINT_DYNTYPE_STRING; + _this->size = strlen(string); + (_this->data)._string = calloc(_this->size + 1, sizeof(char)); + memcpy((_this->data)._string, string, _this->size); + // the json object handles the memory for string.... + } + break; + default: _this->type = BIGPOINT_DYNTYPE_NULL; _this->size = 0; @@ -45,8 +62,15 @@ static void __destruct(struct BIGPOINT_DYNTYPE * _this) { - if (_this && BIGPOINT_DYNTYPE_OBJECT == _this->type && (_this->data)._object) { - free((_this->data)._object); + if (_this) { + switch(_this->type) { + case BIGPOINT_DYNTYPE_STRING: + free((_this->data)._string); + break; + + default: + break; + } } } @@ -61,6 +85,10 @@ __toJson(struct BIGPOINT_DYNTYPE * _this) json = json_object_new_int((_this->data)._int); break; + case BIGPOINT_DYNTYPE_STRING: + json = json_object_new_string((_this->data)._string); + break; + default: json = NULL; } diff --git a/test4.c b/test4.c index f4453ae..9761230 100644 --- a/test4.c +++ b/test4.c @@ -5,12 +5,13 @@ #include "bigpoint_cclass.h" #include "bigpoint_dyntype.h" +#define TEST_STR "this is a foo string" + int main(int argc, char * argv[]) { - struct json_object * json = json_object_new_int(123); - + struct json_object * json = json_object_new_int(123); struct BIGPOINT_DYNTYPE * dyn = newFromJson(BIGPOINT_DYNTYPE, json); printf("%d\n", (dyn->data)._int); @@ -26,6 +27,22 @@ main(int argc, char * argv[]) delete(dyn); json_object_put(json); + json = json_object_new_string(TEST_STR); + dyn = newFromJson(BIGPOINT_DYNTYPE, json); + + printf("%s\n", (dyn->data)._string); + + delete(dyn); + json_object_put(json); + + dyn = new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_STRING, strlen(TEST_STR), TEST_STR); + json = toJson(dyn); + + printf("%s\n", json_object_to_json_string(json)); + + delete(dyn); + json_object_put(json); + return 0; }