1.Cè¯è¨ printfåscanfçå®ç°
2.å¦ä½ç¨cè¯è¨å¨windowså¹³å°ä¸å¼åphp extension
Cè¯è¨ printfåscanfçå®ç°
/* Write formatted output to stdout from the format string FORMAT. */
/* VARARGS1 */
int
__printf (const char *format,喜鹊登枝指标源码 ...)
{
va_list arg;
int done;
va_start (arg, format);
done = vfprintf (stdout, format, arg);
va_end (arg);
return done;
}
int _scanf(char (*get)(void), void (*unget)(char), CONST char *fmt, va_list va)
{
int is_long, c, base;
char *vp;
char s[MAX+1];
int converted = 0;
while (c = *fmt++)
{
if (c == '%')
{
if (*fmt == 'l')
{
is_long = 1;
fmt++;
}
else
is_long = 0;
vp = va_arg(va, void *);
switch (*fmt)
{
case 'c':
*(char *)vp = get();
converted++;
break;
case 'o': base = 8; goto read_strtoul;
case 'u': base = ; goto read_strtoul;
case 'X':
case 'x': base = ;
read_strtoul:
converted++;
ReadInteger(s, get, unget, base);
if (is_long)
*(unsigned long *)vp = strtoul(s, 0, base);
else
*(unsigned *)vp = strtoul(s, 0, base);
break;
case 'd':
converted++;
ReadInteger(s, get, unget, );
if (is_long)
*(long *)vp = strtol(s, 0, );
else
*(int *)vp = strtol(s, 0, );
break;
case 's':
converted++;
ReadString(vp, get, unget);
break;
default:
puts("unsupported format");
break;
}
fmt++;
}
else if (isspace(c))
{
while ((c = get()) && isspace(c))
;
unget(c);
}
else if (get() != c)
break;
}
return converted;
}
å¦ä½ç¨cè¯è¨å¨windowså¹³å°ä¸å¼åphp extension
ãä½ä½¿ç¨Cè¯è¨åPHPæ©å±
ããå½æ°åè½ï¼phpé¢æ´æ°ç¬¦å·æ°å ¶å é¨å®ç°å ¶å®longunsigned longäºä½æºå¨è¯´phpè½è¡¨ç¤ºæ´æ°2^-1è¬åºç¨ç¢°äº2^-1äº2^æ°è½ç¨å符串表示äºmixed int_ext(string in)说å符串in表示æ´æ°äº2^-1è¿æ´æ°äºè¿å符串
ããåæ©å±æ¥éª¤ï¼ï¼é¦å éè¦è½½phpæºç è½½php-5.3.ï¼
ãã1建ç«æ©å±éª¨æ¶
ãã[plain] view plaincopyprint?
ãã.cd php-5.3./ext
ãã../ext_skel --extname=int_ext
ããcd php-5.3./ext
ãã./ext_skel --extname=int_ext
ãã2ä¿®æ¹ç¼è¯åæ°
ãã[plain] view plaincopyprint?
ãã.cd php-5.3./ext/int_ext
ãã.vi config.m4
ããcd php-5.3./ext/int_ext
ããvi config.m4æ PHP_ARG_ENABLE(int_ext, whether to enable int_ext support
ãã[ --enable-int_ext Enable int_ext support]) 两è¡åé¢dnl ä¿®æ¹ï¼
ãã[plain] view plaincopyprint?
ãã.1. dnl Otherwise use enable:
ãã.2. PHP_ARG_ENABLE(int_ext, whether to enable int_ext support,
ãã.3. dnl Make sure that the comment is aligned:
ãã.4. [ --enable-int_ext Enable int_ext support])
ãã1. dnl Otherwise use enable:
ãã2. PHP_ARG_ENABLE(int_ext, whether to enable int_ext support,
ãã3. dnl Make sure that the comment is aligned:
ãã4. [ --enable-int_ext Enable int_ext support])
ãã3ç¼åC代ç
ãã[plain] view plaincopyprint?
ãã.cd php-5.3./ext/int_ext
ãã.vi php_int_ext.h
ãã.# PHP_FUNCTION(confirm_int_ext_compiled); é¢æ°å¢è¡ PHP_FUNCTION(int_ext);
ããcd php-5.3./ext/int_ext
ããvi php_int_ext.h
ãã# PHP_FUNCTION(confirm_int_ext_compiled); é¢æ°å¢è¡ PHP_FUNCTION(int_ext);[plain] view plaincopyprint?
ãã.cd php-5.3./ext/int_ext
ãã.vi int_ext.c
ãã.#PHP_FE(confirm_int_ext_compiled, NULL) é¢æ·»å PHP_FE(int_ext, NULL)æ·»å ï¼
ãã.1. zend_function_entry int_ext_functions[] = {
ãã.2. PHP_FE(confirm_int_ext_compiled, NULL) /* For testing, remove later. */
ãã.3. PHP_FE(int_ext, NULL) /* For testing, remove later. */
ãã.4. { NULL, NULL, NULL} /* Must be the last line in int_ext_functions[] */
ãã.5. };
ããcd php-5.3./ext/int_ext
ããvi int_ext.c
ãã#PHP_FE(confirm_int_ext_compiled, NULL) é¢æ·»å PHP_FE(int_ext, NULL)æ·»å ï¼
ãã1. zend_function_entry int_ext_functions[] = {
ãã2. PHP_FE(confirm_int_ext_compiled, NULL) /* For testing, remove later. */
ãã3. PHP_FE(int_ext, NULL) /* For testing, remove later. */
ãã4. { NULL, NULL, NULL} /* Must be the last line in int_ext_functions[] */
ãã5. };
ããæ ¸ä»£ç ï¼
ãã[plain] view plaincopyprint?
ãã.PHP_FUNCTION(int_ext)
ãã.{
ãã. char * str = NULL;
ãã. int str_len;
ãã. int argc = ZEND_NUM_ARGS();
ãã. if(zend_parse_parameters(argc TSRMLS_CC,"s",&str,&str_len) == FAILURE)
ãã. return ;
ãã. char * result;
ãã. int result_length = str_len;
ãã. result = (char *) emalloc(result_length + 1);
ãã. memcpy(result,str,result_length);
ãã. unsigned long result_num = strtoul(result, NULL, );
ãã. int sizeoflong sizeof(long);
ãã. unsigned long max_long = 1 << (sizeoflong * 8 -1);
ãã. if(result_num < max_long)
ãã. {
ãã. RETURN_LONG(result_num);
ãã. }
ãã. else
ãã. {
ãã. RESULT_STRINGL(result, result_length, 0);
ãã. }
ãã.}
ããPHP_FUNCTION(int_ext)
ãã{
ããchar * str = NULL;
ããint str_len;
ããint argc = ZEND_NUM_ARGS();
ããif(zend_parse_parameters(argc TSRMLS_CC,"s",&str,&str_len) == FAILURE)
ããreturn ;
ããchar * result;
ããint result_length = str_len;
ããresult = (char *) emalloc(result_length + 1);
ããmemcpy(result,str,result_length);
ããunsigned long result_num = strtoul(result, NULL, );
ããint sizeoflong sizeof(long);
ããunsigned long max_long = 1 << (sizeoflong * 8 -1);
ããif(result_num < max_long)
ãã{
ããRETURN_LONG(result_num);
ãã}
ããelse
ãã{
ããRESULT_STRINGL(result, result_length, 0);
ãã}
ãã}
ãã4ç¼è¯
ãã[plain] view plaincopyprint?
ãã.cd php-5.3./ext/int_ext
ãã./usr/local/php/bin/pphpize
ãã../configure --with-php-config=/usr/local/php/bin/php-config
ãã.make
ãã.make install
ããcd php-5.3./ext/int_ext
ãã/usr/local/php/bin/pphpize
ãã./configure --with-php-config=/usr/local/php/bin/php-config
ããmake
ããmake install
ãã产soæä»¶ï¼ /usr/local/php/lib/php/extensions/no-debug-non-zts-/int_ext.so
ããä¿®æ¹php.ini æ·»å æ©å±extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-/"
ãã[int_ext]
ããextension = int_ext.so
ãã5æµè¯
ãã[plain] view plaincopyprint?
ãã.$a = int_ext("");
ãã.var_dump($a);
ãã.$a = int_ext("");
ãã.var_dump($a);
ãã$a = int_ext("");
ããvar_dump($a);
ãã$a = int_ext("");
ããvar_dump($a);
ããç»è¾:
ãã[plain] view plaincopyprint?
ãã.string() ""
ãã.int()