Exemplo n.º 1
0
ERL_NIF_TERM open_calendar(ErlNifEnv* env, int argc, 
    const ERL_NIF_TERM argv[])
{
    ERL_NIF_TERM out;
    ErlNifBinary tz;

    cloner* res;
    UCalendar* cal;
    UCalendarType type = UCAL_DEFAULT;
    UErrorCode status = U_ZERO_ERROR;
    char type_atom[ATOM_LEN], locale[LOCALE_LEN];
    
    tz.size = 0; 
    tz.data = 0;

    switch (argc) {
        case 3:
            if (!enif_get_atom(env, argv[2], (char*) type_atom, ATOM_LEN,
                ERL_NIF_LATIN1))
                return enif_make_badarg(env);
            type = parserCalendarType((const char *) type_atom);
        case 2:
            if (!enif_inspect_binary(env, argv[1], &tz))
                return enif_make_badarg(env);
        case 1:
            if (!enif_get_atom(env, argv[0], (char*) locale, 
                    LOCALE_LEN, ERL_NIF_LATIN1))
                return enif_make_badarg(env);
        break;
        default:
            return enif_make_badarg(env);
    }


    /* get a calendar type */
    cal = ucal_open(
        (const UChar *) tz.data,
        (int32_t) tz.size,
        (const char *) locale,
        type,
        &status);
    CHECK(env, status);


    res = (cloner*) enif_alloc_resource(calendar_type, sizeof(cloner));
    if (calendar_open(cal, res)) {
        enif_release_resource(res);
        return enif_make_badarg(env);
    }
    out = enif_make_resource(env, res);
    enif_release_resource(res);
    /* resource now only owned by "Erlang" */
    return out;
}
Exemplo n.º 2
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;
}
Exemplo n.º 3
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;
}
Exemplo n.º 4
0
static ERL_NIF_TERM format_error(ErlNifEnv* env, int argc,
				 const ERL_NIF_TERM argv[])
{
  unsigned len;
  int res;

  if (!enif_get_atom_length(env, argv[0], &len, ERL_NIF_LATIN1))
    return enif_make_badarg(env);

  char *atom = malloc(len+1);
  if (!enif_get_atom(env, argv[0], atom, len+1, ERL_NIF_LATIN1)) {
    free(atom);
    return enif_make_badarg(env);
  }

  if (!strcmp("done", atom)) res = RS_DONE;
  else if (!strcmp("blocked", atom)) res = RS_BLOCKED;
  else if (!strcmp("running", atom)) res = RS_RUNNING;
  else if (!strcmp("test_skipped", atom)) res = RS_TEST_SKIPPED;
  else if (!strcmp("io_error", atom)) res = RS_IO_ERROR;
  else if (!strcmp("syntax_error", atom)) res = RS_SYNTAX_ERROR;
  else if (!strcmp("mem_error", atom)) res = RS_MEM_ERROR;
  else if (!strcmp("input_ended", atom)) res = RS_INPUT_ENDED;
  else if (!strcmp("bad_magic", atom)) res = RS_BAD_MAGIC;
  else if (!strcmp("unimplemented", atom)) res = RS_UNIMPLEMENTED;
  else if (!strcmp("corrupt", atom)) res = RS_CORRUPT;
  else if (!strcmp("internal_error", atom)) res = RS_INTERNAL_ERROR;
  else if (!strcmp("param_error", atom)) res = RS_PARAM_ERROR;
  else res = -1;

  free(atom);

  return enif_make_string(env, rs_strerror(res), ERL_NIF_LATIN1);
}
Exemplo n.º 5
0
static ERL_NIF_TERM
ex_slp_open(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
  char         *lang;
  char         *ret_code;
  int           isasync;
  unsigned int  term_len;
  SLPHandle    *hslp;
  SLPEXP SLPError SLPAPI error;

  if (enif_get_atom_length(env, argv[0], &term_len, ERL_NIF_LATIN1) < 0) {
    return enif_make_badarg(env);
  }

  if (enif_get_atom(env, argv[0], lang, term_len, ERL_NIF_LATIN1) < 0) {
    return enif_make_badarg(env);
  }

  if ( ! enif_get_int(env, argv[1], &isasync)) return enif_make_badarg(env);

  hslp = enif_alloc_resource(NIF_SLP_HANDLE, sizeof(SLPHandle));

  if (hslp == NULL) return enif_make_badarg(env);

  //error = SLPOpen(lang, isasync > 0 ? SLP_TRUE : SLP_FALSE, hslp);
  error = SLPOpen(lang, isasync > 0 ? SLP_TRUE : SLP_FALSE, hslp);

  ERL_NIF_TERM term = enif_make_resource(env, hslp);

  enif_release_resource(hslp);

  return enif_make_tuple2(env, enif_make_int(env, error), term);
}
Exemplo n.º 6
0
/* Get a collator */
ERL_NIF_TERM get_collator(ErlNifEnv* env, int argc, 
    const ERL_NIF_TERM argv[])
{
    ERL_NIF_TERM out;
    char locale[LOCALE_LEN];
    UErrorCode status = U_ZERO_ERROR;
    UCollator* col;
    cloner* res;
    unsigned int index;


    if ((argc != 2) && (argc != 1))
        return enif_make_badarg(env);

    if (!enif_get_atom(env, argv[0], (char*) locale, 
            LOCALE_LEN, ERL_NIF_LATIN1)) {
        return enif_make_badarg(env);
    }

    col = ucol_open((char *) locale, &status);
    CHECK(env, status);



    res = (cloner*) enif_alloc_resource(collator_type, sizeof(cloner));

    if (collator_open(col, res)) {
        enif_release_resource(res);
        return enif_make_badarg(env);
    }
    CHECK_DEST(env, status,
        enif_release_resource(res);
    );
Exemplo n.º 7
0
static ERL_NIF_TERM nif_get_json(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {

  gchar *path = get_char_argument(env, &argv[0]);
  if (!path)
    return enif_make_badarg(env);

  gchar include_content_atom[10];
  guint content_option = 0;
  if (argc == 2 && enif_get_atom(env, argv[1], include_content_atom, sizeof(include_content_atom), ERL_NIF_LATIN1)) {
    if (!g_ascii_strcasecmp(include_content_atom, "true"))
      content_option = 1;
    else if (!g_ascii_strcasecmp(include_content_atom, "raw"))
      content_option = 2;
  }

  GString *json_str = gmimex_get_json(path, content_option);
  g_free(path);

  if (!json_str)
    return enif_make_badarg(env);

  ErlNifBinary result_binary = {0};
  enif_alloc_binary(json_str->len, &result_binary);
  (void)memcpy(result_binary.data, json_str->str, json_str->len);

  g_string_free(json_str, TRUE);

  return enif_make_binary(env, &result_binary);

}
Exemplo n.º 8
0
ERL_NIF_TERM locale_language_tag(ErlNifEnv* env, int argc, 
    const ERL_NIF_TERM argv[])
{
    UErrorCode status = U_ZERO_ERROR;

    int32_t /*value_len,*/     key_len; 
    char    value[LOCALE_LEN], key[LOCALE_LEN];

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

    key_len = enif_get_atom(env, argv[0], (char*) key, 
                    LOCALE_LEN, ERL_NIF_LATIN1);

    if (!key_len) {
        return enif_make_badarg(env);
    }

    
    /*value_len =*/ uloc_toLanguageTag((const char*) key, /* Locale Id */
        (char *)  value, /* Name */
        (int32_t) LOCALE_LEN,
        FALSE,
        &status);
    CHECK(env, status);

    return enif_make_atom(env, value);
}
Exemplo n.º 9
0
static ERL_NIF_TERM do_date_get_field(ErlNifEnv* env, UCalendar* cal,
    const ERL_NIF_TERM field_atom, UErrorCode& status)
{
    char value[ATOM_LEN];
    int parsed_value, amount;
    UCalendarDateFields field;

    if (!enif_get_atom(env, field_atom, (char*) value, ATOM_LEN,
            ERL_NIF_LATIN1)) {
        status = U_ILLEGAL_ARGUMENT_ERROR;
        return 0;
    }

    parsed_value = parseCalendarDateField(value);
    if (parsed_value == -1) {
        status = U_ILLEGAL_ARGUMENT_ERROR;
        return 0;
    }

    field = (UCalendarDateFields) parsed_value;

    amount = (int) ucal_get(cal, field, &status);
    if (U_FAILURE(status))
        return 0;

    if (field == UCAL_MONTH)
        amount++; /* month from 0 */

    return enif_make_int(env, amount);
}
Exemplo n.º 10
0
ERL_NIF_TERM case_compare(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary in, in2;
    char locale[LOCALE_LEN];
    UErrorCode status = U_ZERO_ERROR;
    uint32_t options = U_COMPARE_IGNORE_CASE;
    int32_t res;

    if (argc != 3)
        return enif_make_badarg(env);

    if (!(enif_get_atom(env, argv[0], locale, LOCALE_LEN, ERL_NIF_LATIN1)
       && enif_inspect_binary(env, argv[1], &in)
       && enif_inspect_binary(env, argv[2], &in2))) {
        return enif_make_badarg(env);
    }

    /* Is the locale tr? */
    if ((locale[0] == 't') && (locale[1] == 'r')) 
        options |= U_FOLD_CASE_EXCLUDE_SPECIAL_I;

    /*res = u_strCaseCompare(*/
    res = unorm_compare(
            (const UChar *) in.data,
            TO_ULEN(in.size),
            (const UChar *) in2.data,
            TO_ULEN(in2.size),
            options,
            &status);
    CHECK(env, status);

    return compare_result_to_atom(env, res);
}
Exemplo n.º 11
0
jsval
to_js_special(ErlNifEnv* env, JSContext* cx, ERL_NIF_TERM term)
{
    JSString* str = NULL;
    char atom[512]; // Pretty sure there's a 256 byte limit

    if(!enif_get_atom(env, term, atom, 512, ERL_NIF_LATIN1))
    {
        return JSVAL_VOID;
    }
    else if(strcmp(atom, "true") == 0)
    {
        return JSVAL_TRUE;
    }
    else if(strcmp(atom, "false") == 0)
    {
        return JSVAL_FALSE;
    }
    else if(strcmp(atom, "null") == 0)
    {
        return JSVAL_NULL;
    }
    else
    {
        str = JS_NewStringCopyZ(cx, atom);
        if(str == NULL) return JSVAL_VOID;
        return STRING_TO_JSVAL(str);
    }
}
Exemplo n.º 12
0
static inline int
match_atom(ErlNifEnv* env, ERL_NIF_TERM term, State *st){
  if(!enif_is_atom(env, term)){
    return 0;
  }
  
  if(enif_is_identical(term, st->priv->am_true)){
    b_puts(4, "true", st);
    return 1;
  }else if(enif_is_identical(term, st->priv->am_false)){
    b_puts(5, "false", st);
    return 1;
  }else if(enif_is_identical(term, st->priv->am_null)){
    b_puts(4, "null", st);
    return 1;
  }
  
  unsigned len, reserve;
  b_reserve(256 + 2, st);
  unsigned char *p = st->cur;
  if((len = enif_get_atom(env, term, (char*)p + 1, 256U, ERL_NIF_LATIN1))){
    *p = '"';
    if(!check_str_for_json((unsigned char*)p + 1, len-1, &reserve)){
      return 0;
    }
    if(reserve > 0){
      b_reserve(len + reserve + 2, st);
      extend_str_to_jstr((unsigned char*)p + 1, len-1, reserve);
    }
    st->cur += (len + reserve);
    b_putc('"', st);
    return 1;
  }
  return 0;
}
Exemplo n.º 13
0
    int
notify_hints_type(ErlNifEnv *env, NotifyNotification *notify, int arity, ERL_NIF_TERM key, ERL_NIF_TERM value)
{
    char a_key[256] = {0};

    union {
        ErlNifBinary b;
        int i;
        double d;
    } val;

    const ERL_NIF_TERM *byte = NULL;
    char a_byte[256] = {0};
    int len = 0;

    if (!enif_get_atom(env, key, a_key, sizeof(a_key), ERL_NIF_LATIN1))
        return -1;

    if (enif_get_int(env, value, &val.i))
        notify_notification_set_hint_int32(notify, a_key, val.i);
    else if (enif_get_double(env, value, &val.d))
        notify_notification_set_hint_double(notify, a_key, val.d);
    else if (enif_get_tuple(env, value, &len, &byte)) {
        if (len != 2 ||
                !enif_get_atom(env, byte[0], a_byte, sizeof(a_byte), ERL_NIF_LATIN1) ||
                (strcmp(a_byte, "byte") != 0) ||
                !enif_get_int(env, byte[1], &val.i))
            return -1;
        notify_notification_set_hint_byte(notify, a_key, (u_int8_t)val.i);
    }
    else if (arity == 0 || enif_inspect_iolist_as_binary(env, value, &val.b)) {
        gchar *tmpstr = NULL;

        if (arity > 0)
            tmpstr = stralloc(&val.b);

        notify_notification_set_hint_string(notify, a_key,
                (arity > 0 ? "" : tmpstr));

        strfree(tmpstr);
    }
    else
        return -1;

    return 0;
}
Exemplo n.º 14
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);
}
Exemplo n.º 15
0
ERL_NIF_TERM date_clear(ErlNifEnv* env, int argc, 
    const ERL_NIF_TERM argv[])
{
    UErrorCode status = U_ZERO_ERROR;
    UCalendar* cal;
    cloner* ptr;
    double date;

    UCalendarDateFields field;
    ERL_NIF_TERM head, tail;
    unsigned int count, i = 0;

    char    value[ATOM_LEN];
    int     parsed_value;

    if(!((argc == 3)
      && enif_get_resource(env, argv[0], calendar_type, (void**) &ptr)  
      && enif_get_double(env, argv[1], &date)
      && enif_get_list_length(env, argv[2], &count))) {
        return enif_make_badarg(env);
    }

    cal = (UCalendar*) cloner_get(ptr);
    CHECK_RES(env, cal);

    ucal_setMillis(cal, (UDate) date, &status);
    CHECK(env, status);

    tail = argv[2];
    while (enif_get_list_cell(env, tail, &head, &tail)) {

            /* Set an attribute start */

            if (!enif_get_atom(env, head, (char*) value, 
                    ATOM_LEN, ERL_NIF_LATIN1)) 
                goto bad_elem;
                
            parsed_value = parseCalendarDateField(value);
            if ((parsed_value == -1)) 
                goto bad_elem;

            field = (UCalendarDateFields) parsed_value;
 
            ucal_clearField(cal, field);

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

    }

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

    bad_elem:
        return list_element_error(env, argv[2], i);
}
Exemplo n.º 16
0
/* Get a transliterator */
ERL_NIF_TERM get_transliterator(ErlNifEnv* env, int argc, 
    const ERL_NIF_TERM argv[])
{
    ERL_NIF_TERM out;
    char id[LOCALE_LEN], dir[ATOM_LEN];
    UErrorCode status = U_ZERO_ERROR;
    UTransliterator* obj;
    cloner* res;
    int parsed_dir;


    if (argc != 2)
        return enif_make_badarg(env);

    if (!(enif_get_atom(env, argv[0], (char*) id, LOCALE_LEN, ERL_NIF_LATIN1)
       && enif_get_atom(env, argv[1], (char*) dir, ATOM_LEN, ERL_NIF_LATIN1)
        )) {
        return enif_make_badarg(env);
    }

    parsed_dir = parseDir(dir);
    if ((parsed_dir == -1)) 
        return enif_make_badarg(env);

    obj = utrans_open((char *) id, (UTransDirection) parsed_dir, 
            NULL, 0, NULL, &status);
    CHECK(env, status);



    res = (cloner*) enif_alloc_resource(trans_type, sizeof(cloner));

    if (trans_open(obj, res)) {
        enif_release_resource(res);
        return enif_make_badarg(env);
    }

    out = enif_make_resource(env, res);
    enif_release_resource(res);
    /* resource now only owned by "Erlang" */
    return out;
}
Exemplo n.º 17
0
static void *TestCall(void *ptr) {
  TCall *call = (TCall *)ptr;
  ERL_NIF_TERM result = ErlangCall(call->env, call->fun, call->args);
  int length;
  char *buffer;
  enif_get_atom_length(call->env, result, &length, ERL_NIF_LATIN1);
  buffer = (char *)malloc((length + 1) * sizeof(char));
  enif_get_atom(call->env, result, buffer, length + 1, ERL_NIF_LATIN1);
  printf("THIS IS YOUR RESULT(%d)! %s\n", length, buffer);

  return NULL;
}
Exemplo n.º 18
0
static ERL_NIF_TERM atom_to_bin(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    ErlNifBinary obin;
    unsigned size;
    int n;
    if (!enif_get_int(env,argv[1],(int*)&size) 
	|| !enif_alloc_binary(size,&obin)) {
	return enif_make_badarg(env);
    }
    n = enif_get_atom(env, argv[0], (char*)obin.data, size, ERL_NIF_LATIN1);
    return enif_make_tuple(env, 2, enif_make_int(env,n),
			   enif_make_binary(env,&obin));
}
Exemplo n.º 19
0
/**
 * Erlang Wrapper for  geonum_neighbor
 */
ERL_NIF_TERM
erl_geonum_neighbor(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    long long bin_neighbor[1];
    long long geonum;
    char dir[2];

    int dir_val;
    unsigned int dir_len;

    if(!enif_get_int64(env, argv[0], &geonum)) {
        return enif_make_badarg(env);
    }

    if(!enif_get_atom_length(env, argv[1], &dir_len, ERL_NIF_LATIN1))
    {
        return enif_make_badarg(env);
    }

    if(dir_len > sizeof(dir)) {
        return enif_make_badarg(env);
    }

    if(!enif_get_atom(env, argv[1], dir, sizeof(dir), ERL_NIF_LATIN1))
    {
        return enif_make_badarg(env);
    }

    switch (dir[0]) {
        case 'w':
            dir_val = 0;
            break;
        case 'e':
            dir_val = 1;
            break;
        case 'n':
            dir_val = 2;
            break;
        case 's':
            dir_val = 3;
            break;
        default:
            return enif_make_badarg(env);
    }


    geonum_neighbor(geonum, dir_val, bin_neighbor);

    return make_ok(env, enif_make_int64(env, bin_neighbor[0]));
}
Exemplo n.º 20
0
ERL_NIF_TERM to_title(ErlNifEnv* env, int argc, 
    const ERL_NIF_TERM argv[])
{
    ErlNifBinary in, out;
    int32_t ulen; 
    char locale[LOCALE_LEN];
    const char* locptr;
    cloner* ptr; 
    UBreakIterator* iter; 
    UErrorCode status = U_ZERO_ERROR;

    if (argc != 2)
        return enif_make_badarg(env);

    /* Second argument must be a binary */
    if(!enif_inspect_binary(env, argv[1], &in)) {
        return enif_make_badarg(env);
    }

    if (enif_get_atom(env, argv[0], locale, LOCALE_LEN, ERL_NIF_LATIN1)) {
        /* First element is an atom. */
        locptr = (const char*) locale;
        iter = NULL;

    } else if (enif_get_resource(env, argv[0], iterator_type, (void**) &ptr)) {
        /* First element is an iterator. */
        iter = (UBreakIterator*) cloner_get(ptr);
        CHECK_RES(env, iter);
            

        /* Iterator contains a locale name. Extract it. */
        locptr = ubrk_getLocaleByType((const UBreakIterator*) iter,
            ULOC_ACTUAL_LOCALE, /* locale type */
            &status);

        CHECK(env, status);
    } else {
        return enif_make_badarg(env);
    }  

    ulen = TO_ULEN(in.size);
    do_to_title(in, out, ulen, iter, locptr, status);
    if (status == U_BUFFER_OVERFLOW_ERROR) {
        do_to_title(in, out, ulen, iter, locptr, status);
    }
    CHECK(env, status);
    return enif_make_binary(env, &out);

}
Exemplo n.º 21
0
    static ERL_NIF_TERM
