Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Change api so that v4 returns binary representation and v4_str return…

…s human-friendly string output.
  • Loading branch information...
commit 5bcd50ab107e91890fbdab2a5798239cf5ff7663 1 parent 70a5b04
Kelly McLaughlin authored
Showing with 47 additions and 3 deletions.
  1. +42 −2 c_src/druid.c
  2. +5 −1 src/druuid.erl
44 c_src/druid.c
View
@@ -28,6 +28,8 @@ static ErlNifResourceType* druuid_RESOURCE;
// Prototypes
static ERL_NIF_TERM druuid_uuid_v4(ErlNifEnv* env, int argc,
const ERL_NIF_TERM argv[]);
+static ERL_NIF_TERM druuid_uuid_v4_str(ErlNifEnv* env, int argc,
+ const ERL_NIF_TERM argv[]);
static ERL_NIF_TERM error_tuple(ErlNifEnv* env,
ERL_NIF_TERM reason,
uuid_rc_t rc);
@@ -42,11 +44,49 @@ static ERL_NIF_TERM ATOM_UUID_DESTROY_FAILED;
static ErlNifFunc nif_funcs[] =
{
- {"v4", 0, druuid_uuid_v4}
+ {"v4", 0, druuid_uuid_v4},
+ {"v4_str", 0, druuid_uuid_v4_str}
};
static ERL_NIF_TERM druuid_uuid_v4(ErlNifEnv* env, int argc,
- const ERL_NIF_TERM argv[])
+ const ERL_NIF_TERM argv[])
+{
+ uuid_t *uuid;
+ uuid_rc_t rc;
+ void *vp = NULL;
+ size_t n;
+
+ if ((rc = uuid_create(&uuid)) != UUID_RC_OK)
+ {
+ return error_tuple(env, ATOM_UUID_CREATE_FAILED, rc);
+ }
+
+ if ((rc = uuid_make(uuid, UUID_MAKE_V4)) != UUID_RC_OK)
+ {
+ return error_tuple(env, ATOM_UUID_MAKE_FAILED, rc);
+ }
+
+ if ((rc = uuid_export(uuid, UUID_FMT_BIN, &vp, &n)) != UUID_RC_OK)
+ {
+ return error_tuple(env, ATOM_UUID_EXPORT_FAILED, rc);
+ }
+
+ ERL_NIF_TERM value_bin;
+ unsigned char* value = enif_make_new_binary(env, UUID_LEN_BIN, &value_bin);
+ memcpy(value, vp, UUID_LEN_BIN);
+
+ free(vp);
+
+ if ((rc = uuid_destroy(uuid)) != UUID_RC_OK)
+ {
+ return error_tuple(env, ATOM_UUID_DESTROY_FAILED, rc);
+ }
+
+ return value_bin;
+}
+
+static ERL_NIF_TERM druuid_uuid_v4_str(ErlNifEnv* env, int argc,
+ const ERL_NIF_TERM argv[])
{
uuid_t *uuid;
uuid_rc_t rc;
6 src/druuid.erl
View
@@ -25,7 +25,8 @@
-export([v1/0,
v2/0,
v3/0,
- v4/0]).
+ v4/0,
+ v4_str/0]).
-on_load(init/0).
@@ -62,6 +63,9 @@ v3() ->
v4() ->
erlang:nif_error({error, not_loaded}).
+v4_str() ->
+ erlang:nif_error({error, not_loaded}).
+
%% ===================================================================
%% EUnit tests
%% ===================================================================
Please sign in to comment.
Something went wrong with that request. Please try again.