myfile.def
----------
resource file_open(string filename,string mode)
bool file_close(resource filehandle)
string file_read(resouce filehandle,int size)
string file_write(resource filehandle,string buffer)
bool file_eof(resouce filehandle)
myfile.c
--------
//开始部分加入,折构函数的定义
static void myfile_dtor(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
FILE *fp=(FILE *)rsrc->ptr;
fclose(fp);
}
//更新模块启动函数
PHP_MINIT_FUNCTION(myfile)
{
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/
le_myfile = zend_register_list_destructors_ex(myfile_dtor,NULL,"standard-c-file",module_number);
return SUCCESS;
}
//之后是五个函数的具体实现,注意VCWD宏库
//注意其中资源的调用函数己全局变量的定义方式,参考PHP权威编程。
PHP_FUNCTION(file_open)
{
char *filename = NULL;
char *mode = NULL;
int argc = ZEND_NUM_ARGS();
int filename_len;
int mode_len;
FILE *fp;
if (zend_parse_parameters(argc TSRMLS_CC, "ss", &filename, &filename_len, &mode, &mode_len) == FAILURE)
{ return;}
fp = VCWD_FOPEN(filename,mode);
if (fp == NULL)
{
RETURN_FALSE;
}
ZEND_REGISTER_RESOURCE(return_value,fp,le_myfile);
// php_error(E_WARNING, "file_open: not yet implemented");
}
/* }}} */
/* {{{ proto bool file_close(resource filehandle)
*/
PHP_FUNCTION(file_close)
{
int argc = ZEND_NUM_ARGS();
// int filehandle_id = -1;
zval *filehandle = NULL;
if (zend_parse_parameters(argc TSRMLS_CC, "r", &filehandle) == FAILURE)
{ return;}
if (zend_list_delete(Z_RESVAL_P(filehandle)) == FAILURE)
{
RETURN_FALSE;
}
RETURN_TRUE;
/*
if (filehandle) {
ZEND_FETCH_RESOURCE(???, ???, filehandle, filehandle_id, "???", ???_rsrc_id);
}
php_error(E_WARNING, "file_close: not yet implemented");
*/
}
/* }}} */
/* {{{ proto string file_read()
resouce filehandle,int size) */
PHP_FUNCTION(file_read)
{
int argc = ZEND_NUM_ARGS();
long size;
zval *filehandle = NULL;
FILE *fp;
char *result;
size_t bytes_read;
if(zend_parse_parameters(argc TSRMLS_CC,"rl",&filehandle,&size)==FAILURE)
{
return;
}
ZEND_FETCH_RESOURCE(fp,FILE *,&filehandle,-1,"standard-cfile",le_myfile);
result= (char *) emalloc(size+1);
bytes_read= fread(result,1,size,fp);
result[bytes_read]='/0';
RETURN_STRING(result,0);
/*
if (ZEND_NUM_ARGS() != 0) {
WRONG_PARAM_COUNT;
}
php_error(E_WARNING, "file_read: not yet implemented");
*/
}
/* }}} */
/* {{{ proto string file_write(resource filehandle, string buffer)
*/
PHP_FUNCTION(file_write)
{
char *buffer = NULL;
int argc = ZEND_NUM_ARGS();
// int filehandle_id = -1;
int buffer_len;
zval *filehandle = NULL;
FILE *fp;
if (zend_parse_parameters(argc TSRMLS_CC, "rs", &filehandle, &buffer, &buffer_len) == FAILURE)
{ return;}
ZEND_FETCH_RESOURCE(fp,FILE *,&filehandle,-1,"standard-cfile",le_myfile)
;
if (fwrite(buffer,1,buffer_len,fp) != buffer_len)
{
RETURN_FALSE;
}
RETURN_TRUE;
/*
if (filehandle) {
ZEND_FETCH_RESOURCE(???, ???, filehandle, filehandle_id, "???", ???_rsrc_id);
}
php_error(E_WARNING, "file_write: not yet implemented");
*/
}
/* }}} */
/* {ETURN_FALSE;
: file_eof()
resouce filehandle) */
PHP_FUNCTION(file_eof)
{
int argc = ZEND_NUM_ARGS();
zval *filehandle=NULL;
FILE *fp;
if (zend_parse_parameters(argc TSRMLS_CC,"r",&filehandle) == FAILURE)
{
return;
}
ZEND_FETCH_RESOURCE(fp,FILE *,&filehandle,-1,"standard-cfile",le_myfile);
if (fp==NULL)
{
RETURN_FALSE;
}
if (feof(fp) <= 0)
{
RETURN_TRUE;
}
RETURN_FALSE;
/* if (ZEND_NUM_ARGS() != 0) {
WRONG_PARAM_COUNT;
}
php_error(E_WARNING, "file_eof: not yet implemented");
*/
}