Skip to content

Instantly share code, notes, and snippets.

@mnunberg
Created November 29, 2012 02:06
Show Gist options
  • Select an option

  • Save mnunberg/4166313 to your computer and use it in GitHub Desktop.

Select an option

Save mnunberg/4166313 to your computer and use it in GitHub Desktop.
/**
* Create some view options..
*/
lcb_view_option_t opt_stale;
lcb_view_option_t opt_onerr;
lcb_view_option_t opt_limit;
lcb_view_option_t opt_invalid;
lcb_view_option_t *optlist[] = {
&opt_stale,
&opt_onerr,
&opt_limit,
&opt_invalid
};
lcb_error_t err;
char *estr = NULL;
int optval;
int opttype;
/**
* Use constants (and avoid mistyping strings) for options which support it
*/
opttype = LCB_VOPT_OPT_STALE;
optval = 0;
err = lcb_make_view_option(&opt_stale,
&opttype, 0,
&optval, 0,
LCB_VOPT_F_OPTNAME_NUMERIC|LCB_VOPT_F_OPTVAL_NUMERIC,
&estr);
assert (err == LCB_SUCCESS);
/**
* Strings!
*/
err = lcb_make_view_option(&opt_onerr,
"on_error", -1,
"continue", -1,
0,
&estr);
assert (err == LCB_SUCCESS);
optval = 10;
/* mix them around */
err = lcb_make_view_option(&opt_limit,
"limit", -1,
&optval, 0,
LCB_VOPT_F_OPTVAL_NUMERIC,
&estr);
assert (err == LCB_SUCCESS);
/* Invalid options fail */
err = lcb_make_view_option(&opt_invalid,
"invalid_option_string", -1,
"invalid value", -1,
0, &estr);
/* Try it again, with a special flag */
assert(err == LCB_EINVAL);
printf("%s\n", estr);
/* Try it again, with a special flag */
err = lcb_make_view_option(&opt_invalid,
"invalid_option_string", -1,
"[\"json\",\"encoded\"]", -1,
LCB_VOPT_F_PASSTHROUGH|LCB_VOPT_F_PCTENCODE,
&estr);
assert(err == LCB_SUCCESS);
int num_options = sizeof(optlist) / sizeof(*optlist);
printf("Estimated length: %d\n",
lcb_vqstr_calc_len(optlist, num_options));
char buf[128] = { 0 };
lcb_vqstr_write(optlist, num_options, buf);
printf("Got buffer %s\n", buf);
lcb_cleanup_view_option(&opt_stale);
lcb_cleanup_view_option(&opt_onerr);
lcb_cleanup_view_option(&opt_limit);
lcb_cleanup_view_option(&opt_invalid);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment