Skip to content

Commit

Permalink
Merge pull request #399 from drewnoakes/port-unknown-tag-handler
Browse files Browse the repository at this point in the history
Port UnknownTagHandler
  • Loading branch information
drewnoakes authored Feb 5, 2024
2 parents 60750c7 + 1f89a2c commit 5f22ae3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
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}");
}
}
}

0 comments on commit 5f22ae3

Please sign in to comment.