CodeGen_PECL で sary バインディング その1・紹介編

CodeGen_PECL の使い方は Do You PHP はてな - PEAR::CodeGen_PECL事始め でよくまとめられているので、そちらと併せて読んでいただけると分かりやすいと思います。
spec ファイル (sary.xml) の内容は以下の通りです。

<?xml version="1.0" ?>
<extension name="sary" version="0.0.1">
	<summary>Sary PHP extension</summary>
	<description><![CDATA[
		PHP bindig of sary - a suffix array library and tools.
	]]></description>
	<deps language="c" platform="all">
		<with name="sary" mode="pkg-config" testfile="include/sary.h">
			<header name="sary.h" />
			<header name="glib.h" />
		</with>
	</deps>
	<resources>
		<resource name="sary_searcher" payload="SarySearcher" alloc="no">
			<destruct><![CDATA[
sary_searcher_destroy(resource);
			]]></destruct>
		</resource>
	</resources>
	<function name="sary_searcher_new">
		<proto>resource sary_searcher sary_searcher_new(string file_name[, string array_name])</proto>
		<code><![CDATA[
if (array_name_len) {
	return_res = sary_searcher_new2((const gchar *)file_name, (const gchar *)array_name);
} else {
	return_res = sary_searcher_new((const gchar *)file_name);
}
if (!return_res) {
	php_error(E_WARNING, "sary_searcher_new(): failed to open %s and/or its suffix array.", file_name);
	RETURN_FALSE;
}
		]]></code>
	</function>
	<function name="sary_searcher_destroy">
		<proto>void sary_searcher_destroy(resource sary_searcher searcher)</proto>
		<code><![CDATA[
FREE_RESOURCE(searcher);
		]]></code>
	</function>
	<function name="sary_searcher_search">
		<proto>bool sary_searcher_search(resource sary_searcher searcher, string pattern)</proto>
		<code><![CDATA[
RETURN_BOOL((int)sary_searcher_search2(res_searcher, (const gchar *)pattern, (SaryInt)pattern_len));
		]]></code>
	</function>
	<function name="sary_searcher_get_next_line">
		<proto>string sary_searcher_get_next_line(resource sary_searcher searcher)</proto>
		<code><![CDATA[
gchar *line = NULL;
SaryInt len = 0;
if ((line = sary_searcher_get_next_line2(res_searcher, &len))) {
	RETURN_STRINGL((char *)line, (int)len, 1);
}
RETURN_FALSE;
		]]></code>
	</function>
	<function name="sary_searcher_get_all_lines">
		<proto>array sary_searcher_get_all_lines(resource sary_searcher searcher)</proto>
		<code><![CDATA[
gchar *line = NULL;
SaryInt len = 0;
while ((line = sary_searcher_get_next_line2(res_searcher, &len))) {
	add_next_index_stringl(return_value, (char *)line, (int)len, 1);
}
		]]></code>
	</function>
	<function name="sary_searcher_count_occurrences">
		<proto>int sary_searcher_count_occurrences(resource sary_searcher searcher)</proto>
		<code><![CDATA[
RETURN_LONG((long)sary_searcher_count_occurrences(res_searcher));
		]]></code>
	</function>
	<function name="sary_searcher_sort_occurrences">
		<proto>void sary_searcher_sort_occurrences(resource sary_searcher searcher)</proto>
		<code><![CDATA[
sary_searcher_sort_occurrences(res_searcher);
		]]></code>
	</function>
</extension>

このファイルを元に

pecl-gen sary.xml
cd sary
phpize
./configure
make
sudo make install

これだけで PHP でも Suffix Array を用いた検索ができるようになります。
(※検索用の Suffix Array は mksary コマンド等で別に構築する必要があります)