erlang c erl_interface

相关文档http://www.erlang.org/doc/tutorial/erl_interface.html

http://www.erlang.org/doc/apps/erl_interface/ei_users_guide.html

(1),字符串被截断问题

片段说明: 

输入[1,2,0,0,2,3] 

149行会返回[1,2,0,0.2,3]

150行会返回[1,2] 0认为是结束符号,所以截断了

----

 

149     intp = erl_mk_estring(estring,i);

150     //intp = erl_mk_string(estring);

ETERM * erl_mk_estring(string, len)

Types:

char *string;
int len;

 

This function creates a list from a sequence of bytes.

string is a buffer containing a sequence of bytes. The buffer does not need to be zero-terminated.

len is the length of string.

The function returns an Erlang list object corresponding to the character sequence in string.

 

 

 

(2)、   tuplep = erl_decode(buf);

      tuplep第一个元素模认是方法名称,后面依次是参数。如:foo(Name,Age)<--->{foo,Name,Age}

 

(3)、关于ETERM 及部分类型

char *fname=(fnp)->uval.aval.a; 等价于char *fname=ERL_ATOM_PTR(fnp).此处只是一个宏定义

ETERM数据类型是一个结构体,内容又是一个联合体,联合体里面又是结构体...

如果是整型则用ERL_INT_VALUE(fnp);

(4)、采用erl_hd/1|erl_tl/1 或 ERL_CONS_HEAD/1|ERL_CONS_TAIL/1遍历list 

 

 60             ETERM *headH;

 61             int headHInt;

 62             while(erl_length(argp2)){

 63                 headH = erl_hd(argp2); 

 64                 argp2 = erl_tl(argp2); 

 65                 headHInt= ERL_INT_VALUE(headH); 

 66                 fprintf(stderr, "headHInt=%i\n\r", headHInt);

 67                 estring[i]=headHInt;

 68                 fprintf(stderr, "headHInt=%c\n\r", headHInt);

 69                 i++;

 70             }

(5)如果是字符串可以用erl_iolist_to_string

     binary 用 erl_iolist_to_binary(term)

(6)构造 list erl_cons(head, tail)
(7)pattern1和match
113     ETERM *pattern1,*pattern,*term, *ep;
114     pattern = erl_format("{Name,Age,Age}");
115     pattern1 = erl_format("Pa");
116     term    = erl_format("{madonna,21,21}");
117     if(erl_match(pattern1, term)){
118         ep = erl_var_content(pattern, "Age"); %%取出匹配值
119         //int a = ((ep)->uval.ival.i;
120         //*ep.uval.tval;
121         //fprintf(stderr, "A = %i\n\r",a);
122         fprintf(stderr, "Pa =a\n\r ");
123         erl_print_term(stderr, ep);
124     }

学习过程测试代码

 

