forked from vrokolos/VrokSub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
200 lines (180 loc) · 7.2 KB
/
Utils.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;
using System.Text.RegularExpressions;
namespace AutomagicSubs
{
class Utils
{
#region MovieHash
public static byte[] ComputeMovieHash(string filename)
{
byte[] result;
using (Stream input = File.OpenRead(filename))
{
result = ComputeMovieHash(input);
}
return result;
}
public static byte[] ComputeMovieHash(Stream input)
{
long lhash, streamsize;
streamsize = input.Length;
lhash = streamsize;
long i = 0;
byte[] buffer = new byte[sizeof(long)];
while (i < 65536 / sizeof(long) && (input.Read(buffer, 0, sizeof(long)) > 0))
{
i++;
lhash += BitConverter.ToInt64(buffer, 0);
}
input.Position = Math.Max(0, streamsize - 65536);
i = 0;
while (i < 65536 / sizeof(long) && (input.Read(buffer, 0, sizeof(long)) > 0))
{
i++;
lhash += BitConverter.ToInt64(buffer, 0);
}
input.Close();
byte[] result = BitConverter.GetBytes(lhash);
Array.Reverse(result);
if (result.Length==0) return null;
else return result;
}
public static string ToHexadecimal(byte[] bytes)
{
StringBuilder hexBuilder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
hexBuilder.Append(bytes[i].ToString("x2"));
}
return hexBuilder.ToString();
}
#endregion
#region RecurseSubDirs
public static string[] GetFilesByExtensions(string path, string extensions, SearchOption option)
{
List<string> result = new List<string>();
InternalGetFiles(path, option, extensions.Split(';'), result);
return result.ToArray();
}
private static void InternalGetFiles(string path, SearchOption option, string[] extensions, List<string> result)
{
// Get the files.
string[] files = Directory.GetFiles(path);
foreach (string file in files)
{
// Get the extension of the file.
string ext = Path.GetExtension(file);
// Validate the extension against the searched extensions.
foreach (string p in extensions)
{
if (ext == p)
{
result.Add(file);
break;
}
}
}
// Returns if only top directory is requested.
if (option == SearchOption.TopDirectoryOnly)
return;
// Get all other directories.
foreach (string directory in Directory.GetDirectories(path))
{
InternalGetFiles(directory, option, extensions, result);
}
}
#endregion
#region DecodeAndDecompress
public static byte[] Decode(string str)
{
byte[] decbuff = Convert.FromBase64String(str);
return decbuff;
}
public static byte[] DecodeAndDecompress(string str)
{
return Decompress(Decode(str));
}
static public byte[] Decompress(byte[] b)
{
MemoryStream ms = new MemoryStream(b.Length);
ms.Write(b, 0, b.Length);
// last 4 bytes of GZipStream = length of decompressed data
ms.Seek(-4, SeekOrigin.Current);
byte[] lb = new byte[4];
ms.Read(lb, 0, 4);
int len = BitConverter.ToInt32(lb, 0);
ms.Seek(0, SeekOrigin.Begin);
byte[] ob = new byte[len];
GZipStream zs = new GZipStream(ms, CompressionMode.Decompress);
zs.Read(ob, 0, len);
return ob;
}
#endregion
#region LangCodes
public static List<langMap> generateLangMap()
{
List<langMap> theLangMap = new List<langMap>();
theLangMap.Add(new langMap("cs", "cze,ces", "Czech"));
theLangMap.Add(new langMap("fi", "fin", "Finnish"));
theLangMap.Add(new langMap("tr", "tur", "Turkish"));
theLangMap.Add(new langMap("sv", "swe", "Swedish"));
theLangMap.Add(new langMap("sk", "slo,slk", "Slovak"));
theLangMap.Add(new langMap("bg", "bul", "Bulgarian"));
theLangMap.Add(new langMap("fr", "fre,fra", "French"));
theLangMap.Add(new langMap("ar", "ara", "Arabic"));
theLangMap.Add(new langMap("vi", "vie", "Vietnamese"));
theLangMap.Add(new langMap("sr", "scc,srp", "Serbian"));
theLangMap.Add(new langMap("mk", "mac,mkd", "FYROM"));
theLangMap.Add(new langMap("ko", "kor", "Korean"));
theLangMap.Add(new langMap("et", "est", "Estonian"));
theLangMap.Add(new langMap("zh", "chi,zho", "Chinese"));
theLangMap.Add(new langMap("hr", "scr,hrv", "Croatian"));
theLangMap.Add(new langMap("gl", "glg", "Galician"));
theLangMap.Add(new langMap("ja", "jpn", "Japanese"));
theLangMap.Add(new langMap("el,gr", "gre,ell", "Greek"));
theLangMap.Add(new langMap("en", "eng", "English"));
theLangMap.Add(new langMap("da", "dan", "Danish"));
theLangMap.Add(new langMap("de", "ger,deu", "German"));
theLangMap.Add(new langMap("he", "heb", "Hebrew"));
theLangMap.Add(new langMap("id", "ind", "Indonesian"));
theLangMap.Add(new langMap("it", "ita", "Italian"));
theLangMap.Add(new langMap("es", "spa", "Spanish"));
theLangMap.Add(new langMap("no", "nor", "Norwegian"));
theLangMap.Add(new langMap("nl", "dut,nld", "Dutch"));
theLangMap.Add(new langMap("pl", "pol", "Polish"));
theLangMap.Add(new langMap("fa", "per,fas", "Persian"));
theLangMap.Add(new langMap("hu", "hun", "Hungarian"));
theLangMap.Add(new langMap("pt", "por", "Portuguese"));
theLangMap.Add(new langMap("ro", "rum,ron", "Romanian"));
theLangMap.Add(new langMap("ru", "rus", "Russian"));
return theLangMap;
}
public static string parseLangCodeFromFile(string filename, List<langMap> langs)
{
string name = Path.GetFileNameWithoutExtension(filename);
string[] nameSplit = name.Split('.');
string langCode = nameSplit[nameSplit.Length - 1];
foreach (langMap l in langs)
{
if (l.two.Contains(langCode))
{
return langCode;
}
}
return "";
}
#endregion
public static string FilenameFromTitle(string name)
{
// first trim the raw string
string safe = name.Trim();
// trim out illegal characters
safe = Regex.Replace(safe, "[^a-zA-Z0-9\\- ]", "");
return safe;
}
}
}