扩展PHP,使用C库

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");
*/
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值