Chinaunix首页 | 论坛 | 博客
  • 博客访问: 315111
  • 博文数量: 321
  • 博客积分: 10055
  • 博客等级: 上将
  • 技术积分: 3780
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-29 13:03
文章分类

全部博文(321)

文章存档

2012年(1)

2010年(7)

2009年(1)

2008年(312)

我的朋友

分类: C/C++

2010-12-12 10:26:38

以前在空闲时,研究过PHP的扩展模块开发,苦于没有找到一个比较好的调试方法,因此只做了少量的基本工作,感觉zend公司开发的PHP扩展模块有点过于复杂,调试真是让人头痛的事,若哪位有比跟踪调试的好方法,请留言。
zend PHP的扩展模块开发帮助方面的,google等能找到,这里把曾经做过的接口部分给出来大家看看
clucene可能有不稳定的部分,在写接口前最好先测试,至于哪里有问题,当时忘做笔记了,暂时忘了

//StandardIctclas
enum {STANDARD_ANALYZER};
static zend_object_handlers index_searcher_object_handlers;
static zend_object_handlers hits_object_handlers;
zend_class_entry *index_searcher_class_entry;
zend_class_entry *hits_class_entry;

zend_module_entry clucene_module_entry = {
 STANDARD_MODULE_HEADER,
 "clucene",
 NULL,
 PHP_MINIT(clucene),   
 NULL,                 
 PHP_RINIT(clucene),              
 NULL,                 
 PHP_MINFO(clucene),
 PHP_CLUCENE_MODULE_VERSION,
 STANDARD_MODULE_PROPERTIES
};
 
static void index_searcher_object_dtor(void *object, zend_object_handle handle TSRMLS_DC)
{
 index_searcher_object *intern = (index_searcher_object*) object;
 
 zend_hash_destroy(intern->std.properties);
 FREE_HASHTABLE(intern->std.properties);
 
 
 if (intern->searcher != NULL) {
  intern->searcher->close();
  delete intern->searcher;
 }
 if (intern->field != NULL) {
  delete [] intern->field;
 }
 efree(object);
}

