This repository has been archived by the owner on Nov 3, 2019. It is now read-only.
forked from libertyernie/tgbdual_L
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathendian_conv.cs
62 lines (58 loc) · 2.03 KB
/
endian_conv.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
/* endian_conv.cs -- convert a group of files, or any file in a directory with
* one of several extensions, to a certain encoding.
*
* To the extent possible under law, the author has dedicated all copyright and related and neighboring rights to endian_conv to the public domain worldwide. This software is distributed without any warranty.
*
* The CC0 Public Domain Dedication applies to this file - see <http://creativecommons.org/publicdomain/zero/1.0/>. */
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace endian_conv {
class endian_conv {
static void usage() {
Console.Error.WriteLine(@"Usage: endian_conv {encoding} [files ...]
or: endian_conv {encoding} [directory] [extensions ...]
{encoding} is the output encoding - uses the Encoding.GetEncoding() method.
Use utf-8 for UTF-8, utf-16 for UTF-16 (little endian).
utf-8 is a special case - this program will NOT write a BOM for UTF-8.");
Environment.Exit(1);
}
static void Main(string[] args) {
if (args.Length < 2) {
usage();
}
Encoding target;
if (args[0] == "utf-8") {
target = new UTF8Encoding(false);
} else {
target = Encoding.GetEncoding(args[0]);
}
List<string> toProcess = new List<string>();
if (Directory.Exists(args[1])) {
for (int i = 2; i < args.Length; i++) {
toProcess.AddRange(Directory.EnumerateFiles(args[1], args[i]));
}
} else {
for (int i = 1; i < args.Length; i++) {
if (!File.Exists(args[i])) {
Console.Error.WriteLine(args[i] + " is not a file or does not exist");
usage();
}
toProcess.Add(args[i]);
}
}
Console.Write("Converting to " + target.WebName + ": ");
foreach (string filename in toProcess) {
string native = File.ReadAllText(filename);
var time = File.GetLastWriteTime(filename);
Console.Write(Path.GetFileName(filename) + " ");
File.WriteAllText(filename, native, target);
File.SetLastWriteTime(filename, time);
}
Console.WriteLine();
}
}
}