Justtechjobs.com Find a programming school near you






Online Campus Both


php4-beta | 200004

[PHP4BETA] cvs: /php4/ext/standard file.c From: Rasmus Lerdorf (rasmus <email protected>)
Date: 04/19/00

rasmus Wed Apr 19 06:04:18 2000 EDT

  Modified files:
    /php4/ext/standard file.c
  Log:
  fgetcsv() patch from Nick Talbott
  @ The fgetcsv() function now handles embedded end-of-line in a quoted field
  @ (Nick Talbott)
  
  
Index: php4/ext/standard/file.c
diff -u php4/ext/standard/file.c:1.72 php4/ext/standard/file.c:1.73
--- php4/ext/standard/file.c:1.72 Thu Mar 16 16:44:55 2000
+++ php4/ext/standard/file.c Wed Apr 19 06:03:47 2000
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: file.c,v 1.72 2000/03/17 00:44:55 sterling Exp $ */
+/* $Id: file.c,v 1.73 2000/04/19 13:03:47 rasmus Exp $ */
 
 /* Synced with php 3.0 revision 1.218 1999-06-16 [ssb] */
 
@@ -1629,7 +1629,7 @@
    Get line from file pointer and parse for CSV fields */
  
 PHP_FUNCTION(fgetcsv) {
- char *temp, *tptr, *bptr;
+ char *temp, *tptr, *bptr, *lineEnd;
         char delimiter = ','; /* allow this to be set as parameter */
 
         /* first section exactly as php_fgetss */
@@ -1688,15 +1688,18 @@
 
         /* Now into new section that parses buf for comma/quote delimited fields */
 
- /* Strip trailing space from buf */
+ /* Strip trailing space from buf, saving end of line in case required for quoted field */
 
- bptr = buf;
- tptr = buf + strlen(buf) -1;
- while ( isspace((int)*tptr) && (tptr > bptr) ) *tptr--=0;
+ lineEnd = emalloc(sizeof(char) * (len + 1));
+ bptr = buf;
+ tptr = buf + strlen(buf) -1;
+ while ( isspace(*tptr) && (tptr > bptr) ) tptr--;
+ tptr++;
+ strcpy(lineEnd, tptr);
 
         /* add single space - makes it easier to parse trailing null field */
- *++tptr = ' ';
- *++tptr = 0;
+ *tptr++ = ' ';
+ *tptr = 0;
 
         /* reserve workspace for building each individual field */
 
@@ -1705,6 +1708,7 @@
 
         /* Initialize return array */
         if (array_init(return_value) == FAILURE) {
+ efree(lineEnd);
                 efree(temp);
                 efree(buf);
                 RETURN_FALSE;
@@ -1736,6 +1740,26 @@
                                 } else {
                                 /* normal character */
                                         *tptr++ = *bptr++;
+
+ if (*bptr == 0) { /* embedded line end? */
+ *(tptr-1)=0; /* remove space character added on reading line */
+ strcat(temp,lineEnd); /* add the embedded line end to the field */
+
+ /* read a new line from input, as at start of routine */
+ memset(buf,0,len+1);
+ if (FP_FGETS(buf, len, socketd, (FILE*)what, issock) == NULL) {
+ efree(lineEnd); efree(temp); efree(buf);
+ RETURN_FALSE;
+ }
+ bptr = buf;
+ tptr = buf + strlen(buf) -1;
+ while ( isspace(*tptr) && (tptr > bptr) ) tptr--;
+ tptr++; strcpy(lineEnd, tptr);
+ *tptr++ = ' '; *tptr = 0;
+
+ tptr=temp; /* reset temp pointer to end of field as read so far */
+ while (*tptr) tptr++;
+ }
                                 }
                         }
                 } else {
@@ -1752,6 +1776,8 @@
                 add_next_index_string(return_value, temp, 1);
                 tptr=temp;
         } while (*bptr);
+
+ efree(lineEnd);
         efree(temp);
         efree(buf);
 }

-- 
PHP 4.0 Beta Mailing List <http://www.php.net/version4/>
To unsubscribe, e-mail: php4beta-unsubscribe <email protected>
For additional commands, e-mail: php4beta-help <email protected>
To contact the list administrators, e-mail: php4beta-admin <email protected>