Example #1
0
static ERL_NIF_TERM
do_column_names(ErlNifEnv *env, sqlite3_stmt *stmt)
{
    int i, size;
    const char *name;
    ERL_NIF_TERM *array;
    ERL_NIF_TERM column_names;
     
    size = sqlite3_column_count(stmt);
    if(size <= 0)
        return make_error_tuple(env, "no_columns");

    array = (ERL_NIF_TERM *) malloc(sizeof(ERL_NIF_TERM) * size);
    if(!array)
        return make_error_tuple(env, "no_memory");

    for(i = 0; i < size; i++) {
        name = sqlite3_column_name(stmt, i);
        if(name == NULL) {
            free(array);
            return make_error_tuple(env, "sqlite3_malloc_failure");
        }

        array[i] = make_atom(env, name);
    }

    column_names = enif_make_tuple_from_array(env, array, size);
    free(array);
    return column_names;
}
Example #2
0
static ERL_NIF_TERM perspective(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
			double fovy,aspect,zNear,zFar;
			ERL_NIF_TERM d[16];

			if(!enif_get_double(env,argv[0], &fovy)) {
							return enif_make_badarg(env);
			}
			if(!enif_get_double(env,argv[1], &aspect)) {
							return enif_make_badarg(env);
			}
			if(!enif_get_double(env,argv[2], &zNear)) {
							return enif_make_badarg(env);
			}
			if(!enif_get_double(env,argv[3], &zFar)) {
							return enif_make_badarg(env);
			}

			glm::dmat4 p = glm::perspective(fovy, aspect,zNear, zFar);
			const double *pSource = (const double*)glm::value_ptr(p);
			for( int i=0; i< 16; ++i) 
							d[i] = enif_make_double(env,pSource[i]);

			return enif_make_tuple_from_array(env, d, 16);
}
Example #3
0
/*

{ok, DataSource} = lgeo_ogr:open("test/polygon.shp"),
{ok, Layer} = lgeo_ogr:ds_get_layer(DataSource, 0),
{ok, FeatureDefn} = lgeo_ogr:l_get_layer_defn(Layer),
{ok, Types} = lgeo_ogr:fd_get_fields_type(FeatureDefn).
{"Integer","String"}
*/
static ERL_NIF_TERM
fd_get_fields_type(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    EnvFeatureDefn_t **feat_defn;
    ERL_NIF_TERM eterm;

    if (argc != 1) {
        return enif_make_badarg(env);
    }

    if(!enif_get_resource(env, argv[0], OGR_FD_RESOURCE, (void**)&feat_defn)) {
        return enif_make_badarg(env);
    }

    int count = OGR_FD_GetFieldCount((**feat_defn).obj);
    ERL_NIF_TERM *arr = (ERL_NIF_TERM *) malloc(sizeof(ERL_NIF_TERM)*count);
    int index;
    for(index=0; index<count; index++)
    {
        OGRFieldDefnH field_defn = OGR_FD_GetFieldDefn((**feat_defn).obj, index);
        arr[index] = enif_make_string(env,
            OGR_GetFieldTypeName(OGR_Fld_GetType(field_defn)),
            ERL_NIF_LATIN1);
    }

    eterm = enif_make_tuple_from_array(env, arr, index);
    free(arr);
    return enif_make_tuple2(env, enif_make_atom(env, "ok"), eterm);
}
Example #4
0
static ERL_NIF_TERM
do_column_types(ErlNifEnv *env, sqlite3_stmt *stmt)
{
    int i, size;
    const char *type;
    ERL_NIF_TERM *array;
    ERL_NIF_TERM column_types;

    size = sqlite3_column_count(stmt);
    if(size == 0)
        return enif_make_tuple(env, 0);
    else if(size < 0)
        return make_error_tuple(env, "invalid_column_count");

    array = (ERL_NIF_TERM *) enif_alloc(sizeof(ERL_NIF_TERM) * size);
    if(!array)
        return make_error_tuple(env, "no_memory");

    for(i = 0; i < size; i++) {
        type = sqlite3_column_decltype(stmt, i);
        if(type == NULL) {
	    type = "nil";
        }

        array[i] = make_atom(env, type);
    }

    column_types = enif_make_tuple_from_array(env, array, size);
    enif_free(array);
    return column_types;
}
Example #5
0
int
make_document(ErlNifEnv     *env, 
              vec_term_t    *vec,
              ERL_NIF_TERM  *out,
              int            return_maps)
{
    if(vec->length % 2 != 0) {
        return 0;
    }
    if(return_maps) {
        int idx, count = vec->length;
        ERL_NIF_TERM ret = enif_make_new_map(env);
        
        for(idx = 0; idx < count; idx += 2) {
            if(!enif_make_map_put(
                    env, 
                    ret, 
                    vec->data[idx], 
                    vec->data[idx+1], 
                    &ret)) {
                return 0;
            }
        }

        *out = ret;
        return 1;
    }

    *out = enif_make_tuple_from_array(env, vec->data, vec->length);
    return 1;
}
Example #6
0
static ERL_NIF_TERM make_term_tuple_from_array(struct make_term_info* mti, int n)
{
    ERL_NIF_TERM t[3];
    t[0] = pull_term(mti);
    t[1] = pull_term(mti);
    t[2] = pull_term(mti);
    return enif_make_tuple_from_array(mti->dst_env, t, 3);	
}
Example #7
0
static ERL_NIF_TERM tuple_2_list_and_tuple(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    const ERL_NIF_TERM* arr;
    int arity;
    if (!enif_get_tuple(env,argv[0],&arity,&arr)) {
	return enif_make_badarg(env);
    }
    return enif_make_tuple2(env, 
			    enif_make_list_from_array(env, arr, arity),
			    enif_make_tuple_from_array(env, arr, arity));
}
Example #8
0
static void line_call_back(UNUSED(int c), void *data)
{
    ecsv_parser_t *parser = (ecsv_parser_t *)data;
    if (parser->err) return;
    ErlNifEnv *env = parser->env;
    line_t *line = &parser->current_line;
    parser->lines = enif_make_list_cell(env,
            enif_make_tuple_from_array(env, line->fields, line->pos),
            parser->lines);
    line->pos = 0;
}
Example #9
0
static ERL_NIF_TERM
make_row(ErlNifEnv *env, sqlite3_stmt *statement, ERL_NIF_TERM *array, int size)
{
    if(!array)
        return make_error_tuple(env, "no_memory");

    for(int i = 0; i < size; i++)
        array[i] = make_cell(env, statement, i);

    return enif_make_tuple_from_array(env, array, size);
}
Example #10
0
static ERL_NIF_TERM lookat(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
			int arity;
			const ERL_NIF_TERM *eye,*center,*up;
			ERL_NIF_TERM d[16];

			if(!enif_get_tuple(env,argv[0],&arity,&eye)) {
							return enif_make_badarg(env);
			}
			if(arity!=3) {
							return enif_make_badarg(env);
			}

			if(!enif_get_tuple(env,argv[1],&arity,&center)) {
							return enif_make_badarg(env);
			}
			if(arity!=3) {
							return enif_make_badarg(env);
			}

			if(!enif_get_tuple(env,argv[2],&arity,&up)) {
							return enif_make_badarg(env);
			}
			if(arity != 3) {
							return enif_make_badarg(env);
			}

			glm::dvec3 veye;
			if(!(enif_get_double(env,eye[0],&veye.x) && 
					 enif_get_double(env,eye[1],&veye.y) &&	
					 enif_get_double(env,eye[2],&veye.z))) {
							return enif_make_badarg(env);
			}

			glm::dvec3 vcenter;
			if(!(enif_get_double(env,center[0],&vcenter.x) && 
					 enif_get_double(env,center[1],&vcenter.y) &&	
					 enif_get_double(env,center[2],&vcenter.z))) {
							return enif_make_badarg(env);
			}

			glm::dvec3 vup;
			if(!(enif_get_double(env,up[0],&vup.x) && 
					 enif_get_double(env,up[1],&vup.y) &&	
					 enif_get_double(env,up[2],&vup.z))) {
							return enif_make_badarg(env);
			}

			glm::dmat4 m = glm::lookAt(veye,vcenter,vup);
			const double *pSource = (const double*)glm::value_ptr(m);
			for(int i =0; i<16;++i) 
							d[i] = enif_make_double(env,pSource[i]);
			return enif_make_tuple_from_array(env,d,16);
}
Example #11
0
BEGIN_C

