Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ucm: Code simplification and fixes #11

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 28 additions & 24 deletions NUcmSerializer/src/Components.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ public Section(string identifier)
{
Identifier = identifier;
}

public Section()
: this(null)
{
}
}

public static class TPLG_CTL
Expand Down Expand Up @@ -267,11 +262,6 @@ protected VendorTuples(string identifier)
{
}

protected VendorTuples()
: this(null)
{
}

public abstract int Size();
}

Expand Down Expand Up @@ -418,11 +408,6 @@ public SectionControl(string identifier)
: base(identifier)
{
}

public SectionControl()
: this(null)
{
}
}

public class SectionControlMixer : SectionControl
Expand Down Expand Up @@ -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<PCM_FORMAT>().Contains(s))
Formats.Add(Enum.Parse<PCM_FORMAT>(s));
}
}

Expand All @@ -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<PCM_RATE>().Contains(tmp))
Rates.Add(Enum.Parse<PCM_RATE>(tmp));
}
}
}
Expand Down
17 changes: 8 additions & 9 deletions NUcmSerializer/src/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions NUcmSerializer/src/UcmReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions NUcmSerializer/src/UcmSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public UcmSerializer()

public void Serialize(Stream stream, IEnumerable<Section> sections, Encoding encoding)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));
if (sections == null)
throw new ArgumentNullException("sections");

Expand All @@ -45,6 +47,9 @@ public void Serialize(Stream stream, IEnumerable<Section> topology)

public IEnumerable<Section> Deserialize(Stream stream, Encoding encoding)
{
if (stream == null)
throw new ArgumentNullException(nameof(stream));

List<Section> result = new List<Section>();
Section section;

Expand Down
3 changes: 3 additions & 0 deletions NUcmSerializer/src/UcmWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down