Beispiel #1
0
 TreeTest()
 {
   Tree16 = co_tree16_create();
   Tree32 = co_tree32_create();
   
   TestString1 = co_str8_create("1TESTVALUE1", 12, 0);
   TestString2 = co_str8_create("2TESTVALUE2", 12, 0);
   
   ReplaceString1 = co_str8_create("REPLACE", 9, 0);
   
   ret = 0;
   ptr = NULL;
 }
Beispiel #2
0
size_t
co_tree_import(co_obj_t **tree, const char *input, const size_t ilen)
{
  size_t length = 0, olen = 0, read = 0, klen = 0;
  char *kstr = NULL;
  int i = 0;
  co_obj_t *obj = NULL;
  const char *cursor = input;
  switch((uint8_t)input[0])
  {
    case _tree16:
      length = *((uint16_t *)(input + 1));
      *tree = co_tree16_create();
      cursor += sizeof(uint16_t) + 1;
      read = sizeof(uint16_t) + 1;
      break;
    case _tree32:
      length = (uint32_t)(*(uint32_t*)(input + 1));
      *tree = co_tree32_create();
      cursor += sizeof(uint32_t) + 1;
      read = sizeof(uint32_t) + 1;
      break;
    default:
      SENTINEL("Not a tree.");
      break;
  }
  while(i < length && read <= ilen)
  {
    DEBUG("Importing tuple:");
    if((uint8_t)cursor[0] == _str8)
    {
      DEBUG("Reading key...");
      cursor += 1;
      read += 1;
      klen = (uint8_t)cursor[0];
      kstr = (char *)&cursor[1];
      cursor += klen + 1;
      read += klen + 1;

      DEBUG("Reading value...");
      switch((uint8_t)cursor[0])
      {
        case _list16:
        case _list32:
          olen = co_list_import(&obj, cursor, ilen - read);
          break;
        case _tree16:
        case _tree32:
          olen = co_tree_import(&obj, cursor, ilen - read);
          break;
        default:
          olen = co_obj_import(&obj, cursor, ilen - read, 0);
          break;
      }
      CHECK(olen > 0, "Failed to import object.");
      cursor +=olen;
      read += olen;

      DEBUG("Inserting value into tree with key.");
      CHECK(co_tree_insert(*tree, kstr, klen, obj), "Failed to insert object.");
      i++;
    }
  }
  return read;
error:
  if(obj != NULL) co_obj_free(obj);
  return -1;
}