Esempio n. 1
0
static ERL_NIF_TERM do_write(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
  static PortMidiStream ** stream;

  ErlNifResourceType* streamType = (ErlNifResourceType*)enif_priv_data(env);
  if(!enif_get_resource(env, argv[0], streamType, (PortMidiStream **) &stream)) {
    return enif_make_badarg(env);
  }

  ERL_NIF_TERM erlMessages = argv[1];
  const ERL_NIF_TERM * erlEvent;
  const ERL_NIF_TERM * erlMessage;
  ERL_NIF_TERM erlTuple;

  unsigned int numOfMessages;
  int tupleSize;
  enif_get_list_length(env, erlMessages, &numOfMessages);

  PmEvent events[numOfMessages];
  long int status, note, velocity, timestamp;

  for(unsigned int i = 0; i < numOfMessages; i++) {
    enif_get_list_cell(env, erlMessages, &erlTuple, &erlMessages);
    enif_get_tuple(env, erlTuple, &tupleSize, &erlEvent);

    enif_get_tuple(env, erlEvent[0], &tupleSize, &erlMessage);
    enif_get_long(env, erlMessage[0], &status);
    enif_get_long(env, erlMessage[1], &note);
    enif_get_long(env, erlMessage[2], &velocity);

    enif_get_long(env, erlEvent[1], &timestamp);

    PmEvent event;
    event.message = Pm_Message(status, note, velocity);
    event.timestamp = timestamp;

    events[i] = event;
  }

  PmError writeError;
  writeError = Pm_Write(*stream, events, numOfMessages);

  if (writeError == pmNoError) {
    return enif_make_atom(env, "ok");
  }

  const char * writeErrorMsg;
  writeErrorMsg = Pm_GetErrorText(writeError);

  return enif_make_tuple2(
    env,
    enif_make_atom(env, "error"),
    enif_make_string(env, writeErrorMsg, ERL_NIF_LATIN1)
  );
}
Esempio n. 2
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);
}
Esempio n. 3
0
static int x509_parse_subject(ErlNifEnv* env, ERL_NIF_TERM subject_tuple, int *num_subject_entries, x509_subject_entry **subject_entries){
  int num_subject_tuple;
  unsigned num_subject_terms;
  ERL_NIF_TERM head, tail;
  int pair_arity;
  char *name;
  char *value;
  char *subject_string = NULL;
  const ERL_NIF_TERM* pair;
  int idx;
  x509_subject_entry* se;
  unsigned value_len;
  const ERL_NIF_TERM* subject_terms;

  *subject_entries = NULL;
  *num_subject_entries = 0;

  /* make sure this is a tuple with first term 'subject' */
  if(!enif_get_tuple(env, subject_tuple, &num_subject_tuple, &subject_terms) ||
     !atom_to_string(env, subject_terms[0], &subject_string) ||
     strncmp(subject_string, SUBJECT_STR, subject_strlen)) {
    if(subject_string) free(subject_string);
    return 0;
  }
  free(subject_string);

  /* create room for the x509_subject_entry structs */
  if(!enif_get_list_length(env, subject_terms[1], &num_subject_terms) || 
     (NULL == (se = (x509_subject_entry*)malloc(num_subject_terms * sizeof(x509_subject_entry))))) return 0;

  /* get the first entry and prime the pump for walking the rest */
  if(!enif_get_list_cell(env, subject_terms[1], &head, &tail) || 
     !enif_get_tuple(env, head, &pair_arity, &pair) ||
     pair_arity!=2) { return 0; }

  for(idx=0; idx<num_subject_terms; idx++){
    atom_to_string(env, pair[0], &name);

    enif_get_list_length(env, pair[1], &value_len);
    value = (char*)malloc(value_len+1);
    enif_get_string(env, pair[1], value, value_len+1, ERL_NIF_LATIN1);
    (se+idx)->name = name;
    (se+idx)->value = value;

    if(!enif_get_list_cell(env, tail, &head, &tail) || 
       !enif_get_tuple(env, head, &pair_arity, &pair) ||
       pair_arity!=2) { break; }
  }

  *num_subject_entries = num_subject_terms;
  *subject_entries = se;
  return 1;
}
Esempio n. 4
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);
}
Esempio n. 5
0
static inline int
match_tuple(ErlNifEnv* env, ERL_NIF_TERM term, State *st){ 
  const ERL_NIF_TERM *tuple;
  int arity;
  if(!enif_get_tuple(env, term, &arity, &tuple))
    return 0;
  if(arity > 0){ 
    if(enif_is_atom(env, tuple[0])){
      if(arity == 2){
	if(enif_is_identical(tuple[0], st->priv->am_struct)){
	  //struct
	  return match_proplist(env, tuple[1], st);
	}else   if(enif_is_identical(tuple[0], st->priv->am_json)){
	  //json
	  return  match_json(env, tuple[1], st);
	}
      }
      //records
      if(st->records){
	return match_record(env, arity, tuple, st);
      }
    }
    if(arity == 1){
      //eep18
      return (match_proplist(env, tuple[0], st));
    }
  }
  return 0;  
}
Esempio n. 6
0
static btTransform get_transform(ErlNifEnv *env, const ERL_NIF_TERM arg) {
    double x,y,z,w;
    const ERL_NIF_TERM *tuple;
    int arity;
    enif_get_tuple(env, arg, &arity, &tuple);
    return btTransform(get_quaternion(env,tuple[0]),get_vector(env,tuple[1]));
}
Esempio n. 7
0
static void do_load_info(ErlNifEnv* env, ERL_NIF_TERM load_info, int* retvalp)
{
    NifModPrivData* data = priv_data(env);
    ERL_NIF_TERM head, tail;
    unsigned ix;

    for (ix=0; ix<RT_MAX; ix++) {
	data->rt_arr[ix] = NULL;
    }
    for (head = load_info; enif_get_list_cell(env, head, &head, &tail);
	  head = tail) {
	const ERL_NIF_TERM* arr;
	int arity;

	CHECK(enif_get_tuple(env, head, &arity, &arr));
	switch (arity) {
	case 6:
	    open_resource_type(env, arr);
	    break;
	case 2:
	    CHECK(arr[0] == am_return);
	    CHECK(enif_get_int(env, arr[1], retvalp));
	    break;
	default:
	    CHECK(0);
	}
    }    
    CHECK(enif_is_empty_list(env, head));
}
Esempio n. 8
0
static ERL_NIF_TERM
mem_read(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
  unsigned int err, l, i, tmp;
  char element;
  int ar;
  char* ptr;
  ERL_NIF_TERM list, head, *tpl;


  err = enif_get_tuple(env, argv[0], &ar, (const ERL_NIF_TERM**)(&tpl));
  if (err) {
    err = nifty_get_ptr(env, tpl[0], (ptr_t*)&ptr);
  }
  if (!err) {
    goto error;
  }

  err = enif_get_uint(env, argv[1], &l);
  if (!err) {
    goto error;
  }

  list = enif_make_list(env, 0);

  for (i=0;i<l;i++) {
    element = (char)*(ptr+(l-1)-i);
    tmp = element & 0xff;
    head = enif_make_uint(env, tmp);
    list = enif_make_list_cell(env, head, list);
  }
  return list;
 error:
  return enif_make_badarg(env);
}
Esempio n. 9
0
static int make_attrs(ErlNifEnv* env, struct buf *rbuf, ERL_NIF_TERM attrs)
{
  ErlNifBinary name, data;
  ERL_NIF_TERM head, tail;
  const ERL_NIF_TERM *tuple;
  int arity, ret = 1;
  
  while (enif_get_list_cell(env, attrs, &head, &tail)) {
    if (enif_get_tuple(env, head, &arity, &tuple)) {
      if (arity == 2) {
	if (enif_inspect_iolist_as_binary(env, tuple[0], &name) &&
	    enif_inspect_iolist_as_binary(env, tuple[1], &data)) {
	  buf_add_char(env, rbuf, ' ');
	  buf_add_str(env, rbuf, (char *)name.data, name.size);
	  buf_add_str(env, rbuf, "='", 2);
	  crypt(env, rbuf, data.data, data.size);
	  buf_add_char(env, rbuf, '\'');
	  attrs = tail;
	} else {
	  ret = 0;
	  break;
	};
      } else {
	ret = 0;
	break;
      };
    } else {
      ret = 0;
      break;
    };
  };
  
  return ret;
}
Esempio n. 10
0
ERL_NIF_TERM parse_histogram_option(ErlNifEnv* env, ERL_NIF_TERM item, 
                                    histogram_handle& handle)
{
    int arity;
    const ERL_NIF_TERM* option;
    if (enif_get_tuple(env, item, &arity, &option))
    {
        if (option[0] == ATOM_SIZE)
        {
            unsigned long sample_size;
            if (enif_get_ulong(env, option[1], &sample_size))
            {
                handle.size = sample_size;
            }
        }
        if (option[0] == ATOM_WINDOW_WIDTH)
        {
            unsigned long window_width;
            if (enif_get_ulong(env, option[1], &window_width))
            {
                handle.width = window_width;
            }
        }
    }
    return ATOM_OK;
}
Esempio n. 11
0
static ERL_NIF_TERM
mem_write_binary(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
  unsigned int err;
  int ar;
  unsigned char* ptr;
  ERL_NIF_TERM *tpl;
  ErlNifBinary bin;

  if (!enif_inspect_binary(env, argv[0], &bin)) {
    goto error;
  }

  err = enif_get_tuple(env, argv[1], &ar, (const ERL_NIF_TERM**)(&tpl));
  if (err) {
    err = nifty_get_ptr(env, tpl[0], (ptr_t*)&ptr);
  }
  if (!err) {
    goto error;
  }

  memcpy(ptr, bin.data, bin.size);

  return argv[1];

 error:
  return enif_make_badarg(env);
}
Esempio n. 12
0
// creates a new simple dataspace and opens it for access
ERL_NIF_TERM h5screate_simple(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
  hid_t dataspace_id;
  ERL_NIF_TERM ret;
  const ERL_NIF_TERM *terms;
  int rank; // number of dimensions of dataspace
  hsize_t* dimsf; // array specifiying the size of each dimension
  int arity;

  // parse arguments
  check(argc == 2, "Incorrent number of arguments");
  check(enif_get_int(env, argv[0], &rank ), "Can't get rank from argv");
  check(enif_get_tuple(env, argv[1], &arity, &terms), "Can't get terms from argv");
  // make sure that rank is matching arity
  check(rank <= 2, "does not support > 2 dimensions");

  // allocate array of size rank
  dimsf = (hsize_t*) malloc(arity * sizeof(hsize_t));
  check(!convert_nif_to_hsize_array(env, arity, terms, dimsf), "can't convert dims arr");

  // create a new file using default properties
  dataspace_id = H5Screate_simple(rank, dimsf, NULL);
  check(dataspace_id > 0, "Failed to create dataspace.");

  // cleanup
  free(dimsf);
  ret = enif_make_int(env, dataspace_id);
  return enif_make_tuple2(env, ATOM_OK, ret);

 error:
  if(dataspace_id) H5Sclose(dataspace_id);
  if(dimsf) free(dimsf);
  return error_tuple(env, "Can not create dataspace");
};
Esempio n. 13
0
int decode_command_call_into_process(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[], process_t **ptr)
{
    // Instantiate a new process
    if (pm_new_process(ptr))
        return -1;

    process_t *process = *ptr;
    const ERL_NIF_TERM* big_tuple;
    int arity = 2;
    // Get the outer tuple
    if(!enif_get_tuple(env, argv[0], &arity, &big_tuple)) return -1;

    // The first command is a string
    char command[MAX_BUFFER_SZ], key[MAX_BUFFER_SZ], value[MAX_BUFFER_SZ];
    memset(&command, '\0', sizeof(command));

    // Get the command
    if (enif_get_string(env, big_tuple[0], command, sizeof(command), ERL_NIF_LATIN1) < 0) return -1;
    pm_malloc_and_set_attribute(&process->command, command);

    // The second element of the tuple is a list of options
    const ERL_NIF_TERM* tuple;
    ERL_NIF_TERM head, tail, list = big_tuple[1];

    // int enif_get_tuple(ErlNifEnv* env, ERL_NIF_TERM term, int* arity, const ERL_NIF_TERM** array)
    while(enif_get_list_cell(env, list, &head, &tail)) {
        // Get the tuple
        if(!enif_get_tuple(env, head, &arity, &tuple)) return -1;
        // First element is an atom
        if (!enif_get_atom(env, tuple[0], key, sizeof(key))) return -2;
        if (enif_get_string(env, tuple[1], value, sizeof(value), ERL_NIF_LATIN1) < 0) return -3;
        if (!strcmp(key, "do_before")) {
            // Do before
            pm_malloc_and_set_attribute(&process->before, value);
        } else if (!strcmp(key, "do_after")) {
            pm_malloc_and_set_attribute(&process->after, value);
        } else if (!strcmp(key, "cd")) {
            pm_malloc_and_set_attribute(&process->cd, value);
        } else if (!strcmp(key, "env")) {
            pm_add_env(&process, value);
        } else if (!strcmp(key, "nice")) {
            process->nice = atoi(value);
        }
        list = tail;
    }
    return 0;
}
Esempio n. 14
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);
}
Esempio n. 15
0
static ERL_NIF_TERM trace_garbage_collection(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    lttng_decl_procbuf(pid);
    ERL_NIF_TERM gci, tup;
    const ERL_NIF_TERM *vals;
    int arity;
    unsigned long ohbsz, nhbsz, size;

    ASSERT(argc == 6);

    /* Assume gc info order does not change */
    gci = argv[3];

    /* get reclaimed or need */
    enif_get_list_cell(env, gci, &tup, &gci);
    enif_get_tuple(env, tup, &arity, &vals);
    ASSERT(arity == 2);
    enif_get_ulong(env, vals[1], &size);

    /* get old heap block size */
    enif_get_list_cell(env, gci, &tup, &gci);
    enif_get_tuple(env, tup, &arity, &vals);
    ASSERT(arity == 2);
    ASSERT(vals[0] == atom_old_heap_block_size);
    enif_get_ulong(env, vals[1], &ohbsz);

    /* get new heap block size */
    enif_get_list_cell(env, gci, &tup, &gci);
    enif_get_tuple(env, tup, &arity, &vals);
    ASSERT(arity == 2);
    ASSERT(vals[0] == atom_heap_block_size);
    enif_get_ulong(env, vals[1], &nhbsz);

    lttng_pid_to_str(argv[2], pid);

    if (argv[0] == atom_gc_minor_start) {
        LTTNG4(gc_minor_start, pid, size, nhbsz, ohbsz);
    } else if (argv[0] == atom_gc_minor_end) {
        LTTNG4(gc_minor_end, pid, size, nhbsz, ohbsz);
    } else if (argv[0] == atom_gc_major_start) {
        LTTNG4(gc_major_start, pid, size, nhbsz, ohbsz);
    } else if (argv[0] == atom_gc_major_end) {
        LTTNG4(gc_major_end, pid, size, nhbsz, ohbsz);
    }
    return atom_ok;
}
Esempio n. 16
0
inline static ERL_NIF_TERM do_offset(ErlNifEnv* env, 
    UCalendar* cal,
    date_fun_ptr fun,
    const ERL_NIF_TERM in) 
{
    UCalendarDateFields field;
    UErrorCode status = U_ZERO_ERROR;

    ERL_NIF_TERM head, tail;
    ERL_NIF_TERM* tuple;
    unsigned int count, i;
    int32_t len, offset; 

    char    value[ATOM_LEN];
    int     parsed_value;
    
    
    i = 0;
    if (!enif_get_list_length(env, in, &count)) 
        return enif_make_badarg(env);

    tail = in;

    while (enif_get_list_cell(env, tail, &head, &tail)) {

        if (enif_get_tuple(env, head, &len, (const ERL_NIF_TERM**) &tuple)
            && (len == 2)) { 

            /* Set an attribute start */

            if (!(enif_get_atom(env, tuple[0], (char*) value, 
                        ATOM_LEN, ERL_NIF_LATIN1) 
               && enif_get_int(env, tuple[1], &offset))) 
                goto bad_elem;
                
            parsed_value = parseCalendarDateField(value);
            if ((parsed_value == -1)) 
                goto bad_elem;

            field = (UCalendarDateFields) parsed_value;
 
            fun(cal, field, offset, &status);

            if (U_FAILURE(status))
                goto bad_elem;
            
            /* Set an attribute end */

        } else 
            goto bad_elem;
    }


    return calendar_to_double(env, (const UCalendar*) cal);

    bad_elem:
        return list_element_error(env, in, i);
}
Esempio n. 17
0
static btVector3 get_vector(ErlNifEnv *env, const ERL_NIF_TERM arg) {
    double x,y,z;
    const ERL_NIF_TERM *tuple;
    int arity;
    enif_get_tuple(env, arg, &arity, &tuple);
    enif_get_double(env, tuple[0], &x);
    enif_get_double(env, tuple[1], &y);
    enif_get_double(env, tuple[2], &z);
    return btVector3(x,y,z);
}
Esempio n. 18
0
    void do_set(robin_q_handle *handle, ERL_NIF_TERM value) {
        const ERL_NIF_TERM *elements_tuple;
        int elements_size;
        ERL_NIF_TERM elements_term = enif_make_copy(handle->env, value);
        enif_get_tuple(handle->env, elements_term, &elements_size, &elements_tuple);

        handle->size        = elements_size;
        handle->elements    = elements_tuple;
        handle->index       = 0;
    }
