📄 php_core_lib.cpp
字号:
//// This file is part of the aMule Project.// Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )// Copyright (C) 2005-2008 Froenchenko Leonid ( lfroen@amule.org )//// Any parts of this program derived from the xMule, lMule or eMule project,// or contributed by third-party developers are copyrighted by their// respective authors.//// This program is free software; you can redistribute it and/or modify// it under the terms of the GNU General Public License as published by// the Free Software Foundation; either version 2 of the License, or// (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.// // You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA//#ifdef HAVE_CONFIG_H# include "config.h"#endif#include <string> // Do_not_auto_remove (g++-4.0.1)#ifdef HAVE_SYS_TYPES_H# include <sys/types.h>#endif#include <regex.h>#include "WebServer.h"#include <ec/cpp/ECSpecialTags.h>#include "php_syntree.h"#include "php_core_lib.h"#include <wx/datetime.h>#ifdef ENABLE_NLS#include <libintl.h>#endif/* * Built-in php functions. Those are both library and core internals. * * I'm not going event to get near to what Zend provide, but * at least base things must be here *//* * Print info about variable: php var_dump() */void php_var_dump(PHP_VALUE_NODE *node, int ident, int ref){ for(int i = 0; i < ident;i++) { printf("\t"); } if ( ref ) printf("&"); switch(node->type) { case PHP_VAL_BOOL: printf("bool(%s)\n", node->int_val ? "true" : "false"); break; case PHP_VAL_INT: printf("int(%"PRIu64")\n", node->int_val); break; case PHP_VAL_FLOAT: printf("float(%f)\n", node->float_val); break; case PHP_VAL_STRING: printf("string(%d) \"%s\"\n", strlen(node->str_val), node->str_val); break; case PHP_VAL_OBJECT: printf("Object(%s)\n", node->obj_val.class_name); break; case PHP_VAL_ARRAY: { int arr_size = array_get_size(node); printf("array(%d) {\n", arr_size); for(int i = 0; i < arr_size;i++) { const std::string &curr_key = array_get_ith_key(node, i); PHP_VAR_NODE *curr_val = array_get_by_str_key(node, curr_key); printf("\t[%s]=>\n", curr_key.c_str()); php_var_dump(&curr_val->value, ident+1, curr_val->ref_count > 1); } for(int i = 0; i < ident;i++) { printf("\t"); } printf("}\n"); break; } case PHP_VAL_NONE: printf("NULL\n"); break; case PHP_VAL_VAR_NODE: case PHP_VAL_INT_DATA: assert(0); break; }}void php_native_var_dump(PHP_VALUE_NODE *){ PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0"); if ( si ) { assert((si->type == PHP_SCOPE_VAR)||(si->type == PHP_SCOPE_PARAM)); php_var_dump(&si->var->value, 0, 0); } else { php_report_error(PHP_ERROR, "Invalid or missing argument"); }}/* * Sorting stl-way requires operator ">" */class SortElem { public: SortElem() {} SortElem(PHP_VAR_NODE *p) { obj = p; } PHP_VAR_NODE *obj; static PHP_SYN_FUNC_DECL_NODE *callback; friend bool operator<(const SortElem &o1, const SortElem &o2);};PHP_SYN_FUNC_DECL_NODE *SortElem::callback = 0;bool operator<(const SortElem &o1, const SortElem &o2){ PHP_VALUE_NODE result; value_value_assign(&SortElem::callback->params[0].si_var->var->value, &o1.obj->value); value_value_assign(&SortElem::callback->params[1].si_var->var->value, &o2.obj->value); switch_push_scope_table((PHP_SCOPE_TABLE_TYPE *)SortElem::callback->scope); // // params passed by-value, all & notations ignored // result.type = PHP_VAL_NONE; php_execute(SortElem::callback->code, &result); cast_value_dnum(&result); // // restore stack, free arg list // switch_pop_scope_table(0); value_value_free(&SortElem::callback->params[0].si_var->var->value); value_value_free(&SortElem::callback->params[1].si_var->var->value); return result.int_val;}void php_native_usort(PHP_VALUE_NODE *){ PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0"); if ( !si || (si->var->value.type != PHP_VAL_ARRAY)) { php_report_error(PHP_ERROR, "Invalid or missing argument (array)"); return; } PHP_VAR_NODE *array = si->var; si = get_scope_item(g_current_scope, "__param_1"); if ( !si || (si->var->value.type != PHP_VAL_STRING)) { php_report_error(PHP_ERROR, "Invalid or missing argument (func name)"); return; } char *cmp_func_name = si->var->value.str_val; si = get_scope_item(g_global_scope, cmp_func_name); if ( !si || (si->type != PHP_SCOPE_FUNC)) { php_report_error(PHP_ERROR, "Compare function [%s] not found", cmp_func_name); return; } PHP_SYN_FUNC_DECL_NODE *func_decl = si->func->func_decl; // // usort invalidates keys, and sorts values // PHP_ARRAY_TYPE *arr_obj = (PHP_ARRAY_TYPE *)array->value.ptr_val; // // create vector of values // if ( arr_obj->array.size() == 0 ) { php_report_error(PHP_WARNING, "Sorting array of size 0"); return; } std::list<SortElem> sort_list; for(PHP_ARRAY_ITER_TYPE i = arr_obj->array.begin(); i != arr_obj->array.end(); i++) { sort_list.push_back(SortElem(i->second)); } SortElem::callback = func_decl; sort_list.sort(); arr_obj->array.clear(); arr_obj->sorted_keys.clear(); unsigned int key = 0; for(std::list<SortElem>::iterator i = sort_list.begin(); i != sort_list.end(); i++) { array_add_to_int_key(&array->value, key++, i->obj); }}/* * String functions */void php_native_strlen(PHP_VALUE_NODE *result){ PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0"); if ( si ) { PHP_VALUE_NODE *param = &si->var->value; cast_value_str(param); if ( result ) { cast_value_dnum(result); result->int_val = strlen(param->str_val); } } else { php_report_error(PHP_ERROR, "Invalid or missing argument"); }}void php_native_count(PHP_VALUE_NODE *result){ PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0"); if ( si ) { PHP_VALUE_NODE *param = &si->var->value; if ( result ) { cast_value_dnum(result); if ( (si->var->value.type == PHP_VAL_NONE) || (si->var->value.type != PHP_VAL_ARRAY) ) { result->int_val = 0; } else { result->int_val = array_get_size(param); } } } else { php_report_error(PHP_ERROR, "Invalid or missing argument"); }}void php_native_isset(PHP_VALUE_NODE *result){ PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0"); if ( si ) { PHP_VALUE_NODE *param = &si->var->value; cast_value_str(param); if ( result ) { cast_value_bool(result); result->int_val = (si->var->value.type == PHP_VAL_NONE) ? 0 : 1; } } else { php_report_error(PHP_ERROR, "Invalid or missing argument"); }}void php_native_substr(PHP_VALUE_NODE * /*result*/){ PHP_SCOPE_ITEM *si_str = get_scope_item(g_current_scope, "__param_0"); PHP_VALUE_NODE *str = &si_str->var->value; if ( si_str ) { cast_value_str(str); } else { php_report_error(PHP_ERROR, "Invalid or missing argument 'str' for 'substr'"); return; } PHP_SCOPE_ITEM *si_start = get_scope_item(g_current_scope, "__param_1"); PHP_VALUE_NODE *start = &si_start->var->value; if ( si_start ) { cast_value_dnum(start); } else { php_report_error(PHP_ERROR, "Invalid or missing argument 'start' for 'substr'"); return; } // 3-rd is optional PHP_SCOPE_ITEM *si_end = get_scope_item(g_current_scope, "__param_2"); PHP_VALUE_NODE end = { PHP_VAL_INT, { 0 } }; if ( si_end ) { end = si_end->var->value; } cast_value_dnum(&end);}void php_native_split(PHP_VALUE_NODE *result){ if ( result ) { cast_value_array(result); } else { return; } PHP_VALUE_NODE *pattern, *string_to_split, *split_limit; PHP_SCOPE_ITEM *si = get_scope_item(g_current_scope, "__param_0"); if ( si ) { pattern = &si->var->value; cast_value_str(pattern); } else { php_report_error(PHP_ERROR, "Invalid or missing argument: pattern"); return; } si = get_scope_item(g_current_scope, "__param_1"); if ( si ) { string_to_split = &si->var->value; cast_value_str(string_to_split); } else { php_report_error(PHP_ERROR, "Invalid or missing argument: string"); return; } si = get_scope_item(g_current_scope, "__param_2"); if ( si ) { split_limit = &si->var->value; cast_value_dnum(split_limit); } else { php_report_error(PHP_ERROR, "Invalid or missing argument: string"); return; } regex_t preg; char error_buff[256]; int reg_result = regcomp(&preg, pattern->str_val, REG_EXTENDED); if ( reg_result ) { regerror(reg_result, &preg, error_buff, sizeof(error_buff)); php_report_error(PHP_ERROR, "Failed in regcomp: %s", error_buff); return; } size_t nmatch = strlen(string_to_split->str_val); regmatch_t *pmatch = new regmatch_t[nmatch]; char *str_2_match = string_to_split->str_val; char *tmp_buff = new char[strlen(string_to_split->str_val)+1]; while ( 1 ) {// printf("matching: %s\n", str_2_match); reg_result = regexec(&preg, str_2_match, nmatch, pmatch, 0); if ( reg_result ) { // no match break; }// for(int i = 0; pmatch[i].rm_so >= 0; i++) {// printf("match [%d] %d - %d\n", i, pmatch[i].rm_so, pmatch[i].rm_eo);// } /* * I will use only first match, since I don't see any sense to have more * then 1 match in split() call */ for(int i = 0; i < pmatch[0].rm_so; i++) { tmp_buff[i] = str_2_match[i]; } tmp_buff[pmatch[0].rm_so] = 0;// printf("Match added [%s]\n", tmp_buff); PHP_VAR_NODE *match_val = array_push_back(result); match_val->value.type = PHP_VAL_STRING; match_val->value.str_val = strdup(tmp_buff); str_2_match += pmatch[0].rm_eo; } PHP_VAR_NODE *match_val = array_push_back(result); match_val->value.type = PHP_VAL_STRING; match_val->value.str_val = strdup(str_2_match); delete [] pmatch; delete [] tmp_buff; regfree(&preg);}#ifdef ENABLE_NLSvoid php_native_gettext(PHP_VALUE_NODE *result){ PHP_SCOPE_ITEM *si_str = get_scope_item(g_current_scope, "__param_0"); PHP_VALUE_NODE *str = &si_str->var->value; if ( si_str ) { cast_value_str(str); } else { php_report_error(PHP_ERROR, "Invalid or missing argument 'msgid' for 'gettext'");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -