Showing
2 changed files
with
50 additions
and
5 deletions
| @@ -15,7 +15,12 @@ __construct(struct BIGPOINT_DYNTYPE * _this, va_list * params) | @@ -15,7 +15,12 @@ __construct(struct BIGPOINT_DYNTYPE * _this, va_list * params) | ||
| 15 | 15 | ||
| 16 | switch(_this->type) { | 16 | switch(_this->type) { |
| 17 | case BIGPOINT_DYNTYPE_INT: | 17 | case BIGPOINT_DYNTYPE_INT: |
| 18 | - (_this->data)._int = va_arg(* params, long); | 18 | + (_this->data)._int = va_arg(* params, int); |
| 19 | + break; | ||
| 20 | + | ||
| 21 | + case BIGPOINT_DYNTYPE_STRING: | ||
| 22 | + (_this->data)._string = calloc(_this->size + 1, sizeof(char)); | ||
| 23 | + memcpy((_this->data)._string, va_arg(* params, const char *), _this->size); | ||
| 19 | break; | 24 | break; |
| 20 | 25 | ||
| 21 | default: | 26 | default: |
| @@ -34,6 +39,18 @@ __jsonConst(struct BIGPOINT_DYNTYPE * _this, struct json_object * json) | @@ -34,6 +39,18 @@ __jsonConst(struct BIGPOINT_DYNTYPE * _this, struct json_object * json) | ||
| 34 | (_this->data)._int = (int)json_object_get_int(json); | 39 | (_this->data)._int = (int)json_object_get_int(json); |
| 35 | break; | 40 | break; |
| 36 | 41 | ||
| 42 | + case json_type_string: | ||
| 43 | + { | ||
| 44 | + const char * string = json_object_get_string(json); | ||
| 45 | + | ||
| 46 | + _this->type = BIGPOINT_DYNTYPE_STRING; | ||
| 47 | + _this->size = strlen(string); | ||
| 48 | + (_this->data)._string = calloc(_this->size + 1, sizeof(char)); | ||
| 49 | + memcpy((_this->data)._string, string, _this->size); | ||
| 50 | + // the json object handles the memory for string.... | ||
| 51 | + } | ||
| 52 | + break; | ||
| 53 | + | ||
| 37 | default: | 54 | default: |
| 38 | _this->type = BIGPOINT_DYNTYPE_NULL; | 55 | _this->type = BIGPOINT_DYNTYPE_NULL; |
| 39 | _this->size = 0; | 56 | _this->size = 0; |
| @@ -45,8 +62,15 @@ static | @@ -45,8 +62,15 @@ static | ||
| 45 | void | 62 | void |
| 46 | __destruct(struct BIGPOINT_DYNTYPE * _this) | 63 | __destruct(struct BIGPOINT_DYNTYPE * _this) |
| 47 | { | 64 | { |
| 48 | - if (_this && BIGPOINT_DYNTYPE_OBJECT == _this->type && (_this->data)._object) { | ||
| 49 | - free((_this->data)._object); | 65 | + if (_this) { |
| 66 | + switch(_this->type) { | ||
| 67 | + case BIGPOINT_DYNTYPE_STRING: | ||
| 68 | + free((_this->data)._string); | ||
| 69 | + break; | ||
| 70 | + | ||
| 71 | + default: | ||
| 72 | + break; | ||
| 73 | + } | ||
| 50 | } | 74 | } |
| 51 | } | 75 | } |
| 52 | 76 | ||
| @@ -61,6 +85,10 @@ __toJson(struct BIGPOINT_DYNTYPE * _this) | @@ -61,6 +85,10 @@ __toJson(struct BIGPOINT_DYNTYPE * _this) | ||
| 61 | json = json_object_new_int((_this->data)._int); | 85 | json = json_object_new_int((_this->data)._int); |
| 62 | break; | 86 | break; |
| 63 | 87 | ||
| 88 | + case BIGPOINT_DYNTYPE_STRING: | ||
| 89 | + json = json_object_new_string((_this->data)._string); | ||
| 90 | + break; | ||
| 91 | + | ||
| 64 | default: | 92 | default: |
| 65 | json = NULL; | 93 | json = NULL; |
| 66 | } | 94 | } |
| @@ -5,12 +5,13 @@ | @@ -5,12 +5,13 @@ | ||
| 5 | #include "bigpoint_cclass.h" | 5 | #include "bigpoint_cclass.h" |
| 6 | #include "bigpoint_dyntype.h" | 6 | #include "bigpoint_dyntype.h" |
| 7 | 7 | ||
| 8 | +#define TEST_STR "this is a foo string" | ||
| 9 | + | ||
| 8 | 10 | ||
| 9 | int | 11 | int |
| 10 | main(int argc, char * argv[]) | 12 | main(int argc, char * argv[]) |
| 11 | { | 13 | { |
| 12 | - struct json_object * json = json_object_new_int(123); | ||
| 13 | - | 14 | + struct json_object * json = json_object_new_int(123); |
| 14 | struct BIGPOINT_DYNTYPE * dyn = newFromJson(BIGPOINT_DYNTYPE, json); | 15 | struct BIGPOINT_DYNTYPE * dyn = newFromJson(BIGPOINT_DYNTYPE, json); |
| 15 | 16 | ||
| 16 | printf("%d\n", (dyn->data)._int); | 17 | printf("%d\n", (dyn->data)._int); |
| @@ -26,6 +27,22 @@ main(int argc, char * argv[]) | @@ -26,6 +27,22 @@ main(int argc, char * argv[]) | ||
| 26 | delete(dyn); | 27 | delete(dyn); |
| 27 | json_object_put(json); | 28 | json_object_put(json); |
| 28 | 29 | ||
| 30 | + json = json_object_new_string(TEST_STR); | ||
| 31 | + dyn = newFromJson(BIGPOINT_DYNTYPE, json); | ||
| 32 | + | ||
| 33 | + printf("%s\n", (dyn->data)._string); | ||
| 34 | + | ||
| 35 | + delete(dyn); | ||
| 36 | + json_object_put(json); | ||
| 37 | + | ||
| 38 | + dyn = new(BIGPOINT_DYNTYPE, BIGPOINT_DYNTYPE_STRING, strlen(TEST_STR), TEST_STR); | ||
| 39 | + json = toJson(dyn); | ||
| 40 | + | ||
| 41 | + printf("%s\n", json_object_to_json_string(json)); | ||
| 42 | + | ||
| 43 | + delete(dyn); | ||
| 44 | + json_object_put(json); | ||
| 45 | + | ||
| 29 | return 0; | 46 | return 0; |
| 30 | } | 47 | } |
| 31 | 48 |
Please
register
or
login
to post a comment