Commit 25428a5f92592bf2846376cd4bf3a9312f48f96b

Authored by Georg Hopp
1 parent beb2017d

dyntype string stuff ready

Showing 2 changed files with 50 additions and 5 deletions
... ... @@ -15,7 +15,12 @@ __construct(struct BIGPOINT_DYNTYPE * _this, va_list * params)
15 15
16 16 switch(_this->type) {
17 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 24 break;
20 25
21 26 default:
... ... @@ -34,6 +39,18 @@ __jsonConst(struct BIGPOINT_DYNTYPE * _this, struct json_object * json)
34 39 (_this->data)._int = (int)json_object_get_int(json);
35 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 54 default:
38 55 _this->type = BIGPOINT_DYNTYPE_NULL;
39 56 _this->size = 0;
... ... @@ -45,8 +62,15 @@ static
45 62 void
46 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 85 json = json_object_new_int((_this->data)._int);
62 86 break;
63 87
  88 + case BIGPOINT_DYNTYPE_STRING:
  89 + json = json_object_new_string((_this->data)._string);
  90 + break;
  91 +
64 92 default:
65 93 json = NULL;
66 94 }
... ...
... ... @@ -5,12 +5,13 @@
5 5 #include "bigpoint_cclass.h"
6 6 #include "bigpoint_dyntype.h"
7 7
  8 +#define TEST_STR "this is a foo string"
  9 +
8 10
9 11 int
10 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 15 struct BIGPOINT_DYNTYPE * dyn = newFromJson(BIGPOINT_DYNTYPE, json);
15 16
16 17 printf("%d\n", (dyn->data)._int);
... ... @@ -26,6 +27,22 @@ main(int argc, char * argv[])
26 27 delete(dyn);
27 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 46 return 0;
30 47 }
31 48
... ...
Please register or login to post a comment