aboutsummaryrefslogtreecommitdiffstats
path: root/system/doc/tutorial/ei.c
diff options
context:
space:
mode:
Diffstat (limited to 'system/doc/tutorial/ei.c')
-rw-r--r--system/doc/tutorial/ei.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/system/doc/tutorial/ei.c b/system/doc/tutorial/ei.c
new file mode 100644
index 0000000000..b234a00768
--- /dev/null
+++ b/system/doc/tutorial/ei.c
@@ -0,0 +1,38 @@
+/* ei.c */
+
+#include "erl_interface.h"
+#include "ei.h"
+
+typedef unsigned char byte;
+
+int main() {
+ ETERM *tuplep, *intp;
+ ETERM *fnp, *argp;
+ int res;
+ byte buf[100];
+ long allocated, freed;
+
+ erl_init(NULL, 0);
+
+ while (read_cmd(buf) > 0) {
+ tuplep = erl_decode(buf);
+ fnp = erl_element(1, tuplep);
+ argp = erl_element(2, tuplep);
+
+ if (strncmp(ERL_ATOM_PTR(fnp), "foo", 3) == 0) {
+ res = foo(ERL_INT_VALUE(argp));
+ } else if (strncmp(ERL_ATOM_PTR(fnp), "bar", 17) == 0) {
+ res = bar(ERL_INT_VALUE(argp));
+ }
+
+ intp = erl_mk_int(res);
+ erl_encode(intp, buf);
+ write_cmd(buf, erl_term_len(intp));
+
+ erl_free_compound(tuplep);
+ erl_free_term(fnp);
+ erl_free_term(argp);
+ erl_free_term(intp);
+ }
+}
+