/// <summary> /// Check for invalid song files. /// </summary> /// <param name="song">The song to check.</param> /// <param name="logOutput">The log where error messages shall be written to.</param> /// <returns><c>true</c> if an error was found; otherwise <c>false</c>.</returns> public static bool CheckInvalidSong(SongEntry song, List <string> logOutput) { if (song.Title == "" || song.Artist == "" || song.MP3 == "") { logOutput.Add(" => Invalid song found: " + song.FileName); return(true); } else { return(false); } }
/// <summary> /// /// </summary> /// <param name="songA">Song entry with a particular encoding.</param> /// <param name="songB">Song entry with a different encoding.</param> /// <returns><c>0</c> to use songA; <c>1</c> to use songB; <c>2</c> to use songC.</returns> private static int chooseSongEncoding(SongEntry songA, SongEntry songB, SongEntry songC) { // Basic check if (songB.MP3 == "" && songC.MP3 == "") { return(0); } if (songA.MP3 == "" && songC.MP3 == "") { return(1); } if (songA.MP3 == "" && songB.MP3 == "") { return(2); } // Get path of the file string pathA = songA.FileName.Substring(0, songA.FileName.LastIndexOf(Path.DirectorySeparatorChar) + 1); string pathB = songB.FileName.Substring(0, songB.FileName.LastIndexOf(Path.DirectorySeparatorChar) + 1); string pathC = songC.FileName.Substring(0, songC.FileName.LastIndexOf(Path.DirectorySeparatorChar) + 1); // Get full qualified file name string fullName_songA = pathA + songA.MP3; string fullName_songB = pathB + songB.MP3; string fullName_songC = pathC + songC.MP3; FileInfo fiA = new FileInfo(fullName_songA); FileInfo fiB = new FileInfo(fullName_songB); FileInfo fiC = new FileInfo(fullName_songC); if (fiA.Exists) { return(0); } else if (fiB.Exists) { return(1); } else if (fiC.Exists) { return(2); } else { return(0); } }
/// <summary> /// Analyze all song directories. /// </summary> private static List <SongEntry> analyzeSongDirectories(IEnumerable <string> songDirectories) { // Create return object List <SongEntry> songEntries = new List <SongEntry>(); // Get list of all text files foreach (string songDirectory in songDirectories) { print(" ... searching in '" + songDirectory + "' ..."); IEnumerable <string> files = Directory.EnumerateFiles(songDirectory, "*.txt", SearchOption.AllDirectories); // Parse through all song files foreach (string file in files) { // Read file (code does not look good, but is working :-) string[] songFileA = null, songFileB = null, songFileC = null; try { songFileA = readAllLinesFromFile(file, Encoding.Default); songFileB = readAllLinesFromFile(file, Encoding.UTF8); songFileC = readAllLinesFromFile(file, Encoding.GetEncoding(1252)); } catch (PathTooLongException) { print(" => File found, exceeding the maximum path length in your operating system. Try shorten the path/file name."); print(" " + file); continue; } byte[] bytes = File.ReadAllBytes(file); SongEntry songA = createSongEntry(file, songFileA); SongEntry songB = createSongEntry(file, songFileB); SongEntry songC = createSongEntry(file, songFileC); // Choose best encoding (code does not look good, but is working :-) string[] songFile; SongEntry song; int res = chooseSongEncoding(songA, songB, songC); if (res == 0) { songFile = songFileA; song = songA; } else if (res == 1) { songFile = songFileB; song = songB; } else { songFile = songFileC; song = songC; } // The first test is mandatory (check for invalid song). It doesn't make sense to do further tests when the whole file is corrupt. if (tests["invalidsong"].Run(song, songFile, bytes, songEntries)) { continue; } // Run all other tests foreach (Test t in tests.Values) { if (t.GetType() == typeof(TestInvalidSong)) { continue; } else { t.Run(song, songFile, bytes, songEntries); } } // Add song to entry list songEntries.Add(song); } } // Return return(songEntries); }