-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtilities.cs
128 lines (115 loc) · 2.5 KB
/
Utilities.cs
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 System.Text.RegularExpressions;
namespace Utilities.DVDImport
{
/// <summary>
/// Byte swap pairs of bytes, and write to file.
/// </summary>
public class ByteSwapper
{
private FileStream m_fs = null;
private bool needPair = false;
private byte prevByte = 0;
public ByteSwapper(FileStream fs)
{
m_fs = fs;
}
public void NextByte(byte b)
{
byte[] data = new byte[2];
if(needPair)
{
data[0] = b;
data[1] = prevByte;
m_fs.Write(data, 0, 2);
needPair = false;
}
else
{
prevByte = b;
needPair = true;
}
}
public void TRMSFinalize()
{
byte[] data = new byte[2];
if(needPair)
{
data[0] = 0;
data[1] = prevByte;
m_fs.Write(data, 0, 2);
}
needPair = false;
}
}
/// <summary>
/// Summary description for Utilities.
/// </summary>
public class Utilities
{
public Utilities()
{
}
static public string BytesToSize(ulong bytes)
{
if(bytes > 1099511627776)
return(String.Format("{0:.##} TB", (bytes / 1099511627776.0)));
if(bytes > 1073741824)
return(String.Format("{0:.##} GB", (bytes / 1073741824.0)));
if(bytes > 1048576)
return(String.Format("{0:.#} MB", (bytes / 1048576.0)));
if(bytes > 1024)
return(String.Format("{0} K", (bytes / 1024)));
return(String.Format("{0} bytes", bytes));
}
static public string BytesToSize(long bytes)
{
return(BytesToSize((ulong)bytes));
}
static public string SecondsToLength(int input)
{
int inputSecs = input;
int seconds = 0;
int minutes = 0;
int hours = 0;
string secondsStr = "";
if (inputSecs > 0)
{
seconds = inputSecs % 60;
inputSecs = inputSecs - seconds;
if (inputSecs > 0)
{
inputSecs = inputSecs / 60;
minutes = inputSecs % 60;
inputSecs = inputSecs - minutes;
if (inputSecs > 0)
{
inputSecs = inputSecs / 60;
hours = inputSecs;
}
}
if (hours < 10)
secondsStr = "0" + hours.ToString() + ":";
else
secondsStr = hours.ToString() + ":";
if (minutes < 10)
secondsStr += "0" + minutes.ToString() + ":";
else
secondsStr += minutes.ToString() + ":";
if (seconds < 10)
secondsStr += "0" + seconds.ToString();
else
secondsStr += seconds.ToString();
}
else
secondsStr = "00:00:00";
return secondsStr;
}
public string MakeSafeFilename(string file)
{
string result = Regex.Replace(file, @"[^\w\d ;'\-_\+=\(\)!@#$%\^&,\.]", "");
return (result);
}
}
}