aboutsummaryrefslogtreecommitdiff
path: root/Emby.Naming/Video/Format3DParser.cs
blob: 333a48641e3c496dcd8f9cecc1266fa8d5762060 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.Linq;
using Emby.Naming.Common;

namespace Emby.Naming.Video
{
    public class Format3DParser
    {
        private readonly NamingOptions _options;

        public Format3DParser(NamingOptions options)
        {
            _options = options;
        }

        public Format3DResult Parse(string path)
        {
            int oldLen = _options.VideoFlagDelimiters.Length;
            var delimeters = new char[oldLen + 1];
            _options.VideoFlagDelimiters.CopyTo(delimeters, 0);
            delimeters[oldLen] = ' ';

            return Parse(new FlagParser(_options).GetFlags(path, delimeters));
        }

        internal Format3DResult Parse(string[] videoFlags)
        {
            foreach (var rule in _options.Format3DRules)
            {
                var result = Parse(videoFlags, rule);

                if (result.Is3D)
                {
                    return result;
                }
            }

            return new Format3DResult();
        }

        private static Format3DResult Parse(string[] videoFlags, Format3DRule rule)
        {
            var result = new Format3DResult();

            if (string.IsNullOrEmpty(rule.PreceedingToken))
            {
                result.Format3D = new[] { rule.Token }.FirstOrDefault(i => videoFlags.Contains(i, StringComparer.OrdinalIgnoreCase));
                result.Is3D = !string.IsNullOrEmpty(result.Format3D);

                if (result.Is3D)
                {
                    result.Tokens.Add(rule.Token);
                }
            }
            else
            {
                var foundPrefix = false;
                string format = null;

                foreach (var flag in videoFlags)
                {
                    if (foundPrefix)
                    {
                        result.Tokens.Add(rule.PreceedingToken);

                        if (string.Equals(rule.Token, flag, StringComparison.OrdinalIgnoreCase))
                        {
                            format = flag;
                            result.Tokens.Add(rule.Token);
                        }

                        break;
                    }

                    foundPrefix = string.Equals(flag, rule.PreceedingToken, StringComparison.OrdinalIgnoreCase);
                }

                result.Is3D = foundPrefix && !string.IsNullOrEmpty(format);
                result.Format3D = format;
            }

            return result;
        }
    }
}