Skip to content

Commit 1f34c73

Browse files
committed
Touch up GenerateTestFixture program
1 parent be8122a commit 1f34c73

File tree

1 file changed

+56
-56
lines changed

1 file changed

+56
-56
lines changed

examples/GenerateTestFixture/GenerateTestFixture.cs renamed to examples/GenerateTestFixture/Program.cs

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616
using TagLib.IFD.Tags;
1717
using TagLib.Xmp;
1818

19-
public class GenerateTestFixtureApp
19+
namespace GenerateTestFixture;
20+
21+
public class Program
2022
{
21-
private static MD5 md5 = MD5.Create ();
23+
static MD5 md5 = MD5.Create ();
2224

2325
// Helper to run a process and capture output, error, and exit code
24-
private static bool RunProcess(string exe, string args, out string output, out string error, out int exitCode)
26+
static bool RunProcess (string exe, string args, out string output, out string error, out int exitCode)
2527
{
26-
var startInfo = new ProcessStartInfo
27-
{
28+
var startInfo = new ProcessStartInfo {
2829
FileName = exe,
2930
Arguments = args,
3031
RedirectStandardOutput = true,
@@ -34,17 +35,17 @@ private static bool RunProcess(string exe, string args, out string output, out s
3435
};
3536

3637
using var process = new Process { StartInfo = startInfo };
37-
process.Start();
38-
output = process.StandardOutput.ReadToEnd();
39-
error = process.StandardError.ReadToEnd();
40-
process.WaitForExit();
38+
process.Start ();
39+
output = process.StandardOutput.ReadToEnd ();
40+
error = process.StandardError.ReadToEnd ();
41+
process.WaitForExit ();
4142
exitCode = process.ExitCode;
4243
return exitCode == 0;
4344
}
4445

45-
public static void Main (string [] args)
46+
public static void Main (string[] args)
4647
{
47-
if(args.Length != 2) {
48+
if (args.Length != 2) {
4849
Console.Error.WriteLine ("USAGE: mono GenerateTestFixture.exe NAME PATH");
4950
return;
5051
}
@@ -64,8 +65,7 @@ public static void Main (string [] args)
6465
static void GenerateIFDFixture (string name, string path)
6566
{
6667
// First run exiv2 on it.
67-
string output, err; int code;
68-
var result = RunProcess("listData", $"e {path}", out output, out err, out code);
68+
var result = RunProcess ("listData", $"e {path}", out var output, out var err, out var code);
6969
if (!result) {
7070
Console.Error.WriteLine ("Invoking listData failed, are you running from the examples folder?\n" + err);
7171
return;
@@ -78,7 +78,7 @@ static void GenerateIFDFixture (string name, string path)
7878
if (parts.Length == 0 || line.Trim ().Equals (string.Empty) || parts.Length != 5)
7979
continue;
8080
string tag_label = parts[0];
81-
ushort tag = ushort.Parse (parts[1].Substring(2), System.Globalization.NumberStyles.HexNumber);
81+
ushort tag = ushort.Parse (parts[1].Substring (2), System.Globalization.NumberStyles.HexNumber);
8282
string ifd = parts[2];
8383
string type = parts[3];
8484
uint length = uint.Parse (parts[4]);
@@ -146,15 +146,15 @@ static void GenerateIFDFixture (string name, string path)
146146
} else if (ifd.Equals ("GPSInfo")) {
147147
EmitTestIFDEntryOpen ("gps_structure", 0, tag, ifd);
148148
} else if (ifd.Equals ("CanonCs")) {
149-
EmitTestIFDEntryOpen ("makernote_structure", 0, (ushort) CanonMakerNoteEntryTag.CameraSettings, ifd);
149+
EmitTestIFDEntryOpen ("makernote_structure", 0, (ushort)CanonMakerNoteEntryTag.CameraSettings, ifd);
150150
} else if (ifd.Equals ("CanonSi")) {
151-
EmitTestIFDEntryOpen ("makernote_structure", 0, (ushort) CanonMakerNoteEntryTag.ShotInfo, ifd);
151+
EmitTestIFDEntryOpen ("makernote_structure", 0, (ushort)CanonMakerNoteEntryTag.ShotInfo, ifd);
152152
} else if (ifd.Equals ("CanonCf")) {
153-
EmitTestIFDEntryOpen ("makernote_structure", 0, (ushort) CanonMakerNoteEntryTag.CustomFunctions, ifd);
153+
EmitTestIFDEntryOpen ("makernote_structure", 0, (ushort)CanonMakerNoteEntryTag.CustomFunctions, ifd);
154154
} else if (ifd.Equals ("CanonPi")) {
155-
EmitTestIFDEntryOpen ("makernote_structure", 0, (ushort) CanonMakerNoteEntryTag.PictureInfo, ifd);
155+
EmitTestIFDEntryOpen ("makernote_structure", 0, (ushort)CanonMakerNoteEntryTag.PictureInfo, ifd);
156156
} else if (ifd.Equals ("CanonFi")) {
157-
EmitTestIFDEntryOpen ("makernote_structure", 0, (ushort) 0x93, ifd);
157+
EmitTestIFDEntryOpen ("makernote_structure", 0, (ushort)0x93, ifd);
158158
} else if (ifd.Equals ("PanasonicRaw")) {
159159
EmitTestIFDEntryOpen ("pana_structure", 0, tag, ifd);
160160
} else if (sub_ifds.ContainsKey (ifd)) {
@@ -171,7 +171,7 @@ static void GenerateIFDFixture (string name, string path)
171171
// And the fist both entries are combined to a long by exiv2.
172172
if (tag == 0x0001) {
173173
string val1 = ((ushort)uint.Parse (val)).ToString ();
174-
string val2 = ((ushort) (uint.Parse (val) >> 16)).ToString ();
174+
string val2 = ((ushort)(uint.Parse (val) >> 16)).ToString ();
175175
EmitTestIFDIndexedShortEntry (tag, val1);
176176
EmitTestIFDIndexedShortEntry (tag + 1, val2);
177177
} else {
@@ -240,8 +240,7 @@ static void GenerateIFDFixture (string name, string path)
240240
static void GenerateXMPFixture (string name, string path)
241241
{
242242
// First run exiv2 on it.
243-
string output, err; int code;
244-
var result = RunProcess("listData", $"x {path}", out output, out err, out code);
243+
var result = RunProcess ("listData", $"x {path}", out var output, out var err, out var code);
245244
if (!result) {
246245
Console.Error.WriteLine ("Invoking exiv2 failed, do you have it installed?\n" + err);
247246
return;
@@ -257,13 +256,13 @@ static void GenerateXMPFixture (string name, string path)
257256
Write ("XmpTag xmp = file.GetTag (TagTypes.XMP) as XmpTag;");
258257

259258
// Build prefix lookup dictionary.
260-
Type t = typeof(XmpTag);
261-
foreach (var member in t.GetMembers()) {
259+
Type t = typeof (XmpTag);
260+
foreach (var member in t.GetMembers ()) {
262261
if (!member.Name.EndsWith ("_NS"))
263262
continue;
264263
string val = (member as System.Reflection.FieldInfo).GetValue (null) as string;
265-
string prefix = XmpTag.NamespacePrefixes [val];
266-
xmp_prefixes [prefix] = member.Name;
264+
string prefix = XmpTag.NamespacePrefixes[val];
265+
xmp_prefixes[prefix] = member.Name;
267266
}
268267

269268
foreach (string line in output.Split ('\n')) {
@@ -303,9 +302,9 @@ static void EmitXmpTest (string label, string type, uint length, string val)
303302
// Plain node
304303
int index = 0;
305304
string name = parts[2];
306-
if (parts[2].EndsWith("]")) {
305+
if (parts[2].EndsWith ("]")) {
307306
int index_start = parts[2].LastIndexOf ("[");
308-
string index_str = parts[2].Substring (index_start+1, parts[2].Length-index_start-2);
307+
string index_str = parts[2].Substring (index_start + 1, parts[2].Length - index_start - 2);
309308
index = int.Parse (index_str);
310309
name = parts[2].Substring (0, index_start);
311310
}
@@ -323,7 +322,7 @@ static void EmitXmpTest (string label, string type, uint length, string val)
323322
Write ($"node = node.GetChild ({ns}, \"{name}\");");
324323
Write ("Assert.IsNotNull (node);");
325324
} else {
326-
throw new Exception ("Can't navigate to "+node);
325+
throw new Exception ("Can't navigate to " + node);
327326
}
328327
}
329328

@@ -338,7 +337,7 @@ static void EmitXmpTest (string label, string type, uint length, string val)
338337
Write ($"Assert.AreEqual (\"{val}\", node.Children [0].Value);");
339338
} else if (type.Equals ("LangAlt") && length == 1) {
340339
var langparts = val.Split ([' '], 2);
341-
string lang = langparts[0].Substring (langparts[0].IndexOf ('"')+1, langparts [0].Length - langparts[0].IndexOf ('"')-2);
340+
string lang = langparts[0].Substring (langparts[0].IndexOf ('"') + 1, langparts[0].Length - langparts[0].IndexOf ('"') - 2);
342341
Write ($"Assert.AreEqual (\"{lang}\", node.Children [0].GetQualifier (XmpTag.XML_NS, \"lang\").Value);");
343342
Write ($"Assert.AreEqual (\"{langparts[1]}\", node.Children [0].Value);");
344343
} else if (type.Equals ("XmpSeq") && length == 1) {
@@ -347,20 +346,20 @@ static void EmitXmpTest (string label, string type, uint length, string val)
347346
Write ($"Assert.AreEqual ({length}, node.Children.Count);");
348347
Write ($"Assert.AreEqual (\"{val}\", node.Children [0].Value);");
349348
} else if (type.Equals ("XmpSeq") && length > 1) {
350-
string [] vals = val.Split (',');
349+
string[] vals = val.Split (',');
351350
Write ("Assert.AreEqual (XmpNodeType.Seq, node.Type);");
352351
Write ("Assert.AreEqual (\"\", node.Value);");
353352
Write ($"Assert.AreEqual ({length}, node.Children.Count);");
354353
var per_iter = vals.Length / length;
355354
for (int i = 0; i < length; i++) {
356355
var builder = new List<string> ();
357356
for (int j = 0; j < per_iter; j++) {
358-
builder.Add (vals[per_iter*i + j].Trim ());
357+
builder.Add (vals[per_iter * i + j].Trim ());
359358
}
360359
Write ($"Assert.AreEqual (\"{string.Join (", ", builder.ToArray ())}\", node.Children [{i}].Value);");
361360
}
362361
} else if (type.Equals ("XmpBag") && length > 1) {
363-
string [] vals = val.Split (',');
362+
string[] vals = val.Split (',');
364363
Write ("Assert.AreEqual (XmpNodeType.Bag, node.Type);");
365364
Write ("Assert.AreEqual (\"\", node.Value);");
366365
Write ($"Assert.AreEqual ({length}, node.Children.Count);");
@@ -373,7 +372,7 @@ static void EmitXmpTest (string label, string type, uint length, string val)
373372
for (int i = 0; i < length; i++) {
374373
var builder = new List<string> ();
375374
for (int j = 0; j < per_iter; j++) {
376-
builder.Add (vals[per_iter*i + j].Trim ());
375+
builder.Add (vals[per_iter * i + j].Trim ());
377376
}
378377
Write ($"Assert.IsTrue (children_array.Contains (\"{string.Join (", ", builder.ToArray ())}\"));");
379378
}
@@ -396,8 +395,7 @@ static void EmitXmpTest (string label, string type, uint length, string val)
396395

397396
static string ExtractKey (string file, string key)
398397
{
399-
string output, err; int code;
400-
var result = RunProcess("extractKey", $"{file} {key}", out output, out err, out code);
398+
var result = RunProcess ("extractKey", $"{file} {key}", out var output, out var err, out var code);
401399
if (!result) {
402400
Console.Error.WriteLine ("Invoking extractKey failed, are you running from the examples folder?\n" + err);
403401
return string.Empty;
@@ -418,10 +416,11 @@ static string GetXmpNs (string prefix)
418416
prefix = "MicrosoftPhoto";
419417
if (xmp_prefixes.TryGetValue (prefix, out var result))
420418
return $"XmpTag.{result}";
421-
throw new Exception ("Unknown namespace prefix: "+prefix);
419+
throw new Exception ("Unknown namespace prefix: " + prefix);
422420
}
423421

424-
static bool IsPartOfMakernote (string ifd) {
422+
static bool IsPartOfMakernote (string ifd)
423+
{
425424
return ifd.Equals ("MakerNote") ||
426425
ifd.Equals ("Canon") ||
427426
ifd.Equals ("Sony") ||
@@ -436,7 +435,7 @@ static bool IsPartOfMakernote (string ifd) {
436435
static void EmitHeader (string name, string path)
437436
{
438437
int start = path.LastIndexOf ('/');
439-
string filename = path.Substring (start+1);
438+
string filename = path.Substring (start + 1);
440439
Write ("// TODO: This file is automatically generated");
441440
Write ("// TODO: Further manual verification is needed");
442441
Write ();
@@ -493,7 +492,8 @@ static void EmitFooter ()
493492
static bool iop_emitted = false;
494493
static bool gps_emitted = false;
495494

496-
static void EnsureIFD (string ifd) {
495+
static void EnsureIFD (string ifd)
496+
{
497497
if (ifd.Equals ("PanasonicRaw")) {
498498
if (is_panasonic_raw)
499499
return;
@@ -691,19 +691,19 @@ static void EmitTestIFDSShortArrayEntry (string val)
691691
static void EmitTestIFDRationalEntry (string val)
692692
{
693693
Write ("Assert.IsNotNull (entry as RationalIFDEntry, \"Entry is not a rational!\");");
694-
string[] parts = val.Split('/');
694+
string[] parts = val.Split ('/');
695695
Write ($"Assert.AreEqual ({parts[0]}, (entry as RationalIFDEntry).Value.Numerator);");
696696
Write ($"Assert.AreEqual ({parts[1]}, (entry as RationalIFDEntry).Value.Denominator);");
697697
}
698698

699699
static void EmitTestIFDRationalArrayEntry (string val)
700700
{
701-
var parts = val.Split(' ');
701+
var parts = val.Split (' ');
702702
Write ("Assert.IsNotNull (entry as RationalArrayIFDEntry, \"Entry is not a rational array!\");");
703703
Write ("var parts = (entry as RationalArrayIFDEntry).Values;");
704704
Write ($"Assert.AreEqual ({parts.Length}, parts.Length);");
705705
for (int i = 0; i < parts.Length; i++) {
706-
var pieces = parts[i].Split('/');
706+
var pieces = parts[i].Split ('/');
707707
Write ($"Assert.AreEqual ({pieces[0]}, parts[{i}].Numerator);");
708708
Write ($"Assert.AreEqual ({pieces[1]}, parts[{i}].Denominator);");
709709
}
@@ -712,19 +712,19 @@ static void EmitTestIFDRationalArrayEntry (string val)
712712
static void EmitTestIFDSRationalEntry (string val)
713713
{
714714
Write ("Assert.IsNotNull (entry as SRationalIFDEntry, \"Entry is not a srational!\");");
715-
string[] parts = val.Split('/');
715+
string[] parts = val.Split ('/');
716716
Write ($"Assert.AreEqual ({parts[0]}, (entry as SRationalIFDEntry).Value.Numerator);");
717717
Write ($"Assert.AreEqual ({parts[1]}, (entry as SRationalIFDEntry).Value.Denominator);");
718718
}
719719

720720
static void EmitTestIFDSRationalArrayEntry (string val)
721721
{
722-
var parts = val.Split(' ');
722+
var parts = val.Split (' ');
723723
Write ("Assert.IsNotNull (entry as SRationalArrayIFDEntry, \"Entry is not a srational array!\");");
724724
Write ("var parts = (entry as SRationalArrayIFDEntry).Values;");
725725
Write ($"Assert.AreEqual ({parts.Length}, parts.Length);");
726726
for (int i = 0; i < parts.Length; i++) {
727-
var pieces = parts[i].Split('/');
727+
var pieces = parts[i].Split ('/');
728728
Write ($"Assert.AreEqual ({pieces[0]}, parts[{i}].Numerator);");
729729
Write ($"Assert.AreEqual ({pieces[1]}, parts[{i}].Denominator);");
730730
}
@@ -785,26 +785,26 @@ static void EmitByteArrayComparison (string val, string type, string type_desc)
785785
{
786786
Write ($"Assert.IsNotNull (entry as {type}, \"Entry is not {type_desc}!\");");
787787
Write ($"var parsed_bytes = (entry as {type}).Data.Data;");
788-
var parts = val.Trim ().Split(' ');
788+
var parts = val.Trim ().Split (' ');
789789
if (parts.Length < 512) {
790790
Write ($"var bytes = new byte [] {{ {string.Join (", ", parts)} }};");
791791
Write ("Assert.AreEqual (bytes, parsed_bytes);");
792792
} else {
793793
// Starting with 512 byte items, we compare based on an MD5 hash, should be faster and reduces
794794
// the size of the test fixtures.
795-
byte [] data = new byte [parts.Length];
795+
byte[] data = new byte[parts.Length];
796796
for (int i = 0; i < parts.Length; i++) {
797-
data [i] = byte.Parse (parts [i]);
797+
data[i] = byte.Parse (parts[i]);
798798
}
799799
var hash = md5.ComputeHash (data);
800800

801-
StringBuilder shash = new StringBuilder ();
801+
var shash = new StringBuilder ();
802802
for (int i = 0; i < hash.Length; i++) {
803803
shash.Append (hash[i].ToString ("x2"));
804804
}
805805

806806
Write ("var parsed_hash = Utils.Md5Encode (parsed_bytes);");
807-
Write ($"Assert.AreEqual (\"{shash.ToString ()}\", parsed_hash);");
807+
Write ($"Assert.AreEqual (\"{shash}\", parsed_hash);");
808808
Write ($"Assert.AreEqual ({parts.Length}, parsed_bytes.Length);");
809809
}
810810
}
@@ -851,7 +851,7 @@ static void EmitTestIFDIndexedShortEntry (int index, string val)
851851
Write ($"Assert.AreEqual ({parts[i]}, (entry as ShortArrayIFDEntry).Values [{index + i}]);");
852852
}
853853

854-
#region IFD tag names lookup
854+
#region IFD tag names lookup
855855

856856
static Dictionary<string, Dictionary<ushort, string>> tag_names = null;
857857

@@ -904,14 +904,14 @@ static void IndexTagType (string ifd, Type t, string typename)
904904
if (!tag_names.ContainsKey (ifd))
905905
tag_names[ifd] = new Dictionary<ushort, string> ();
906906
foreach (string name in Enum.GetNames (t)) {
907-
ushort tag = (ushort) Enum.Parse (t, name);
907+
ushort tag = (ushort)Enum.Parse (t, name);
908908
tag_names[ifd][tag] = $"{typename}.{name}";
909909
}
910910
}
911911

912-
#endregion
912+
#endregion
913913

914-
#region Code emission
914+
#region Code emission
915915

916916
static int level = 0;
917917

@@ -936,5 +936,5 @@ static void Write (string str)
936936
level++;
937937
}
938938

939-
#endregion
939+
#endregion
940940
}

0 commit comments

Comments
 (0)