Java代码   收藏代码
  1.   1 /* ei.c */                                                                                                                                          
  2.   2 #include "erl_interface.h"  
  3.   3 #include "ei.h"  
  4.   4 #include "stdio.h"  
  5.   5 typedef unsigned char byte;  
  6.   6 int main() {  
  7.   7   ETERM *tuplep, *intp;  
  8.   8   ETERM *fnp, *argp, *argp1, *argp2;  
  9.   9   ETERM testt;  
  10.  10   int res;  
  11.  11   byte buf[100];  
  12.  12   long allocated, freed;  
  13.  13   erl_init(NULL, 0);  
  14.  14   while (read_cmd(buf) > 0) {  
  15.  15     tuplep = erl_decode(buf);  
  16.  16     fnp = erl_element(1, tuplep);  
  17.  17     argp = erl_element(2, tuplep);  
  18.  18     int tuplesize=ERL_TUPLE_SIZE(tuplep);  
  19.  19     fprintf(stderr, "tuplesize=%i\n\r", tuplesize);  
  20.  20     if (strncmp(ERL_ATOM_PTR(fnp), "foo"3) == 0) {  
  21.  21         res = foo(ERL_INT_VALUE(argp));  
  22.  22     } else if (strncmp(ERL_ATOM_PTR(fnp), "bar"17) == 0) {  
  23.  23         int a = ((argp)->uval.ival.i);  
  24.  24         //a = ((argp)->uval.llval.i);  
  25.  25         char *fname=(fnp)->uval.aval.a;  
  26.  26         //a = testt.uval.ival.i;  
  27.  27         char *arg2;  
  28.  28         if(tuplesize==3){  
  29.  29             argp1 = erl_element(3, tuplep);  
  30.  30             arg2=ERL_ATOM_PTR(argp1);  
  31.  31             fprintf(stderr, "A = %i,arg2=%s\n\r",a,arg2);  
  32.  32         }else if(tuplesize=4){  
  33.  33             char *arg3;  
  34.  34             argp2 = erl_element(4, tuplep);  
  35.  35             arg3=ERL_ATOM_PTR(argp2);  
  36.  36             int atomsize=ERL_ATOM_SIZE(argp2);  
  37.  37             int ioListSize =  erl_iolist_length(argp2);  
  38.  38             int listSize = erl_length(argp2);  
  39.   
  40. 39             fprintf(stderr, "\n\rioListSize=%i\n\r", ioListSize);  
  41.  40             fprintf(stderr, "\n\rlistSize=%i\n\r", listSize);  
  42.  41             int isAtom = ERL_IS_ATOM(argp2);  
  43.  42             int isList = ERL_IS_LIST(argp2);  
  44.  43             int isBinary = ERL_IS_BINARY(argp2);  
  45.  44             ETERM *ioListBin = erl_iolist_to_binary(argp2);  
  46.  45             char *ioListString = erl_iolist_to_string(argp2);  
  47.  46             fprintf(stderr, "\n\rioListBin=");  
  48.  47             erl_print_term(stderr, ioListBin);  
  49.  48             fprintf(stderr, "\n\riargp2=");  
  50.  49             erl_print_term(stderr, argp2);  
  51.  50             fprintf(stderr, "\n\rioListString=%s\n\r",ioListString);  
  52.  51             fprintf(stderr, "A = %i,arg3=%s,atomsize=%i,isAtom=%i,isList=%i,isBinary=%i\n\r",  
  53.  52                     a,arg3,atomsize,isAtom,isList,isBinary);  
  54.  53             ETERM *listhss = erl_hd(argp2);  
  55.  54             int lh = ERL_INT_VALUE(listhss);  
  56.  55             fprintf(stderr, "lh=%i\n\r",lh);  
  57.  56   
  58.  57             ETERM *headH;  
  59.  58             while(erl_length(argp2)){  
  60.  59                 headH = erl_hd(argp2);  
  61.  60                 argp2 = erl_tl(argp2);  
  62.  61                 int headHInt = ERL_INT_VALUE(headH);  
  63.  62                 fprintf(stderr, "headHInt=%i\n\r", headHInt);  
  64.  63                 fprintf(stderr, "headHInt=%c\n\r", headHInt);  
  65.  64             }  
  66.  65   
  67.  66             fprintf(stderr, "\n\r");  
  68.  67             fprintf(stderr, "\n\r");  
  69.  68   
  70.  69   
  71.  70   
  72.  71 //  operator  
  73.  72             int erl_siz;  
  74.  73             erl_siz=erl_size(tuplep);  
  75.  74             ETERM *list   = erl_mk_empty_list();  
  76.  75             ETERM *anAtom,*anInt;  
  77. 76             anAtom = erl_mk_atom("langxw");  
  78.  77             anInt = erl_mk_int(26);  
  79.  78             list = erl_cons(anAtom, list);  
  80.  79             list = erl_cons(anInt, list);  
  81.  80             list = erl_cons(anAtom, list);  
  82.  81             int listsize = erl_length(list);  
  83.  82             ETERM *listh = erl_hd(list);  
  84.  83             ETERM *listt = erl_tl(list);  
  85.  84             ETERM *listh1 = ERL_CONS_HEAD(list);  
  86.  85             ETERM *listt1 = ERL_CONS_TAIL(list);  
  87.  86             int listhint = ERL_INT_VALUE(listh);  
  88.  87             int listhint1 = ERL_INT_VALUE(listh1);  
  89.  88             fprintf(stderr, "erl_size=%i,listsize=%i,listhint=%i\n\r", erl_siz,listsize,listhint);  
  90.  89             erl_print_term(stderr,listt);  
  91.  90             fprintf(stderr, "erl_size=%i,listsize=%i,listhint1=%i\n\r", erl_siz,listsize,listhint1);  
  92.  91             erl_print_term(stderr,listt1);  
  93.  92             erl_free_compound(list);  
  94.  93   
  95.  94         }  
  96.  95   
  97.  96         fprintf(stderr, "A = %i,fname=%s\n\r",a,fname);  
  98.  97         res = bar(ERL_INT_VALUE(argp));  
  99.  98     }  
  100.  99 /** 
  101. 100     ETERM *ep; 
  102. 101     ep = erl_format("[{name,~a},{age,~i},{data,~w}]", 
  103. 102                    "madonna", 
  104. 103                    21, 
  105. 104                    erl_format("[{adr,~s,~i}]","E-street",42)); 
  106. 105     fprintf(stderr, "s=%i\n\r",); 
  107. 106 */  
  108. 107     ETERM *pattern1,*pattern,*term, *ep;  
  109. 108     pattern = erl_format("{Name,Age,Age}");  
  110. 109     pattern1 = erl_format("Pa");  
  111. 110     term    = erl_format("{madonna,21,21}");  
  112. 111     if(erl_match(pattern1, term)){  
  113. 112         ep = erl_var_content(pattern, "Age");  
  114.  112         ep = erl_var_content(pattern, "Age");  
  115. 113         //int a = ((ep)->uval.ival.i;  
  116. 114         //*ep.uval.tval;  
  117. 115         //fprintf(stderr, "A = %i\n\r",a);  
  118. 116         fprintf(stderr, "Pa = ");  
  119. 117         erl_print_term(stderr, ep);  
  120. 118     }  
  121. 119 /**  
  122. 120     if (erl_match(pattern, term)) {  
  123. 121       fprintf(stderr, "Yes, they matched: Age = ");  
  124. 122       ep = erl_var_content(pattern, "Age");   
  125. 123       fprintf(stderr, "Yes, they matched: Name = ");  
  126. 124       erl_print_term(stderr, ep);  
  127. 125       ep = erl_var_content(pattern, "Name");   
  128. 126       erl_print_term(stderr, ep);  
  129. 127       fprintf(stderr,"\n\r");  
  130. 128       erl_free_term(ep);  
  131. 129     }  
  132. 130 */  
  133. 131     erl_free_term(pattern);  
  134. 132     erl_free_term(term);  
  135. 133   
  136. 134   
  137. 135   
  138. 136   
  139. 137   
  140. 138   
  141. 139   
  142. 140   
  143. 141     intp = erl_mk_int(res);  
  144. 142     erl_encode(intp, buf);  
  145. 143     write_cmd(buf, erl_term_len(intp));  
  146. 144     erl_free_compound(tuplep);  
  147. 145     erl_free_term(fnp);  
  148. 146     erl_free_term(argp);  
  149. 147     erl_free_term(intp);  
  150. 148   }  
  151. 149  }  
 

 

 

 