nif_socket_protocol(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
    char buf[256] = {0};
    const struct procket_define *p = NULL;

    if (!enif_get_atom(env, argv[0], buf, sizeof(buf), ERL_NIF_LATIN1))
        return enif_make_badarg(env);

    for (p = procket_socket_protocol; p->key != NULL; p++) {
        if (!strcmp(buf, p->key))
            return enif_make_int(env, p->val);
    }

    return atom_undefined;
}
Exemplo n.º 22
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;
}
Exemplo n.º 23
0
static
int exmagick_get_boolean_u (ErlNifEnv *env, ERL_NIF_TERM arg, unsigned int *p)
{
  int ecode;
  char atom[EXM_MAX_ATOM_SIZE];
  if (0 != (ecode = enif_get_atom(env, arg, atom, EXM_MAX_ATOM_SIZE, ERL_NIF_LATIN1)))
  {
    if (strcmp("true", atom) == 0)
    { *p = 1; }
    else if (strcmp("false", atom) == 0)
    { *p = 0; }
    else
    { ecode = 0; }
  }

  return(ecode);
}
Exemplo n.º 24
0
static int atom_to_string(ErlNifEnv* env, ERL_NIF_TERM atom_term, char **bin_str)
{
  unsigned atom_len;
  char *atom_string;
  
  *bin_str = NULL;

  if(!enif_is_atom(env, atom_term) || !enif_get_atom_length(env, atom_term, &atom_len, ERL_NIF_LATIN1)) return 0;
  if(!(atom_string  = (char*)malloc(atom_len+1))) return 0;
  if(!enif_get_atom(env, atom_term, atom_string, atom_len+1, ERL_NIF_LATIN1)){
    free(atom_string);
    return 0;
  }

  *bin_str = atom_string;
  return 1;
}
Exemplo n.º 25
0
ERL_NIF_TERM date_diff_field(ErlNifEnv* env, int argc, 
    const ERL_NIF_TERM argv[])
{
    UErrorCode status = U_ZERO_ERROR;
    UCalendar* cal;
    cloner* ptr;
    double startMs, targetMs;

    char    value[ATOM_LEN];
    int     parsed_value, amount;
    UCalendarDateFields field;


    if(!((argc == 4)
      && enif_get_resource(env, argv[0], calendar_type, (void**) &ptr)  
      && enif_get_double(env, argv[1], &startMs)
      && enif_get_double(env, argv[2], &targetMs)
      && enif_get_atom(env, argv[3], (char*) value, ATOM_LEN, 
            ERL_NIF_LATIN1))) {
        return enif_make_badarg(env);
    }

    cal = (UCalendar*) cloner_get(ptr);
    CHECK_RES(env, cal);

    ucal_setMillis(cal, (UDate) startMs, &status);
    CHECK(env, status);



    parsed_value = parseCalendarDateField(value);
    if (parsed_value == -1) {
        status = U_ILLEGAL_ARGUMENT_ERROR;
        CHECK(env, status);
    }

    field = (UCalendarDateFields) parsed_value;

    amount = (int) dateFieldDifference(cal, 
        targetMs, 
        field, 
        status);
    CHECK(env, status);

    return enif_make_int(env, amount);
}
Exemplo n.º 26
0
Arquivo: serctl.c Projeto: jj1bdx/srly
    static ERL_NIF_TERM
