-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #399 from drewnoakes/port-unknown-tag-handler
Port UnknownTagHandler
- Loading branch information
Showing
3 changed files
with
71 additions
and
9 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
MetadataExtractor.Tools.FileProcessor/DeconstructionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
MetadataExtractor.Tools.FileProcessor/UnknownTagHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} | ||
} |