June 26, 2006 at 23:42:19 · Filed under C/C++, ZEND API
一、默认情况下,C++是使用.cpp作为扩展名的,而PHP是用C写的,因此必须使用C++提供的C连接交换指定符号extern “C”来解决这个问题,以下两部分语句必须被包含:
extern "C" {
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
... // 其他C头文件
}
和
extern "C" {
#ifdef COMPILE_DL_MYEXT
ZEND_GET_MODULE(myext)
#endif
}
Read more…
No Comments »
June 26, 2006 at 23:25:28 · Filed under C/C++, ZEND API
1. 大家先来看一段PHP代码(gif2h.php):
< ?php
// 将GIF转换为C语言中的H文件
$filename = "mylogo.gif";
$fp = f open($filename, "rb");
$buffer = f read($fp, filesize($filename));
f close($fp);
$len = strlen($buffer);
$fp = f open("mylogo.h", "wb");
f write($fp, "unsigned char php_logo[] = {");
for ($i=0; $i<=$len; $i++) {
if($i % 10 == 0) {
f write($fp, "\n\t\t");
}
if($i == $len) {
$str = str_pad(ord(substr($buffer, $i, 1)), 3, " ", STR_PAD_LEFT);
} else {
$str = str_pad(ord(substr($buffer, $i, 1)), 3, " ", STR_PAD_LEFT) . ", ";
}
f write($fp, $str);
}
f write($fp, " };\n");
f close($fp);
?>
上述代码的作用就是将一张GIF图片文件转换为C语言中的头文件(.h)。
Read more…
No Comments »
June 26, 2006 at 23:15:21 · Filed under ZEND API
PHP_FUNCTION(print_array)
{
zval *z_array; // 外部引入的数组
int count, i;
zval **z_item;
if (FAILURE == zend_parse_parameters(argc TSRMLS_CC, "a", &z_array)) {
return;
}
// 获取数组大小
count = zend_hash_num_elements(Z_ARRVAL_P(z_array));
// 将数组的内部指针指向第一个单元
zend_hash_internal_pointer_reset(Z_ARRVAL_P(z_array));
for (i = 0; i < count; i++) {
char* key;
int idx;
// 获取当前数据
zend_hash_get_current_data(Z_ARRVAL_P(z_array), (void**) &z_item);
convert_to_string_ex(z_item);
if (zend_hash_get_current_key(Z_ARRVAL_P(z_array), &key, &idx, 0) == HASH_KEY_IS_STRING) {
// KEY为字符串
php_printf("array[%s] = %s", key, Z_STRVAL_PP(z_item));
} else {
// KEY为数字
php_printf("array[%d] = %s", idx, Z_STRVAL_PP(z_item));
}
// 将数组中的内部指针向前移动一位
zend_hash_move_forward(Z_ARRVAL_P(z_array));
}
}
在PHP调用如下:
<?php
$arr = array(
'id' => 'ezdevelop',
1 => "test number",
2 => "test number 2",
'name' => 'Wenlong Wu'
);
print_array($arr);
?>
No Comments »
June 26, 2006 at 23:11:44 · Filed under ZEND API
Sara Golemon在新闻组发了一篇关于在PHP扩展中如何使用stream filters的文章 ,参考如下:
If all you want to do is use an already implemented one just do this:
php_stream *stream;
php_stream_filter *filter;
zval *arguments = NULL; /* Populate this with value(s) appropriate to the filter */
stream = php_stream_open_wrapper(.....blah blah blah.....);
filter = php_stream_filter_create("filtername", arguments, php_stream_is_persistent(stream) TSRMLS_CC);
/* Or &stream->writefilters as appropriate */
php_stream_filter_append(&stream->readfilters, filter);
/* Of course, in the real world you’ll want to check both stream and filter for NULL as they may have failed to instantiate */
That said, I don’t think iconv.* will be any help.� It only covers
base64_(en|de)code() and quoted_printable_(en|de)code().
Filter implementation is a bit trickier.� Take a look at
ext/standard/filters.c for more info on that.� If you come up with a UTF
converter, I’m sure it can be added to the standard set of filters.
-Sara
“L0t3k” <cshmoove@xxxxxxxxxxxxx> wrote in message
news:20040823142259.47665.qmail@xxxxxxxxxxxxxxx
> can anyone give a hint as to how to use stream filters from an extension ?
>
> i have to parse input files which may be in a variety of encodings (mainly
> UTF8), and processing is done internally in UTF16.
>
> i noticed that there is an iconv filter, but i havent a clue (even after
> googling) of how to use it.
>
> l0t3k
–
PHP Internals – PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
No Comments »