2019-08-23 11:56:54 -07:00
|
|
|
using System.Text;
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
namespace System.IO
|
|
|
|
{
|
|
|
|
public class StreamWriter
|
|
|
|
{
|
|
|
|
Stream mStream ~ if (mOwnsStream) delete _;
|
|
|
|
public bool mOwnsStream;
|
|
|
|
public Encoding mEncoding;
|
|
|
|
|
|
|
|
public bool AutoFlush;
|
|
|
|
|
2019-09-10 11:25:53 -07:00
|
|
|
public this()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-23 11:56:54 -07:00
|
|
|
public this(Stream stream, Encoding encoding, int32 bufferSize, bool ownsStream = false)
|
|
|
|
{
|
|
|
|
Debug.Assert(encoding != null);
|
|
|
|
mStream = stream;
|
|
|
|
mEncoding = encoding;
|
|
|
|
mOwnsStream = ownsStream;
|
|
|
|
}
|
|
|
|
|
2019-09-10 11:25:53 -07:00
|
|
|
public Result<void, FileOpenError> Create(StringView fileName)
|
|
|
|
{
|
|
|
|
Debug.Assert(mStream == null);
|
|
|
|
|
|
|
|
var fileStream = new FileStream();
|
|
|
|
mStream = fileStream;
|
|
|
|
mEncoding = .UTF8;
|
|
|
|
mOwnsStream = true;
|
|
|
|
|
|
|
|
if (fileStream.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite) case .Err(let err))
|
|
|
|
return .Err(err);
|
|
|
|
|
|
|
|
return .Ok;
|
|
|
|
}
|
|
|
|
|
2019-08-23 11:56:54 -07:00
|
|
|
public Result<void> Write(Span<uint8> data)
|
|
|
|
{
|
|
|
|
var spanLeft = data;
|
|
|
|
while (!spanLeft.IsEmpty)
|
|
|
|
{
|
|
|
|
int bytesWritten = Try!(mStream.TryWrite(spanLeft));
|
|
|
|
spanLeft.Adjust(bytesWritten);
|
|
|
|
}
|
|
|
|
if (AutoFlush)
|
|
|
|
Flush();
|
|
|
|
return .Ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Result<void> Write(StringView str)
|
|
|
|
{
|
|
|
|
var curSpan = str;
|
|
|
|
|
|
|
|
Span<uint8> outSpan = scope uint8[4096];
|
|
|
|
|
|
|
|
while (!curSpan.IsEmpty)
|
|
|
|
{
|
|
|
|
switch (mEncoding.Encode(curSpan, outSpan))
|
|
|
|
{
|
|
|
|
case .Ok(let encodedBytes):
|
|
|
|
Try!(Write(Span<uint8>(outSpan.Ptr, encodedBytes)));
|
|
|
|
return .Ok;
|
|
|
|
case .Err(let err):
|
|
|
|
switch (err)
|
|
|
|
{
|
|
|
|
case .PartialEncode(int inChars, int encodedBytes):
|
|
|
|
Try!(Write(Span<uint8>(outSpan.Ptr, encodedBytes)));
|
|
|
|
curSpan.Adjust(inChars);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return .Ok;
|
|
|
|
}
|
2022-01-01 21:22:18 -03:00
|
|
|
|
|
|
|
public Result<void> WriteLine()
|
|
|
|
{
|
|
|
|
return Write("\n");
|
|
|
|
}
|
2019-08-23 11:56:54 -07:00
|
|
|
|
|
|
|
public Result<void> WriteLine(StringView str)
|
|
|
|
{
|
|
|
|
Try!(Write(str));
|
|
|
|
return Write("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
public Result<void> Write(StringView fmt, params Object[] args)
|
|
|
|
{
|
|
|
|
return Write(scope String()..AppendF(fmt, params args));
|
|
|
|
}
|
|
|
|
|
|
|
|
public Result<void> WriteLine(StringView fmt, params Object[] args)
|
|
|
|
{
|
|
|
|
Try!(Write(scope String()..AppendF(fmt, params args)));
|
|
|
|
return Write("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Flush()
|
|
|
|
{
|
|
|
|
mStream.Flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|