-
Notifications
You must be signed in to change notification settings - Fork 329
Expand file tree
/
Copy pathHelpers.cs
More file actions
128 lines (106 loc) · 2.51 KB
/
Helpers.cs
File metadata and controls
128 lines (106 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.IO;
using TagLib;
using File = TagLib.File;
namespace TaglibSharp.Tests
{
public class Debugger
{
public static void DumpHex (ByteVector data)
{
DumpHex (data.Data);
}
public static void DumpHex (byte[] data)
{
int cols = 16;
int rows = data.Length / cols +
(data.Length % cols != 0 ? 1 : 0);
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (row == rows - 1 &&
data.Length % cols != 0 &&
col >= data.Length % cols)
Console.Write (" ");
else
Console.Write (" {0:x2}",
data[row * cols + col]);
}
Console.Write (" | ");
for (int col = 0; col < cols; col++) {
if (row == rows - 1 &&
data.Length % cols != 0 &&
col >= data.Length % cols)
Console.Write (" ");
else
WriteByte2 (data[row * cols + col]);
}
Console.WriteLine ();
}
Console.WriteLine ();
}
static void WriteByte2 (byte data)
{
foreach (char c in allowed)
if (c == data) {
Console.Write (c);
return;
}
Console.Write (".");
}
static readonly string allowed = "0123456789abcdefghijklmnopqr" +
"stuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`~!@#$%^&*()_+-={}" +
"[];:'\",.<>?/\\|";
}
public class MemoryFileAbstraction : File.IFileAbstraction
{
readonly MemoryStream stream;
public MemoryFileAbstraction (int maxSize, byte[] data)
{
stream = new MemoryStream (maxSize);
stream.Write (data, 0, data.Length);
stream.Position = 0;
}
public string Name => "MEMORY";
public Stream ReadStream => stream;
public Stream WriteStream => stream;
public bool ReadShareWhenWriting {
get => throw new NotSupportedException ();
set => throw new NotSupportedException ();
}
public void CloseStream (Stream stream)
{
// This causes a stackoverflow
//stream?.Close();
}
}
public class CodeTimer : IDisposable
{
readonly DateTime start;
TimeSpan elapsed_time = TimeSpan.Zero;
readonly string label;
public CodeTimer ()
{
start = DateTime.Now;
}
public CodeTimer (string label) : this ()
{
this.label = label;
}
public TimeSpan ElapsedTime {
get {
var now = DateTime.Now;
return elapsed_time == TimeSpan.Zero ? now - start : elapsed_time;
}
}
public void WriteElapsed (string message)
{
Console.WriteLine ("{0} {1} {2}", label, message, ElapsedTime);
}
public void Dispose ()
{
elapsed_time = DateTime.Now - start;
if (label != null)
WriteElapsed ("timer stopped:");
}
}
}