Initial commit
This commit is contained in:
80
MkvToolnixWrapper/MkvInfo/MkvNode.cs
Normal file
80
MkvToolnixWrapper/MkvInfo/MkvNode.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using System.Collections;
|
||||
using System.Text;
|
||||
|
||||
namespace MkvPropEditWrapper.MkvInfo;
|
||||
|
||||
public class MkvNode : IEnumerable<MkvNode>
|
||||
{
|
||||
private static class KnowNodeTypes
|
||||
{
|
||||
public const string Head = "EBML head";
|
||||
public const string Segment = "Segment";
|
||||
public const string Chapter = "Chapters";
|
||||
public const string Tag = "Tags";
|
||||
public const string Track = "Tracks";
|
||||
public const string Attachment = "Attachments";
|
||||
}
|
||||
|
||||
public MkvNode? Parent { get; }
|
||||
public IList<MkvNode> Children { get; }
|
||||
|
||||
public virtual NodeType NodeType { get; }
|
||||
public virtual int Depth { get; }
|
||||
|
||||
public ReadOnlyMemory<char> Name { get; }
|
||||
|
||||
public MkvNode(MkvNode? parent, ReadOnlyMemory<char> name)
|
||||
{
|
||||
Parent = parent;
|
||||
Name = name;
|
||||
|
||||
Depth = parent?.Depth + 1 ?? 0;
|
||||
|
||||
NodeType = parent is null ? NodeType.Root : FindNodeType();
|
||||
|
||||
Children = new List<MkvNode>();
|
||||
}
|
||||
|
||||
private NodeType FindNodeType()
|
||||
{
|
||||
var span = Name.Span;
|
||||
|
||||
return Depth switch
|
||||
{
|
||||
0 when span.SequenceEqual(KnowNodeTypes.Head) => NodeType.Head,
|
||||
0 when span.StartsWith(KnowNodeTypes.Segment) => NodeType.Segment,
|
||||
1 when span.SequenceEqual(KnowNodeTypes.Chapter) => NodeType.Chapter,
|
||||
1 when span.SequenceEqual(KnowNodeTypes.Tag) => NodeType.Tag,
|
||||
1 when span.SequenceEqual(KnowNodeTypes.Track) => NodeType.Track,
|
||||
1 when span.SequenceEqual(KnowNodeTypes.Attachment) => NodeType.Attachment,
|
||||
1 => NodeType.Unknown,
|
||||
_ => Parent!.NodeType
|
||||
};
|
||||
}
|
||||
|
||||
protected virtual void AppendSelf(StringBuilder builder) => builder.AppendLine($"{new string('-', Depth)}> [{NodeType}] {Name}");
|
||||
|
||||
public IEnumerator<MkvNode> GetEnumerator()
|
||||
{
|
||||
return Children.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
|
||||
AppendSelf(builder);
|
||||
|
||||
foreach (var mkvNode in Children)
|
||||
{
|
||||
builder.Append(mkvNode);
|
||||
}
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user