PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/php5/ext/standard/type.c

http://github.com/vpj/PHP-Extension-API
C | 397 lines | 258 code | 53 blank | 86 comment | 66 complexity | 97da7552e58971f211e9dd0bc3332461 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2009 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: Rasmus Lerdorf <rasmus@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id: type.c,v 1.30.2.2.2.3.2.11 2009/04/02 09:56:33 dmitry Exp $ */
  19. #include "php.h"
  20. #include "php_incomplete_class.h"
  21. /* {{{ proto string gettype(mixed var)
  22. Returns the type of the variable */
  23. PHP_FUNCTION(gettype)
  24. {
  25. zval **arg;
  26. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
  27. return;
  28. }
  29. switch (Z_TYPE_PP(arg)) {
  30. case IS_NULL:
  31. RETVAL_STRING("NULL", 1);
  32. break;
  33. case IS_BOOL:
  34. RETVAL_STRING("boolean", 1);
  35. break;
  36. case IS_LONG:
  37. RETVAL_STRING("integer", 1);
  38. break;
  39. case IS_DOUBLE:
  40. RETVAL_STRING("double", 1);
  41. break;
  42. case IS_STRING:
  43. RETVAL_STRING("string", 1);
  44. break;
  45. case IS_ARRAY:
  46. RETVAL_STRING("array", 1);
  47. break;
  48. case IS_OBJECT:
  49. RETVAL_STRING("object", 1);
  50. /*
  51. {
  52. char *result;
  53. int res_len;
  54. res_len = sizeof("object of type ")-1 + Z_OBJCE_P(arg)->name_length;
  55. spprintf(&result, 0, "object of type %s", Z_OBJCE_P(arg)->name);
  56. RETVAL_STRINGL(result, res_len, 0);
  57. }
  58. */
  59. break;
  60. case IS_RESOURCE:
  61. {
  62. char *type_name;
  63. type_name = zend_rsrc_list_get_rsrc_type(Z_LVAL_PP(arg) TSRMLS_CC);
  64. if (type_name) {
  65. RETVAL_STRING("resource", 1);
  66. break;
  67. }
  68. }
  69. default:
  70. RETVAL_STRING("unknown type", 1);
  71. }
  72. }
  73. /* }}} */
  74. /* {{{ proto bool settype(mixed var, string type)
  75. Set the type of the variable */
  76. PHP_FUNCTION(settype)
  77. {
  78. zval **var;
  79. char *type;
  80. int type_len = 0;
  81. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zs", &var, &type, &type_len) == FAILURE) {
  82. return;
  83. }
  84. if (!strcasecmp(type, "integer")) {
  85. convert_to_long(*var);
  86. } else if (!strcasecmp(type, "int")) {
  87. convert_to_long(*var);
  88. } else if (!strcasecmp(type, "float")) {
  89. convert_to_double(*var);
  90. } else if (!strcasecmp(type, "double")) { /* deprecated */
  91. convert_to_double(*var);
  92. } else if (!strcasecmp(type, "string")) {
  93. convert_to_string(*var);
  94. } else if (!strcasecmp(type, "array")) {
  95. convert_to_array(*var);
  96. } else if (!strcasecmp(type, "object")) {
  97. convert_to_object(*var);
  98. } else if (!strcasecmp(type, "bool")) {
  99. convert_to_boolean(*var);
  100. } else if (!strcasecmp(type, "boolean")) {
  101. convert_to_boolean(*var);
  102. } else if (!strcasecmp(type, "null")) {
  103. convert_to_null(*var);
  104. } else if (!strcasecmp(type, "resource")) {
  105. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot convert to resource type");
  106. RETURN_FALSE;
  107. } else {
  108. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid type");
  109. RETURN_FALSE;
  110. }
  111. RETVAL_TRUE;
  112. }
  113. /* }}} */
  114. /* {{{ proto int intval(mixed var [, int base])
  115. Get the integer value of a variable using the optional base for the conversion */
  116. PHP_FUNCTION(intval)
  117. {
  118. zval **num;
  119. long arg_base;
  120. int base;
  121. switch (ZEND_NUM_ARGS()) {
  122. case 1:
  123. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &num) == FAILURE) {
  124. return;
  125. }
  126. base = 10;
  127. break;
  128. case 2:
  129. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zl", &num, &arg_base) == FAILURE) {
  130. return;
  131. }
  132. base = arg_base;
  133. break;
  134. default:
  135. WRONG_PARAM_COUNT;
  136. }
  137. RETVAL_ZVAL(*num, 1, 0);
  138. convert_to_long_base(return_value, base);
  139. }
  140. /* }}} */
  141. /* {{{ proto float floatval(mixed var)
  142. Get the float value of a variable */
  143. PHP_FUNCTION(floatval)
  144. {
  145. zval **num;
  146. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &num) == FAILURE) {
  147. return;
  148. }
  149. RETVAL_ZVAL(*num, 1, 0);
  150. convert_to_double(return_value);
  151. }
  152. /* }}} */
  153. /* {{{ proto string strval(mixed var)
  154. Get the string value of a variable */
  155. PHP_FUNCTION(strval)
  156. {
  157. zval **num, *tmp;
  158. zval expr_copy;
  159. int use_copy;
  160. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &num) == FAILURE) {
  161. return;
  162. }
  163. zend_make_printable_zval(*num, &expr_copy, &use_copy);
  164. if (use_copy) {
  165. tmp = &expr_copy;
  166. RETVAL_ZVAL(tmp, 0, 0);
  167. } else {
  168. RETVAL_ZVAL(*num, 1, 0);
  169. }
  170. }
  171. /* }}} */
  172. static void php_is_type(INTERNAL_FUNCTION_PARAMETERS, int type)
  173. {
  174. zval **arg;
  175. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
  176. RETURN_FALSE;
  177. }
  178. if (Z_TYPE_PP(arg) == type) {
  179. if (type == IS_OBJECT) {
  180. zend_class_entry *ce;
  181. if(Z_OBJ_HT_PP(arg)->get_class_entry == NULL) {
  182. /* if there's no get_class_entry it's not a PHP object, so it can't be INCOMPLETE_CLASS */
  183. RETURN_TRUE;
  184. }
  185. ce = Z_OBJCE_PP(arg);
  186. if (!strcmp(ce->name, INCOMPLETE_CLASS)) {
  187. RETURN_FALSE;
  188. }
  189. }
  190. if (type == IS_RESOURCE) {
  191. char *type_name;
  192. type_name = zend_rsrc_list_get_rsrc_type(Z_LVAL_PP(arg) TSRMLS_CC);
  193. if (!type_name) {
  194. RETURN_FALSE;
  195. }
  196. }
  197. RETURN_TRUE;
  198. } else {
  199. RETURN_FALSE;
  200. }
  201. }
  202. /* {{{ proto bool is_null(mixed var)
  203. Returns true if variable is null */
  204. PHP_FUNCTION(is_null)
  205. {
  206. php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_NULL);
  207. }
  208. /* }}} */
  209. /* {{{ proto bool is_resource(mixed var)
  210. Returns true if variable is a resource */
  211. PHP_FUNCTION(is_resource)
  212. {
  213. php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_RESOURCE);
  214. }
  215. /* }}} */
  216. /* {{{ proto bool is_bool(mixed var)
  217. Returns true if variable is a boolean */
  218. PHP_FUNCTION(is_bool)
  219. {
  220. php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_BOOL);
  221. }
  222. /* }}} */
  223. /* {{{ proto bool is_long(mixed var)
  224. Returns true if variable is a long (integer) */
  225. PHP_FUNCTION(is_long)
  226. {
  227. php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_LONG);
  228. }
  229. /* }}} */
  230. /* {{{ proto bool is_float(mixed var)
  231. Returns true if variable is float point*/
  232. PHP_FUNCTION(is_float)
  233. {
  234. php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_DOUBLE);
  235. }
  236. /* }}} */
  237. /* {{{ proto bool is_string(mixed var)
  238. Returns true if variable is a string */
  239. PHP_FUNCTION(is_string)
  240. {
  241. php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_STRING);
  242. }
  243. /* }}} */
  244. /* {{{ proto bool is_array(mixed var)
  245. Returns true if variable is an array */
  246. PHP_FUNCTION(is_array)
  247. {
  248. php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_ARRAY);
  249. }
  250. /* }}} */
  251. /* {{{ proto bool is_object(mixed var)
  252. Returns true if variable is an object */
  253. PHP_FUNCTION(is_object)
  254. {
  255. php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_OBJECT);
  256. }
  257. /* }}} */
  258. /* {{{ proto bool is_numeric(mixed value)
  259. Returns true if value is a number or a numeric string */
  260. PHP_FUNCTION(is_numeric)
  261. {
  262. zval **arg;
  263. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
  264. return;
  265. }
  266. switch (Z_TYPE_PP(arg)) {
  267. case IS_LONG:
  268. case IS_DOUBLE:
  269. RETURN_TRUE;
  270. break;
  271. case IS_STRING:
  272. if (is_numeric_string(Z_STRVAL_PP(arg), Z_STRLEN_PP(arg), NULL, NULL, 0)) {
  273. RETURN_TRUE;
  274. } else {
  275. RETURN_FALSE;
  276. }
  277. break;
  278. default:
  279. RETURN_FALSE;
  280. break;
  281. }
  282. }
  283. /* }}} */
  284. /* {{{ proto bool is_scalar(mixed value)
  285. Returns true if value is a scalar */
  286. PHP_FUNCTION(is_scalar)
  287. {
  288. zval **arg;
  289. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Z", &arg) == FAILURE) {
  290. return;
  291. }
  292. switch (Z_TYPE_PP(arg)) {
  293. case IS_BOOL:
  294. case IS_DOUBLE:
  295. case IS_LONG:
  296. case IS_STRING:
  297. RETURN_TRUE;
  298. break;
  299. default:
  300. RETURN_FALSE;
  301. break;
  302. }
  303. }
  304. /* }}} */
  305. /* {{{ proto bool is_callable(mixed var [, bool syntax_only [, string callable_name]])
  306. Returns true if var is callable. */
  307. PHP_FUNCTION(is_callable)
  308. {
  309. zval *var, **callable_name = NULL;
  310. char *name;
  311. char *error;
  312. zend_bool retval;
  313. zend_bool syntax_only = 0;
  314. int check_flags = 0;
  315. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|bZ", &var,
  316. &syntax_only, &callable_name) == FAILURE) {
  317. return;
  318. }
  319. if (syntax_only) {
  320. check_flags |= IS_CALLABLE_CHECK_SYNTAX_ONLY;
  321. }
  322. if (ZEND_NUM_ARGS() > 2) {
  323. retval = zend_is_callable_ex(var, NULL, check_flags, &name, NULL, NULL, &error TSRMLS_CC);
  324. zval_dtor(*callable_name);
  325. ZVAL_STRING(*callable_name, name, 0);
  326. } else {
  327. retval = zend_is_callable_ex(var, NULL, check_flags, NULL, NULL, NULL, &error TSRMLS_CC);
  328. }
  329. if (error) {
  330. /* ignore errors */
  331. efree(error);
  332. }
  333. RETURN_BOOL(retval);
  334. }
  335. /* }}} */
  336. /*
  337. * Local variables:
  338. * tab-width: 4
  339. * c-basic-offset: 4
  340. * End:
  341. * vim600: sw=4 ts=4 fdm=marker
  342. * vim<600: sw=4 ts=4
  343. */