[PHP-DEV] [PATCH] ncurses_functions.c

2002-03-27 Thread Vlad Bosinceanu

PHP segfaults if ncurses_refresh is called before ncurses_init...

Index: ncurses_functions.c
===
RCS file: /repository/php4/ext/ncurses/ncurses_functions.c,v
retrieving revision 1.18
diff -u -r1.18 ncurses_functions.c
--- ncurses_functions.c	28 Feb 2002 08:26:27 -	1.18
+++ ncurses_functions.c	27 Mar 2002 13:59:30 -
 -81,6 +81,8 
Stops using ncurses, clean up the screen */
 PHP_FUNCTION(ncurses_end)
 {
+	keypad(stdscr, FALSE);/* disable keyboard mapping */
+	php_ncurses_init_called(0);			/* set call_status to 0 */
 	RETURN_LONG(endwin()); /* endialize the curses library */
 }
 /* }}} */
 -108,9 +110,24 
 	initscr(); /* initialize the curses library */
 	keypad(stdscr, TRUE);  /* enable keyboard mapping */
 	(void) nonl(); /* tell curses not to do NL-CR/NL on output */
-	(void) cbreak();   /* take input chars one at a time, no wait for \n */}
+	(void) cbreak();   /* take input chars one at a time, no wait for \n */
+	php_ncurses_init_called(1);
+}
 /* }}} */
 
+int php_ncurses_init_called(int do_this) {
+	static int call_status = 0;
+	switch(do_this) {
+		case 0:
+		case 1:
+			call_status = do_this;
+			break;
+		default:
+			return call_status;
+			break;
+	}
+}
+
 /* {{{ proto int ncurses_init_pair(int pair, int fg, int bg)
Allocates a color pair */
 PHP_FUNCTION(ncurses_init_pair)
 -160,11 +177,15 
 }
 /* }}} */
 
-/* {{{ proto int ncurses_refresh(int ch)
+/* {{{ proto int ncurses_refresh(void)
Refresh screen */
 PHP_FUNCTION(ncurses_refresh)
 {
-	RETURN_LONG(refresh());
+	if(php_ncurses_init_called(NULL)) {
+		RETURN_LONG(refresh());
+	} else {
+		php_error(E_WARNING, ncurses_refresh: called before ncurses_init);
+	}
 }
 /* }}} */
 


-- 
PHP Development Mailing List http://www.php.net/
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP-DEV] Re: is_array_multidimensional

2001-12-29 Thread Vlad Bosinceanu

Andi Gutmans wrote:

 At 10:42 PM 12/29/2001 +0100, Stig Venaas wrote:
 
 On Sat, Dec 29, 2001 at 11:13:11PM +0200, Andi Gutmans wrote:
  I agree with Jim. Arrays can contain things. Things can also be other
  arrays. You can have an array which contains two other arrays and four
  integers. I don't think we should add these functions because they are
  wrong *especially* the is_array_multidimensional().

 Agree

  Of course, if there's a good reason to have them and we're all 
 convinced
  that the reasons are good we could add them.
  Can you please roll back that patch and open up a discussion with 
 examples
  of why this functionality is needed? It might even lead to a different
  solution.

 One solution (not so sure I like it myself), could be a function that
 tells whether an array contains values of a given type. Then you could
 check if the array contained an array (or any other type). I've never
 had a use for such myself.
 
 
 Let's first try and understand the problem :) In what case does the user 
 need these functions and how often does this happen to people. Once we 
 understand the problem well we can think together of a good solution.
 
 Andi
 

Oh well... I tried to find a use for it, but failed :(. I wrote it for 
no reason, thinking it would be useful to someone, somewhere... So i 
guess i agree with all of you (more or less).


-- 
PHP Development Mailing List http://www.php.net/
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP-DEV] Recursive count - diffs.

2001-12-26 Thread Vlad Bosinceanu

Hacked count() to count all the elements in an array in a recursive manner.
It takes two arguments; the second is optional, and can be one of 
COUNT_NORMAL (good old count) or COUNT_RECURSIVE.

- Vlad


Index: zend_hash.c
===
RCS file: /repository/Zend/zend_hash.c,v
retrieving revision 1.82
diff -u -r1.82 zend_hash.c
--- zend_hash.c 2001/12/04 17:56:19 1.82
+++ zend_hash.c 2001/12/26 15:42:00
@@ -19,13 +19,14 @@
 
 
 #include zend.h
