Skip to content

Commit

Permalink
fputcsv() correct parameters as in PHP
Browse files Browse the repository at this point in the history
- added $eol
- added $escape but not used
  • Loading branch information
jakubmisek committed Aug 16, 2024
1 parent fec3124 commit 2ee3d1b
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions src/Peachpie.Library/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Pchp.Library.Streams.PhpStreams;
Expand Down Expand Up @@ -526,30 +525,39 @@ static int GetCsvDisclosedTextEnd(string line, char delimiter, ref int i, char e
/// Affected by run-time quoting (data are unqouted before written)
/// (<see cref="LocalConfiguration.VariablesSection.QuoteRuntimeVariables"/>).
/// </remarks>
public static int fputcsv(Context ctx, PhpResource handle, PhpArray fields, char delimiter = DefaultCsvDelimiter, char enclosure = DefaultCsvEnclosure)
public static int fputcsv(Context ctx,
PhpResource stream,
PhpArray fields,
char separator = DefaultCsvDelimiter,
char enclosure = DefaultCsvEnclosure,
string escape = "\\",
string eol = "\n"
)
{
PhpStream stream = PhpStream.GetValid(handle, FileAccess.Write);
if (stream == null || !stream.CanWrite) return -1;
// TODO: {escape}

char[] special_chars = { delimiter, ' ', '\\', '\t', '\r', '\n' };
string str_enclosure = enclosure.ToString();
string str_delimiter = delimiter.ToString();
var handle = PhpStream.GetValid(stream, FileAccess.Write);
if (handle == null || !handle.CanWrite) return -1;

int initial_position = stream.WritePosition;
char[] special_chars = { separator, ' ', '\\', '\t', '\r', '\n' };
string str_enclosure = enclosure == '"' ? "\"" : enclosure.ToString();
string str_delimiter = separator == ',' ? "," : separator.ToString();

int initial_position = handle.WritePosition;
var enumerator = fields.GetFastEnumerator();
while (enumerator.MoveNext())
{
var str_field = StringUtils.StripCSlashes(enumerator.CurrentValue.ToString(ctx));

if (stream.WritePosition > initial_position)
stream.WriteString(str_delimiter);
if (handle.WritePosition > initial_position)
handle.WriteString(str_delimiter);

int special_char_index = str_field.IndexOfAny(special_chars);
int enclosure_index = str_field.IndexOf(enclosure);

if (special_char_index >= 0 || enclosure_index >= 0)
{
stream.WriteString(str_enclosure);
handle.WriteString(str_enclosure);

if (enclosure_index >= 0)
{
Expand All @@ -558,8 +566,8 @@ public static int fputcsv(Context ctx, PhpResource handle, PhpArray fields, char
for (; ; )
{
// writes string starting after the last enclosure and ending by the next one:
stream.WriteString(str_field.Substring(start, enclosure_index - start + 1));
stream.WriteString(str_enclosure);
handle.WriteString(str_field.Substring(start, enclosure_index - start + 1));
handle.WriteString(str_enclosure);

start = enclosure_index + 1;
if (start >= str_field.Length) break;
Expand All @@ -568,27 +576,27 @@ public static int fputcsv(Context ctx, PhpResource handle, PhpArray fields, char
if (enclosure_index < 0)
{
// remaining substring:
stream.WriteString(str_field.Substring(start));
handle.WriteString(str_field.Substring(start));
break;
}
}
}
else
{
stream.WriteString(str_field);
handle.WriteString(str_field);
}

stream.WriteString(str_enclosure);
handle.WriteString(str_enclosure);
}
else
{
stream.WriteString(str_field);
handle.WriteString(str_field);
}
}

stream.WriteString("\n");
handle.WriteString(eol);

return (initial_position == -1) ? stream.WritePosition : stream.WritePosition - initial_position;
return (initial_position == -1) ? handle.WritePosition : handle.WritePosition - initial_position;
}

#endregion
Expand Down

0 comments on commit 2ee3d1b

Please sign in to comment.