Example #1
0
static void
_get_array_value(lua_State *L, pbc_array array, char type) {
	switch(type) {
		case 'I': {
			int32_t v = luaL_checkinteger(L, -1);
			uint32_t hi = 0;
			if (v<0) {
				hi = ~0;
			}
			pbc_array_push_integer(array, v, hi);
			break;
		}
		case 'D' : {
			uint64_t v = (uint64_t)luaL_checknumber(L, -1);
			pbc_array_push_integer(array, (uint32_t)v, (uint32_t)(v >> 32));
			break;
		}
		case 'B': {
			int32_t v = lua_toboolean(L, -1);
			pbc_array_push_integer(array, v ? 1: 0, 0);
			break;
		}
		case 'R': {
			double v = luaL_checknumber(L, -1);
			pbc_array_push_real(array, v);
			break;
		}
		case 'S': {
			size_t sz = 0;
			const char * str = luaL_checklstring(L, -1, &sz);
			struct pbc_slice slice;
			slice.buffer = (void*)str;
			slice.len = sz;
			pbc_array_push_slice(array, &slice);
			break;
		}
		case 'M': {
			struct pbc_slice slice;
			if (lua_istable(L,-1)) {
				lua_rawgeti(L,-1,1);
				slice.buffer = lua_touserdata(L,-1);
				lua_rawgeti(L,-2,2);
				slice.len = lua_tointeger(L,-1);
				lua_pop(L,2);
			} else {
				size_t sz = 0;
				const char * buffer = luaL_checklstring(L, -1, &sz);
				slice.buffer = (void *)buffer;
				slice.len = sz;
			}
			pbc_array_push_slice(array, &slice);
			break;
		}
	}
}
Example #2
0
static int
test_pattern_pack(struct pbc_env *env , struct pbc_slice *slice) {
	struct person p;
	/*
	  If you don't care about default values (you will set all values by yourself) ,
      you can also use memset(&p, 0 , sizeof(p)) instead of pbc_pattern_set_default.
	*/
	pbc_pattern_set_default(pat, &p);

	p.name.buffer = (void*)"Alice";
	p.name.len = -1;	// encode '\0'
	p.id = 1234;
	p.email.buffer = (void*)"alice@unknown";
	p.email.len = -1;

	struct person_phone phone;
	phone.number.buffer = (void *)"1234567";
	phone.number.len = -1;
	phone.type = 1;

	char temp[128];		
	struct pbc_slice phone_slice = { temp, sizeof(temp) };

	int unused = pbc_pattern_pack(pat_phone, &phone , &phone_slice);
	
	if (unused < 0) {
		slice->len = 0;
		return slice->len;
	}

	pbc_array_push_slice(p.phone, &phone_slice);

	pbc_pattern_set_default(pat_phone, &phone);

	phone.number.buffer = (void *)"87654321";
	phone.number.len = -1;

	char temp2[128];		
	struct pbc_slice phone_slice2 = { temp2, sizeof(temp2) };

	unused = pbc_pattern_pack(pat_phone, &phone , &phone_slice2);
	
	if (unused < 0) {
		slice->len = 0;
		return slice->len;
	}

	pbc_array_push_slice(p.phone, &phone_slice2);

	int i;
	for (i=0;i<3;i++) {
		pbc_array_push_integer(p.test, -i*4,0);
	}

	int r = pbc_pattern_pack(pat, &p, slice);
	
	pbc_pattern_close_arrays(pat,&p);
	printf("pack into %d bytes\n", slice->len);

	return r;
}