static ERL_NIF_TERM mat4(ErlNifEnv *env, int args, const ERL_NIF_TERM argv[])
{
				glm::mat4 d = glm::mat4(1.0f);
				ERL_NIF_TERM o[16];

				const float *pSource = (const float *)glm::value_ptr(d);
				for(int i =0; i<16; ++i) 
								o[i] = enif_make_double(env,(double)pSource[i]);
				return enif_make_tuple_from_array(env,o,16);
}
Example #12
0
// convert a python object to an erlang term, return the atom 'unknown' if
// the type can't be converted
static ERL_NIF_TERM pynerl_obj_to_term(ErlNifEnv* env, PyObject* obj) {
	ERL_NIF_TERM term;

	if (obj == Py_False) {
		term = enif_make_atom(env, "false");
	}
	else if (obj == Py_True) {
		term = enif_make_atom(env, "true");
	}
	else if (PyLong_Check(obj)) {
		// TODO: make ints when the size allows to.
		term = enif_make_long(env, PyLong_AsLong(obj));
	}
	else if (PyFloat_Check(obj)) {
		term = enif_make_double(env, PyFloat_AsDouble(obj));
	}
	else if (PyTuple_Check(obj)) {
		Py_ssize_t i, arity = PyTuple_Size(obj);
		ERL_NIF_TERM *terms = (ERL_NIF_TERM*) malloc(sizeof(ERL_NIF_TERM) * (int)arity);

		for (i = 0; i < arity; i++) {
			terms[(int)i] = pynerl_obj_to_term(env, PyTuple_GetItem(obj, i));
		}

		term = enif_make_tuple_from_array(env, terms, (unsigned int)arity);
	}
	else if (PyBytes_Check(obj)) {
		// XXX: the encoding must be latin1
		term = enif_make_string(env, PyBytes_AsString(obj), ERL_NIF_LATIN1);
	}
	else if (PyUnicode_Check(obj)) {
		// XXX: the encoding must be latin1
		term = enif_make_string(env, PyBytes_AsString(PyUnicode_AsLatin1String(obj)), ERL_NIF_LATIN1);
	}
	else if (PyList_Check(obj)) {
		Py_ssize_t i, arity = PyList_Size(obj);
		ERL_NIF_TERM *terms = (ERL_NIF_TERM*) malloc(sizeof(ERL_NIF_TERM) * (int)arity);

		for (i = 0; i < arity; i++) {
			terms[(int)i] = pynerl_obj_to_term(env, PyList_GetItem(obj, i));
		}

		term = enif_make_list_from_array(env, terms, (unsigned int)arity);
	}
	else if (obj == Py_None) {
		term = enif_make_atom(env, "none");
    }
	else {
		term = enif_make_atom(env, "unknown");
    }

	return term;
}
Example #13
0
static ERL_NIF_TERM rotate(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
				double angle;
				int arity;
				const ERL_NIF_TERM *t,*v;
				double dm[16]; 
				ERL_NIF_TERM d[16];

				if(!enif_get_tuple(env,argv[0],&arity,&t)) {
								return enif_make_badarg(env);
				}
				if(arity!=16) {
								return enif_make_badarg(env);
				}

				for(int i=0; i<16; ++i) {
								enif_get_double(env,t[i],&dm[i]);
				}

				glm::dmat4 matrix = glm::make_mat4(dm);

				if(!enif_get_double(env,argv[1],&angle)) {
								return enif_make_badarg(env);
				}

				if(!enif_get_tuple(env,argv[2],&arity,&v)) {
								return enif_make_badarg(env);
				}
				if(arity != 3) {
								return enif_make_badarg(env);
				}

				glm::dvec3 axis;

			  if(!(enif_get_double(env,v[0],&axis.x) && enif_get_double(env,v[1],&axis.y) &&	enif_get_double(env,v[2],&axis.z))) {
								return enif_make_badarg(env);
				}

				glm::dmat4 rotation = glm::rotate(matrix,angle,axis);
				
				const double *pSource = (const double*)glm::value_ptr(rotation);
				for(int i =0; i<16; ++i) {
							d[i] = enif_make_double(env,pSource[i]);
				}
				return enif_make_tuple_from_array(env,d,16);
}
static int handle_end(void *ctx, int array)
{
  state_t *st = (state_t *)ctx;
  container_t *c = st->c;
  /* unlink container struct from state */
  st->c = c->next;
  /* create and add container term */
  if (array) {
    add_element(st, enif_make_tuple_from_array(st->env, c->array,
                                               c->count));
  } else {
    add_element(st, enif_make_list_from_array(st->env, c->array,
                                              c->count));
  }
  /* decallocate used container struct */
  enif_free(st->env, c);
  return 1;
}
Example #15
0
/*

{ok, DataSource} = lgeo_ogr:open("test/polygon.shp"),
{ok, Layer} = lgeo_ogr:ds_get_layer(DataSource, 0),
{ok, Feature} = lgeo_ogr:l_get_feature(Layer, 0),
{ok, Fields} = lgeo_ogr:f_get_fields(Feature).
{1,"first"}
*/
static ERL_NIF_TERM
f_get_fields(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    EnvFeature_t **feature;
    ERL_NIF_TERM eterm;

    if (argc != 1) {
        return enif_make_badarg(env);
    }

    if(!enif_get_resource(env, argv[0], OGR_F_RESOURCE, (void**)&feature)) {
        return enif_make_badarg(env);
    }

    OGRFeatureDefnH feature_defn = OGR_F_GetDefnRef((**feature).obj);
    int count = OGR_FD_GetFieldCount(feature_defn);
    ERL_NIF_TERM *arr = (ERL_NIF_TERM *) malloc(sizeof(ERL_NIF_TERM)*count);
    int index;
    for(index=0; index<count; index++)
    {
        OGRFieldDefnH field_defn = OGR_FD_GetFieldDefn(feature_defn, index);
        if(OGR_Fld_GetType(field_defn) == OFTInteger) {
            arr[index] = enif_make_int(env,
                OGR_F_GetFieldAsInteger((**feature).obj, index));
        } else if(OGR_Fld_GetType(field_defn) == OFTReal) {
            arr[index] = enif_make_double(env,
                OGR_F_GetFieldAsDouble((**feature).obj, index));
        } else if(OGR_Fld_GetType(field_defn) == OFTString) {
            arr[index] = enif_make_string(env,
                OGR_F_GetFieldAsString((**feature).obj, index),
                ERL_NIF_LATIN1);
        } else {
            arr[index] = enif_make_string(env,
                OGR_F_GetFieldAsString((**feature).obj, index),
                ERL_NIF_LATIN1);
        }
    }

    eterm = enif_make_tuple_from_array(env, arr, index);
    free(arr);
    return enif_make_tuple2(env, enif_make_atom(env, "ok"), eterm);
}
Example #16
0
static ERL_NIF_TERM
make_row(ErlNifEnv *env, sqlite3_stmt *statement) 
{
    int i, size;
    ERL_NIF_TERM *array;
    ERL_NIF_TERM row;
     
    size = sqlite3_column_count(statement);
    array = (ERL_NIF_TERM *) malloc(sizeof(ERL_NIF_TERM)*size);

    if(!array) 
	    return make_error_tuple(env, "no_memory");

    for(i = 0; i < size; i++) 
	    array[i] = make_cell(env, statement, i);

    row = make_row_tuple(env, enif_make_tuple_from_array(env, array, size));
    free(array);
    return row;
}
Example #17
0
static ERL_NIF_TERM trace(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    int state_arity;
    ErlNifPid self, to;
    ERL_NIF_TERM *tuple, msg;
    const ERL_NIF_TERM *state_tuple;
    ASSERT(argc == 5);

    enif_get_tuple(env, argv[1], &state_arity, &state_tuple);

    tuple = enif_alloc(sizeof(ERL_NIF_TERM)*(argc));
    memcpy(tuple,argv,sizeof(ERL_NIF_TERM)*argc);

    msg = enif_make_tuple_from_array(env, tuple, argc);
    enif_get_local_pid(env, state_tuple[1], &to);
    enif_send(env, &to, NULL, msg);
    enif_free(tuple);

    return atom_ok;
}
Example #18
0
static ERL_NIF_TERM translate(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
			double x,y,z;
			ERL_NIF_TERM d[16];

			if(!enif_get_double(env,argv[0],&x)) {
							return enif_make_badarg(env);
			}
			if(!enif_get_double(env,argv[1],&y)) {
							return enif_make_badarg(env);
			}
			if(!enif_get_double(env,argv[2],&z)) {
							return enif_make_badarg(env);
			}

			glm::mat4 trans = glm::translate(glm::mat4(1.0f), glm::vec3((float)x,(float)y,(float)z));
			const float *pSource = (const float*)glm::value_ptr(trans);
			for(int i =0; i<16;++i) 
							d[i] = enif_make_double(env,(double)pSource[i]);
			return enif_make_tuple_from_array(env,d,16);
}
Example #19
0
static ERL_NIF_TERM trace(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    int state_arity;
    ErlNifPid self, to;
    ERL_NIF_TERM *tuple, msg;
    ASSERT(argc == 5);

    tuple = enif_alloc(sizeof(ERL_NIF_TERM)*(argc+1));
    memcpy(tuple+1,argv,sizeof(ERL_NIF_TERM)*argc);

    if (enif_self(env, &self)) {
        tuple[0] = enif_make_pid(env, &self);
    } else {
        tuple[0] = enif_make_atom(env, "undefined");
    }

    msg = enif_make_tuple_from_array(env, tuple, argc + 1);
    enif_get_local_pid(env, argv[1], &to);
    enif_send(env, &to, NULL, msg);
    enif_free(tuple);

    return atom_ok;
}
Example #20
0
// c_soc:op_lookup(Index) => op
static ERL_NIF_TERM op_lookup(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
  soc::State* st = (soc::State*) enif_priv_data(env);

  int index;
  
  if (enif_get_int(env, argv[0], &index) == 0) {
    printf("[op_alloc] error: failed to initialize index\n");
    return -1;
  }

  const soc::Operation* op;

  printf("loc / msg lookup index %d start\n", index);
  try {
    op = st->Lookup(index);
  } catch (std::out_of_range exn) {
    printf("Exception: index %d out of range\n", index);
  }

  ERL_NIF_TERM loc = enif_make_tuple2(env, enif_make_int(env, index), 
				      enif_make_int(env, op->GetParentKey()));

  ERL_NIF_TERM ext_msg;
  ERL_NIF_TERM int_msg;
  try {
    ext_msg = enif_make_tuple_from_array(st->GetEnv(), op->GetTuple(), op->TupleArity());
    int_msg = enif_make_copy(env, ext_msg);
  } catch (...) {
    printf("ERROR: ...");
  }

  printf("loc / msg lookup success\n");

  return enif_make_tuple2(env, loc, int_msg);
}