nif_constant(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
    char buf[255] = {0};
    const struct SERCTL_DEF *p = NULL;


    if (enif_get_atom(env, argv[0], buf, sizeof(buf), ERL_NIF_LATIN1) < 1)
        return enif_make_badarg(env);

    for (p = serctl_const; p->key != NULL; p++) {
        if (!strcmp(buf, p->key))
            return enif_make_uint(env, p->val);
    }

    return atom_undefined;
}
Exemplo n.º 27
0
ERL_NIF_TERM exmagick_get_attr (ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
  char atom[EXM_MAX_ATOM_SIZE];
  exm_resource_t *resource;

  EXM_INIT;
  ErlNifResourceType *type = (ErlNifResourceType *) enif_priv_data(env);

  if (0 == enif_get_resource(env, argv[0], type, (void **) &resource))
  { EXM_FAIL(ehandler, "invalid handle"); }

  if (0 == enif_get_atom(env, argv[1], atom, EXM_MAX_ATOM_SIZE, ERL_NIF_LATIN1))
  { EXM_FAIL(ehandler, "invalid attribute"); }

  if (strcmp("adjoin", atom) == 0)
  {
    if (resource->i_info->adjoin == 0)
    { return(enif_make_tuple2(env, enif_make_atom(env, "ok"), enif_make_atom(env, "false"))); }
    else if (resource->i_info->adjoin == 1)
    { return(enif_make_tuple2(env, enif_make_atom(env, "ok"), enif_make_atom(env, "true"))); }
  }
  if (strcmp("rows", atom) == 0)
  {
    if (resource->image == NULL)
    { EXM_FAIL(ehandler, "image not loaded"); }

    { return(enif_make_tuple2(env, enif_make_atom(env, "ok"), enif_make_long(env, resource->image->rows))); }
  }
  if (strcmp("columns", atom) == 0)
  {
    if (resource->image == NULL)
    { EXM_FAIL(ehandler, "image not loaded"); }

    { return(enif_make_tuple2(env, enif_make_atom(env, "ok"), enif_make_long(env, resource->image->columns))); }
  }
  if (strcmp("density", atom) == 0)
  { return(enif_make_tuple2(env, enif_make_atom(env, "ok"), exmagick_make_utf8str(env, resource->i_info->density))); }
  else if (strcmp("magick", atom) == 0)
  { return(enif_make_tuple2(env, enif_make_atom(env, "ok"), exmagick_make_utf8str(env, resource->image->magick))); }

ehandler:
  return(enif_make_tuple2(env, enif_make_atom(env, "error"), exmagick_make_utf8str(env, errmsg)));
}
Exemplo n.º 28
0
static
ERL_NIF_TERM exmagick_set_attr (ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
  ErlNifBinary utf8;
  char atom[EXM_MAX_ATOM_SIZE];
  exm_resource_t *resource;

  EXM_INIT;
  ErlNifResourceType *type = (ErlNifResourceType *) enif_priv_data(env);

  if (0 == enif_get_resource(env, argv[0], type, (void **) &resource))
  { EXM_FAIL(ehandler, "invalid handle"); }

  if (0 == enif_get_atom(env, argv[1], atom, EXM_MAX_ATOM_SIZE, ERL_NIF_LATIN1))
  { EXM_FAIL(ehandler, "argv[1]: bad argument"); }

  if (strcmp("adjoin", atom) == 0)
  {
    if (0 == exmagick_get_boolean_u(env, argv[2], &resource->i_info->adjoin))
    { EXM_FAIL(ehandler, "argv[2]: bad argument"); }
  }
  if (strcmp("magick", atom) == 0)
  {
    if (0 == exmagick_get_utf8str(env, argv[2], &utf8))
    { EXM_FAIL(ehandler, "argv[2]: bad argument"); }
    exmagick_utf8strcpy(resource->image->magick, &utf8, MaxTextExtent);
  }
  if (strcmp("density", atom) == 0)
  {
    if (0 == exmagick_get_utf8str(env, argv[2], &utf8))
    { EXM_FAIL(ehandler, "argv[2]: bad argument"); }
    MagickFree(resource->i_info->density);
    resource->i_info->density=exmagick_utf8strdup(&utf8);
    if (resource->i_info->density == NULL)
    { EXM_FAIL(ehandler, "could not set density"); }
  }

  return(enif_make_tuple2(env, enif_make_atom(env, "ok"), argv[0]));

ehandler:
  return(enif_make_tuple2(env, enif_make_atom(env, "error"), exmagick_make_utf8str(env, errmsg)));
}
Exemplo n.º 29
0
static inline int
match_atom_as_string(ErlNifEnv* env, ERL_NIF_TERM term, State *st){
  unsigned len, reserve;
  b_reserve(256 + 2, st);
  unsigned char *p = st->cur;
  if((len = enif_get_atom(env, term, (char*)p + 1, 256U, ERL_NIF_LATIN1))){
    *p = '"';
    if(!check_str_for_json((unsigned char*)p + 1, len-1, &reserve)){
      return 0;
    }
    if(reserve > 0){
      b_reserve(len + reserve + 2, st);
      extend_str_to_jstr((unsigned char*)p + 1, len-1, reserve);
    }
    st->cur += (len + reserve);
    b_putc('"', st);
    return 1;
  }
  return 0;
}
Exemplo n.º 30
0
// Creates a new property list as an instance of a property list class.
ERL_NIF_TERM h5pcreate(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
  hid_t dcpl_id; // dataset creation property list
  Handle* res;
  ERL_NIF_TERM ret;
  unsigned cls_id;
  char cls[MAXBUFLEN];

  // parse arguments
  check(argc == 1, "Incorrent number of arguments");
  check(enif_get_atom(env, argv[0], cls, sizeof(cls), ERL_NIF_LATIN1) != 0, \
	"Can't get class id from argv");

  // convert access flag to format which hdf5 api understand
  check(convert_property_flag(cls, &cls_id) == 0, "Failed to convert access flag");

  // create a new file using default properties
  dcpl_id = H5Pcreate(cls_id);
  check(dcpl_id > 0, "Failed to create property list.");

  // create a resource to pass reference to id back to erlang
  res = enif_alloc_resource(RES_TYPE, sizeof(Handle));
  check(res, "Failed to allocate resource for type %s", "Handle");

  // add ref to resource
  res->id = dcpl_id;
  ret = enif_make_resource(env, res);

  // cleanup
  enif_release_resource(res);

  return enif_make_tuple2(env, ATOM_OK, ret);

 error:
  if(dcpl_id) H5Pclose(dcpl_id);

  return error_tuple(env, "Can not create properties list");
};