Example #1
0
static void
as_command_parse_value(uint8_t* p, uint8_t type, uint32_t value_size, as_val** value)
{
    // Allocate values on heap.
    switch (type) {
    case AS_BYTES_UNDEF: {
        *value = (as_val*)&as_nil;
        break;
    }
    case AS_BYTES_INTEGER: {
        int64_t v = 0;
        as_command_bytes_to_int(p, value_size, &v);
        *value = (as_val*)as_integer_new(v);
        break;
    }
    case AS_BYTES_DOUBLE: {
        double v = cf_swap_from_big_float64(*(double*)p);
        *value = (as_val*)as_double_new(v);
        break;
    }
    case AS_BYTES_STRING: {
        char* v = malloc(value_size + 1);
        memcpy(v, p, value_size);
        v[value_size] = 0;
        *value = (as_val*)as_string_new_wlen(v, value_size, true);
        break;
    }
    case AS_BYTES_LIST:
    case AS_BYTES_MAP: {
        as_buffer buffer;
        buffer.data = p;
        buffer.size = value_size;

        as_serializer ser;
        as_msgpack_init(&ser);
        as_serializer_deserialize(&ser, &buffer, value);
        as_serializer_destroy(&ser);
        break;
    }
    default: {
        void* v = malloc(value_size);
        memcpy(v, p, value_size);
        *value = (as_val*)as_bytes_new_wrap(v, value_size, true);
        break;
    }
    }
}
static inline bool
unpack_double(serial_context *ser_cont, double in, as_val **out)
{
	if (VERBOSE) {
		ver("%sDouble %f", indent(ser_cont), in);
	}

	as_val *tmp = (as_val *)as_double_new(in);

	if (tmp == NULL) {
		err("Error while allocating double");
		return false;
	}

	*out = tmp;
	return true;
}
Example #3
0
static void
as_command_parse_value(uint8_t* p, uint8_t type, uint32_t value_size, as_val** value)
{
	// Allocate values on heap.
	switch (type) {
		case AS_BYTES_UNDEF: {
			*value = (as_val*)&as_nil;
			break;
		}
		case AS_BYTES_INTEGER: {
			int64_t v = 0;
			as_command_bytes_to_int(p, value_size, &v);
			*value = (as_val*)as_integer_new(v);
			break;
		}
		case AS_BYTES_DOUBLE: {
			double v = cf_swap_from_big_float64(*(double*)p);
			*value = (as_val*)as_double_new(v);
			break;
		}
		case AS_BYTES_STRING: {
			char* v = cf_malloc(value_size + 1);
			memcpy(v, p, value_size);
			v[value_size] = 0;
			*value = (as_val*)as_string_new_wlen(v, value_size, true);
			break;
		}
		case AS_BYTES_GEOJSON: {
			uint8_t * ptr = p;

			// skip flags
			ptr++;

			// ncells
			uint16_t ncells = cf_swap_from_be16(* (uint16_t *) ptr);
			ptr += sizeof(uint16_t);

			// skip any cells
			ptr += sizeof(uint64_t) * ncells;

			// Use the json bytes.
			size_t jsonsz = value_size - 1 - 2 - (ncells * sizeof(uint64_t));
			char* v = cf_malloc(jsonsz + 1);
			memcpy(v, ptr, jsonsz);
			v[jsonsz] = 0;
			*value = (as_val*) as_geojson_new_wlen(v, jsonsz, true);
			break;
		}
		case AS_BYTES_LIST:
		case AS_BYTES_MAP: {
			as_buffer buffer;
			buffer.data = p;
			buffer.size = value_size;
			
			as_serializer ser;
			as_msgpack_init(&ser);
			as_serializer_deserialize(&ser, &buffer, value);
			as_serializer_destroy(&ser);
			break;
		}
		default: {
			void* v = cf_malloc(value_size);
			memcpy(v, p, value_size);
			*value = (as_val*)as_bytes_new_wrap(v, value_size, true);
			break;
		}
	}
}