Commit a9ecc6663c06a53b4aafbfd73be5f9ea880c5253

Authored by Georg Hopp
1 parent f34db72e

hash now might be initialized by json object

... ... @@ -23,6 +23,9 @@ __construct(struct BIGPOINT_DYNTYPE * _this, va_list * params)
23 23 memcpy((_this->data)._string, va_arg(* params, const char *), _this->size);
24 24 break;
25 25
  26 + case BIGPOINT_DYNTYPE_HASH:
  27 + break;
  28 +
26 29 default:
27 30 (_this->data)._object = NULL;
28 31 }
... ...
... ... @@ -13,7 +13,7 @@ enum BIGPOINT_DYNTYPE_TYPES {
13 13 BIGPOINT_DYNTYPE_FLOAT,
14 14 BIGPOINT_DYNTYPE_STRING,
15 15 BIGPOINT_DYNTYPE_ARRAY,
16   - BIGPOINT_DYNTYPE_OBJECT
  16 + BIGPOINT_DYNTYPE_HASH
17 17 };
18 18
19 19
... ...
... ... @@ -8,39 +8,7 @@
8 8 #define HASH_ENTRY_CHUNK_SIZE 128
9 9
10 10
11   -static
12   -void
13   -_updateHashSize(struct BIGPOINT_HASH * _this)
14   -{
15   - if (_this->used == _this->size) {
16   - _this->size += HASH_ENTRY_CHUNK_SIZE;
17   -
18   - _this->keys = realloc(_this->keys, sizeof(char*) * _this->size);
19   - memset(_this->keys + (_this->used * sizeof(char*)),
20   - HASH_ENTRY_CHUNK_SIZE * sizeof(char*),
21   - 0);
22   -
23   - _this->values = realloc(
24   - _this->values,
25   - sizeof(struct BIGPOINT_DYNTYPE *) * _this->size);
26   - memset(_this->values + (_this->used * sizeof(struct BIGPOINT_DYNTYPE *)),
27   - HASH_ENTRY_CHUNK_SIZE * sizeof(struct BIGPOINT_DYNTYPE *),
28   - 0);
29   - }
30   -}
31   -
32   -static
33   -size_t
34   -_getHashIdx(struct BIGPOINT_HASH * _this, const char * key)
35   -{
36   - size_t index;
37   -
38   - for (index = 0;
39   - index < _this->used && strcmp(_this->keys[index], key);
40   - index++);
41   -
42   - return index;
43   -}
  11 +static void _updateHashSize(struct BIGPOINT_HASH * _this);
44 12
45 13 static
46 14 void
... ... @@ -55,6 +23,15 @@ static
55 23 void
56 24 __jsonConst(struct BIGPOINT_HASH * _this, struct json_object * json)
57 25 {
  26 + __construct(_this, NULL);
  27 +
  28 + if (json_type_object != json_object_get_type(json)) {
  29 + return;
  30 + }
  31 +
  32 + json_object_object_foreach(json, key, value) {
  33 + bigpoint_hash_set(_this, key, newFromJson(BIGPOINT_DYNTYPE, value));
  34 + }
58 35 }
59 36
60 37 static
... ... @@ -88,6 +65,40 @@ struct BIGPOINT_CCLASS _bigpoint_hash = {
88 65
89 66 const struct BIGPOINT_CCLASS * const BIGPOINT_HASH = &_bigpoint_hash;
90 67
  68 +static
  69 +void
  70 +_updateHashSize(struct BIGPOINT_HASH * _this)
  71 +{
  72 + if (_this->used == _this->size) {
  73 + _this->size += HASH_ENTRY_CHUNK_SIZE;
  74 +
  75 + _this->keys = realloc(_this->keys, sizeof(char*) * _this->size);
  76 + memset(_this->keys + (_this->used * sizeof(char*)),
  77 + HASH_ENTRY_CHUNK_SIZE * sizeof(char*),
  78 + 0);
  79 +
  80 + _this->values = realloc(
  81 + _this->values,
  82 + sizeof(struct BIGPOINT_DYNTYPE *) * _this->size);
  83 + memset(_this->values + (_this->used * sizeof(struct BIGPOINT_DYNTYPE *)),
  84 + HASH_ENTRY_CHUNK_SIZE * sizeof(struct BIGPOINT_DYNTYPE *),
  85 + 0);
  86 + }
  87 +}
  88 +
  89 +static
  90 +size_t
  91 +_getHashIdx(struct BIGPOINT_HASH * _this, const char * key)
  92 +{
  93 + size_t index;
  94 +
  95 + for (index = 0;
  96 + index < _this->used && strcmp(_this->keys[index], key);
  97 + index++);
  98 +
  99 + return index;
  100 +}
  101 +
91 102 void
92 103 bigpoint_hash_set(struct BIGPOINT_HASH * _this,
93 104 const char * key,
... ...
... ... @@ -33,6 +33,20 @@ main(int argc, char * argv[])
33 33
34 34 delete(hash);
35 35
  36 + hash = newFromJson(
  37 + BIGPOINT_HASH,
  38 + json_tokener_parse("{\"key1\":123,\"key2\":321}"));
  39 +
  40 + dyn = bigpoint_hash_get(hash, TEST_KEY1);
  41 + printf("%d\n", (dyn->data)._int);
  42 + delete(dyn);
  43 +
  44 + dyn = bigpoint_hash_get(hash, TEST_KEY2);
  45 + printf("%d\n", (dyn->data)._int);
  46 + delete(dyn);
  47 +
  48 + delete(hash);
  49 +
36 50 return 0;
37 51 }
38 52
... ...
Please register or login to post a comment