diff --git a/NUcmSerializer/src/Components.cs b/NUcmSerializer/src/Components.cs index 4d6e85c..758b020 100644 --- a/NUcmSerializer/src/Components.cs +++ b/NUcmSerializer/src/Components.cs @@ -23,11 +23,6 @@ public Section(string identifier) { Identifier = identifier; } - - public Section() - : this(null) - { - } } public static class TPLG_CTL @@ -267,11 +262,6 @@ protected VendorTuples(string identifier) { } - protected VendorTuples() - : this(null) - { - } - public abstract int Size(); } @@ -418,11 +408,6 @@ public SectionControl(string identifier) : base(identifier) { } - - public SectionControl() - : this(null) - { - } } public class SectionControlMixer : SectionControl @@ -704,11 +689,17 @@ public string FormatsString set { - string[] substrs = value.Split(new[] { ',' }); - Formats.Clear(); - foreach (var s in substrs) - Formats.Add((PCM_FORMAT)Enum.Parse(typeof(PCM_FORMAT), s)); + if (value == null) + return; + + string[] substrs = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) + .Select(s => s.Trim()) + .Distinct().ToArray(); + + foreach (string s in substrs) + if (Enum.GetNames().Contains(s)) + Formats.Add(Enum.Parse(s)); } } @@ -723,14 +714,27 @@ public string RatesString set { - string[] substrs = value.Split(new[] { ',' }); - Rates.Clear(); - foreach (var s in substrs) + if (value == null) + return; + + string[] substrs = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) + .Select(s => s.Trim()) + .Distinct().ToArray(); + + foreach (string s in substrs) { + string tmp; + + if (s[0] == '_') + continue; + if (char.IsDigit(s[0])) - s.Insert(0, "_"); - Rates.Add((PCM_RATE)Enum.Parse(typeof(PCM_RATE), s)); + tmp = s.Insert(0, "_"); + else + tmp = s; + if (Enum.GetNames().Contains(tmp)) + Rates.Add(Enum.Parse(tmp)); } } } diff --git a/NUcmSerializer/src/TypeHelper.cs b/NUcmSerializer/src/TypeHelper.cs index 7f80164..d1f4a7a 100644 --- a/NUcmSerializer/src/TypeHelper.cs +++ b/NUcmSerializer/src/TypeHelper.cs @@ -50,13 +50,13 @@ internal static TokenType GetTypeTokenType(this Type type) { if (type.IsSimpleType()) return TokenType.Element; - else if (type.IsSimpleArrayType()) + if (type.IsSimpleArrayType()) return TokenType.Array; - else if (type.IsSimpleTupleType()) + if (type.IsSimpleTupleType()) return TokenType.Tuple; - else if (type.IsVendorArrayType()) + if (type.IsVendorArrayType()) return TokenType.VendorArray; - else if (type.IsSubclassOf(typeof(Section))) + if (type.IsSubclassOf(typeof(Section)) || type == typeof(Section)) return TokenType.Section; return TokenType.None; @@ -98,8 +98,7 @@ internal static object ConvertFromString(this Type type, string value) } TypeConverter conv = TypeDescriptor.GetConverter(type); - if (conv != null) - return conv.ConvertFromString(value); + return conv.ConvertFromString(value); } catch (NotSupportedException) { @@ -121,7 +120,7 @@ internal static byte[] ToBytes(this string value) { if (substr.StartsWith("0x", StringComparison.CurrentCulture) && byte.TryParse(substr.Substring(2), NumberStyles.HexNumber, - CultureInfo.CurrentCulture, out byte val)) + CultureInfo.CurrentCulture, out byte val)) result.Add(val); else if (byte.TryParse(substr, out val)) result.Add(val); @@ -140,7 +139,7 @@ internal static ushort[] ToUInts16(this string value) { if (substr.StartsWith("0x", StringComparison.CurrentCulture) && ushort.TryParse(substr.Substring(2), NumberStyles.HexNumber, - CultureInfo.CurrentCulture, out ushort val)) + CultureInfo.CurrentCulture, out ushort val)) result.Add(val); else if (ushort.TryParse(substr, out val)) result.Add(val); @@ -159,7 +158,7 @@ internal static uint[] ToUInts32(this string value) { if (substr.StartsWith("0x", StringComparison.CurrentCulture) && uint.TryParse(substr.Substring(2), NumberStyles.HexNumber, - CultureInfo.CurrentCulture, out uint val)) + CultureInfo.CurrentCulture, out uint val)) result.Add(val); else if (uint.TryParse(substr, out val)) result.Add(val); diff --git a/NUcmSerializer/src/UcmReader.cs b/NUcmSerializer/src/UcmReader.cs index 7325c50..bc0cb6c 100644 --- a/NUcmSerializer/src/UcmReader.cs +++ b/NUcmSerializer/src/UcmReader.cs @@ -36,6 +36,9 @@ public class UcmReader : IDisposable public UcmReader(Stream stream, Encoding encoding) { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + this.encoding = encoding; if (encoding == null) reader = new StreamReader(stream); diff --git a/NUcmSerializer/src/UcmSerializer.cs b/NUcmSerializer/src/UcmSerializer.cs index 5a88117..5c29675 100644 --- a/NUcmSerializer/src/UcmSerializer.cs +++ b/NUcmSerializer/src/UcmSerializer.cs @@ -22,6 +22,8 @@ public UcmSerializer() public void Serialize(Stream stream, IEnumerable
sections, Encoding encoding) { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); if (sections == null) throw new ArgumentNullException("sections"); @@ -45,6 +47,9 @@ public void Serialize(Stream stream, IEnumerable
topology) public IEnumerable
Deserialize(Stream stream, Encoding encoding) { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + List
result = new List
(); Section section; diff --git a/NUcmSerializer/src/UcmWriter.cs b/NUcmSerializer/src/UcmWriter.cs index 9a20a08..541c819 100644 --- a/NUcmSerializer/src/UcmWriter.cs +++ b/NUcmSerializer/src/UcmWriter.cs @@ -89,6 +89,9 @@ public void Init(object token, PropertyInfo tokenInfo) public UcmWriter(Stream stream, Encoding encoding) { + if (stream == null) + throw new ArgumentNullException(nameof(stream)); + this.encoding = encoding; if (encoding == null) writer = new StreamWriter(stream);