#nullable enable using System.Diagnostics.CodeAnalysis; using System.Security; namespace Vanara.InteropServices; /// Class that reprents a LPSTR with allocated memory behind it. public class SafeLPSTR : SafeMemString { const CharSet thisCharSet = CharSet.Ansi; /// Initializes a new instance of the class. /// The string value. public SafeLPSTR(string? s) : base(s, thisCharSet) { } /// Initializes a new instance of the class. /// The string value. /// The size of the buffer in characters. public SafeLPSTR(string? s, int capacity) : base(s, capacity, thisCharSet) { } /// Initializes a new instance of the class. /// The string value. public SafeLPSTR(SecureString s) : base(s, thisCharSet) { } /// Initializes a new instance of the class. /// The size of the buffer in characters, including the null character terminator. public SafeLPSTR(int capacity) : base(capacity, thisCharSet) { } /// Prevents a default instance of the class from being created. private SafeLPSTR() : base() { } /// Initializes a new instance of the class. /// The PTR. /// true to reliably release the handle during finalization; false to prevent it. /// The number of bytes allocated to . [ExcludeFromCodeCoverage] private SafeLPSTR(StrPtrAnsi ptr, bool ownsHandle = true, PInvoke.SizeT allocatedBytes = default) : base((IntPtr)ptr, thisCharSet, ownsHandle, allocatedBytes) { } /// Represents a null value. Used primarily for comparison. /// A null value. public static SafeLPSTR Null => new(IntPtr.Zero, false); /// Returns the StrPtrAnsi value held by a . /// The instance. /// A value held by the or default if the handle or value is invalid. public static implicit operator StrPtrAnsi(SafeLPSTR s) => (StrPtrAnsi)s.handle; /// Appends a copy of the specified string to this instance. /// The string to append. /// A reference to this instance after the append operation has completed. public new SafeLPSTR Append(string? value) { base.Append(value); return this; } /// Appends a copy of the specified value to this instance. /// The value to append. /// A reference to this instance after the append operation has completed. public SafeLPSTR Append(SafeLPSTR value) => Append(value.ToString()); /// Inserts a string into this instance at the specified character position. /// The position in this instance where insertion begins. /// The string to insert. /// /// is less than zero or greater than the current length of this instance. /// public new SafeLPSTR Insert(int index, string? value) { base.Insert(index, value); return this; } /// Removes the specified range of characters from this instance. /// The zero-based position in this instance where removal begins. /// The number of characters to remove. /// /// If or is less than zero, or + is greater than the length of this instance. /// public new SafeLPSTR Remove(int startIndex, int length) { base.Remove(startIndex, length); return this; } /// Replaces all occurrences of a specified string in this instance with another specified string. /// The string to replace. /// The string that replaces , or . public SafeLPSTR Replace(string oldValue, string? newValue) => Replace(oldValue, newValue, 0, Length); /// Replaces the specified old value. /// The string to replace. /// The string that replaces , or . /// The position in this instance where the substring begins. /// The length of the substring. public new SafeLPSTR Replace(string oldValue, string? newValue, int startIndex, int count) { base.Replace(oldValue, newValue, startIndex, count); return this; } /// Replaces all occurrences of a specified character in this instance with another specified character. /// The character to replace. /// The character that replaces . public SafeLPSTR Replace(char oldChar, char newChar) => Replace(oldChar, newChar, 0, Length); /// Replaces, within a substring of this instance, all occurrences of a specified character with another specified character. /// The character to replace. /// The character that replaces . /// The position in this instance where the substring begins. /// The length of the substring. /// /// + is greater than the length of the value of this instance. /// -or- /// or is less than zero. /// public new SafeLPSTR Replace(char oldChar, char newChar, int startIndex, int count) { base.Replace(oldChar, newChar, startIndex, count); return this; } } /// Class that reprents a LPTSTR with allocated memory behind it. public class SafeLPTSTR : SafeMemString { const CharSet thisCharSet = CharSet.Auto; /// Initializes a new instance of the class. /// The string value. public SafeLPTSTR(string? s) : base(s, thisCharSet) { } /// Initializes a new instance of the class. /// The string value. /// The size of the buffer in characters. public SafeLPTSTR(string? s, int capacity) : base(s, capacity, thisCharSet) { } /// Initializes a new instance of the class. /// The string value. public SafeLPTSTR(SecureString s) : base(s, thisCharSet) { } /// Initializes a new instance of the class. /// The size of the buffer in characters, including the null character terminator. public SafeLPTSTR(int capacity) : base(capacity, thisCharSet) { } /// Prevents a default instance of the class from being created. private SafeLPTSTR() : base() { } /// Initializes a new instance of the class. /// The PTR. /// true to reliably release the handle during finalization; false to prevent it. /// The number of bytes allocated to . [ExcludeFromCodeCoverage] private SafeLPTSTR(StrPtrAuto ptr, bool ownsHandle = true, PInvoke.SizeT allocatedBytes = default) : base((IntPtr)ptr, thisCharSet, ownsHandle, allocatedBytes) { } /// Represents a null value. Used primarily for comparison. /// A null value. public static SafeLPTSTR Null => new(IntPtr.Zero, false); /// Returns the StrPtrAuto value held by a . /// The instance. /// A value held by the or default if the handle or value is invalid. public static implicit operator StrPtrAuto(SafeLPTSTR s) => (StrPtrAuto)s.handle; /// Appends a copy of the specified string to this instance. /// The string to append. /// A reference to this instance after the append operation has completed. public new SafeLPTSTR Append(string? value) { base.Append(value); return this; } /// Appends a copy of the specified value to this instance. /// The value to append. /// A reference to this instance after the append operation has completed. public SafeLPTSTR Append(SafeLPTSTR value) => Append(value.ToString()); /// Inserts a string into this instance at the specified character position. /// The position in this instance where insertion begins. /// The string to insert. /// /// is less than zero or greater than the current length of this instance. /// public new SafeLPTSTR Insert(int index, string? value) { base.Insert(index, value); return this; } /// Removes the specified range of characters from this instance. /// The zero-based position in this instance where removal begins. /// The number of characters to remove. /// /// If or is less than zero, or + is greater than the length of this instance. /// public new SafeLPTSTR Remove(int startIndex, int length) { base.Remove(startIndex, length); return this; } /// Replaces all occurrences of a specified string in this instance with another specified string. /// The string to replace. /// The string that replaces , or . public SafeLPTSTR Replace(string oldValue, string? newValue) => Replace(oldValue, newValue, 0, Length); /// Replaces the specified old value. /// The string to replace. /// The string that replaces , or . /// The position in this instance where the substring begins. /// The length of the substring. public new SafeLPTSTR Replace(string oldValue, string? newValue, int startIndex, int count) { base.Replace(oldValue, newValue, startIndex, count); return this; } /// Replaces all occurrences of a specified character in this instance with another specified character. /// The character to replace. /// The character that replaces . public SafeLPTSTR Replace(char oldChar, char newChar) => Replace(oldChar, newChar, 0, Length); /// Replaces, within a substring of this instance, all occurrences of a specified character with another specified character. /// The character to replace. /// The character that replaces . /// The position in this instance where the substring begins. /// The length of the substring. /// /// + is greater than the length of the value of this instance. /// -or- /// or is less than zero. /// public new SafeLPTSTR Replace(char oldChar, char newChar, int startIndex, int count) { base.Replace(oldChar, newChar, startIndex, count); return this; } } /// Class that reprents a LPWSTR with allocated memory behind it. public class SafeLPWSTR : SafeMemString { const CharSet thisCharSet = CharSet.Unicode; /// Initializes a new instance of the class. /// The string value. public SafeLPWSTR(string? s) : base(s, thisCharSet) { } /// Initializes a new instance of the class. /// The string value. /// The size of the buffer in characters. public SafeLPWSTR(string? s, int capacity) : base(s, capacity, thisCharSet) { } /// Initializes a new instance of the class. /// The string value. public SafeLPWSTR(SecureString s) : base(s, thisCharSet) { } /// Initializes a new instance of the class. /// The size of the buffer in characters, including the null character terminator. public SafeLPWSTR(int capacity) : base(capacity, thisCharSet) { } /// Prevents a default instance of the class from being created. private SafeLPWSTR() : base() { } /// Initializes a new instance of the class. /// The PTR. /// true to reliably release the handle during finalization; false to prevent it. /// The number of bytes allocated to . [ExcludeFromCodeCoverage] private SafeLPWSTR(StrPtrUni ptr, bool ownsHandle = true, PInvoke.SizeT allocatedBytes = default) : base((IntPtr)ptr, thisCharSet, ownsHandle, allocatedBytes) { } /// Represents a null value. Used primarily for comparison. /// A null value. public static SafeLPWSTR Null => new(IntPtr.Zero, false); /// Returns the StrPtrUni value held by a . /// The instance. /// A value held by the or default if the handle or value is invalid. public static implicit operator StrPtrUni(SafeLPWSTR s) => (StrPtrUni)s.handle; /// Appends a copy of the specified string to this instance. /// The string to append. /// A reference to this instance after the append operation has completed. public new SafeLPWSTR Append(string? value) { base.Append(value); return this; } /// Appends a copy of the specified value to this instance. /// The value to append. /// A reference to this instance after the append operation has completed. public SafeLPWSTR Append(SafeLPWSTR value) => Append(value.ToString()); /// Inserts a string into this instance at the specified character position. /// The position in this instance where insertion begins. /// The string to insert. /// /// is less than zero or greater than the current length of this instance. /// public new SafeLPWSTR Insert(int index, string? value) { base.Insert(index, value); return this; } /// Removes the specified range of characters from this instance. /// The zero-based position in this instance where removal begins. /// The number of characters to remove. /// /// If or is less than zero, or + is greater than the length of this instance. /// public new SafeLPWSTR Remove(int startIndex, int length) { base.Remove(startIndex, length); return this; } /// Replaces all occurrences of a specified string in this instance with another specified string. /// The string to replace. /// The string that replaces , or . public SafeLPWSTR Replace(string oldValue, string? newValue) => Replace(oldValue, newValue, 0, Length); /// Replaces the specified old value. /// The string to replace. /// The string that replaces , or . /// The position in this instance where the substring begins. /// The length of the substring. public new SafeLPWSTR Replace(string oldValue, string? newValue, int startIndex, int count) { base.Replace(oldValue, newValue, startIndex, count); return this; } /// Replaces all occurrences of a specified character in this instance with another specified character. /// The character to replace. /// The character that replaces . public SafeLPWSTR Replace(char oldChar, char newChar) => Replace(oldChar, newChar, 0, Length); /// Replaces, within a substring of this instance, all occurrences of a specified character with another specified character. /// The character to replace. /// The character that replaces . /// The position in this instance where the substring begins. /// The length of the substring. /// /// + is greater than the length of the value of this instance. /// -or- /// or is less than zero. /// public new SafeLPWSTR Replace(char oldChar, char newChar, int startIndex, int count) { base.Replace(oldChar, newChar, startIndex, count); return this; } }