Erlang代码   收藏代码
  1.  1 -module(complex2).                                                                                                                                  
  2.  2 -export([start/1, stop/0, init/1]).  
  3.  3 -export([foo/1,  
  4.  4          bar/2,             
  5.  5          bar/3,  
  6.  6          bar/1]).  
  7.  7 start(ExtPrg) ->            
  8.  8     spawn(?MODULE, init, [ExtPrg]).  
  9.  9 stop() ->                   
  10. 10     complex ! stop.         
  11. 11 foo(X) ->  
  12. 12     call_port({foo, X}).    
  13. 13 bar(Y) ->  
  14. 14     call_port({bar, Y}).    
  15. 15 bar(Y,X) ->  
  16. 16     call_port({bar, Y, X}).  
  17. 17 bar(Y,X,Z) ->               
  18. 18     call_port({bar, Y, X, Z}).  
  19. 19 call_port(Msg) ->  
  20. 20     complex ! {call, self(), Msg},   
  21. 21     receive  
  22. 22     {complex, Result} ->  
  23. 23         Result  
  24. 24     end.  
  25. 25 init(ExtPrg) ->  
  26. 26     register(complex, self()),  
  27. 27     process_flag(trap_exit, true),   
  28. 28     Port = open_port({spawn, ExtPrg}, [{packet, 2}, binary]),  
  29. 29     loop(Port).  
  30. 30 loop(Port) ->  
  31. 31     receive  
  32. 32     {call, Caller, Msg} ->  
  33. 33         Port ! {self(), {command, term_to_binary(Msg)}},  
  34. 34         receive  
  35. 35         {Port, {data, Data}} ->          
  36. 36             Caller ! {complex, binary_to_term(Data)}  
  37. 37         end,  
  38. 38         loop(Port);  
  39. 39     stop ->  
  40. 40         Port ! {self(), close},  
  41. 41         receive  
  42. 42         {Port, closed} ->  
  43. 43             exit(normal)  
  44. 44         end;  
  45. 45     {'EXIT', Port, Reason} ->  
  46. 46         exit(port_terminated)  
  47. 47     end.  

     ///complex.c

C代码   收藏代码
  1. int foo(int x)                                                                                                                                      
  2.  2 {  
  3.  3     return x+1;  
  4.  4 }  
  5.  5 int bar(int x)  
  6.  6 {  
  7.  7     return x*2;  
  8.  8 }  
 

 

 

Java代码   收藏代码
  1. retun.........  
  2.   
  3.   
  4.   
  5.   
  6.   
  7. 147     //intp = erl_mk_int(res);  
  8. 148     //intp = erl_mk_string("ssss");  
  9. 149     intp = erl_mk_estring(estring,i);  
  10. 150     //intp = erl_mk_string(estring);                                                                                                                
  11. 151                      
  12. 152     erl_encode(intp, buf);  
  13. 153     write_cmd(buf, erl_term_len(intp));  
  14. 154     erl_free_compound(tuplep);  
  15. 155     erl_free_term(fnp);  
  16. 156     erl_free_term(argp);  
  17. 157     erl_free_term(intp);  
  18. 158   }  
  19. 159 }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值