Example #1
0
static ERL_NIF_TERM make_error(ErlNifEnv* env, yaml_parser_t *parser)
{
    ERL_NIF_TERM err;

    switch (parser->error) {
    case YAML_MEMORY_ERROR:
	err = enif_make_atom(env, "memory_error");
	break;
    case YAML_PARSER_ERROR:
	err = enif_make_tuple4(env,
			       enif_make_atom(env, "parser_error"),
			       make_binary(env, (const unsigned char*) parser->problem),
			       enif_make_uint(env, parser->problem_mark.line),
			       enif_make_uint(env, parser->problem_mark.column));
	break;
    case YAML_SCANNER_ERROR:
	err = enif_make_tuple4(env,
			       enif_make_atom(env, "scanner_error"),
			       make_binary(env, (const unsigned char*) parser->problem),
			       enif_make_uint(env, parser->problem_mark.line),
			       enif_make_uint(env, parser->problem_mark.column));
	break;
    default:
	err = enif_make_atom(env, "unexpected_error");
	break;
    }

    return enif_make_tuple2(env, enif_make_atom(env, "error"), err);
}
Example #2
0
static bool
decode_visit_regex(const bson_iter_t *iter,
                   const char        *key,
                   const char        *v_regex,
                   const char        *v_options,
                   void              *data)
{
    decode_state *ds = data;
    ERL_NIF_TERM regex, options, out;

    if(!make_binary(ds->env, &regex, v_regex, strlen(v_regex))) {
        return true;
    }
    if(!make_binary(ds->env, &options, v_options, strlen(v_options))) {
        return true;
    }
    out = enif_make_tuple4(ds->env, 
                           ds->st->atom_s_regex, 
                           regex, 
                           ds->st->atom_s_options, 
                           options);
    vec_push(ds->vec, out);

    return false;
}
Example #3
0
static ERL_NIF_TERM macros(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    const ERL_NIF_TERM* a;
    ERL_NIF_TERM lists, tuples;
    int arity;
    if (!enif_get_tuple(env, argv[0], &arity, &a) || arity != 9) {
	return enif_make_badarg(env);
    }
    
    lists = enif_make_list(env,9,
			   enif_make_list1(env,a[0]),
			   enif_make_list2(env,a[0],a[1]),
			   enif_make_list3(env,a[0],a[1],a[2]),
			   enif_make_list4(env,a[0],a[1],a[2],a[3]),
			   enif_make_list5(env,a[0],a[1],a[2],a[3],a[4]),
			   enif_make_list6(env,a[0],a[1],a[2],a[3],a[4],a[5]),
			   enif_make_list7(env,a[0],a[1],a[2],a[3],a[4],a[5],a[6]),
			   enif_make_list8(env,a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]),
			   enif_make_list9(env,a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]));
    tuples = enif_make_list(env,9,
			    enif_make_tuple1(env,a[0]),
			    enif_make_tuple2(env,a[0],a[1]),
			    enif_make_tuple3(env,a[0],a[1],a[2]),
			    enif_make_tuple4(env,a[0],a[1],a[2],a[3]),
			    enif_make_tuple5(env,a[0],a[1],a[2],a[3],a[4]),
			    enif_make_tuple6(env,a[0],a[1],a[2],a[3],a[4],a[5]),
			    enif_make_tuple7(env,a[0],a[1],a[2],a[3],a[4],a[5],a[6]),
			    enif_make_tuple8(env,a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]),
			    enif_make_tuple9(env,a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]));
    return enif_make_tuple2(env,lists,tuples);
}
Example #4
0
static bool
decode_visit_codewscope(const bson_iter_t *iter,
                        const char        *key,
                        size_t             v_code_len,
                        const char        *v_code,
                        const bson_t      *v_scope,
                        void              *data)
{
    decode_state *ds = data;
    decode_state cs;
    ERL_NIF_TERM code, scope, out;

    if(!make_binary(ds->env, &code, v_code, v_code_len)) {
        return true;
    }

    init_child_state(ds, &cs);
    cs.depth = ds->depth;
    cs.keys = true;
    if(!iter_bson(v_scope, &scope, &cs)) {
        return true;
    }

    out = enif_make_tuple4(
            ds->env, 
            ds->st->atom_s_javascript, 
            code,
            ds->st->atom_s_scope,
            scope);
    vec_push(ds->vec, out);

    return false;
}
Example #5
0
static bool
decode_visit_dbpointer(const bson_iter_t *iter,
                       const char        *key,
                       size_t             v_collection_len,
                       const char        *v_collection,
                       const bson_oid_t  *v_oid,
                       void              *data)
{
    decode_state *ds = data;
    ERL_NIF_TERM col, oid, out;

    if(!make_binary(ds->env, &col, v_collection, v_collection_len)) {
        return true;
    }
    if(v_oid) {
        if(!make_binary(ds->env, &oid, v_oid->bytes, 12)) {
            return true;
        }
    } else {
        oid = ds->st->atom_null;
    }
    out = enif_make_tuple4(
            ds->env,
            ds->st->atom_s_ref,
            col,
            ds->st->atom_s_oid,
            oid),
    vec_push(ds->vec, out);

    return false;
}
Example #6
0
static ERL_NIF_TERM make_keypair_record(ErlNifEnv *env, brine_keypair_s *keypair) {
  return enif_make_tuple4(env, enif_make_copy(env, BRINE_ATOM_KEYPAIR),
                          enif_make_resource(env, (void *) keypair),
                          // private key
                          enif_make_resource_binary(env, (void *) keypair, (void *) keypair->private_key, BRINE_PRIVKEY_SZ),
                          // public key
                          enif_make_resource_binary(env, (void *) keypair, (void *) keypair->public_key, BRINE_PUBKEY_SZ));
}
Example #7
0
/* Define an interface for errors. */
ERL_NIF_TERM build_error(ErlNifEnv* env, ERL_NIF_TERM body, 
        const char *pszFile, long lLine) {
    return enif_make_tuple4(env, 
            enif_make_atom(env, "i18n_error"), 
            body,
            enif_make_tuple2(env,
                enif_make_atom(env, "line"),
                enif_make_long(env, lLine)),
            enif_make_tuple2(env,
                enif_make_atom(env, "file"),
                enif_make_atom(env, pszFile)));
}
Example #8
0
void nif_write(couchfile_modify_request *rq, couchfile_pointer_info *dst, nif_writerq* wrq, ssize_t size)
{
    dst->writerq_resource = wrq;
    dst->pointer = 0;
    wrq->ptr = dst;

    ErlNifEnv* msg_env = enif_alloc_env();
    ERL_NIF_TERM msg_term = enif_make_tuple4(msg_env,
            get_atom(msg_env, "append_bin_btnif"),
            get_atom(msg_env, "snappy"), //COMPRESSION TYPE
            enif_make_resource(msg_env, wrq),
            enif_make_resource_binary(msg_env, wrq, &wrq->buf, size));
    enif_send(rq->caller_env, &rq->writer, msg_env, msg_term);
    enif_free_env(msg_env);
    enif_release_resource(wrq);
}
Example #9
0
/**
 * An element it the list has a bad type.
 * Used by i18n_message. 
 */
