CVS update: php3/functions

From: Date: Tue, 27 Apr 1999 12:55:20 +0000
Subject: CVS update: php3/functions
Groups: php.dev 
Request: Send a blank email to php-dev+get-5270@lists.php.net to get a copy of this message
Date: Tuesday April 27, 1999 @ 8:55 Author: rasmus Update of /repository/php3/functions In directory php:/tmp/cvs-serv2764/functions Modified Files: file.c file.h Log Message: Add fgetcsv Index: php3/functions/file.c diff -u php3/functions/file.c:1.204 php3/functions/file.c:1.205 --- php3/functions/file.c:1.204 Wed Apr 14 01:14:48 1999 +++ php3/functions/file.c Tue Apr 27 08:55:20 1999 @@ -26,7 +26,7 @@ | Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> | +----------------------------------------------------------------------+ */ -/* $Id: file.c,v 1.204 1999/04/14 05:14:48 brianlmoon Exp $ */ +/* $Id: file.c,v 1.205 1999/04/27 12:55:20 rasmus Exp $ */ #ifdef THREAD_SAFE #include "tls.h" #endif @@ -212,6 +212,7 @@ PHP_FE(tempnam, NULL) PHP_FE(file, NULL) PHP_FE(flock, NULL) + PHP_FE(fgetcsv, NULL) PHP_FE(get_meta_tags, NULL) PHP_FE(set_socket_blocking, NULL) #if (0 && HAVE_SYS_TIME_H && HAVE_SETSOCKOPT && defined(SO_SNDTIMEO) && defined(SO_RCVTIMEO)) @@ -1481,6 +1482,123 @@ } RETURN_TRUE; +} +/* }}} */ + + +/* {{{ proto array fgetcsv(int fp, int length) + get line from file pointer and parse for CSV fields */ +void php3_fgetcsv(INTERNAL_FUNCTION_PARAMETERS) { + char *temp, *tptr, *bptr; + char delimiter = ','; /* allow this to be set as parameter if required in future version? */ + + /* first section exactly as php3_fgetss */ + + pval *fd, *bytes; + FILE *fp; + int id, len, br, type; + char *buf, *p, *rbuf, *rp, c, lc; + int issock=0; + int *sock,socketd=0; + TLS_VARS; + + if (ARG_COUNT(ht) != 2 || getParameters(ht, 2, &fd, &bytes) == FAILURE) { + WRONG_PARAM_COUNT; + } + + convert_to_long(fd); + convert_to_long(bytes); + + id = fd->value.lval; + len = bytes->value.lval; + + fp = php3_list_find(id,&type); + if (type==GLOBAL(wsa_fp)){ + issock=1; + sock = php3_list_find(id,&type); + socketd=*sock; + } + if ((!fp || (type!=GLOBAL(le_fp) && type!=GLOBAL(le_pp))) && (!socketd || type!=GLOBAL(wsa_fp))) { + php3_error(E_WARNING, "Unable to find file identifier %d", id); + RETURN_FALSE; + } + + buf = emalloc(sizeof(char) * (len + 1)); + /*needed because recv doesnt set null char at end*/ + memset(buf,0,len+1); + if (!(issock?SOCK_FGETS(buf,len,socketd):(int)fgets(buf, len, fp))) { + efree(buf); + RETURN_FALSE; + } + + /* Now into new section that parses buf for comma/quote delimited fields */ + + /* Strip trailing space from buf */ + + bptr = buf; + tptr = buf + strlen(buf) -1; + while ( isspace(*tptr) && (tptr > bptr) ) *tptr--=0; + + /* add single space - makes it easier to parse trailing null field */ + *++tptr = ' '; + *++tptr = 0; + + /* reserve workspace for building each individual field */ + + temp = emalloc(sizeof(char) * len); /* unlikely but possible! */ + tptr=temp; + + /* Initialize return array */ + if (array_init(return_value) == FAILURE) { + efree(temp); + efree(buf); + RETURN_FALSE; + } + + /* Main loop to read CSV fields */ + /* NB this routine will return a single null entry for a blank line */ + + do { + /* 1. Strip any leading space */ + while isspace(*bptr) bptr++; + /* 2. Read field, leaving bptr pointing at start of next field */ + if (*bptr == '"') { + /* 2A. handle quote delimited field */ + bptr++; /* move on to first character in field */ + while (*bptr) { + if (*bptr == '"') { + /* handle the double-quote */ + if ( *(bptr+1) == '"') { + /* embedded double quotes */ + *tptr++ = *bptr; bptr +=2; + } else { + /* must be end of string - skip to start of next field or end */ + while ( (*bptr != delimiter) && *bptr ) bptr++; + if (*bptr == delimiter) bptr++; + *tptr=0; /* terminate temporary string */ + break; /* .. from handling this field - resumes at 3. */ + } + } else { + /* normal character */ + *tptr++ = *bptr++; + } + } + } else { + /* 2B. Handle non-quoted field */ + while ( (*bptr != delimiter) && *bptr ) *tptr++ = *bptr++; + *tptr=0; /* terminate temporary string */ + if (strlen(temp)) { + tptr--; + while (isspace(*tptr)) *tptr-- = 0; /* strip any trailing spaces */ + } + if (*bptr == delimiter) bptr++; + } + /* 3. Now pass our field back to php */ + add_next_index_string(return_value, temp, 1); + tptr=temp; + } while (*bptr); + efree(temp); + efree(buf); } /* }}} */ Index: php3/functions/file.h diff -u php3/functions/file.h:1.28 php3/functions/file.h:1.29 --- php3/functions/file.h:1.28 Wed Apr 14 01:12:42 1999 +++ php3/functions/file.h Tue Apr 27 08:55:20 1999 @@ -27,7 +27,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: file.h,v 1.28 1999/04/14 05:12:42 brianlmoon Exp $ */ +/* $Id: file.h,v 1.29 1999/04/27 12:55:20 rasmus Exp $ */ #ifndef _FILE_H #define _FILE_H @@ -50,6 +50,7 @@ extern void php3_fgetc(INTERNAL_FUNCTION_PARAMETERS); extern void php3_fgets(INTERNAL_FUNCTION_PARAMETERS); extern void php3_fgetss(INTERNAL_FUNCTION_PARAMETERS); +extern void php3_fgetcsv(INTERNAL_FUNCTION_PARAMETERS); extern void php3_fwrite(INTERNAL_FUNCTION_PARAMETERS); extern void php3_set_file_buffer(INTERNAL_FUNCTION_PARAMETERS); extern void php3_rewind(INTERNAL_FUNCTION_PARAMETERS); -- PHP Development Mailing List http://www.php.net/ To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net For help: php-dev-help@lists.php.net

« previous php.dev (#5270) next »