Esempio n. 19
0
static btQuaternion get_quaternion(ErlNifEnv *env, const ERL_NIF_TERM arg) {
    double x,y,z,w;
    const ERL_NIF_TERM *tuple;
    int arity;
    enif_get_tuple(env, arg, &arity, &tuple);
    enif_get_double(env, tuple[0], &x);
    enif_get_double(env, tuple[1], &y);
    enif_get_double(env, tuple[2], &z);
    enif_get_double(env, tuple[3], &w);
    return btQuaternion(x,y,z,w);
}
Esempio n. 20
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));
}
Esempio n. 21
0
File: engine.c Progetto: bjorng/otp
static int get_engine_load_cmd_list(ErlNifEnv* env, const ERL_NIF_TERM term, char **cmds, int i)
{
    ERL_NIF_TERM head, tail;
    const ERL_NIF_TERM *tmp_tuple;
    ErlNifBinary tmpbin;
    int arity;
    char *tuple1 = NULL, *tuple2 = NULL;

    if (enif_is_empty_list(env, term)) {
        cmds[i] = NULL;
        return 0;
    }

    if (!enif_get_list_cell(env, term, &head, &tail))
        goto err;
    if (!enif_get_tuple(env, head, &arity, &tmp_tuple))
        goto err;
    if (arity != 2)
        goto err;
    if (!enif_inspect_binary(env, tmp_tuple[0], &tmpbin))
        goto err;

    if ((tuple1 = enif_alloc(tmpbin.size + 1)) == NULL)
        goto err;

    (void) memcpy(tuple1, tmpbin.data, tmpbin.size);
    tuple1[tmpbin.size] = '\0';
    cmds[i] = tuple1;
    i++;

    if (!enif_inspect_binary(env, tmp_tuple[1], &tmpbin))
        goto err;

    if (tmpbin.size == 0) {
        cmds[i] = NULL;
    } else {
        if ((tuple2 = enif_alloc(tmpbin.size + 1)) == NULL)
            goto err;
        (void) memcpy(tuple2, tmpbin.data, tmpbin.size);
        tuple2[tmpbin.size] = '\0';
        cmds[i] = tuple2;
    }
    i++;
    return get_engine_load_cmd_list(env, tail, cmds, i);

 err:
    if (tuple1 != NULL) {
        i--;
        enif_free(tuple1);
    }
    cmds[i] = NULL;
    return -1;
}
Esempio n. 22
0
static int do_iterator_options(ErlNifEnv* env, UCollator* col, 
    const ERL_NIF_TERM in, unsigned int& i) {

    ERL_NIF_TERM out, list;
    ERL_NIF_TERM* tuple;
    unsigned int count;
    UErrorCode status = U_ZERO_ERROR;
    int32_t len; 

    char    value[ATOM_LEN], key[ATOM_LEN];
    int     parsed_value,    parsed_key;
    
    i = 0;
    if (!enif_get_list_length(env, in, &count)) 
        return 0;

    list = in;

    while (enif_get_list_cell(env, list, &out, &list)) {

        if (enif_get_tuple(env, out, &len, (const ERL_NIF_TERM**) &tuple)
            && (len == 2)) { 

            /* Set an attribute start */

            if (!(enif_get_atom(env, tuple[0], (char*) key,   
                        ATOM_LEN, ERL_NIF_LATIN1) 
               && enif_get_atom(env, tuple[1], (char*) value, 
                        ATOM_LEN, ERL_NIF_LATIN1))) 
                return 0;
                
    
            parsed_key   = parseAttrKey(key);
            parsed_value = parseAttrValue(value);
            if ((parsed_value == -1) || (parsed_key == -1)) 
                return 0;
 
            ucol_setAttribute(col,
                (UColAttribute)      parsed_key,
                (UColAttributeValue) parsed_value,
                &status);

            if (U_FAILURE(status))
                return 0;
            
            /* Set an attribute end */

        } else 
            return 0;
    }
    return 1;
}
Esempio n. 23
0
// convert an erlang term to a python object
// return None if the type can't be converted
static PyObject* pynerl_term_to_obj(ErlNifEnv* env, ERL_NIF_TERM term) {
	int vint;
	Py_ssize_t arity, i;
	long int vlong;
	double vdouble;
	char buff[BUFF_SIZE];
	PyObject* obj;
	ERL_NIF_TERM list, head, tail;
	const ERL_NIF_TERM *terms;

	// TODO: add more types
	if (enif_get_long(env, term, &vlong)) {
		obj = PyLong_FromLong(vlong);
	}
	else if (enif_get_double(env, term, &vdouble)) {
		obj = PyFloat_FromDouble(vlong);
	}
	else if (enif_is_empty_list(env, term)) {
		obj = PyList_New(0);
	}
	else if (enif_get_tuple(env, term, &vint, &terms)) {
		arity = vint;
		obj = PyTuple_New(arity);

		for (i = 0; i < arity; i++) {
			PyTuple_SetItem(obj, i, pynerl_term_to_obj(env, terms[(int)i]));
		}
	}
	else if (enif_is_identical(term, enif_make_atom(env, "true"))) {
		obj = Py_True;
	}
	else if (enif_is_identical(term, enif_make_atom(env, "false"))) {
		obj = Py_False;
	}
	else if (enif_get_string(env, term, buff, BUFF_SIZE, ERL_NIF_LATIN1)) {
		obj = PyUnicode_FromString(buff);
	}
	else if (enif_get_list_cell(env, term, &head, &tail)) {
		obj = PyList_New(0);
		list = term;

		while (enif_get_list_cell(env, list, &head, &tail)) {
			PyList_Append(obj, pynerl_term_to_obj(env, head));
			list = tail;
		}
	}
	else {
		obj = Py_None;
	}

	return obj;
}
Esempio n. 24
0
static int
bind_cell(ErlNifEnv *env, const ERL_NIF_TERM cell, sqlite3_stmt *stmt, unsigned int i)
{
    int the_int;
    ErlNifSInt64 the_long_int;
    double the_double;
    char the_atom[MAX_ATOM_LENGTH+1];
    ErlNifBinary the_blob;
    int arity;
    const ERL_NIF_TERM* tuple;

    if(enif_get_int(env, cell, &the_int)) 
	    return sqlite3_bind_int(stmt, i, the_int);

    if(enif_get_int64(env, cell, &the_long_int)) 
        return sqlite3_bind_int64(stmt, i, the_long_int);

    if(enif_get_double(env, cell, &the_double)) 
	    return sqlite3_bind_double(stmt, i, the_double);

    if(enif_get_atom(env, cell, the_atom, sizeof(the_atom), ERL_NIF_LATIN1)) {
	    if(strcmp("undefined", the_atom) == 0) {
	       return sqlite3_bind_null(stmt, i);
	    }
	  
	    return sqlite3_bind_text(stmt, i, the_atom, strlen(the_atom), SQLITE_TRANSIENT);
    }

    /* Bind as text assume it is utf-8 encoded text */
    if(enif_inspect_iolist_as_binary(env, cell, &the_blob))
        return sqlite3_bind_text(stmt, i, (char *) the_blob.data, the_blob.size, SQLITE_TRANSIENT);

    /* Check for blob tuple */
    if(enif_get_tuple(env, cell, &arity, &tuple)) {
        if(arity != 2) 
            return -1;

        /* length 2! */
        if(enif_get_atom(env, tuple[0], the_atom, sizeof(the_atom), ERL_NIF_LATIN1)) {
            /* its a blob... */
            if(0 == strncmp("blob", the_atom, strlen("blob"))) {
                /* with a iolist as argument */
                if(enif_inspect_iolist_as_binary(env, tuple[1], &the_blob)) {
                    /* kaboom... get the blob */
	                return sqlite3_bind_blob(stmt, i, the_blob.data, the_blob.size, SQLITE_TRANSIENT);
                }
            }
        }
    }

    return -1;
}
Esempio n. 25
0
static ERL_NIF_TERM tuple_2_list(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    int arity = -1;
    const ERL_NIF_TERM* ptr;
    ERL_NIF_TERM list = enif_make_list(env,0);

    if (argc!=1 || !enif_get_tuple(env,argv[0],&arity,&ptr)) {
	return enif_make_badarg(env);
    }
    while (--arity >= 0) {
	list = enif_make_list_cell(env,ptr[arity],list);
    }
    return list;
}
Esempio n. 26
0
jsval
to_js_object(ErlNifEnv* env, JSContext* cx, ERL_NIF_TERM list)
{
    JSObject* ret;
    jsval kval;
    jsval vval;
    jsid idp;
    ERL_NIF_TERM head;
    ERL_NIF_TERM tail;
    const ERL_NIF_TERM* pair;
    int arity;

    ret = JS_NewObject(cx, NULL, NULL, NULL);
    if(ret == NULL) return JSVAL_VOID;
    
    if(enif_is_empty_list(env, list))
    {
        return OBJECT_TO_JSVAL(ret);
    }

    if(!enif_get_list_cell(env, list, &head, &tail))
    {
        return JSVAL_VOID;
    }

    do {
        if(!enif_get_tuple(env, head, &arity, &pair))
        {
            return JSVAL_VOID;
        }
        
        if(arity != 2)
        {
            return JSVAL_VOID;
        }
        
        kval = to_js_key(env, cx, pair[0]);
        if(kval == JSVAL_VOID) return JSVAL_VOID;
        if(!JS_ValueToId(cx, kval, &idp)) return JSVAL_VOID;
        vval = to_js(env, cx, pair[1]);
        if(vval == JSVAL_VOID) return JSVAL_VOID;
        
        if(!JS_SetPropertyById(cx, ret, idp, &vval))
        {
            return JSVAL_VOID;
        }
    } while(enif_get_list_cell(env, tail, &head, &tail));
    
    return OBJECT_TO_JSVAL(ret);
}
Esempio n. 27
0
static int get_bandregion_from(ErlNifEnv* env, const ERL_NIF_TERM *pterm, bandregion* pbr) 
{
    int rarity;
    const ERL_NIF_TERM* r;
    int res = enif_get_tuple(env, *pterm, &rarity, &r);
    if (res) {
        enif_get_int(env, r[0], &(pbr->xoffset));
        enif_get_int(env, r[1], &(pbr->yoffset));
        enif_get_int(env, r[2], &(pbr->xsize));
        enif_get_int(env, r[3], &(pbr->ysize));
        //DEBUG("rx=%d, ry=%d, rxsize=%d, rysize=%d\r\n", rx, ry, rxsize, rysize);
    }
    return res;
}
Esempio n. 28
0
//
// Options = [ Option ]
// Option = caseless | {offset, non_neg_integer()}
//          | {capture,ValueSpec} | {capture,ValueSpec,Type}
// Type = index | binary
// ValueSpec = all | all_but_first | first | none | ValueList
// ValueList = [ ValueID ]
// ValueID = int() | string() | atom()
//
static bool parse_match_options(ErlNifEnv* env, const ERL_NIF_TERM list,
                                matchoptions& opts)
{
    if (enif_is_empty_list(env, list))
        return true;

    ERL_NIF_TERM L,H,T;

    for (L=list; enif_get_list_cell(env, L, &H, &T); L=T) {
        const ERL_NIF_TERM *tuple;
        int tuplearity = -1;

        if (enif_is_identical(H, a_caseless)) {

            // caseless

            opts.caseless = true;

        } else if (enif_get_tuple(env, H, &tuplearity, &tuple)) {

            if (tuplearity == 2 || tuplearity == 3) {

                // {offset,N} or {capture,ValueSpec}

                if (enif_is_identical(tuple[0], a_offset)) {

                    // {offset, int()}

                    int offset = 0;
                    if (enif_get_int(env, tuple[1], &offset)) {
                        opts.offset = offset;
                    } else {
                        return false;
                    }

                } else if (enif_is_identical(tuple[0], a_capture)) {

                    // {capture,ValueSpec,Type}
                    parse_match_capture_options(env, opts, tuple, tuplearity);

                }
            }
        } else {
            return false;
        }
    }

    return true;
}
Esempio n. 29
0
/*-----------------------------------------------------------------------------------------------------------------------*/
static ERL_NIF_TERM get_parser_msg(
      ErlNifEnv* env, ERL_NIF_TERM ref,
      ERL_NIF_TERM* pres, ERL_NIF_TERM* mres,
      ParserRes** parser, FIXMsg** msg)
{
   int32_t arity;
   ERL_NIF_TERM const* tuple = NULL;
   if (!enif_get_tuple(env, ref, &arity, &tuple) || arity != 3)
   {
      return make_error(env, FIX_FAILED, "Wrong msg reference.");
   }
   if (enif_compare(tuple[0], msg_atom))
   {
      return make_error(env, FIX_FAILED, "Wrong msg reference.");
   }
   ERL_NIF_TERM const* data = NULL;
   if (!enif_get_tuple(env, tuple[2], &arity, &data) || arity != 2)
   {
      return make_error(env, FIX_FAILED, "Wrong msg reference.");
   }
   *pres = data[0];
   *mres = data[1];
   void* res = NULL;
   if (!enif_get_resource(env, *pres, parser_res, &res))
   {
      return make_error(env, FIX_FAILED, "Wrong parser resource.");
   }
   *parser = (ParserRes*)res;

   if (!enif_get_resource(env, *mres, message_res, &res))
   {
      return make_error(env, FIX_FAILED, "Wrong message resource.");
   }
   *msg = ((MsgRes*)res)->msg;
   return ok_atom;
}
Esempio n. 30
0
static inline int
match_pair(ErlNifEnv* env, ERL_NIF_TERM term, State *st){
  const ERL_NIF_TERM *tuple;
  int arity;
  if(enif_get_tuple(env, term, &arity, &tuple)){
    if(arity == 2){
      if(match_string(env, tuple[0], st)){
	b_putc(':', st);
	if(match_term(env, tuple[1], st)){
	  return 1;
	}
      }
    }
  }
  return 0;
}