aboutsummaryrefslogtreecommitdiff
path: root/lib/Utils/src
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-06-08 21:54:52 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2024-06-08 21:54:52 -0400
commitb4b506a4b6c7c1e90b5b0980e4cfe0460e7546a2 (patch)
treeaab51a3d671e90aea59df2d13aee08882a4b7dfa /lib/Utils/src
parent2160510fcc22a8574b0090fd91ca29072f45ab59 (diff)
some minor touchups
Diffstat (limited to 'lib/Utils/src')
-rw-r--r--lib/Utils/src/ArgumentList.cs26
-rw-r--r--lib/Utils/src/IO/ArrayPoolStreamBuffer.cs26
-rw-r--r--lib/Utils/src/IO/VnStreamWriter.cs36
3 files changed, 43 insertions, 45 deletions
diff --git a/lib/Utils/src/ArgumentList.cs b/lib/Utils/src/ArgumentList.cs
index c02ebee..7226b27 100644
--- a/lib/Utils/src/ArgumentList.cs
+++ b/lib/Utils/src/ArgumentList.cs
@@ -41,18 +41,16 @@ namespace VNLib.Utils
/// </summary>
/// <param name="args">The array of arguments to clone</param>
/// <exception cref="ArgumentNullException"></exception>
- public ArgumentList(string[] args)
- {
- ArgumentNullException.ThrowIfNull(args);
- _args = args.ToList();
- }
+ [Obsolete("Deprecated in preference to ctor(IEnumerable<string>)")]
+ public ArgumentList(string[] args) : this(args as IEnumerable<string>)
+ { }
/// <summary>
/// Initalizes the argument parser by copying the given argument list
/// </summary>
/// <param name="args">The argument list to clone</param>
/// <exception cref="ArgumentNullException"></exception>
- public ArgumentList(IReadOnlyList<string> args)
+ public ArgumentList(IEnumerable<string> args)
{
ArgumentNullException.ThrowIfNull(args);
_args = args.ToList();
@@ -102,5 +100,21 @@ namespace VNLib.Utils
///<inheritdoc/>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
+
+ /// <summary>
+ /// Captures the command line arguments from the currently executing process
+ /// and returns them as an ArgumentList
+ /// </summary>
+ /// <returns>The <see cref="ArgumentList"/> containing the current process's argument list</returns>
+ public static ArgumentList CaptureCurrentArgs()
+ {
+ /*
+ * Capture the current command line arguments and
+ * pop the first argument which is always the program
+ * name
+ */
+ string[] strings = Environment.GetCommandLineArgs();
+ return new ArgumentList(strings.Skip(1));
+ }
}
} \ No newline at end of file
diff --git a/lib/Utils/src/IO/ArrayPoolStreamBuffer.cs b/lib/Utils/src/IO/ArrayPoolStreamBuffer.cs
index b62412f..a943a5b 100644
--- a/lib/Utils/src/IO/ArrayPoolStreamBuffer.cs
+++ b/lib/Utils/src/IO/ArrayPoolStreamBuffer.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Utils
@@ -28,7 +28,13 @@ using System.Buffers;
namespace VNLib.Utils.IO
{
- internal class ArrayPoolStreamBuffer<T> : ISlindingWindowBuffer<T>
+ /// <summary>
+ /// Creates a new <see cref="ArrayPoolStreamBuffer{T}"/> from the
+ /// given array instance and <see cref="ArrayPool{T}"/> it came from.
+ /// </summary>
+ /// <param name="array">The rented array to use</param>
+ /// <param name="pool">The pool to return the array to when completed</param>
+ internal class ArrayPoolStreamBuffer<T>(T[] array, ArrayPool<T> pool) : ISlindingWindowBuffer<T>
{
/// <summary>
/// The shared <see cref="IStreamBufferFactory{T}"/> instance to allocate buffers
@@ -36,20 +42,8 @@ namespace VNLib.Utils.IO
/// </summary>
public static IStreamBufferFactory<T> Shared { get; } = new DefaultFactory();
- private readonly ArrayPool<T> _pool;
- private T[] _buffer;
-
- /// <summary>
- /// Creates a new <see cref="ArrayPoolStreamBuffer{T}"/> from the
- /// given array instance and <see cref="ArrayPool{T}"/> it came from.
- /// </summary>
- /// <param name="array">The rented array to use</param>
- /// <param name="pool">The pool to return the array to when completed</param>
- public ArrayPoolStreamBuffer(T[] array, ArrayPool<T> pool)
- {
- _pool = pool;
- _buffer = array;
- }
+ private readonly ArrayPool<T> _pool = pool;
+ private T[] _buffer = array;
///<inheritdoc/>
public int WindowStartPos { get; set; }
diff --git a/lib/Utils/src/IO/VnStreamWriter.cs b/lib/Utils/src/IO/VnStreamWriter.cs
index ddebc07..7d43f48 100644
--- a/lib/Utils/src/IO/VnStreamWriter.cs
+++ b/lib/Utils/src/IO/VnStreamWriter.cs
@@ -41,21 +41,29 @@ namespace VNLib.Utils.IO
/// Provides a memory optimized <see cref="TextWriter"/> implementation. Optimized for writing
/// to network streams
/// </summary>
- public class VnStreamWriter : TextWriter
+ /// <remarks>
+ /// Creates a new <see cref="VnStreamWriter"/> that writes encoded data to the base stream
+ /// and uses the specified buffer.
+ /// </remarks>
+ /// <param name="baseStream">The underlying stream to write data to</param>
+ /// <param name="encoding">The <see cref="Encoding"/> to use when writing to the stream</param>
+ /// <param name="buffer">The internal <see cref="ISlindingWindowBuffer{T}"/> to use</param>
+ /// <exception cref="ArgumentNullException"></exception>
+ public class VnStreamWriter(Stream baseStream, Encoding encoding, ISlindingWindowBuffer<byte> buffer) : TextWriter
{
- private readonly Encoder Enc;
+ private readonly Encoder Enc = encoding.GetEncoder();
- private readonly ISlindingWindowBuffer<byte> _buffer;
+ private readonly ISlindingWindowBuffer<byte> _buffer = buffer ?? throw new ArgumentNullException(nameof(buffer));
private bool closed;
/// <summary>
/// Gets the underlying stream that interfaces with the backing store
/// </summary>
- public virtual Stream BaseStream { get; }
+ public virtual Stream BaseStream { get; } = baseStream ?? throw new ArgumentNullException(nameof(buffer));
///<inheritdoc/>
- public override Encoding Encoding { get; }
+ public override Encoding Encoding { get; } = encoding ?? throw new ArgumentNullException(nameof(encoding));
/// <summary>
/// Line termination to use when writing lines to the output
@@ -97,24 +105,6 @@ namespace VNLib.Utils.IO
{
}
- /// <summary>
- /// Creates a new <see cref="VnStreamWriter"/> that writes encoded data to the base stream
- /// and uses the specified buffer.
- /// </summary>
- /// <param name="baseStream">The underlying stream to write data to</param>
- /// <param name="encoding">The <see cref="Encoding"/> to use when writing to the stream</param>
- /// <param name="buffer">The internal <see cref="ISlindingWindowBuffer{T}"/> to use</param>
- /// <exception cref="ArgumentNullException"></exception>
- public VnStreamWriter(Stream baseStream, Encoding encoding, ISlindingWindowBuffer<byte> buffer)
- {
- BaseStream = baseStream ?? throw new ArgumentNullException(nameof(buffer));
- Encoding = encoding ?? throw new ArgumentNullException(nameof(encoding));
- _buffer = buffer ?? throw new ArgumentNullException(nameof(buffer));
-
- //Get an encoder
- Enc = encoding.GetEncoder();
- }
-
///<inheritdoc/>
public void Write(byte value)
{