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

Port UnknownTagHandler #399

Merged
merged 1 commit into from
Feb 5, 2024
Merged
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
17 changes: 17 additions & 0 deletions MetadataExtractor.Tools.FileProcessor/DeconstructionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

#if NETFRAMEWORK

namespace System.Collections.Generic;

internal static class DeconstructionExtensions
{
[DebuggerStepThrough]
public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> pair, out TKey key, out TValue value)
{
key = pair.Key;
value = pair.Value;
}
}

#endif
15 changes: 6 additions & 9 deletions MetadataExtractor.Tools.FileProcessor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@

namespace MetadataExtractor.Tools.FileProcessor
{
// TODO port UnknownTagHandler

internal static class Program
{
private static int Main(string[] args)
Expand Down Expand Up @@ -191,11 +189,11 @@ private static int ProcessRecursively(string[] args)
// If "--markdown" is specified, write a summary table in markdown format
fileHandler = new MarkdownTableOutputHandler();
}
// else if (arg == "--unknown")
// {
// // If "--unknown" is specified, write CSV tallying unknown tag counts
// fileHandler = new UnknownTagHandler();
// }
else if (arg == "--unknown")
{
// If "--unknown" is specified, write CSV tallying unknown tag counts
fileHandler = new UnknownTagHandler();
}
else if (arg == "--log-file")
{
if (i == args.Length - 1)
Expand Down Expand Up @@ -224,8 +222,7 @@ private static int ProcessRecursively(string[] args)
return 1;
}

if (fileHandler is null)
fileHandler = new BasicFileHandler();
fileHandler ??= new BasicFileHandler();

var stopwatch = Stopwatch.StartNew();

Expand Down
48 changes: 48 additions & 0 deletions MetadataExtractor.Tools.FileProcessor/UnknownTagHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

namespace MetadataExtractor.Tools.FileProcessor;

/// <summary>
/// Keeps track of unknown tags.
/// </summary>
internal sealed class UnknownTagHandler : FileHandlerBase
{
private readonly record struct Key(string DirectoryName, int TagType);

private readonly Dictionary<Key, int> _occurrenceByKey = [];

public override void OnExtractionSuccess(string filePath, IList<Directory> directories, string relativePath, TextWriter log, long streamPosition)
{
base.OnExtractionSuccess(filePath, directories, relativePath, log, streamPosition);

foreach (Directory directory in directories)
{
foreach (Tag tag in directory.Tags)
{
// Only interested in unknown tags (those without names)
if (tag.HasName)
continue;

Key key = new(directory.Name, tag.Type);

_occurrenceByKey.TryGetValue(key, out int count);
_occurrenceByKey[key] = count + 1;
}
}
}

public override void OnScanCompleted(TextWriter log)
{
base.OnScanCompleted(log);

var results = _occurrenceByKey
.OrderByDescending(pair => pair.Value)
.ThenBy(pair => pair.Key.DirectoryName)
.ThenBy(pair => pair.Key.TagType);

foreach ((Key key, int count) in results)
{
log.WriteLine($"{key.DirectoryName}, 0x{key.TagType:X4}, {count}");
}
}
}
Loading