Beispiel #1
0
        public string GetCleanFileName(NamingStrategy strategy, string calcedAlbumArtist, int trackWidth)
        {
            System.Diagnostics.Debug.Assert(strategy == NamingStrategy.Manual || trackWidth > 0);

            string dirtyName;
            bool   isVarious = calcedAlbumArtist == null || (calcedAlbumArtist.ToLower() + ' ').StartsWith("various ");

            var track   = GetTag("TRACKNUMBER");
            var padding = trackWidth - track.Length;

            if (padding > 0)
            {
                track = new String('0', padding) + track;
            }

            var artist = GetTag("ARTIST");

            if (String.IsNullOrEmpty(artist))
            {
                artist = GetTag("COMPOSER");
                if (String.IsNullOrEmpty(artist))
                {
                    artist = "(NoArtist)";
                }
            }

            var title = GetTag("TITLE");

            if (String.IsNullOrEmpty(title))
            {
                title = "(NoTitle)";
            }

            switch (strategy)
            {
            case NamingStrategy.ArtistTitle:
                dirtyName = track + " - " + artist + " - " + title + ".flac";
                break;

            case NamingStrategy.ShortTitle:
            case NamingStrategy.UnloadedAlbum:
                if (isVarious || artist != calcedAlbumArtist)
                {
                    dirtyName = track + " - " + artist + " - " + title + ".flac";
                }
                else
                {
                    dirtyName = track + " - " + title + ".flac";
                }
                break;

            default:
                dirtyName = Name;
                break;
            }

            return(Map1252.ToClean1252FileName(dirtyName));
        }
Beispiel #2
0
        string IDataErrorInfo.this[string propName]
        {
            get
            {
                if (propName == "UserSig")
                {
                    string cleanSig = Map1252.ToClean1252FileName(UserSig);
                    if (cleanSig != null && (cleanSig != UserSig || cleanSig.Any(Char.IsWhiteSpace)))
                    {
                        return("cannot have spaces or characters not allowed in file names");
                    }
                }

                return(null);
            }
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            long tot0 = 0, tot1 = 0;
            var  watch = new Stopwatch();

            for (int reps1 = 0; reps1 < 5; ++reps1)
            {
                // Benchmark the .NET provided binary searcher.
                watch.Reset();
                watch.Start();
                for (int rx = 0; rx < reps; ++rx)
                {
                    for (var ix = 0; ix < Map1252.Length; ++ix)
                    {
                        var pos = Map1252.ArrayBinarySearch(Map1252.At(ix) & 0x7FFFFF00);
                        var fit = Map1252.At(~pos) & 0xFF;
                        Debug.Assert(ix == ~pos);
                    }
                }
                tot0 += watch.ElapsedMilliseconds;

                // Benchmark the custom binary searcher.
                watch.Reset();
                watch.Start();
                for (int rx = 0; rx < reps; ++rx)
                {
                    for (var ix = 0; ix < Map1252.Length; ++ix)
                    {
                        var result = Map1252.To1252Bestfit(Map1252.At(ix) >> 8);
                        Debug.Assert(result == (Map1252.At(ix) & 0xFF));
                    }
                }
                tot1 += watch.ElapsedMilliseconds;
            }

            Console.WriteLine("Custom binary search change over Array.BinarySearch = " + 100f * (tot0 - tot1) / tot0 + "%");

            /* Output:
             *
             * Custom binary search change over Array.BinarySearch = 32.51122%
             *
             */
        }
Beispiel #4
0
        public void Test_Map1252Search()
        {
            for (var ix = 0; ix < Map1252.Length; ++ix)
            {
                var expectedResult = Map1252.At(ix) & 0xFF;
                var result         = Map1252.To1252Bestfit(Map1252.At(ix) >> 8);

                Assert.IsTrue(expectedResult > 0);
                Assert.IsTrue(result == expectedResult);
            }

            var minvalResult = Map1252.To1252Bestfit(0);
            var maxvalResult = Map1252.To1252Bestfit(0x10FFFF);

            Assert.IsTrue(minvalResult == 0);

            // U+10FFFF not expected and not actually against the spec.
            Assert.IsTrue(maxvalResult < 0);
        }
Beispiel #5
0
        public void Test_Map1252Order()
        {
            Assert.AreNotEqual(0, Map1252.Length);

            int prev = 0;

            for (var ix = 0; ix < Map1252.Length; ++ix)
            {
                int  key   = Map1252.At(ix) >> 8;
                byte value = unchecked ((byte)Map1252.At(ix));

                // Table must contain ordered, distinct keys.
                Assert.IsTrue(key > prev);

                // Not against the spec, just not expected.
                Assert.IsTrue(value != 0);

                prev = key;
            }
        }
