Browse Source

dyntype string stuff ready

master
Georg Hopp 14 years ago
parent
commit
25428a5f92
  1. 34
      bigpoint_dyntype.c
  2. 19
      test4.c

34
bigpoint_dyntype.c

@ -15,7 +15,12 @@ __construct(struct BIGPOINT_DYNTYPE * _this, va_list * params)
switch(_this->type) { switch(_this->type) {
case BIGPOINT_DYNTYPE_INT: 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; break;
default: default:
@ -34,6 +39,18 @@ __jsonConst(struct BIGPOINT_DYNTYPE * _this, struct json_object * json)
(_this->data)._int = (int)json_object_get_int(json); (_this->data)._int = (int)json_object_get_int(json);
break; 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: default:
_this->type = BIGPOINT_DYNTYPE_NULL; _this->type = BIGPOINT_DYNTYPE_NULL;
_this->size = 0; _this->size = 0;
@ -45,8 +62,15 @@ static
void void
__destruct(struct BIGPOINT_DYNTYPE * _this) __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); json = json_object_new_int((_this->data)._int);
break; break;
case BIGPOINT_DYNTYPE_STRING:
json = json_object_new_string((_this->data)._string);
break;
default: default:
json = NULL; json = NULL;
} }

19
test4.c

@ -5,12 +5,13 @@
#include "bigpoint_cclass.h" #include "bigpoint_cclass.h"
#include "bigpoint_dyntype.h" #include "bigpoint_dyntype.h"
#define TEST_STR "this is a foo string"
int int
main(int argc, char * argv[]) 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); struct BIGPOINT_DYNTYPE * dyn = newFromJson(BIGPOINT_DYNTYPE, json);
printf("%d\n", (dyn->data)._int); printf("%d\n", (dyn->data)._int);
@ -26,6 +27,22 @@ main(int argc, char * argv[])
delete(dyn); delete(dyn);
json_object_put(json); 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; return 0;
} }

Loading…
Cancel
Save