PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/pspell - pspell.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 167 248 67.3 %
Date: 2014-04-19 Functions: 19 24 79.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2014 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: Vlad Krupin <phpdevel@echospace.com>                         |
      16             :    +----------------------------------------------------------------------+
      17             : */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #define IS_EXT_MODULE
      22             : 
      23             : #ifdef HAVE_CONFIG_H
      24             : #include "config.h"
      25             : #endif
      26             : 
      27             : #include "php.h"
      28             : 
      29             : #include <stdlib.h>
      30             : #include <ctype.h>
      31             : #include <stdio.h>
      32             : 
      33             : #if HAVE_PSPELL
      34             : 
      35             : /* this will enforce compatibility in .12 version (broken after .11.2) */
      36             : #define USE_ORIGINAL_MANAGER_FUNCS
      37             : 
      38             : #include "php_pspell.h"
      39             : #include <pspell.h>
      40             : #include "ext/standard/info.h"
      41             : 
      42             : #define PSPELL_FAST 1L
      43             : #define PSPELL_NORMAL 2L
      44             : #define PSPELL_BAD_SPELLERS 3L
      45             : #define PSPELL_SPEED_MASK_INTERNAL 3L
      46             : #define PSPELL_RUN_TOGETHER 8L
      47             : 
      48             : /* Largest ignored word can be 999 characters (this seems sane enough), 
      49             :  * and it takes 3 bytes to represent that (see pspell_config_ignore)
      50             :  */
      51             : #define PSPELL_LARGEST_WORD 3
      52             : 
      53             : static PHP_MINIT_FUNCTION(pspell);
      54             : static PHP_MINFO_FUNCTION(pspell);
      55             : static PHP_FUNCTION(pspell_new);
      56             : static PHP_FUNCTION(pspell_new_personal);
      57             : static PHP_FUNCTION(pspell_new_config);
      58             : static PHP_FUNCTION(pspell_check);
      59             : static PHP_FUNCTION(pspell_suggest);
      60             : static PHP_FUNCTION(pspell_store_replacement);
      61             : static PHP_FUNCTION(pspell_add_to_personal);
      62             : static PHP_FUNCTION(pspell_add_to_session);
      63             : static PHP_FUNCTION(pspell_clear_session);
      64             : static PHP_FUNCTION(pspell_save_wordlist);
      65             : static PHP_FUNCTION(pspell_config_create);
      66             : static PHP_FUNCTION(pspell_config_runtogether);
      67             : static PHP_FUNCTION(pspell_config_mode);
      68             : static PHP_FUNCTION(pspell_config_ignore);
      69             : static PHP_FUNCTION(pspell_config_personal);
      70             : static PHP_FUNCTION(pspell_config_dict_dir);
      71             : static PHP_FUNCTION(pspell_config_data_dir);
      72             : static PHP_FUNCTION(pspell_config_repl);
      73             : static PHP_FUNCTION(pspell_config_save_repl);
      74             : 
      75             : /* {{{ arginfo */
      76             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pspell_new, 0, 0, 1)
      77             :         ZEND_ARG_INFO(0, language)
      78             :         ZEND_ARG_INFO(0, spelling)
      79             :         ZEND_ARG_INFO(0, jargon)
      80             :         ZEND_ARG_INFO(0, encoding)
      81             :         ZEND_ARG_INFO(0, mode)
      82             : ZEND_END_ARG_INFO()
      83             : 
      84             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pspell_new_personal, 0, 0, 2)
      85             :         ZEND_ARG_INFO(0, personal)
      86             :         ZEND_ARG_INFO(0, language)
      87             :         ZEND_ARG_INFO(0, spelling)
      88             :         ZEND_ARG_INFO(0, jargon)
      89             :         ZEND_ARG_INFO(0, encoding)
      90             :         ZEND_ARG_INFO(0, mode)
      91             : ZEND_END_ARG_INFO()
      92             : 
      93             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pspell_new_config, 0, 0, 1)
      94             :         ZEND_ARG_INFO(0, config)
      95             : ZEND_END_ARG_INFO()
      96             : 
      97             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pspell_check, 0, 0, 2)
      98             :         ZEND_ARG_INFO(0, pspell)
      99             :         ZEND_ARG_INFO(0, word)
     100             : ZEND_END_ARG_INFO()
     101             : 
     102             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pspell_suggest, 0, 0, 2)
     103             :         ZEND_ARG_INFO(0, pspell)
     104             :         ZEND_ARG_INFO(0, word)
     105             : ZEND_END_ARG_INFO()
     106             : 
     107             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pspell_store_replacement, 0, 0, 3)
     108             :         ZEND_ARG_INFO(0, pspell)
     109             :         ZEND_ARG_INFO(0, misspell)
     110             :         ZEND_ARG_INFO(0, correct)
     111             : ZEND_END_ARG_INFO()
     112             : 
     113             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pspell_add_to_personal, 0, 0, 2)
     114             :         ZEND_ARG_INFO(0, pspell)
     115             :         ZEND_ARG_INFO(0, word)
     116             : ZEND_END_ARG_INFO()
     117             : 
     118             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pspell_add_to_session, 0, 0, 2)
     119             :         ZEND_ARG_INFO(0, pspell)
     120             :         ZEND_ARG_INFO(0, word)
     121             : ZEND_END_ARG_INFO()
     122             : 
     123             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pspell_clear_session, 0, 0, 1)
     124             :         ZEND_ARG_INFO(0, pspell)
     125             : ZEND_END_ARG_INFO()
     126             : 
     127             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pspell_save_wordlist, 0, 0, 1)
     128             :         ZEND_ARG_INFO(0, pspell)
     129             : ZEND_END_ARG_INFO()
     130             : 
     131             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pspell_config_create, 0, 0, 1)
     132             :         ZEND_ARG_INFO(0, language)
     133             :         ZEND_ARG_INFO(0, spelling)
     134             :         ZEND_ARG_INFO(0, jargon)
     135             :         ZEND_ARG_INFO(0, encoding)
     136             : ZEND_END_ARG_INFO()
     137             : 
     138             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pspell_config_runtogether, 0, 0, 2)
     139             :         ZEND_ARG_INFO(0, conf)
     140             :         ZEND_ARG_INFO(0, runtogether)
     141             : ZEND_END_ARG_INFO()
     142             : 
     143             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pspell_config_mode, 0, 0, 2)
     144             :         ZEND_ARG_INFO(0, conf)
     145             :         ZEND_ARG_INFO(0, mode)
     146             : ZEND_END_ARG_INFO()
     147             : 
     148             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pspell_config_ignore, 0, 0, 2)
     149             :         ZEND_ARG_INFO(0, conf)
     150             :         ZEND_ARG_INFO(0, ignore)
     151             : ZEND_END_ARG_INFO()
     152             : 
     153             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pspell_config_personal, 0, 0, 2)
     154             :         ZEND_ARG_INFO(0, conf)
     155             :         ZEND_ARG_INFO(0, personal)
     156             : ZEND_END_ARG_INFO()
     157             : 
     158             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pspell_config_dict_dir, 0, 0, 2)
     159             :         ZEND_ARG_INFO(0, conf)
     160             :         ZEND_ARG_INFO(0, directory)
     161             : ZEND_END_ARG_INFO()
     162             : 
     163             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pspell_config_data_dir, 0, 0, 2)
     164             :         ZEND_ARG_INFO(0, conf)
     165             :         ZEND_ARG_INFO(0, directory)
     166             : ZEND_END_ARG_INFO()
     167             : 
     168             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pspell_config_repl, 0, 0, 2)
     169             :         ZEND_ARG_INFO(0, conf)
     170             :         ZEND_ARG_INFO(0, repl)
     171             : ZEND_END_ARG_INFO()
     172             : 
     173             : ZEND_BEGIN_ARG_INFO_EX(arginfo_pspell_config_save_repl, 0, 0, 2)
     174             :         ZEND_ARG_INFO(0, conf)
     175             :         ZEND_ARG_INFO(0, save)
     176             : ZEND_END_ARG_INFO()
     177             : /* }}} */
     178             : 
     179             : /* {{{ pspell_functions[]
     180             :  */
     181             : static const zend_function_entry pspell_functions[] = {
     182             :         PHP_FE(pspell_new,                                      arginfo_pspell_new)
     183             :         PHP_FE(pspell_new_personal,                     arginfo_pspell_new_personal)
     184             :         PHP_FE(pspell_new_config,                       arginfo_pspell_new_config)
     185             :         PHP_FE(pspell_check,                            arginfo_pspell_check)
     186             :         PHP_FE(pspell_suggest,                          arginfo_pspell_suggest)
     187             :         PHP_FE(pspell_store_replacement,        arginfo_pspell_store_replacement)
     188             :         PHP_FE(pspell_add_to_personal,          arginfo_pspell_add_to_personal)
     189             :         PHP_FE(pspell_add_to_session,           arginfo_pspell_add_to_session)
     190             :         PHP_FE(pspell_clear_session,            arginfo_pspell_clear_session)
     191             :         PHP_FE(pspell_save_wordlist,            arginfo_pspell_save_wordlist)
     192             :         PHP_FE(pspell_config_create,            arginfo_pspell_config_create)
     193             :         PHP_FE(pspell_config_runtogether,       arginfo_pspell_config_runtogether)
     194             :         PHP_FE(pspell_config_mode,                      arginfo_pspell_config_mode)
     195             :         PHP_FE(pspell_config_ignore,            arginfo_pspell_config_ignore)
     196             :         PHP_FE(pspell_config_personal,          arginfo_pspell_config_personal)
     197             :         PHP_FE(pspell_config_dict_dir,          arginfo_pspell_config_dict_dir)
     198             :         PHP_FE(pspell_config_data_dir,          arginfo_pspell_config_data_dir)
     199             :         PHP_FE(pspell_config_repl,                      arginfo_pspell_config_repl)
     200             :         PHP_FE(pspell_config_save_repl,         arginfo_pspell_config_save_repl)
     201             :         PHP_FE_END
     202             : };
     203             : /* }}} */
     204             : 
     205             : static int le_pspell, le_pspell_config;
     206             : 
     207             : zend_module_entry pspell_module_entry = {
     208             :     STANDARD_MODULE_HEADER,
     209             :         "pspell", pspell_functions, PHP_MINIT(pspell), NULL, NULL, NULL, PHP_MINFO(pspell), NO_VERSION_YET, STANDARD_MODULE_PROPERTIES
     210             : };
     211             : 
     212             : #ifdef COMPILE_DL_PSPELL
     213             : ZEND_GET_MODULE(pspell)
     214             : #endif
     215             : 
     216          13 : static void php_pspell_close(zend_rsrc_list_entry *rsrc TSRMLS_DC)
     217             : {
     218          13 :         PspellManager *manager = (PspellManager *)rsrc->ptr;
     219             : 
     220          13 :         delete_pspell_manager(manager);
     221          13 : }
     222             : 
     223           4 : static void php_pspell_close_config(zend_rsrc_list_entry *rsrc TSRMLS_DC)
     224             : {
     225           4 :         PspellConfig *config = (PspellConfig *)rsrc->ptr;
     226             : 
     227           4 :         delete_pspell_config(config);
     228           4 : }
     229             : 
     230             : #define PSPELL_FETCH_CONFIG \
     231             :         config = (PspellConfig *) zend_list_find(conf, &type);      \
     232             :         if (config == NULL || type != le_pspell_config) {       \
     233             :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%ld is not a PSPELL config index", conf);        \
     234             :                 RETURN_FALSE;   \
     235             :         }       \
     236             : 
     237             : #define PSPELL_FETCH_MANAGER \
     238             :         manager = (PspellManager *) zend_list_find(scin, &type);    \
     239             :         if (!manager || type != le_pspell) {    \
     240             :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "%ld is not a PSPELL result index", scin);        \
     241             :                 RETURN_FALSE;   \
     242             :         }       \
     243             : 
     244             : /* {{{ PHP_MINIT_FUNCTION
     245             :  */
     246       20133 : static PHP_MINIT_FUNCTION(pspell)
     247             : {
     248       20133 :         REGISTER_LONG_CONSTANT("PSPELL_FAST", PSPELL_FAST, CONST_PERSISTENT | CONST_CS);
     249       20133 :         REGISTER_LONG_CONSTANT("PSPELL_NORMAL", PSPELL_NORMAL, CONST_PERSISTENT | CONST_CS);
     250       20133 :         REGISTER_LONG_CONSTANT("PSPELL_BAD_SPELLERS", PSPELL_BAD_SPELLERS, CONST_PERSISTENT | CONST_CS);
     251       20133 :         REGISTER_LONG_CONSTANT("PSPELL_RUN_TOGETHER", PSPELL_RUN_TOGETHER, CONST_PERSISTENT | CONST_CS);
     252       20133 :         le_pspell = zend_register_list_destructors_ex(php_pspell_close, NULL, "pspell", module_number);
     253       20133 :         le_pspell_config = zend_register_list_destructors_ex(php_pspell_close_config, NULL, "pspell config", module_number);
     254       20133 :         return SUCCESS;
     255             : }
     256             : /* }}} */
     257             : 
     258             : /* {{{ proto int pspell_new(string language [, string spelling [, string jargon [, string encoding [, int mode]]]])
     259             :    Load a dictionary */
     260           7 : static PHP_FUNCTION(pspell_new)
     261             : {
     262           7 :         char *language, *spelling = NULL, *jargon = NULL, *encoding = NULL;
     263           7 :         int language_len, spelling_len = 0, jargon_len = 0, encoding_len = 0;
     264           7 :         long mode = 0L,  speed = 0L;
     265           7 :         int argc = ZEND_NUM_ARGS();
     266             :         int ind;
     267             : 
     268             : #ifdef PHP_WIN32
     269             :         TCHAR aspell_dir[200];
     270             :         TCHAR data_dir[220];
     271             :         TCHAR dict_dir[220];
     272             :         HKEY hkey;
     273             :         DWORD dwType,dwLen;
     274             : #endif
     275             : 
     276             :         PspellCanHaveError *ret;
     277             :         PspellManager *manager;
     278             :         PspellConfig *config;
     279             :         
     280           7 :         if (zend_parse_parameters(argc TSRMLS_CC, "s|sssl", &language, &language_len, &spelling, &spelling_len,
     281             :                 &jargon, &jargon_len, &encoding, &encoding_len, &mode) == FAILURE) {
     282           0 :                 return;
     283             :         }
     284             : 
     285           7 :         config = new_pspell_config();
     286             : 
     287             : #ifdef PHP_WIN32
     288             :         /* If aspell was installed using installer, we should have a key
     289             :          * pointing to the location of the dictionaries
     290             :          */
     291             :         if (0 == RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Aspell", &hkey)) {
     292             :                 LONG result;
     293             :                 dwLen = sizeof(aspell_dir) - 1;
     294             :                 result = RegQueryValueEx(hkey, "", NULL, &dwType, (LPBYTE)&aspell_dir, &dwLen);
     295             :                 RegCloseKey(hkey);
     296             :                 if (result == ERROR_SUCCESS) {
     297             :                         strlcpy(data_dir, aspell_dir, sizeof(data_dir));
     298             :                         strlcat(data_dir, "\\data", sizeof(data_dir));
     299             :                         strlcpy(dict_dir, aspell_dir, sizeof(dict_dir));
     300             :                         strlcat(dict_dir, "\\dict", sizeof(dict_dir));
     301             : 
     302             :                         pspell_config_replace(config, "data-dir", data_dir);
     303             :                         pspell_config_replace(config, "dict-dir", dict_dir);
     304             :                 }
     305             :         }
     306             : #endif
     307             : 
     308           7 :         pspell_config_replace(config, "language-tag", language);
     309             : 
     310           7 :         if (spelling_len) {
     311           0 :                 pspell_config_replace(config, "spelling", spelling);
     312             :         }
     313             : 
     314           7 :         if (jargon_len) {
     315           0 :                 pspell_config_replace(config, "jargon", jargon);
     316             :         }
     317             : 
     318           7 :         if (encoding_len) {
     319           0 :                 pspell_config_replace(config, "encoding", encoding);
     320             :         }
     321             : 
     322           7 :         if (argc > 4) {
     323           2 :                 speed = mode & PSPELL_SPEED_MASK_INTERNAL;
     324             : 
     325             :                 /* First check what mode we want (how many suggestions) */
     326           2 :                 if (speed == PSPELL_FAST) {
     327           2 :                         pspell_config_replace(config, "sug-mode", "fast");
     328           0 :                 } else if (speed == PSPELL_NORMAL) {
     329           0 :                         pspell_config_replace(config, "sug-mode", "normal");
     330           0 :                 } else if (speed == PSPELL_BAD_SPELLERS) {
     331           0 :                         pspell_config_replace(config, "sug-mode", "bad-spellers");
     332             :                 }
     333             :                 
     334             :                 /* Then we see if run-together words should be treated as valid components */
     335           2 :                 if (mode & PSPELL_RUN_TOGETHER) {
     336           2 :                         pspell_config_replace(config, "run-together", "true");
     337             :                 }
     338             :         }
     339             : 
     340           7 :         ret = new_pspell_manager(config);
     341           7 :         delete_pspell_config(config);
     342             : 
     343           7 :         if (pspell_error_number(ret) != 0) {
     344           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "PSPELL couldn't open the dictionary. reason: %s", pspell_error_message(ret));
     345           0 :                 delete_pspell_can_have_error(ret);
     346           0 :                 RETURN_FALSE;
     347             :         }
     348             :         
     349           7 :         manager = to_pspell_manager(ret);
     350           7 :         ind = zend_list_insert(manager, le_pspell TSRMLS_CC);
     351           7 :         RETURN_LONG(ind);
     352             : }
     353             : /* }}} */
     354             : 
     355             : /* {{{ proto int pspell_new_personal(string personal, string language [, string spelling [, string jargon [, string encoding [, int mode]]]])
     356             :    Load a dictionary with a personal wordlist*/
     357           2 : static PHP_FUNCTION(pspell_new_personal)
     358             : {
     359           2 :         char *personal, *language, *spelling = NULL, *jargon = NULL, *encoding = NULL;
     360           2 :         int personal_len, language_len, spelling_len = 0, jargon_len = 0, encoding_len = 0;
     361           2 :         long mode = 0L,  speed = 0L;
     362           2 :         int argc = ZEND_NUM_ARGS();
     363             :         int ind;
     364             : 
     365             : #ifdef PHP_WIN32
     366             :         TCHAR aspell_dir[200];
     367             :         TCHAR data_dir[220];
     368             :         TCHAR dict_dir[220];
     369             :         HKEY hkey;
     370             :         DWORD dwType,dwLen;
     371             : #endif
     372             : 
     373             :         PspellCanHaveError *ret;
     374             :         PspellManager *manager;
     375             :         PspellConfig *config;
     376             : 
     377           2 :         if (zend_parse_parameters(argc TSRMLS_CC, "ps|sssl", &personal, &personal_len, &language, &language_len, 
     378             :                 &spelling, &spelling_len, &jargon, &jargon_len, &encoding, &encoding_len, &mode) == FAILURE) {
     379           0 :                 return;
     380             :         }
     381             : 
     382           2 :         config = new_pspell_config();
     383             : 
     384             : #ifdef PHP_WIN32
     385             :         /* If aspell was installed using installer, we should have a key
     386             :          * pointing to the location of the dictionaries
     387             :          */
     388             :         if (0 == RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Aspell", &hkey)) {
     389             :                 LONG result;
     390             :                 dwLen = sizeof(aspell_dir) - 1;
     391             :                 result = RegQueryValueEx(hkey, "", NULL, &dwType, (LPBYTE)&aspell_dir, &dwLen);
     392             :                 RegCloseKey(hkey);
     393             :                 if (result == ERROR_SUCCESS) {
     394             :                         strlcpy(data_dir, aspell_dir, sizeof(data_dir));
     395             :                         strlcat(data_dir, "\\data", sizeof(data_dir));
     396             :                         strlcpy(dict_dir, aspell_dir, sizeof(dict_dir));
     397             :                         strlcat(dict_dir, "\\dict", sizeof(dict_dir));
     398             : 
     399             :                         pspell_config_replace(config, "data-dir", data_dir);
     400             :                         pspell_config_replace(config, "dict-dir", dict_dir);
     401             :                 }
     402             :         }
     403             : #endif
     404             : 
     405           2 :         if (php_check_open_basedir(personal TSRMLS_CC)) {
     406           0 :                 delete_pspell_config(config);
     407           0 :                 RETURN_FALSE;
     408             :         }
     409             : 
     410           2 :         pspell_config_replace(config, "personal", personal);
     411           2 :         pspell_config_replace(config, "save-repl", "false");
     412             : 
     413           2 :         pspell_config_replace(config, "language-tag", language);
     414             : 
     415           2 :         if (spelling_len) {
     416           0 :                 pspell_config_replace(config, "spelling", spelling);
     417             :         }
     418             : 
     419           2 :         if (jargon_len) {
     420           0 :                 pspell_config_replace(config, "jargon", jargon);
     421             :         }
     422             : 
     423           2 :         if (encoding_len) {
     424           0 :                 pspell_config_replace(config, "encoding", encoding);
     425             :         }
     426             : 
     427           2 :         if (argc > 5) {
     428           0 :                 speed = mode & PSPELL_SPEED_MASK_INTERNAL;
     429             : 
     430             :                 /* First check what mode we want (how many suggestions) */
     431           0 :                 if (speed == PSPELL_FAST) {
     432           0 :                         pspell_config_replace(config, "sug-mode", "fast");
     433           0 :                 } else if (speed == PSPELL_NORMAL) {
     434           0 :                         pspell_config_replace(config, "sug-mode", "normal");
     435           0 :                 } else if (speed == PSPELL_BAD_SPELLERS) {
     436           0 :                         pspell_config_replace(config, "sug-mode", "bad-spellers");
     437             :                 }
     438             :                 
     439             :                 /* Then we see if run-together words should be treated as valid components */
     440           0 :                 if (mode & PSPELL_RUN_TOGETHER) {
     441           0 :                         pspell_config_replace(config, "run-together", "true");
     442             :                 }
     443             :         }
     444             : 
     445           2 :         ret = new_pspell_manager(config);
     446           2 :         delete_pspell_config(config);
     447             : 
     448           2 :         if (pspell_error_number(ret) != 0) {
     449           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "PSPELL couldn't open the dictionary. reason: %s", pspell_error_message(ret));
     450           1 :                 delete_pspell_can_have_error(ret);
     451           1 :                 RETURN_FALSE;
     452             :         }
     453             :         
     454           1 :         manager = to_pspell_manager(ret);
     455           1 :         ind = zend_list_insert(manager, le_pspell TSRMLS_CC);
     456           1 :         RETURN_LONG(ind);
     457             : }
     458             : /* }}} */
     459             : 
     460             : /* {{{ proto int pspell_new_config(int config)
     461             :    Load a dictionary based on the given config */
     462           6 : static PHP_FUNCTION(pspell_new_config)
     463             : {
     464             :         int type, ind;
     465             :         long conf;      
     466             :         PspellCanHaveError *ret;
     467             :         PspellManager *manager;
     468             :         PspellConfig *config;
     469             :         
     470           6 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &conf) == FAILURE) {
     471           0 :                 return;
     472             :         }
     473             : 
     474           6 :         PSPELL_FETCH_CONFIG;
     475             : 
     476           6 :         ret = new_pspell_manager(config);
     477             : 
     478           6 :         if (pspell_error_number(ret) != 0) {
     479           1 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "PSPELL couldn't open the dictionary. reason: %s", pspell_error_message(ret));
     480           1 :                 delete_pspell_can_have_error(ret);
     481           1 :                 RETURN_FALSE;
     482             :         }
     483             :         
     484           5 :         manager = to_pspell_manager(ret);
     485           5 :         ind = zend_list_insert(manager, le_pspell TSRMLS_CC);
     486           5 :         RETURN_LONG(ind);
     487             : }
     488             : /* }}} */
     489             : 
     490             : /* {{{ proto bool pspell_check(int pspell, string word)
     491             :    Returns true if word is valid */
     492          88 : static PHP_FUNCTION(pspell_check)
     493             : {
     494             :         int type, word_len;
     495             :         long scin;
     496             :         char *word;
     497             :         PspellManager *manager;
     498             : 
     499          88 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &scin, &word, &word_len) == FAILURE) {
     500           1 :                 return;
     501             :         }
     502             : 
     503          87 :         PSPELL_FETCH_MANAGER;
     504             : 
     505          86 :         if (pspell_manager_check(manager, word)) {
     506          79 :                 RETURN_TRUE;
     507             :         } else {
     508           7 :                 RETURN_FALSE;
     509             :         }
     510             : }
     511             : /* }}} */
     512             : 
     513             : /* {{{ proto array pspell_suggest(int pspell, string word)
     514             :    Returns array of suggestions */
     515           2 : static PHP_FUNCTION(pspell_suggest)
     516             : {
     517             :         long scin;
     518             :         char *word;
     519             :         int word_len;
     520             :         PspellManager *manager;
     521             :         int type;
     522             :         const PspellWordList *wl;
     523             :         const char *sug;
     524             : 
     525           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &scin, &word, &word_len) == FAILURE) {
     526           0 :                 return;
     527             :         }
     528             : 
     529           2 :         PSPELL_FETCH_MANAGER;
     530             : 
     531           2 :         array_init(return_value);
     532             : 
     533           2 :         wl = pspell_manager_suggest(manager, word);
     534           2 :         if (wl) {
     535           2 :                 PspellStringEmulation *els = pspell_word_list_elements(wl);
     536          49 :                 while ((sug = pspell_string_emulation_next(els)) != 0) {
     537          45 :                         add_next_index_string(return_value,(char *)sug,1);
     538             :                 }
     539           2 :                 delete_pspell_string_emulation(els);
     540             :         } else {
     541           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "PSPELL had a problem. details: %s", pspell_manager_error_message(manager));
     542           0 :                 RETURN_FALSE;
     543             :         }
     544             : }
     545             : /* }}} */
     546             : 
     547             : /* {{{ proto bool pspell_store_replacement(int pspell, string misspell, string correct)
     548             :    Notify the dictionary of a user-selected replacement */
     549           0 : static PHP_FUNCTION(pspell_store_replacement)
     550             : {
     551             :         int type, miss_len, corr_len;
     552             :         long scin;
     553             :         char *miss, *corr;
     554             :         PspellManager *manager;
     555             : 
     556           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lss", &scin, &miss, &miss_len, &corr, &corr_len) == FAILURE) {
     557           0 :                 return;
     558             :         }
     559             : 
     560           0 :         PSPELL_FETCH_MANAGER;
     561             : 
     562           0 :         pspell_manager_store_replacement(manager, miss, corr);
     563           0 :         if (pspell_manager_error_number(manager) == 0) {
     564           0 :                 RETURN_TRUE;
     565             :         } else {
     566           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pspell_store_replacement() gave error: %s", pspell_manager_error_message(manager));
     567           0 :                 RETURN_FALSE;
     568             :         }
     569             : }
     570             : /* }}} */
     571             : 
     572             : /* {{{ proto bool pspell_add_to_personal(int pspell, string word)
     573             :    Adds a word to a personal list */
     574           1 : static PHP_FUNCTION(pspell_add_to_personal)
     575             : {
     576             :         int type, word_len;
     577             :         long scin;
     578             :         char *word;
     579             :         PspellManager *manager;
     580             : 
     581           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &scin, &word, &word_len) == FAILURE) {
     582           0 :                 return;
     583             :         }
     584             : 
     585           1 :         PSPELL_FETCH_MANAGER;
     586             : 
     587             :         /*If the word is empty, we have to return; otherwise we'll segfault! ouch!*/
     588           1 :         if (word_len == 0) {
     589           0 :                 RETURN_FALSE;
     590             :         }
     591             :         
     592           1 :         pspell_manager_add_to_personal(manager, word);
     593           1 :         if (pspell_manager_error_number(manager) == 0) {
     594           1 :                 RETURN_TRUE;
     595             :         } else {
     596           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pspell_add_to_personal() gave error: %s", pspell_manager_error_message(manager));
     597           0 :                 RETURN_FALSE;
     598             :         }
     599             : }
     600             : /* }}} */
     601             : 
     602             : /* {{{ proto bool pspell_add_to_session(int pspell, string word)
     603             :    Adds a word to the current session */
     604           2 : static PHP_FUNCTION(pspell_add_to_session)
     605             : {
     606             :         int type, word_len;
     607             :         long scin;
     608             :         char *word;
     609             :         PspellManager *manager;
     610             : 
     611           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ls", &scin, &word, &word_len) == FAILURE) {
     612           0 :                 return;
     613             :         }
     614             : 
     615           2 :         PSPELL_FETCH_MANAGER;
     616             : 
     617             :         /*If the word is empty, we have to return; otherwise we'll segfault! ouch!*/
     618           2 :         if (word_len == 0) {
     619           1 :                 RETURN_FALSE;
     620             :         }
     621             : 
     622           1 :         pspell_manager_add_to_session(manager, word);
     623           1 :         if (pspell_manager_error_number(manager) == 0) {
     624           1 :                 RETURN_TRUE;
     625             :         } else {
     626           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pspell_add_to_session() gave error: %s", pspell_manager_error_message(manager));
     627           0 :                 RETURN_FALSE;
     628             :         }
     629             : }
     630             : /* }}} */
     631             : 
     632             : /* {{{ proto bool pspell_clear_session(int pspell)
     633             :    Clears the current session */
     634           2 : static PHP_FUNCTION(pspell_clear_session)
     635             : {
     636             :         int type;
     637             :         long scin;
     638             :         PspellManager *manager;
     639             : 
     640           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &scin) == FAILURE) {
     641           1 :                 return;
     642             :         }
     643             :     
     644           1 :         PSPELL_FETCH_MANAGER;   
     645             : 
     646           1 :         pspell_manager_clear_session(manager);
     647           1 :         if (pspell_manager_error_number(manager) == 0) {
     648           1 :                 RETURN_TRUE;
     649             :         } else {
     650           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pspell_clear_session() gave error: %s", pspell_manager_error_message(manager));
     651           0 :                 RETURN_FALSE;
     652             :         }
     653             : }
     654             : /* }}} */
     655             : 
     656             : /* {{{ proto bool pspell_save_wordlist(int pspell)
     657             :    Saves the current (personal) wordlist */
     658           1 : static PHP_FUNCTION(pspell_save_wordlist)
     659             : {
     660             :         int type;
     661             :         long scin;
     662             :         PspellManager *manager;
     663             : 
     664           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &scin) == FAILURE) {
     665           0 :                 return;
     666             :         }
     667             :     
     668           1 :         PSPELL_FETCH_MANAGER;   
     669             : 
     670           1 :         pspell_manager_save_all_word_lists(manager);
     671             : 
     672           1 :         if (pspell_manager_error_number(manager) == 0) {
     673           1 :                 RETURN_TRUE;
     674             :         } else {
     675           0 :                 php_error_docref(NULL TSRMLS_CC, E_WARNING, "pspell_save_wordlist() gave error: %s", pspell_manager_error_message(manager));
     676           0 :                 RETURN_FALSE;
     677             :         }
     678             : 
     679             : }
     680             : /* }}} */
     681             : 
     682             : /* {{{ proto int pspell_config_create(string language [, string spelling [, string jargon [, string encoding]]])
     683             :    Create a new config to be used later to create a manager */
     684           4 : static PHP_FUNCTION(pspell_config_create)
     685             : {
     686           4 :         char *language, *spelling = NULL, *jargon = NULL, *encoding = NULL;
     687           4 :         int language_len, spelling_len = 0, jargon_len = 0, encoding_len = 0;
     688             :         int ind;
     689             :         PspellConfig *config;
     690             : 
     691             : #ifdef PHP_WIN32
     692             :         TCHAR aspell_dir[200];
     693             :         TCHAR data_dir[220];
     694             :         TCHAR dict_dir[220];
     695             :         HKEY hkey;
     696             :         DWORD dwType,dwLen;
     697             : #endif
     698             :         
     699           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sss", &language, &language_len, &spelling, &spelling_len, 
     700             :                 &jargon, &jargon_len, &encoding, &encoding_len) == FAILURE) {
     701           0 :                 return;
     702             :         }
     703             : 
     704           4 :         config = new_pspell_config();
     705             : 
     706             : #ifdef PHP_WIN32
     707             :     /* If aspell was installed using installer, we should have a key
     708             :      * pointing to the location of the dictionaries
     709             :      */
     710             :         if (0 == RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Aspell", &hkey)) {
     711             :                 LONG result;
     712             :                 dwLen = sizeof(aspell_dir) - 1;
     713             :                 result = RegQueryValueEx(hkey, "", NULL, &dwType, (LPBYTE)&aspell_dir, &dwLen);
     714             :                 RegCloseKey(hkey);
     715             :                 if (result == ERROR_SUCCESS) {
     716             :                         strlcpy(data_dir, aspell_dir, sizeof(data_dir));
     717             :                         strlcat(data_dir, "\\data", sizeof(data_dir));
     718             :                         strlcpy(dict_dir, aspell_dir, sizeof(dict_dir));
     719             :                         strlcat(dict_dir, "\\dict", sizeof(dict_dir));
     720             : 
     721             :                         pspell_config_replace(config, "data-dir", data_dir);
     722             :                         pspell_config_replace(config, "dict-dir", dict_dir);
     723             :                 }
     724             :         }
     725             : #endif
     726             : 
     727           4 :         pspell_config_replace(config, "language-tag", language);
     728             : 
     729           4 :         if (spelling_len) {
     730           3 :                 pspell_config_replace(config, "spelling", spelling);
     731             :         }
     732             : 
     733           4 :         if (jargon_len) {
     734           0 :                 pspell_config_replace(config, "jargon", jargon);
     735             :         }
     736             : 
     737           4 :         if (encoding_len) {
     738           3 :                 pspell_config_replace(config, "encoding", encoding);
     739             :         }
     740             : 
     741             :         /* By default I do not want to write anything anywhere because it'll try to write to $HOME
     742             :         which is not what we want */
     743           4 :         pspell_config_replace(config, "save-repl", "false");
     744             : 
     745           4 :         ind = zend_list_insert(config, le_pspell_config TSRMLS_CC);
     746           4 :         RETURN_LONG(ind);
     747             : }
     748             : /* }}} */
     749             : 
     750             : /* {{{ proto bool pspell_config_runtogether(int conf, bool runtogether)
     751             :    Consider run-together words as valid components */
     752           3 : static PHP_FUNCTION(pspell_config_runtogether)
     753             : {
     754             :         int type;
     755             :         long conf;
     756             :         zend_bool runtogether;
     757             :         PspellConfig *config;
     758             :         
     759           3 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lb", &conf, &runtogether) == FAILURE) {
     760           0 :                 return;
     761             :         }
     762             : 
     763           3 :         PSPELL_FETCH_CONFIG;    
     764             : 
     765           3 :         pspell_config_replace(config, "run-together", runtogether ? "true" : "false");
     766             :         
     767           3 :         RETURN_TRUE;
     768             : }
     769             : /* }}} */
     770             : 
     771             : /* {{{ proto bool pspell_config_mode(int conf, long mode)
     772             :    Select mode for config (PSPELL_FAST, PSPELL_NORMAL or PSPELL_BAD_SPELLERS) */
     773           1 : static PHP_FUNCTION(pspell_config_mode)
     774             : {
     775             :         int type;
     776             :         long conf, mode;
     777             :         PspellConfig *config;
     778             : 
     779           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &conf, &mode) == FAILURE) {
     780           0 :                 return;
     781             :         }
     782             : 
     783           1 :         PSPELL_FETCH_CONFIG;
     784             : 
     785             :         /* First check what mode we want (how many suggestions) */
     786           1 :         if (mode == PSPELL_FAST) {
     787           0 :                 pspell_config_replace(config, "sug-mode", "fast");
     788           1 :         } else if (mode == PSPELL_NORMAL) {
     789           0 :                 pspell_config_replace(config, "sug-mode", "normal");
     790           1 :         } else if (mode == PSPELL_BAD_SPELLERS) {
     791           1 :                 pspell_config_replace(config, "sug-mode", "bad-spellers");
     792             :         }
     793             : 
     794           1 :         RETURN_TRUE;
     795             : }
     796             : /* }}} */
     797             : 
     798             : /* {{{ proto bool pspell_config_ignore(int conf, int ignore)
     799             :    Ignore words <= n chars */
     800           2 : static PHP_FUNCTION(pspell_config_ignore)
     801             : {
     802             :         int type;
     803             :         char ignore_str[MAX_LENGTH_OF_LONG + 1];        
     804           2 :         long conf, ignore = 0L;
     805             :         PspellConfig *config;
     806             :         
     807           2 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &conf, &ignore) == FAILURE) {
     808           0 :                 return;
     809             :         }
     810             : 
     811           2 :         PSPELL_FETCH_CONFIG;
     812             : 
     813           2 :         snprintf(ignore_str, sizeof(ignore_str), "%ld", ignore);
     814             : 
     815           2 :         pspell_config_replace(config, "ignore", ignore_str);
     816           2 :         RETURN_TRUE;
     817             : }
     818             : /* }}} */
     819             : 
     820           1 : static void pspell_config_path(INTERNAL_FUNCTION_PARAMETERS, char *option)
     821             : {
     822             :         int type;
     823             :         long conf;
     824             :         char *value;
     825             :         int value_len;
     826             :         PspellConfig *config;
     827             :         
     828           1 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lp", &conf, &value, &value_len) == FAILURE) {
     829           0 :                 return;
     830             :         }
     831             : 
     832           1 :         PSPELL_FETCH_CONFIG;
     833             : 
     834           1 :         if (php_check_open_basedir(value TSRMLS_CC)) {
     835           0 :                 RETURN_FALSE;
     836             :         }
     837             : 
     838           1 :         pspell_config_replace(config, option, value);
     839             : 
     840           1 :         RETURN_TRUE;
     841             : }
     842             : 
     843             : /* {{{ proto bool pspell_config_personal(int conf, string personal)
     844             :    Use a personal dictionary for this config */
     845           1 : static PHP_FUNCTION(pspell_config_personal)
     846             : {
     847           1 :         pspell_config_path(INTERNAL_FUNCTION_PARAM_PASSTHRU, "personal");
     848           1 : }
     849             : /* }}} */
     850             : 
     851             : /* {{{ proto bool pspell_config_dict_dir(int conf, string directory)
     852             :    location of the main word list */
     853           0 : static PHP_FUNCTION(pspell_config_dict_dir)
     854             : {
     855           0 :         pspell_config_path(INTERNAL_FUNCTION_PARAM_PASSTHRU, "dict-dir");
     856           0 : }
     857             : /* }}} */
     858             : 
     859             : /* {{{ proto bool pspell_config_data_dir(int conf, string directory)
     860             :     location of language data files */
     861           0 : static PHP_FUNCTION(pspell_config_data_dir)
     862             : {
     863           0 :         pspell_config_path(INTERNAL_FUNCTION_PARAM_PASSTHRU, "data-dir");
     864           0 : }
     865             : /* }}} */
     866             : 
     867             : /* {{{ proto bool pspell_config_repl(int conf, string repl)
     868             :    Use a personal dictionary with replacement pairs for this config */
     869           0 : static PHP_FUNCTION(pspell_config_repl)
     870             : {
     871             :         int type;
     872             :         long conf;
     873             :         char *repl;
     874             :         int repl_len;
     875             :         PspellConfig *config;
     876             :         
     877           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lp", &conf, &repl, &repl_len) == FAILURE) {
     878           0 :                 return;
     879             :         }
     880             : 
     881           0 :         PSPELL_FETCH_CONFIG;
     882             : 
     883           0 :         pspell_config_replace(config, "save-repl", "true");
     884             : 
     885           0 :         if (php_check_open_basedir(repl TSRMLS_CC)) {
     886           0 :                 RETURN_FALSE;
     887             :         }
     888             : 
     889           0 :         pspell_config_replace(config, "repl", repl);
     890             : 
     891           0 :         RETURN_TRUE;
     892             : }
     893             : /* }}} */
     894             : 
     895             : /* {{{ proto bool pspell_config_save_repl(int conf, bool save)
     896             :    Save replacement pairs when personal list is saved for this config */
     897           0 : static PHP_FUNCTION(pspell_config_save_repl)
     898             : {
     899             :         int type;
     900             :         long conf;
     901             :         zend_bool save;
     902             :         PspellConfig *config;
     903             :         
     904           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lb", &conf, &save) == FAILURE) {
     905           0 :                 return;
     906             :         }
     907             : 
     908           0 :         PSPELL_FETCH_CONFIG;
     909             : 
     910           0 :         pspell_config_replace(config, "save-repl", save ? "true" : "false");
     911             : 
     912           0 :         RETURN_TRUE;
     913             : }
     914             : /* }}} */
     915             : 
     916             : /* {{{ PHP_MINFO_FUNCTION
     917             :  */
     918         148 : static PHP_MINFO_FUNCTION(pspell)
     919             : {
     920         148 :         php_info_print_table_start();
     921         148 :         php_info_print_table_row(2, "PSpell Support", "enabled");
     922         148 :         php_info_print_table_end();
     923         148 : }
     924             : /* }}} */
     925             : 
     926             : #endif
     927             : 
     928             : /*
     929             :  * Local variables:
     930             :  * tab-width: 4
     931             :  * c-basic-offset: 4
     932             :  * End:
     933             :  * vim600: sw=4 ts=4 fdm=marker
     934             :  * vim<600: sw=4 ts=4
     935             :  */

Generated by: LCOV version 1.10

Generated at Sun, 20 Apr 2014 03:52:14 +0000 (3 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.