66using System . Collections . Generic ;
77using System . Diagnostics ;
88using System . IO ;
9- using System . Linq ;
109using System . Text ;
1110using System . Threading . Tasks ;
1211using static Pchp . Library . Streams . PhpStreams ;
@@ -526,30 +525,39 @@ static int GetCsvDisclosedTextEnd(string line, char delimiter, ref int i, char e
526525 /// Affected by run-time quoting (data are unqouted before written)
527526 /// (<see cref="LocalConfiguration.VariablesSection.QuoteRuntimeVariables"/>).
528527 /// </remarks>
529- public static int fputcsv ( Context ctx , PhpResource handle , PhpArray fields , char delimiter = DefaultCsvDelimiter , char enclosure = DefaultCsvEnclosure )
528+ public static int fputcsv ( Context ctx ,
529+ PhpResource stream ,
530+ PhpArray fields ,
531+ char separator = DefaultCsvDelimiter ,
532+ char enclosure = DefaultCsvEnclosure ,
533+ string escape = "\\ " ,
534+ string eol = "\n "
535+ )
530536 {
531- PhpStream stream = PhpStream . GetValid ( handle , FileAccess . Write ) ;
532- if ( stream == null || ! stream . CanWrite ) return - 1 ;
537+ // TODO: {escape}
533538
534- char [ ] special_chars = { delimiter , ' ' , '\\ ' , '\t ' , '\r ' , '\n ' } ;
535- string str_enclosure = enclosure . ToString ( ) ;
536- string str_delimiter = delimiter . ToString ( ) ;
539+ var handle = PhpStream . GetValid ( stream , FileAccess . Write ) ;
540+ if ( handle == null || ! handle . CanWrite ) return - 1 ;
537541
538- int initial_position = stream . WritePosition ;
542+ char [ ] special_chars = { separator , ' ' , '\\ ' , '\t ' , '\r ' , '\n ' } ;
543+ string str_enclosure = enclosure == '"' ? "\" " : enclosure . ToString ( ) ;
544+ string str_delimiter = separator == ',' ? "," : separator . ToString ( ) ;
545+
546+ int initial_position = handle . WritePosition ;
539547 var enumerator = fields . GetFastEnumerator ( ) ;
540548 while ( enumerator . MoveNext ( ) )
541549 {
542550 var str_field = StringUtils . StripCSlashes ( enumerator . CurrentValue . ToString ( ctx ) ) ;
543551
544- if ( stream . WritePosition > initial_position )
545- stream . WriteString ( str_delimiter ) ;
552+ if ( handle . WritePosition > initial_position )
553+ handle . WriteString ( str_delimiter ) ;
546554
547555 int special_char_index = str_field . IndexOfAny ( special_chars ) ;
548556 int enclosure_index = str_field . IndexOf ( enclosure ) ;
549557
550558 if ( special_char_index >= 0 || enclosure_index >= 0 )
551559 {
552- stream . WriteString ( str_enclosure ) ;
560+ handle . WriteString ( str_enclosure ) ;
553561
554562 if ( enclosure_index >= 0 )
555563 {
@@ -558,8 +566,8 @@ public static int fputcsv(Context ctx, PhpResource handle, PhpArray fields, char
558566 for ( ; ; )
559567 {
560568 // writes string starting after the last enclosure and ending by the next one:
561- stream . WriteString ( str_field . Substring ( start , enclosure_index - start + 1 ) ) ;
562- stream . WriteString ( str_enclosure ) ;
569+ handle . WriteString ( str_field . Substring ( start , enclosure_index - start + 1 ) ) ;
570+ handle . WriteString ( str_enclosure ) ;
563571
564572 start = enclosure_index + 1 ;
565573 if ( start >= str_field . Length ) break ;
@@ -568,27 +576,27 @@ public static int fputcsv(Context ctx, PhpResource handle, PhpArray fields, char
568576 if ( enclosure_index < 0 )
569577 {
570578 // remaining substring:
571- stream . WriteString ( str_field . Substring ( start ) ) ;
579+ handle . WriteString ( str_field . Substring ( start ) ) ;
572580 break ;
573581 }
574582 }
575583 }
576584 else
577585 {
578- stream . WriteString ( str_field ) ;
586+ handle . WriteString ( str_field ) ;
579587 }
580588
581- stream . WriteString ( str_enclosure ) ;
589+ handle . WriteString ( str_enclosure ) ;
582590 }
583591 else
584592 {
585- stream . WriteString ( str_field ) ;
593+ handle . WriteString ( str_field ) ;
586594 }
587595 }
588596
589- stream . WriteString ( " \n " ) ;
597+ handle . WriteString ( eol ) ;
590598
591- return ( initial_position == - 1 ) ? stream . WritePosition : stream . WritePosition - initial_position ;
599+ return ( initial_position == - 1 ) ? handle . WritePosition : handle . WritePosition - initial_position ;
592600 }
593601
594602 #endregion
0 commit comments