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