ERL_NIF_TERM list_element_error(ErlNifEnv* env, UErrorCode status, 
    const ERL_NIF_TERM list, int32_t num, 
        const char *pszFile, long lLine) {
    return build_error(env, 
        enif_make_tuple4(env,
            enif_make_atom(env, "bad_element"),
            enif_make_atom(env, u_errorName(status)),
            enif_make_tuple2(env,
                enif_make_atom(env, "list"),
                list),
            enif_make_tuple2(env,
                enif_make_atom(env, "index"),
                enif_make_int(env, (int) num))
            ),
        pszFile, lLine);
}
Example #10
0
ERL_NIF_TERM
geef_revparse_single(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
	ErlNifBinary bin, id;
	geef_repository *repo;
	geef_object *obj;
	ERL_NIF_TERM type, term_obj;

	if (!enif_get_resource(env, argv[0], geef_repository_type, (void **) &repo))
		return enif_make_badarg(env);

	if (!enif_inspect_iolist_as_binary(env, argv[1], &bin))
		return enif_make_badarg(env);

	if (geef_terminate_binary(&bin) < 0)
		return geef_oom(env);

	obj = enif_alloc_resource(geef_object_type, sizeof(geef_object));
	if (!obj)
		return geef_oom(env);

	if (git_revparse_single(&obj->obj, repo->repo, (char *) bin.data) < 0) {
		enif_release_binary(&bin);
		enif_release_resource(obj);
		return geef_error(env);
	}

	type = geef_object_type2atom(git_object_type(obj->obj));

	if (geef_oid_bin(&id, git_object_id(obj->obj)) < 0)
		return geef_oom(env);


	term_obj = enif_make_resource(env, obj);
	enif_release_resource(obj);

	obj->repo = repo;
	enif_keep_resource(repo);

	return enif_make_tuple4(env, atoms.ok, term_obj, type, enif_make_binary(env, &id));
}
Example #11
0
static bool
decode_visit_timestamp(const bson_iter_t *iter,
                       const char        *key,
                       uint32_t           v_timestamp,
                       uint32_t           v_increment,
                       void              *data)
{
    decode_state *ds = data;

    ERL_NIF_TERM timestamp = enif_make_int(ds->env, v_timestamp);
    ERL_NIF_TERM increment = enif_make_int(ds->env, v_increment);
    ERL_NIF_TERM out = enif_make_tuple4(
            ds->env,
            ds->st->atom_s_timestamp,
            timestamp,
            ds->st->atom_s_increment,
            increment);
    vec_push(ds->vec, out);

    return false;
}
Example #12
0
    ERL_NIF_TERM
vert_make_resource(ErlNifEnv *env, VERT_RESOURCE *vp, ERL_NIF_TERM type)
{
    ERL_NIF_TERM res = {0};


    if (vp->res == NULL) {
        enif_release_resource(vp);
        return verterr(env);
    }

    res = enif_make_resource(env, vp);
    enif_release_resource(vp);

    return enif_make_tuple2(env,
        atom_ok,
        enif_make_tuple4(env,
        atom_resource,
        type,
        enif_make_ref(env), res));
}
Example #13
0
static ERL_NIF_TERM make_call_history(ErlNifEnv* env, CallInfo** headp)
{
    ERL_NIF_TERM list = enif_make_list(env, 0); /* NIL */

    while (*headp != NULL) {
	CallInfo* call = *headp;
	ERL_NIF_TERM func_term = enif_make_atom(env,call->func_name);
	ERL_NIF_TERM tpl;
	if (call->arg != NULL) {
	    ERL_NIF_TERM arg_bin;	    
	    memcpy(enif_make_new_binary(env, call->arg_sz, &arg_bin),
		   call->arg, call->arg_sz);
	    func_term = enif_make_tuple2(env, func_term, arg_bin);
	}
	tpl = enif_make_tuple4(env, func_term, 					    
			       enif_make_int(env,call->lib_ver),
			       enif_make_int(env,call->static_cntA),
			       enif_make_int(env,call->static_cntB));
	list = enif_make_list_cell(env, tpl, list);
	*headp = call->next;
	enif_free(call);
    }
    return list;
}
Example #14
0
static bool
decode_visit_binary(const bson_iter_t  *iter,
                    const char         *key,
                    bson_subtype_t      v_subtype,
                    size_t              v_binary_len,
                    const uint8_t      *v_binary,
                    void               *data)
{
    decode_state *ds = data;
    ERL_NIF_TERM type, binary, out;

    type = enif_make_int(ds->env, v_subtype);
    if(!make_binary(ds->env, &binary, v_binary, v_binary_len)) {
        return true;
    }
    out = enif_make_tuple4(ds->env, 
                           ds->st->atom_s_type, 
                           type, 
                           ds->st->atom_s_binary, 
                           binary);
    vec_push(ds->vec, out);

    return false;
}
Example #15
0
File: alsa.c Project: DEGAUSSE/alsa
void *capture_thread(void *data) {
  AudioCapture *capture = (AudioCapture *)data;
  // enif_keep_resource(capture);
  
  detect_pcm(capture);
  
  set_volume(capture, 100);
  
  snd_pcm_open(&capture->handle, capture->pcm_name, SND_PCM_STREAM_CAPTURE, 0);
  if(!capture->handle) {
    fprintf(stderr, "No PCM!!\r\n");
    exit(1);
  }
  snd_pcm_hw_params_t *hw_params;
  snd_pcm_hw_params_malloc(&hw_params);
  if(!hw_params) {
    fprintf(stderr, "Damn!! No hw_params\r\n");
    exit(1);
  }
  
  snd_pcm_hw_params_any(capture->handle, hw_params);
  snd_pcm_hw_params_set_access(capture->handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
  snd_pcm_hw_params_set_format(capture->handle, hw_params, SND_PCM_FORMAT_S16_LE);
  snd_pcm_hw_params_set_rate_near(capture->handle, hw_params, &capture->sample_rate, 0);
  snd_pcm_hw_params_set_channels(capture->handle, hw_params, capture->channels);
  
  // int buffer_size = 16384;
  // int period_time = 32000;
  // int period_size = 1024;
  // snd_pcm_hw_params_set_period_time_near(capture->handle, hw_params, &period_time, 0);
  // snd_pcm_hw_params_set_period_size_near(capture->handle, hw_params, &period_size, 0);
  //  snd_pcm_hw_params_set_buffer_size_near(capture->handle, hw_params, &buffer_size);
  //  
	
  capture->last_dts = 0;
  
  fprintf(stderr, "Setting params: %p, %p\r\n", capture->handle, hw_params);
  
  snd_pcm_hw_params(capture->handle, hw_params);
	snd_pcm_hw_params_free(hw_params);
  snd_pcm_prepare(capture->handle);
  
  snd_output_t *log;
  snd_output_stdio_attach(&log, stderr, 0);
  snd_pcm_hw_params_dump(hw_params, log);
  
  fprintf(stderr, "Started capture\r\n");
  char *buffer = (char *)malloc(8192);
  
  // char *ptr = buffer;
  int size = 0;
  
  while(capture->thread_started) {
    int r = snd_pcm_readi(capture->handle, buffer + size, 1024);
    size += r*2*capture->channels;
    if(size < capture->frame_size) {
      continue;
    }
    
    ErlNifEnv* env = enif_alloc_env();
    ErlNifBinary frame;
    
    enif_alloc_binary(capture->frame_size, &frame);
    memmove(frame.data, buffer, frame.size);
    size -= frame.size;
    memmove(buffer, buffer + frame.size, size);
    
    ErlNifUInt64 dts = (uint64_t)capture->counter*1024ULL*1000 / capture->sample_rate;
    
    // fprintf(stderr, "A: %d -> %d\r\n", capture->counter, dts);
    
    if(capture->last_dts > dts) {
      fprintf(stderr, "Achtung! ALSA audio jump: %u, %u, %u\r\n", (unsigned)capture->counter, (unsigned)capture->last_dts, (unsigned)dts);
    }
    capture->last_dts = dts;
    enif_send(NULL, &capture->owner_pid, env, 
      enif_make_tuple4(env,
        enif_make_atom(env, "alsa"),
        enif_make_resource(env, capture),
        enif_make_uint64(env, dts),
        enif_make_binary(env, &frame)
      )
    );
    
    enif_release_binary(&frame);
    
    enif_free_env(env);
    capture->counter++;
  }
  fprintf(stderr, "Capture thread stopping\r\n");
  // enif_release_resource(capture);
  snd_pcm_close(capture->handle);
  return 0;
}
Example #16
0
File: cb.c Project: muut/cberl
ERL_NIF_TERM cb_mget(ErlNifEnv* env, handle_t* handle, void* obj)
{
    mget_args_t* args = (mget_args_t*)obj;

    struct libcouchbase_callback_m cb;

    lcb_error_t ret;

    ERL_NIF_TERM* results;
    ERL_NIF_TERM returnValue;
    ErlNifBinary databin;
    ErlNifBinary key_binary;
    unsigned int numkeys = args->numkeys;
    void** keys = args->keys;
    size_t* nkeys = args->nkeys;
    int exp = args->exp;
    int lock = args->lock;
    int i = 0;

    cb.currKey = 0;
    cb.ret = malloc(sizeof(struct libcouchbase_callback*) * numkeys);


    const lcb_get_cmd_t* commands[numkeys];
    i = 0;
    for (; i < numkeys; i++) {
      lcb_get_cmd_t *get = calloc(1, sizeof(*get));
      get->version = 0;
      get->v.v0.key = keys[i];
      get->v.v0.nkey = nkeys[i];
      get->v.v0.exptime = exp;
      get->v.v0.lock = lock;
      commands[i] = get;
    }

    ret = lcb_get(handle->instance, &cb, numkeys, commands);

    if (ret != LCB_SUCCESS) {
        return return_lcb_error(env, ret);
    }
    lcb_wait(handle->instance);

    results = malloc(sizeof(ERL_NIF_TERM) * numkeys);
    i = 0;
    for(; i < numkeys; i++) {
        enif_alloc_binary(cb.ret[i]->nkey, &key_binary);
        memcpy(key_binary.data, cb.ret[i]->key, cb.ret[i]->nkey);
        if (cb.ret[i]->error == LCB_SUCCESS) {
            enif_alloc_binary(cb.ret[i]->size, &databin);
            memcpy(databin.data, cb.ret[i]->data, cb.ret[i]->size);
            results[i] = enif_make_tuple4(env,
                    enif_make_uint64(env, cb.ret[i]->cas),
                    enif_make_int(env, cb.ret[i]->flag),
                    enif_make_binary(env, &key_binary),
                    enif_make_binary(env, &databin));
            free(cb.ret[i]->data);
        } else {
            results[i] = enif_make_tuple2(env,
                    enif_make_binary(env, &key_binary),
                    return_lcb_error(env, cb.ret[i]->error));
        }
        free(cb.ret[i]->key);
        free(cb.ret[i]);
        free(keys[i]);
        free((lcb_get_cmd_t*) commands[i]);
    }

    returnValue = enif_make_list_from_array(env, results, numkeys);

    free(results);
    free(cb.ret);
    free(keys);
    free(nkeys);

    return enif_make_tuple2(env, A_OK(env), returnValue);
}
Example #17
0
ERL_NIF_TERM
encode_iter(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    Encoder* e;
    jiffy_st* st = (jiffy_st*) enif_priv_data(env);

    ERL_NIF_TERM ret = 0;

    ERL_NIF_TERM stack;
    ERL_NIF_TERM curr;
    ERL_NIF_TERM item;
    const ERL_NIF_TERM* tuple;
    int arity;
    ErlNifSInt64 lval;
    double dval;

    size_t start;
    size_t processed;

    if(argc != 3) {
        return enif_make_badarg(env);
    } else if(!enif_get_resource(env, argv[0], st->res_enc, (void**) &e)) {
        return enif_make_badarg(env);
    } else if(!enif_is_list(env, argv[1])) {
        return enif_make_badarg(env);
    } else if(!enif_is_list(env, argv[2])) {
        return enif_make_badarg(env);
    }

    if(!enc_init(e, env)) {
        return enif_make_badarg(env);
    }

    stack = argv[1];
    e->iolist = argv[2];

    start = e->iosize + e->i;

    while(!enif_is_empty_list(env, stack)) {

        processed = (e->iosize + e->i) - start;
        if(should_yield(processed, e->bytes_per_iter)) {
            consume_timeslice(env, processed, e->bytes_per_iter);
            return enif_make_tuple4(
                    env,
                    st->atom_iter,
                    argv[0],
                    stack,
                    e->iolist
                );
        }

        if(!enif_get_list_cell(env, stack, &curr, &stack)) {
            ret = enc_error(e, "internal_error");
            goto done;
        }
        if(enif_is_identical(curr, e->atoms->ref_object)) {
            if(!enif_get_list_cell(env, stack, &curr, &stack)) {
                ret = enc_error(e, "internal_error");
                goto done;
            }
            if(enif_is_empty_list(env, curr)) {
                if(!enc_end_object(e)) {
                    ret = enc_error(e, "internal_error");
                    goto done;
                }
                continue;
            }
            if(!enif_get_list_cell(env, curr, &item, &curr)) {
                ret = enc_error(e, "internal_error");
                goto done;
            }
            if(!enif_get_tuple(env, item, &arity, &tuple)) {
                ret = enc_obj_error(e, "invalid_object_member", item);
                goto done;
            }
            if(arity != 2) {
                ret = enc_obj_error(e, "invalid_object_member_arity", item);
                goto done;
            }
            if(!enc_comma(e)) {
                ret = enc_error(e, "internal_error");
                goto done;
            }
            if(!enc_string(e, tuple[0])) {
                ret = enc_obj_error(e, "invalid_object_member_key", tuple[0]);
                goto done;
            }
            if(!enc_colon(e)) {
                ret = enc_error(e, "internal_error");
                goto done;
            }
            stack = enif_make_list_cell(env, curr, stack);
            stack = enif_make_list_cell(env, e->atoms->ref_object, stack);
            stack = enif_make_list_cell(env, tuple[1], stack);
        } else if(enif_is_identical(curr, e->atoms->ref_array)) {
            if(!enif_get_list_cell(env, stack, &curr, &stack)) {
                ret = enc_error(e, "internal_error");
                goto done;
            }
            if(enif_is_empty_list(env, curr)) {
                if(!enc_end_array(e)) {
                    ret = enc_error(e, "internal_error");
                    goto done;
                }
                continue;
            }
            if(!enc_comma(e)) {
                ret = enc_error(e, "internal_error");
                goto done;
            }
            if(!enif_get_list_cell(env, curr, &item, &curr)) {
                ret = enc_error(e, "internal_error");
                goto done;
            }
            stack = enif_make_list_cell(env, curr, stack);
            stack = enif_make_list_cell(env, e->atoms->ref_array, stack);
            stack = enif_make_list_cell(env, item, stack);
        } else if(enif_compare(curr, e->atoms->atom_null) == 0) {
            if(!enc_literal(e, "null", 4)) {
                ret = enc_error(e, "null");
                goto done;
            }
        } else if(e->use_nil && enif_compare(curr, e->atoms->atom_nil) == 0) {
            if(!enc_literal(e, "null", 4)) {
                ret = enc_error(e, "null");
                goto done;
            }
        } else if(enif_compare(curr, e->atoms->atom_true) == 0) {
            if(!enc_literal(e, "true", 4)) {
                ret = enc_error(e, "true");
                goto done;
            }
        } else if(enif_compare(curr, e->atoms->atom_false) == 0) {
            if(!enc_literal(e, "false", 5)) {
                ret = enc_error(e, "false");
                goto done;
            }
        } else if(enif_is_binary(env, curr)) {
            if(!enc_string(e, curr)) {
                ret = enc_obj_error(e, "invalid_string", curr);
                goto done;
            }
        } else if(enif_is_atom(env, curr)) {
            if(!enc_string(e, curr)) {
                ret = enc_obj_error(e, "invalid_string", curr);
                goto done;
            }
        } else if(enif_get_int64(env, curr, &lval)) {
            if(!enc_long(e, lval)) {
                ret = enc_error(e, "internal_error");
                goto done;
            }
        } else if(enif_get_double(env, curr, &dval)) {
            if(!enc_double(e, dval)) {
                ret = enc_error(e, "internal_error");
                goto done;
            }
        } else if(enif_get_tuple(env, curr, &arity, &tuple)) {
			ret = enc_obj_error(e, "invalid_ejson", curr);
			goto done;
#if MAP_TYPE_PRESENT
        } else if(enif_is_map(env, curr)) {
            if(!enc_map_to_ejson(env, curr, &curr)) {
                ret = enc_error(e, "internal_error");
                goto done;
            }
            stack = enif_make_list_cell(env, curr, stack);
#endif
        } else if(enif_is_list(env, curr)) {
            if(enif_is_empty_list(env, curr)) {
                if(!enc_start_array(e) || !enc_end_array(e)) {
                    ret = enc_error(e, "internal_error");
                    goto done;
                }
                continue;
            }
            if(!enif_get_list_cell(env, curr, &item, &curr)) {
                ret = enc_error(e, "internal_error");
                goto done;
            }
            if(!enif_get_tuple(env, item, &arity, &tuple)) {
				if(!enc_start_array(e)) {
					ret = enc_error(e, "internal_error");
					goto done;
				}
				stack = enif_make_list_cell(env, curr, stack);
				stack = enif_make_list_cell(env, e->atoms->ref_array, stack);
				stack = enif_make_list_cell(env, item, stack);
            } else {
				if(!enc_start_object(e)) {
					ret = enc_error(e, "internal_error");
					goto done;
				}
				if(arity == 0) {
					if(!enc_end_object(e)) {
						ret = enc_error(e, "internal_error");
						goto done;
					}
					continue;
				} else if(arity != 2) {
					ret = enc_obj_error(e, "invalid_object_member_arity", item);
					goto done;
				}
				if(!enc_string(e, tuple[0])) {
					ret = enc_obj_error(e, "invalid_object_member_key", tuple[0]);
					goto done;
				}
				if(!enc_colon(e)) {
					ret = enc_error(e, "internal_error");
					goto done;
				}
				stack = enif_make_list_cell(env, curr, stack);
				stack = enif_make_list_cell(env, e->atoms->ref_object, stack);
				stack = enif_make_list_cell(env, tuple[1], stack);
			}
        } else {
            if(!enc_unknown(e, curr)) {
                ret = enc_error(e, "internal_error");
                goto done;
            }
        }
    }

    if(!enc_done(e, &item)) {
        ret = enc_error(e, "internal_error");
        goto done;
    }

    if(e->iolen == 0) {
        ret = item;
    } else {
        ret = enif_make_tuple2(env, e->atoms->atom_partial, item);
    }

done:
    processed = (e->iosize + e->i) - start;
    consume_timeslice(env, processed, e->bytes_per_iter);

    return ret;
}
Example #18
0
static ERL_NIF_TERM event_to_term(ErlNifEnv *env, yaml_event_t *event)
{
    yaml_event_type_t type;
    ERL_NIF_TERM term;
    ERL_NIF_TERM tail;

    // for iteration
    yaml_tag_directive_t *tag_directive;

    type = event->type;
    switch(type)
    {
    case YAML_STREAM_START_EVENT:
        term = enum_to_atom(env, event->data.stream_start.encoding, encoding_atoms);
        break;

    case YAML_DOCUMENT_START_EVENT:
        // form list of directives
        tail = enif_make_list(env,0);

        // iterate backwards so list ends up in right order
        tag_directive = event->data.document_start.tag_directives.end;
        while(event->data.document_start.tag_directives.start != tag_directive)
        {
            tail = enif_make_list_cell(env, tag_directive_to_term(env, tag_directive), tail);
            tag_directive--;
        }
        term = enif_make_tuple3(env,
                                version_directive_to_term(env, event->data.document_start.version_directive),
                                tail,
                                bool_to_term(env, event->data.document_start.implicit));
        break;


    case YAML_DOCUMENT_END_EVENT:
        term = bool_to_term(env, event->data.document_end.implicit);
        break;

    case YAML_ALIAS_EVENT:
        term = cstr_to_binary(env, event->data.alias.anchor);
        break;


    case YAML_SCALAR_EVENT:
        term = enif_make_tuple4(env,
                                cstr_to_binary(env, event->data.scalar.anchor),
                                cstr_to_binary(env, event->data.scalar.tag),
                                mem_to_binary(env, event->data.scalar.value, event->data.scalar.length),
//                                bool_to_term(env, event->data.scalar.plain_implicit),
//                                bool_to_term(env, event->data.scalar.quoted_implicit),
                                enum_to_atom(env, event->data.scalar.style, scalar_style_atoms));
        break;


    case YAML_SEQUENCE_START_EVENT:
        term = enif_make_tuple3(env,
                                cstr_to_binary(env, event->data.sequence_start.anchor),
                                cstr_to_binary(env, event->data.sequence_start.tag),
//                                bool_to_term(env, event->data.sequence_start.implicit),
                                enum_to_atom(env, event->data.sequence_start.style, sequence_style_atoms));
        break;

    case YAML_MAPPING_START_EVENT:
        term = enif_make_tuple3(env,
                                cstr_to_binary(env, event->data.mapping_start.anchor),
                                cstr_to_binary(env, event->data.mapping_start.tag),
//                                bool_to_term(env, event->data.mapping_start.implicit),
                                enum_to_atom(env, event->data.mapping_start.style, mapping_style_atoms));
        break;

    default:
        term = enif_make_atom(env, "null");
        break;
    }

    return enif_make_tuple4(env,
                            enum_to_atom(env, type, event_type_atoms),
                            term,
                            mark_to_term(env, &event->start_mark),
                            mark_to_term(env, &event->end_mark));
}
Example #19
0
ERL_NIF_TERM parse_buffer(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
	Parser par;
	Parser* parser = &par;

	ErlNifBinary buffer;

	if(!enif_inspect_binary(env, argv[0], &buffer)) return enif_make_badarg(env);
	if(!init_parser(parser, env, argv[0], &buffer, argv[1])) return enif_make_badarg(env);

	ERL_NIF_TERM reply = argv[2];
	ERL_NIF_TERM return_value;

	parser->remaining_length = buffer.size;
	parser->pointer = buffer.data + parser->frame_start;

	while (parser->frame_start < parser->buffer_size) {
		
		parser->frame_size = D3I(parser->pointer);

		parser->pointer += 3;
		unsigned long seg_num = D1(parser->pointer);
		parser->pointer += 1;

		parser->remaining_length -= (parser->frame_size + 4);

		if (parser->remaining_length < 0) {
			return_value = enif_make_tuple3(env, 
				parser->atoms->atom_incomplete,
				reply, 
				enif_make_sub_binary(env, parser->raw, parser->frame_start, parser->buffer_size - parser->frame_start));

			break;
		}

		if (*parser->pointer == MYSQL_RESP_EOF) {
			ERL_NIF_TERM remaining_buffer = enif_make_sub_binary(env, parser->raw, parser->frame_start + parser->frame_size + 4, parser->remaining_length);

			if (parser->frame_size == 5) {
				unsigned long server_status = D2I(parser->pointer + 3);

				return_value = enif_make_tuple5(env, 
					parser->atoms->atom_eof,
					enif_make_uint(env, server_status), 
					enif_make_uint(env, seg_num), 
					reply,
					remaining_buffer);
			} 

			else return_value = enif_make_tuple4(env, 
					parser->atoms->atom_eof,
					enif_make_uint(env, seg_num),
					reply,
					remaining_buffer);

			break;

		}

		parser->frame_start += 4;

		ERL_NIF_TERM row = parse_line(parser);
		reply = enif_make_list_cell(env, row, reply);

		parser->frame_start += parser->frame_size;
		parser->pointer += parser->frame_size;
	}

	if (parser->frame_start >= parser->buffer_size) return_value = enif_make_tuple3(env, 
												parser->atoms->atom_incomplete,
												reply, 
												parser->atoms->atom_empty);

	destroy_parser(parser);
	return return_value;
}
Example #20
0
static ERL_NIF_TERM quaternion_to_tuple(ErlNifEnv* env, btQuaternion v) {
    return enif_make_tuple4(env, enif_make_double(env, v.getX()), enif_make_double(env, v.getY()), enif_make_double(env,v.getZ()), enif_make_double(env,v.getW()));
}