小DEMO二

因为我用ubuntu,就懒得用源码编译了,结果认识了一个装B利器pecl_gen
有个前提:跟上个DEMO一样,装了TC
先装PHP命令行环境

然后安装

sudo apt-get install php-pear php5-dev
sudo pear install -a codegen_pecl

然后弄个XML文件,比如foo.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?xml version="1.0" encoding="UTF-8"?>
<extension name="tc" version="0.0.1">
  <summary>tc</summary>
  <description><?data
This is tc extension module.
  ?></description>
 
 <deps language="c">
  <with name="libtc">
   <header name="tcutil.h" /> 
   <header name="tcfdb.h" />
   <header name="stdint.h" />
   <lib name="tokyocabinet" /> 
  </with>
 </deps> 
 
  <maintainers>
    <maintainer>
      <user>xiaolu</user>
      <name>mikale</name>
      <email>mikale@yeah.net</email>
      <role>lead</role>
    </maintainer>
  </maintainers>
 
  <license>PHP</license>
 
  <channel>__uri</channel>
 
  <release>
    <version>0.0.1</version>
    <date>2010-02-10</date>
    <notes><?data
- Initial release.
    ?></notes>
  </release>
 
 <code position="top"> 
  <![CDATA[
   TCFDB *fdb;
   int ecode;
  ]]> 
 </code> 
 
 <function role="internal" name="MINIT">
      <code>
<![CDATA[
fdb = tcfdbnew();
 
if(!tcfdbopen(fdb, "casket.tcf", FDBOWRITER | FDBOREADER| FDBOCREAT)){
    ecode = tcfdbecode(fdb);
    php_printf( "open error: %s\n", tcfdberrmsg(ecode));
}
 
 
]]>
      </code>
    </function>
 
 <function role="internal" name="MSHUTDOWN">
      <code>
<![CDATA[
  if(!tcfdbclose(fdb)){
    ecode = tcfdbecode(fdb);
    php_printf( "close error: %s\n", tcfdberrmsg(ecode));
  }
 
  tcfdbdel(fdb);
 
]]>
      </code>
    </function>
 
 
 
  <function name="tc_get" >
    <proto>string tc_get(string key)</proto>
    <code><?data
	char * val;
	val = tcfdbget3(fdb,key);
	RETURN_STRING(val,1);	
 
    ?></code>
    <test>
      <code><?data
print get('1');
      ?></code>
      <result ><?data
122
      ?></result>
    </test>  
  </function>
 
  <function name="tc_set">
    <proto>void tc_set(string key,string value)</proto>
    <code><?data
	tcfdbput3(fdb, key, value);
    ?></code>
    <test>
      <code><?data
print tc_set('1','122');
      ?></code>
    </test>  
  </function>
</extension>

然后运行命令

pecl-gen foo.xml
cd foo
phpize
./configure --enable-tc
make
sudo make install
echo "extension=tc.so" >> /etc/php5/cli/php.ini

创建一个test.php

1
2
3
4
5
6
<?php
tc_set('1','122');
print tc_get('1');
 
print "\n";
?>

测试一下:

php test.php
结果是 122

也可以点击下载这个XML文件foo.tar.gz

1 Comment

神仙二月 11th, 2010 at 10:31 上午

貌似比原来的 skel 好

Reply

Leave a comment

Your comment

Comment spam protected by SpamBam