static zend_object_value index_searcher_object_new(zend_class_entry *class_type TSRMLS_DC)
{
 zend_object_value retval;
 index_searcher_object *intern;
 zval *tmp;
 
 intern = (index_searcher_object*) emalloc(sizeof(index_searcher_object));
 memset(intern, 0, sizeof(index_searcher_object));
 intern->searcher = NULL;
 intern->field    = NULL;
 intern->std.ce   = class_type;
 ALLOC_HASHTABLE(intern->std.properties);
 zend_hash_init(intern->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
 zend_hash_copy(intern->std.properties,
       &class_type->default_properties,
       (copy_ctor_func_t) zval_add_ref,
       (void *) &tmp, sizeof(zval *));
 retval.handle = zend_objects_store_put(intern,
             index_searcher_object_dtor,
             NULL,
             NULL TSRMLS_CC);
 retval.handlers = &index_searcher_object_handlers;
 return retval;
}

static void hits_object_dtor(void *object, zend_object_handle handle TSRMLS_DC)
{
 hits_object *intern = (hits_object*) object;
 
 zend_hash_destroy(intern->std.properties);
 FREE_HASHTABLE(intern->std.properties);
 
 
 if (intern->hits != NULL) {
  delete intern->hits;
 }
 efree(object);
}

static zend_object_value hits_object_new(zend_class_entry *class_type TSRMLS_DC)
{
 zend_object_value retval;
 hits_object *intern;
 zval *tmp;
 
 intern = (hits_object*) emalloc(sizeof(hits_object));
 memset(intern, 0, sizeof(hits_object));
 intern->hits   = NULL;
 intern->std.ce = class_type;
 ALLOC_HASHTABLE(intern->std.properties);
 zend_hash_init(intern->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
 zend_hash_copy(intern->std.properties,
       &class_type->default_properties,
       (copy_ctor_func_t) zval_add_ref,
       (void *) &tmp, sizeof(zval *));
 retval.handle = zend_objects_store_put(intern,
             hits_object_dtor,
             NULL,
             NULL TSRMLS_CC);
 retval.handlers = &hits_object_handlers;
 return retval;
}

static zend_object_value hits_object_create(zend_class_entry *class_type, Hits *hits TSRMLS_DC)
{
 zend_object_value retval;
 hits_object *intern;
 zval *tmp;
 
 intern = (hits_object*) emalloc(sizeof(hits_object));
 memset(intern, 0, sizeof(hits_object));
 intern->hits = hits;
 intern->std.ce = class_type;
 ALLOC_HASHTABLE(intern->std.properties);
 zend_hash_init(intern->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
 zend_hash_copy(intern->std.properties,
       &class_type->default_properties,
       (copy_ctor_func_t) zval_add_ref,
       (void *) &tmp, sizeof(zval *));
 retval.handle = zend_objects_store_put(intern,
             hits_object_dtor,
             NULL,
             NULL TSRMLS_CC);
 retval.handlers = &hits_object_handlers;
 return retval;
}
#ifdef COMPILE_DL_CLUCENE
extern "C" {
ZEND_GET_MODULE(clucene)
}
#endif

PHP_METHOD(IndexSearcher, __construct);
PHP_METHOD(IndexSearcher, search);
PHP_METHOD(IndexSearcher, close);
static zend_function_entry index_searcher_functions[] = {
 PHP_ME(IndexSearcher, __construct,    NULL, ZEND_ACC_PUBLIC)
 PHP_ME(IndexSearcher, search,         NULL, ZEND_ACC_PUBLIC)
 PHP_ME(IndexSearcher, close,          NULL, ZEND_ACC_PUBLIC)
 {NULL, NULL, NULL}
};

PHP_METHOD(Hits, __construct);
PHP_METHOD(Hits, length);
PHP_METHOD(Hits, get);
PHP_METHOD(Hits, id);
PHP_METHOD(Hits, score);

static zend_function_entry hits_functions[] = {
 PHP_ME(Hits, __construct, NULL, ZEND_ACC_PRIVATE)
 PHP_ME(Hits, length,      NULL, ZEND_ACC_PUBLIC)
 PHP_ME(Hits, get,         NULL, ZEND_ACC_PUBLIC)
 PHP_ME(Hits, id,          NULL, ZEND_ACC_PUBLIC)
 PHP_ME(Hits, score,       NULL, ZEND_ACC_PUBLIC)
 {NULL, NULL, NULL}
};

PHP_METHOD(IndexSearcher, __construct)
{
 index_searcher_object *intern;
 char *path;
 long path_len;
 zval *fields,**z_item;
 const wchar_t **fields_ary;
 int i,count;
 
 php_set_error_handling(EH_THROW, zend_exception_get_default()
         TSRMLS_CC);
 
 
 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa", &path,
         &path_len,&fields) == FAILURE) {
  php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
  return;
 }
 //for(i=0;i<)
  count = zend_hash_num_elements(Z_ARRVAL_P(fields));
 fields_ary =_CL_NEWARRAY(const wchar_t*,count+1);
 
 zend_hash_internal_pointer_reset(Z_ARRVAL_P(fields));
  for (i = 0; i < count; i ++) {
      char* key;
      ulong idx;
      // 获取当前数据
      zend_hash_get_current_data(Z_ARRVAL_P(fields), (void**) &z_item);
      convert_to_string_ex(z_item);
      fields_ary[i] = Misc::_charToWide(Z_STRVAL_PP(z_item));
  
      // 将数组中的内部指针向前移动一位
      zend_hash_move_forward(Z_ARRVAL_P(fields));
  }
 fields_ary[count]=NULL;
 intern = (index_searcher_object*) zend_object_store_get_object(getThis() TSRMLS_CC);
 php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
 try {
  intern->searcher = new IndexSearcher(path);
 } catch (CLuceneError& error) {
  zend_throw_exception(zend_exception_get_default(), error.what(), 0 TSRMLS_CC);
  return;
 }
 intern->field = fields_ary;
}
 
阅读(1061) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-12-13 15:18:14

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com