Beispiel #6
0
        public void Test_ToClean1252FileName1()
        {
            var aZ             = Map1252.ToClean1252FileName("aZ");
            var c1ControlSpots = Map1252.ToClean1252FileName("€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ");
            var quote          = Map1252.ToClean1252FileName("\"");
            var heart          = Map1252.ToClean1252FileName("❤");
            var question       = Map1252.ToClean1252FileName("?");
            var strokedH       = Map1252.ToClean1252FileName("Ħ");
            var backslash      = Map1252.ToClean1252FileName("\\");
            var spacedSlash    = Map1252.ToClean1252FileName("foo / bar.mp3");
            var notFileChars   = Map1252.ToClean1252FileName("<>");
            var fullwidth      = Map1252.ToClean1252FileName("Azfullwidth\U0001F175astral");

            Assert.AreEqual("aZ", aZ);
            Assert.AreEqual("€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ", c1ControlSpots);
            Assert.AreEqual("'", quote);
            Assert.AreEqual("-", heart);
            Assert.AreEqual("", question);
            Assert.AreEqual("H", strokedH);
            Assert.AreEqual("-", backslash);
            Assert.AreEqual("foo; bar.mp3", spacedSlash);
            Assert.AreEqual("‹›", notFileChars);
            Assert.AreEqual("AzfullwidthFastral", fullwidth);
        }
Beispiel #7
0
            public Severity ValidateLameRip(string arg, string signature, bool doLogTag)
            {
                string err     = null;
                string newPath = null;

                if (signature != null)
                {
                    if (signature.Length == 0)
                    {
                        signature = null;
                    }
                    else if (Map1252.ToClean1252FileName(signature.Trim(null)) != signature || signature.Any(Char.IsWhiteSpace))
                    {
                        ReportLine($"Invalid signature '{signature}'.", Severity.Error, false);
                        return(Severity.Fatal);
                    }
                }

                try
                {
                    var fInfo = new FileInfo(arg);
                    if (fInfo.Attributes.HasFlag(FileAttributes.Directory))
                    {
                        newPath = fInfo.FullName;
                    }
                    else
                    {
                        err = "Not a directory.";
                    }
                }
                catch (ArgumentException ex)
                { err = ex.Message.Trim(null); }
                catch (DirectoryNotFoundException ex)
                { err = ex.Message.Trim(null); }
                catch (IOException ex)
                { err = ex.Message.Trim(null); }
                catch (NotSupportedException)
                { err = "Path is not valid."; }

                RipModel = new LameRip.Model(this, newPath, signature, doLogTag);

                if (err != null)
                {
                    ReportLine(err, Severity.Error, false);
                    SetCurrentFile(null);
                    RipModel.SetStatus(Severity.Error);
                    return(Severity.Error);
                }

                RipModel.Bind.IsWip = true;

                try
                {
                    RipModel.Validate();
                }
                catch (IOException ex)
                {
                    err = ex.Message.Trim(null);
                    ReportLine(err);
                    SetCurrentFile(null);
                    RipModel.SetStatus(Severity.Fatal);
                    return(Severity.Fatal);
                }

                SetCurrentFile(null);

                if (RipModel.Bind.Status == Severity.NoIssue && RipModel.Bind.Log == null)
                {
                    if (RipModel.Bind.Signature != null)
                    {
                        try
                        {
                            var errPath = newPath + Path.DirectorySeparatorChar + Data.NoncompliantName;
                            if (File.Exists(errPath))
                            {
                                File.Delete(errPath);
                            }
                        }
                        catch (Exception)
                        { /* discard all */ }
                    }

                    RipModel.Bind.IsWip = false;
                    ReportLine(RipModel.Bind.Trailer, Severity.Trivia, false);
                    return(Severity.Trivia);
                }

                if (RipModel.Bind.Status >= Severity.Error)
                {
                    RipModel.CloseFiles();

                    var baseName = Path.GetFileName(newPath);
                    if (RipModel.Bind.Signature != null && !baseName.StartsWith("!!") && newPath.Length < 235)
                    {
                        var errPath = Directory.GetParent(newPath).FullName;
                        if (errPath.Length > 0 && errPath[errPath.Length - 1] != Path.DirectorySeparatorChar)
                        {
                            errPath += Path.DirectorySeparatorChar;
                        }
                        errPath += Data.FailPrefix + baseName;
                        try
                        { Directory.Move(newPath, errPath); }
                        catch (Exception)
                        { /* discard all */ }

                        ++consecutiveInvalidations;
                    }
                }

                if (!RipModel.Bind.IsWip)
                {
                    ReportLine(RipModel.Bind.Trailer, RipModel.Bind.Status, false);
                }

                return(RipModel.Bind.Status);
            }