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 :
19 : /* $Id: type.c 272374 2008-12-31 11:17:49Z sebastian $ */
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 2603 : {
28 : zval **arg;
29 :
30 2603 : if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
31 2 : WRONG_PARAM_COUNT;
32 : }
33 :
34 2601 : switch (Z_TYPE_PP(arg)) {
35 : case IS_NULL:
36 151 : RETVAL_STRING("NULL", 1);
37 151 : break;
38 :
39 : case IS_BOOL:
40 287 : RETVAL_STRING("boolean", 1);
41 287 : break;
42 :
43 : case IS_LONG:
44 259 : RETVAL_STRING("integer", 1);
45 259 : break;
46 :
47 : case IS_DOUBLE:
48 478 : RETVAL_STRING("double", 1);
49 478 : break;
50 :
51 : case IS_STRING:
52 1097 : RETVAL_STRING("string", 1);
53 1097 : break;
54 :
55 : case IS_ARRAY:
56 187 : RETVAL_STRING("array", 1);
57 187 : break;
58 :
59 : case IS_OBJECT:
60 136 : 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 136 : 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 1265 : {
93 : zval **var, **type;
94 : char *new_type;
95 :
96 1265 : if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &var, &type) == FAILURE) {
97 2 : WRONG_PARAM_COUNT;
98 : }
99 :
100 1263 : convert_to_string_ex(type);
101 1263 : new_type = Z_STRVAL_PP(type);
102 :
103 1263 : if (!strcasecmp(new_type, "integer")) {
104 105 : convert_to_long(*var);
105 1158 : } else if (!strcasecmp(new_type, "int")) {
106 107 : convert_to_long(*var);
107 1051 : } else if (!strcasecmp(new_type, "float")) {
108 94 : convert_to_double(*var);
109 957 : } else if (!strcasecmp(new_type, "double")) { /* deprecated */
110 118 : convert_to_double(*var);
111 839 : } else if (!strcasecmp(new_type, "string")) {
112 118 : convert_to_string(*var);
113 721 : } else if (!strcasecmp(new_type, "array")) {
114 118 : convert_to_array(*var);
115 603 : } else if (!strcasecmp(new_type, "object")) {
116 118 : convert_to_object(*var);
117 485 : } else if (!strcasecmp(new_type, "bool")) {
118 125 : convert_to_boolean(*var);
119 360 : } else if (!strcasecmp(new_type, "boolean")) {
120 123 : convert_to_boolean(*var);
121 237 : } else if (!strcasecmp(new_type, "null")) {
122 118 : convert_to_null(*var);
123 119 : } else if (!strcasecmp(new_type, "resource")) {
124 118 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot convert to resource type");
125 118 : RETURN_FALSE;
126 : } else {
127 1 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid type");
128 1 : RETURN_FALSE;
129 : }
130 1144 : RETVAL_TRUE;
131 : }
132 : /* }}} */
133 :
134 : /* {{{ proto int intval(mixed var [, int base])
135 : Get the integer value of a variable using the optional base for the conversion */
136 : PHP_FUNCTION(intval)
137 3819 : {
138 : zval **num, **arg_base;
139 : int base;
140 :
141 3819 : switch (ZEND_NUM_ARGS()) {
142 : case 1:
143 3709 : if (zend_get_parameters_ex(1, &num) == FAILURE) {
144 0 : WRONG_PARAM_COUNT;
145 : }
146 3709 : base = 10;
147 3709 : break;
148 :
149 : case 2:
150 108 : if (zend_get_parameters_ex(2, &num, &arg_base) == FAILURE) {
151 0 : WRONG_PARAM_COUNT;
152 : }
153 108 : convert_to_long_ex(arg_base);
154 108 : base = Z_LVAL_PP(arg_base);
155 108 : break;
156 :
157 : default:
158 2 : WRONG_PARAM_COUNT;
159 : }
160 :
161 3817 : RETVAL_ZVAL(*num, 1, 0);
162 3817 : convert_to_long_base(return_value, base);
163 : }
164 : /* }}} */
165 :
166 : /* {{{ proto float floatval(mixed var)
167 : Get the float value of a variable */
168 : PHP_FUNCTION(floatval)
169 152 : {
170 : zval **num;
171 :
172 152 : if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
173 8 : WRONG_PARAM_COUNT;
174 : }
175 :
176 144 : RETVAL_ZVAL(*num, 1, 0);
177 144 : convert_to_double(return_value);
178 : }
179 : /* }}} */
180 :
181 : /* {{{ proto string strval(mixed var)
182 : Get the string value of a variable */
183 : PHP_FUNCTION(strval)
184 100140 : {
185 : zval **num, *tmp;
186 : zval expr_copy;
187 : int use_copy;
188 :
189 100140 : if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &num) == FAILURE) {
190 4 : WRONG_PARAM_COUNT;
191 : }
192 :
193 100136 : zend_make_printable_zval(*num, &expr_copy, &use_copy);
194 100135 : if (use_copy) {
195 100087 : tmp = &expr_copy;
196 100087 : RETVAL_ZVAL(tmp, 0, 0);
197 : } else {
198 48 : RETVAL_ZVAL(*num, 1, 0);
199 : }
200 : }
201 : /* }}} */
202 :
203 : static void php_is_type(INTERNAL_FUNCTION_PARAMETERS, int type)
204 645286 : {
205 : zval **arg;
206 :
207 645286 : if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
208 24 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Only one argument expected");
209 24 : RETURN_FALSE;
210 : }
211 :
212 645262 : if (Z_TYPE_PP(arg) == type) {
213 25981 : if (type == IS_OBJECT) {
214 : zend_class_entry *ce;
215 47 : if(Z_OBJ_HT_PP(arg)->get_class_entry == NULL) {
216 : /* if there's no get_class_entry it's not a PHP object, so it can't be INCOMPLETE_CLASS */
217 0 : RETURN_TRUE;
218 : }
219 47 : ce = Z_OBJCE_PP(arg);
220 47 : if (!strcmp(ce->name, INCOMPLETE_CLASS)) {
221 1 : RETURN_FALSE;
222 : }
223 : }
224 25980 : if (type == IS_RESOURCE) {
225 : char *type_name;
226 77 : type_name = zend_rsrc_list_get_rsrc_type(Z_LVAL_PP(arg) TSRMLS_CC);
227 77 : if (!type_name) {
228 2 : RETURN_FALSE;
229 : }
230 : }
231 25978 : RETURN_TRUE;
232 : } else {
233 619281 : RETURN_FALSE;
234 : }
235 : }
236 :
237 :
238 : /* {{{ proto bool is_null(mixed var)
239 : Returns true if variable is null */
240 : PHP_FUNCTION(is_null)
241 13625 : {
242 13625 : php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_NULL);
243 13625 : }
244 : /* }}} */
245 :
246 : /* {{{ proto bool is_resource(mixed var)
247 : Returns true if variable is a resource */
248 : PHP_FUNCTION(is_resource)
249 91 : {
250 91 : php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_RESOURCE);
251 91 : }
252 : /* }}} */
253 :
254 : /* {{{ proto bool is_bool(mixed var)
255 : Returns true if variable is a boolean */
256 : PHP_FUNCTION(is_bool)
257 82 : {
258 82 : php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_BOOL);
259 82 : }
260 : /* }}} */
261 :
262 : /* {{{ proto bool is_long(mixed var)
263 : Returns true if variable is a long (integer) */
264 : PHP_FUNCTION(is_long)
265 2932 : {
266 2932 : php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_LONG);
267 2932 : }
268 : /* }}} */
269 :
270 : /* {{{ proto bool is_float(mixed var)
271 : Returns true if variable is float point*/
272 : PHP_FUNCTION(is_float)
273 372 : {
274 372 : php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_DOUBLE);
275 372 : }
276 : /* }}} */
277 :
278 : /* {{{ proto bool is_string(mixed var)
279 : Returns true if variable is a string */
280 : PHP_FUNCTION(is_string)
281 5830 : {
282 5830 : php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_STRING);
283 5830 : }
284 : /* }}} */
285 :
286 : /* {{{ proto bool is_array(mixed var)
287 : Returns true if variable is an array */
288 : PHP_FUNCTION(is_array)
289 622278 : {
290 622278 : php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_ARRAY);
291 622278 : }
292 : /* }}} */
293 :
294 : /* {{{ proto bool is_object(mixed var)
295 : Returns true if variable is an object */
296 : PHP_FUNCTION(is_object)
297 76 : {
298 76 : php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_OBJECT);
299 76 : }
300 : /* }}} */
301 :
302 : /* {{{ proto bool is_numeric(mixed value)
303 : Returns true if value is a number or a numeric string */
304 : PHP_FUNCTION(is_numeric)
305 109 : {
306 : zval **arg;
307 :
308 109 : if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
309 2 : WRONG_PARAM_COUNT;
310 : }
311 :
312 107 : switch (Z_TYPE_PP(arg)) {
313 : case IS_LONG:
314 : case IS_DOUBLE:
315 58 : RETURN_TRUE;
316 : break;
317 :
318 : case IS_STRING:
319 36 : if (is_numeric_string(Z_STRVAL_PP(arg), Z_STRLEN_PP(arg), NULL, NULL, 0)) {
320 21 : RETURN_TRUE;
321 : } else {
322 15 : RETURN_FALSE;
323 : }
324 : break;
325 :
326 : default:
327 13 : RETURN_FALSE;
328 : break;
329 : }
330 : }
331 : /* }}} */
332 :
333 : /* {{{ proto bool is_scalar(mixed value)
334 : Returns true if value is a scalar */
335 : PHP_FUNCTION(is_scalar)
336 62 : {
337 : zval **arg;
338 :
339 62 : if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg) == FAILURE) {
340 3 : WRONG_PARAM_COUNT;
341 : }
342 :
343 59 : switch (Z_TYPE_PP(arg)) {
344 : case IS_BOOL:
345 : case IS_DOUBLE:
346 : case IS_LONG:
347 : case IS_STRING:
348 37 : RETURN_TRUE;
349 : break;
350 :
351 : default:
352 22 : RETURN_FALSE;
353 : break;
354 : }
355 : }
356 : /* }}} */
357 :
358 : /* {{{ proto bool is_callable(mixed var [, bool syntax_only [, string callable_name]])
359 : Returns true if var is callable. */
360 : PHP_FUNCTION(is_callable)
361 588 : {
362 : zval **var, **syntax_only, **callable_name;
363 : char *name;
364 : zend_bool retval;
365 588 : zend_bool syntax = 0;
366 588 : int argc=ZEND_NUM_ARGS();
367 :
368 588 : if (argc < 1 || argc > 3 || zend_get_parameters_ex(argc, &var, &syntax_only, &callable_name) == FAILURE) {
369 2 : WRONG_PARAM_COUNT;
370 : }
371 :
372 586 : if (argc > 1) {
373 457 : convert_to_boolean_ex(syntax_only);
374 457 : syntax = Z_BVAL_PP(syntax_only);
375 : }
376 :
377 586 : if (argc > 2) {
378 228 : retval = zend_is_callable(*var, syntax, &name);
379 228 : zval_dtor(*callable_name);
380 228 : ZVAL_STRING(*callable_name, name, 0);
381 : } else {
382 358 : retval = zend_is_callable(*var, syntax, NULL);
383 : }
384 :
385 586 : RETURN_BOOL(retval);
386 : }
387 : /* }}} */
388 :
389 : /*
390 : * Local variables:
391 : * tab-width: 4
392 : * c-basic-offset: 4
393 : * End:
394 : * vim600: sw=4 ts=4 fdm=marker
395 : * vim<600: sw=4 ts=4
396 : */
|