+#include zend_operators.h
 
 #include stdio.h
 
 #ifdef HAVE_STDLIB_H
 # include stdlib.h
 #endif
-
+#include zend_API.h
 #define HANDLE_NUMERIC(key, length, func) {   
 \
register char *tmp=key;
 \
   
   
  \
@@ -1257,6 +1258,30 @@
return SUCCESS;
 }
 
+ZEND_API int zend_hash_count_recursive(zval **array, zval **mode)
+{
+   long cnt = 0, i;
+   zval **element;
+HashTable *target_hash;
+   target_hash = HASH_OF(*array);
+   if(Z_TYPE_PP(array)==IS_ARRAY  Z_LVAL_PP(mode)==1)
+   {
+   cnt += zend_hash_num_elements(target_hash);
+   for(i=0;izend_hash_num_elements(target_hash);i++)
+   {
+   if(zend_hash_index_find(Z_ARRVAL_PP(array), i, (void **) 
+element)==SUCCESS)
+   {
+   (*mode)-type = IS_LONG;
+   (*mode)-value.lval = 1;
+   cnt += zend_hash_count_recursive(element, mode);
+   }
+   }
+   }
+   else if(Z_TYPE_PP(array)==IS_ARRAY  Z_LVAL_PP(mode)==0)
+   cnt += zend_hash_num_elements(target_hash);
+   return cnt;
+}
+   
 ZEND_API ulong zend_hash_next_free_element(HashTable *ht)
 {
IS_CONSISTENT(ht);


Index: array.c
===
RCS file: /repository/php4/ext/standard/array.c,v
retrieving revision 1.149
diff -u -r1.149 array.c
--- array.c 22 Dec 2001 11:49:56 -  1.149
+++ array.c 26 Dec 2001 15:39:12 -
@@ -25,7 +25,6 @@
 
 #include php.h
 #include php_ini.h
-#include zend_operators.h
 #include stdarg.h
 #include stdlib.h
 #include math.h
@@ -68,6 +67,8 @@
 #define CASE_LOWER  0
 #define CASE_UPPER  1
 
+#define COUNT_NORMAL   0
+#define COUNT_RECURSIVE1
 
 PHP_MINIT_FUNCTION(array)
 {
@@ -90,6 +91,9 @@
REGISTER_LONG_CONSTANT(CASE_LOWER, CASE_LOWER, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT(CASE_UPPER, CASE_UPPER, CONST_CS | CONST_PERSISTENT);
 
+   REGISTER_LONG_CONSTANT(COUNT_NORMAL, COUNT_NORMAL, CONST_CS | 
+CONST_PERSISTENT);
+   REGISTER_LONG_CONSTANT(COUNT_RECURSIVE, COUNT_RECURSIVE, CONST_CS | 
+CONST_PERSISTENT);
+   
return SUCCESS;
 }
 
@@ -223,26 +227,27 @@
 }
 /* }}} */
 
-/* {{{ proto int count(mixed var)
+/* {{{ proto int count(mixed var [, int mode])
Count the number of elements in a variable (usually an array) */
 PHP_FUNCTION(count)
 {
-   pval **array;
-   HashTable *target_hash;
+   pval **array, **mode;
 
-   if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, array) == FAILURE) {
+   if (ZEND_NUM_ARGS()  1 || ZEND_NUM_ARGS()  2 || zend_get_parameters_ex(1, 
+array) == FAILURE) {
WRONG_PARAM_COUNT;
}
-   target_hash = HASH_OF(*array);
-   if (!target_hash) {
-   if (Z_TYPE_PP(array) == IS_NULL) {
-   RETURN_LONG(0);
-   } else {
-   RETURN_LONG(1);
-   }
+   if(ZEND_NUM_ARGS() == 2) {
+   zend_get_parameters_ex(2, array, mode);
+   } else {
+   (*mode)-type = IS_LONG;
+   (*mode)-value.lval = 1;
+   }
+   if(Z_TYPE_PP(array)==IS_ARRAY) {
+   RETURN_LONG(zend_hash_count_recursive(array, mode));
+   } else {
+   /* return 1 for non-array arguments */
+   RETURN_LONG(1);
}
-
-   RETURN_LONG(zend_hash_num_elements(target_hash));
 }
 /* }}} */
 



-- 
PHP Development Mailing List http://www.php.net/
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]