From abdd070a7711a3e5d1e9e576a15e09b2b592d490 Mon Sep 17 00:00:00 2001 From: Scott Chacon Date: Tue, 25 May 2010 07:37:45 -0700 Subject: [PATCH] hex_to_raw actually works --- .gitignore | 1 + Makefile | 2 +- geef | 21 +++++++++++++++++++++ geef.c | 52 ++++++++++++++++------------------------------------ geef.erl | 4 ++-- test | 15 --------------- 6 files changed, 41 insertions(+), 54 deletions(-) create mode 100755 geef delete mode 100755 test diff --git a/.gitignore b/.gitignore index 9214bc1..dbd4f00 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.beam *.so *.o +*.swp diff --git a/Makefile b/Makefile index b8ffcb5..9ead2d5 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -OTPROOT=/Users/schacon/otp_src_R13B03 +OTPROOT=/Users/schacon/projects/otp INCLUDES = -I$(OTPROOT)/erts/emulator/beam/ # OS X flags. diff --git a/geef b/geef new file mode 100755 index 0000000..8e6d39c --- /dev/null +++ b/geef @@ -0,0 +1,21 @@ +#!/Users/schacon/projects/otp/bin/escript +%% -*- erlang -*- +%%! debug verbose -noshell + +main([Command|_Args]) -> + geef:start(), + case Command of + "h2r" -> + [Object|_Rest] = _Args, + Raw = geef:hex_to_raw(Object), + io:format("Raw ~w~n", [Raw]); + _Else -> + usage() + end; +main(_) -> + usage(). + +usage() -> + io:format("usage: ./geef h2r HEX_SHA\n"), + halt(1). + diff --git a/geef.c b/geef.c index 4d574e8..99cc3ba 100644 --- a/geef.c +++ b/geef.c @@ -3,52 +3,32 @@ #include #include - -static int -load(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_info) -{ - return 0; -} - -static int -reload(ErlNifEnv* env, void** priv, ERL_NIF_TERM load_info) -{ - return 0; -} - -static int -upgrade(ErlNifEnv* env, void** priv, void** old_priv, - ERL_NIF_TERM load_info) -{ - return 0; -} - -static void -unload(ErlNifEnv* env, void* priv) -{ - return; -} +#define MAXBUFLEN 1024 static ERL_NIF_TERM -object_exists(ErlNifEnv* env, ERL_NIF_TERM a1) +geef_hex_to_raw(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { + char sha[MAXBUFLEN]; + (void)memset(&sha, '\0', sizeof(sha)); + + if (enif_get_string(env, argv[0], sha, sizeof(sha), ERL_NIF_LATIN1) < 1) + return enif_make_badarg(env); + git_oid oid; - static char *sha = "ce08fe4884650f067bd5703b6a59a8b3b3c99a09"; + printf("sha: %s\n", sha); git_oid_mkstr(&oid, sha); - //printf("raw: %s", (&oid)->id); - unsigned long val; - if(!enif_get_ulong(env, a1, &val)) { - return enif_make_badarg(env); - } else { - return enif_make_ulong(env, val*2); - } + ErlNifBinary ibin; + enif_alloc_binary(env, 20, &ibin); + memcpy(ibin.data, (&oid)->id, 20); + + return enif_make_binary(env, &ibin); } static ErlNifFunc geef_funcs[] = { - {"object_exists", 1, object_exists} + {"hex_to_raw", 1, geef_hex_to_raw} }; -ERL_NIF_INIT(geef, geef_funcs, load, reload, upgrade, unload) +ERL_NIF_INIT(geef, geef_funcs, NULL, NULL, NULL, NULL) diff --git a/geef.erl b/geef.erl index f89bf03..22b3319 100644 --- a/geef.erl +++ b/geef.erl @@ -1,10 +1,10 @@ -module(geef). --export([start/0, object_exists/1]). +-export([start/0, hex_to_raw/1]). start() -> erlang:load_nif("geef", 0). -object_exists(_Val) -> +hex_to_raw(_Val) -> nif_error(?LINE). nif_error(Line) -> diff --git a/test b/test deleted file mode 100755 index a3459a4..0000000 --- a/test +++ /dev/null @@ -1,15 +0,0 @@ -#!/Users/schacon/otp_src_R13B03/bin/escript -%% -*- erlang -*- -%%! debug verbose -noshell - -main([Object]) -> - geef:start(), - Test = geef:object_exists(4), - io:format("exists ~s:~w~n", [Object, Test]); -main(_) -> - usage(). - -usage() -> - io:format("usage: ./test SHA\n"), - halt(1). -