├── CREDITS ├── config.m4 ├── config.w32 ├── opencc.c ├── opencc.php └── php_opencc.h /CREDITS: -------------------------------------------------------------------------------- 1 | Author: BYVoid 2 | -------------------------------------------------------------------------------- /config.m4: -------------------------------------------------------------------------------- 1 | #AC_DEFINE(PACKAGE_VERSION, "0.2.0") 2 | #AC_DEFINE(PACKAGE_NAME, "opencc") 3 | #AC_DEFINE(PACKAGE_BUGREPORT, "http://code.google.com/p/opencc/issues/entry") 4 | 5 | PHP_ARG_ENABLE(opencc, whether to enable opencc support, 6 | [ --enable-opencc Enable opencc support]) 7 | 8 | if test "$PHP_OPENCC" != "no"; then 9 | 10 | SEARCH_PATH="/usr/local /usr" 11 | SEARCH_FOR="/include/opencc/opencc.h" 12 | if test -r $PHP_OPENCC/$SEARCH_FOR; then 13 | OPENCC_DIR=$PHP_OPENCC 14 | else # search default path list 15 | 16 | AC_MSG_CHECKING([for opencc files in default path]) 17 | 18 | for i in $SEARCH_PATH ; do 19 | if test -r $i/$SEARCH_FOR; then 20 | OPENCC_DIR=$i 21 | AC_MSG_RESULT(found in $i) 22 | fi 23 | done 24 | 25 | fi 26 | 27 | if test -z "$OPENCC_DIR"; then 28 | AC_MSG_RESULT([not found]) 29 | AC_MSG_ERROR([Please reinstall the opencc distribution]) 30 | fi 31 | 32 | PHP_ADD_INCLUDE($OPENCC_DIR/include/opencc) 33 | PHP_ADD_LIBRARY_WITH_PATH(opencc, $OPENCC_DIR/lib, OPENCC_SHARED_LIBADD) 34 | 35 | PHP_SUBST(OPENCC_SHARED_LIBADD) 36 | 37 | PHP_NEW_EXTENSION(opencc, opencc.c, $ext_shared) 38 | fi 39 | -------------------------------------------------------------------------------- /config.w32: -------------------------------------------------------------------------------- 1 | // $Id$ 2 | // vim:ft=javascript 3 | 4 | // If your extension references something external, use ARG_WITH 5 | // ARG_WITH("opencc", "for opencc support", "no"); 6 | 7 | // Otherwise, use ARG_ENABLE 8 | // ARG_ENABLE("opencc", "enable opencc support", "no"); 9 | 10 | if (PHP_OPENCC != "no") { 11 | EXTENSION("opencc", "opencc.c"); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /opencc.c: -------------------------------------------------------------------------------- 1 | /* 2 | +----------------------------------------------------------------------+ 3 | | PHP Version 5 | 4 | +----------------------------------------------------------------------+ 5 | | Copyright (c) 1997-2008 The PHP Group | 6 | +----------------------------------------------------------------------+ 7 | | This source file is subject to version 3.01 of the PHP license, | 8 | | that is bundled with this package in the file LICENSE, and is | 9 | | available through the world-wide-web at the following url: | 10 | | http://www.php.net/license/3_01.txt | 11 | | If you did not receive a copy of the PHP license and are unable to | 12 | | obtain it through the world-wide-web, please send a note to | 13 | | license@php.net so we can mail you a copy immediately. | 14 | +----------------------------------------------------------------------+ 15 | | Author: BYVoid | 16 | +----------------------------------------------------------------------+ 17 | */ 18 | 19 | /* $Id: header 252479 2008-02-07 19:39:50Z iliaa $ */ 20 | 21 | #ifdef HAVE_CONFIG_H 22 | #include "config.h" 23 | #endif 24 | 25 | #include "php.h" 26 | #include "php_ini.h" 27 | #include "ext/standard/info.h" 28 | #include "php_opencc.h" 29 | 30 | ZEND_DECLARE_MODULE_GLOBALS(opencc) 31 | 32 | /* True global resources - no need for thread safety here */ 33 | static int le_opencc; 34 | 35 | /* Function Entry */ 36 | const zend_function_entry opencc_functions[] = { 37 | PHP_FE(opencc_open, NULL) 38 | PHP_FE(opencc_close, NULL) 39 | PHP_FE(opencc_convert, NULL) 40 | PHP_FE(opencc_set_conversion_mode, NULL) 41 | {NULL, NULL, NULL} 42 | }; 43 | 44 | /* Module Entry */ 45 | zend_module_entry opencc_module_entry = { 46 | #if ZEND_MODULE_API_NO >= 20010901 47 | STANDARD_MODULE_HEADER, 48 | #endif 49 | "opencc", 50 | opencc_functions, 51 | PHP_MINIT(opencc), 52 | PHP_MSHUTDOWN(opencc), 53 | PHP_RINIT(opencc), 54 | PHP_RSHUTDOWN(opencc), 55 | PHP_MINFO(opencc), 56 | #if ZEND_MODULE_API_NO >= 20010901 57 | PACKAGE_VERSION, 58 | #endif 59 | STANDARD_MODULE_PROPERTIES 60 | }; 61 | 62 | #ifdef COMPILE_DL_OPENCC 63 | ZEND_GET_MODULE(opencc) 64 | #endif 65 | 66 | PHP_INI_BEGIN() 67 | //STD_PHP_INI_ENTRY("opencc.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_opencc_globals, opencc_globals) 68 | STD_PHP_INI_ENTRY("opencc.default_config", "zhs2zht.ini", PHP_INI_ALL, OnUpdateString, default_config, zend_opencc_globals, opencc_globals) 69 | PHP_INI_END() 70 | 71 | static void php_opencc_init_globals(zend_opencc_globals *opencc_globals) 72 | { 73 | opencc_globals->default_config = NULL; 74 | } 75 | 76 | /* Module Initialize */ 77 | PHP_MINIT_FUNCTION(opencc) 78 | { 79 | REGISTER_INI_ENTRIES(); 80 | return SUCCESS; 81 | } 82 | 83 | /* Module Shotdown */ 84 | PHP_MSHUTDOWN_FUNCTION(opencc) 85 | { 86 | UNREGISTER_INI_ENTRIES(); 87 | return SUCCESS; 88 | } 89 | 90 | PHP_MINFO_FUNCTION(opencc) 91 | { 92 | php_info_print_table_start(); 93 | php_info_print_table_header(2, "opencc support", "enabled"); 94 | php_info_print_table_end(); 95 | 96 | DISPLAY_INI_ENTRIES(); 97 | } 98 | 99 | /* Initialize */ 100 | PHP_RINIT_FUNCTION(opencc) 101 | { 102 | return SUCCESS; 103 | } 104 | 105 | /* Module Shutdown */ 106 | PHP_RSHUTDOWN_FUNCTION(opencc) 107 | { 108 | return SUCCESS; 109 | } 110 | 111 | PHP_FUNCTION(opencc_open) 112 | { 113 | opencc_t od; 114 | char * config = NULL; 115 | int config_len; 116 | 117 | if (ZEND_NUM_ARGS() > 0) 118 | { 119 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &config, &config_len) == FAILURE) 120 | { 121 | RETURN_BOOL(0); 122 | } 123 | } 124 | 125 | if (config == NULL || *config == 0) 126 | { 127 | config = OPENCC_G(default_config); 128 | } 129 | 130 | od = opencc_open(config); 131 | if (od == (opencc_t) -1) 132 | { 133 | RETURN_BOOL(0); 134 | } 135 | 136 | RETURN_RESOURCE((long) od); 137 | } 138 | 139 | PHP_FUNCTION(opencc_close) 140 | { 141 | zval * zod; 142 | opencc_t od; 143 | 144 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zod) == FAILURE) 145 | { 146 | RETURN_BOOL(0); 147 | } 148 | 149 | od = (opencc_t) zod->value.lval; 150 | if (od == (opencc_t) -1 || od == NULL) 151 | { 152 | RETURN_BOOL(0); 153 | } 154 | 155 | if (opencc_close(od) == -1) 156 | { 157 | RETURN_BOOL(0); 158 | } 159 | else 160 | { 161 | RETURN_BOOL(1); 162 | } 163 | } 164 | 165 | PHP_FUNCTION(opencc_convert) 166 | { 167 | zval * zod; 168 | char * inbuf = NULL; 169 | int inbuf_len; 170 | opencc_t od; 171 | 172 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &zod, &inbuf, &inbuf_len) == FAILURE) 173 | { 174 | RETURN_BOOL(0); 175 | } 176 | 177 | od = (opencc_t) zod->value.lval; 178 | if (od == (opencc_t) -1 || od == NULL) 179 | { 180 | RETURN_BOOL(0); 181 | } 182 | 183 | if (inbuf_len == 0) 184 | { 185 | char * rs = emalloc(sizeof(char)); 186 | *rs = '\0'; 187 | RETURN_STRINGL(rs, 0, 0); 188 | } 189 | 190 | char * outbuf = opencc_convert_utf8(od, inbuf, inbuf_len); 191 | int len = strlen(outbuf); 192 | 193 | char * rs = emalloc(sizeof(char) * (len + 1)); 194 | strncpy(rs, outbuf, len + 1); 195 | free(outbuf); 196 | 197 | RETURN_STRINGL(rs, len, 0); 198 | } 199 | 200 | PHP_FUNCTION(opencc_set_conversion_mode) 201 | { 202 | zval * zod; 203 | opencc_t od; 204 | long conversion_mode; 205 | 206 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zod, &conversion_mode) == FAILURE) 207 | { 208 | RETURN_BOOL(0); 209 | } 210 | 211 | od = (opencc_t) zod->value.lval; 212 | 213 | if (od == (opencc_t) -1 || od == NULL) 214 | { 215 | RETURN_BOOL(0); 216 | } 217 | 218 | opencc_set_conversion_mode(od, conversion_mode); 219 | 220 | RETURN_BOOL(1); 221 | } 222 | -------------------------------------------------------------------------------- /opencc.php: -------------------------------------------------------------------------------- 1 | "; 3 | 4 | if(!extension_loaded('opencc')) { 5 | dl('opencc.' . PHP_SHLIB_SUFFIX); 6 | echo "dl\n"; 7 | } 8 | $module = 'opencc'; 9 | $functions = get_extension_funcs($module); 10 | print_r($functions); 11 | 12 | $text = "你干什么不干我事。\n"; 13 | 14 | $od = opencc_open("zhs2zht.ini"); 15 | var_dump($od); 16 | $text = opencc_convert($od, $text); 17 | 18 | opencc_close($od); 19 | 20 | echo $text; 21 | -------------------------------------------------------------------------------- /php_opencc.h: -------------------------------------------------------------------------------- 1 | /* 2 | +----------------------------------------------------------------------+ 3 | | PHP Version 5 | 4 | +----------------------------------------------------------------------+ 5 | | Copyright (c) 1997-2008 The PHP Group | 6 | +----------------------------------------------------------------------+ 7 | | This source file is subject to version 3.01 of the PHP license, | 8 | | that is bundled with this package in the file LICENSE, and is | 9 | | available through the world-wide-web at the following url: | 10 | | http://www.php.net/license/3_01.txt | 11 | | If you did not receive a copy of the PHP license and are unable to | 12 | | obtain it through the world-wide-web, please send a note to | 13 | | license@php.net so we can mail you a copy immediately. | 14 | +----------------------------------------------------------------------+ 15 | | Author: BYVoid | 16 | +----------------------------------------------------------------------+ 17 | */ 18 | 19 | /* $Id: header 252479 2008-02-07 19:39:50Z iliaa $ */ 20 | 21 | #ifndef PHP_OPENCC_H 22 | #define PHP_OPENCC_H 23 | 24 | #include 25 | 26 | extern zend_module_entry opencc_module_entry; 27 | #define phpext_opencc_ptr &opencc_module_entry 28 | 29 | #ifdef PHP_WIN32 30 | # define PHP_OPENCC_API __declspec(dllexport) 31 | #elif defined(__GNUC__) && __GNUC__ >= 4 32 | # define PHP_OPENCC_API __attribute__ ((visibility("default"))) 33 | #else 34 | # define PHP_OPENCC_API 35 | #endif 36 | 37 | #ifdef ZTS 38 | #include "TSRM.h" 39 | #endif 40 | 41 | PHP_MINIT_FUNCTION(opencc); 42 | PHP_MSHUTDOWN_FUNCTION(opencc); 43 | PHP_RINIT_FUNCTION(opencc); 44 | PHP_RSHUTDOWN_FUNCTION(opencc); 45 | PHP_MINFO_FUNCTION(opencc); 46 | 47 | PHP_FUNCTION(opencc_open); 48 | PHP_FUNCTION(opencc_close); 49 | PHP_FUNCTION(opencc_convert); 50 | PHP_FUNCTION(opencc_set_conversion_mode); 51 | 52 | 53 | ZEND_BEGIN_MODULE_GLOBALS(opencc) 54 | char * default_config; 55 | ZEND_END_MODULE_GLOBALS(opencc) 56 | 57 | /* In every utility function you add that needs to use variables 58 | in php_opencc_globals, call TSRMLS_FETCH(); after declaring other 59 | variables used by that function, or better yet, pass in TSRMLS_CC 60 | after the last function argument and declare your utility function 61 | with TSRMLS_DC after the last declared argument. Always refer to 62 | the globals in your function as OPENCC_G(variable). You are 63 | encouraged to rename these macros something shorter, see 64 | examples in any other php module directory. 65 | */ 66 | 67 | #ifdef ZTS 68 | #define OPENCC_G(v) TSRMG(opencc_globals_id, zend_opencc_globals *, v) 69 | #else 70 | #define OPENCC_G(v) (opencc_globals.v) 71 | #endif 72 | 73 | #endif /* PHP_OPENCC_H */ 74 | --------------------------------------------------------------------------------