-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
120 lines (101 loc) · 4.01 KB
/
Copy pathProgram.cs
File metadata and controls
120 lines (101 loc) · 4.01 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
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
namespace LineEndings
{
class Program
{
private const byte Cr = 0x0D;
private const byte Lf = 0x0A;
private static readonly HashSet<string> KnownBinaryExtensions = new HashSet<string>
{
".exe", ".jar", ".dll", ".class", ".zip", ".rar", ".suo", ".doc", ".docx", ".xls", ".xlsx", ".png", ".gif",
".jpg", ".jpeg", ".tif", ".tiff", ".swf", ".fla", ".bin"
};
static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Usage: LineEndings.exe ([D2U/U2D]) (FullPathToFiles)");
return;
}
bool convertToUnix = args[0] == "D2U";
bool convertToDos = args[0] == "U2D";
if (!convertToUnix && !convertToDos)
{
Console.WriteLine("Invalid conversion type. Use \"D2U\" or \"U2D\".");
return;
}
if (!Directory.Exists(args[1]))
{
Console.WriteLine("Invalid path. Ensure the directory exists.");
return;
}
Console.WriteLine($"Scanning: '{args[1]}' and subdirectories");
var allFiles = Directory.EnumerateFiles(args[1], "*.*", SearchOption.AllDirectories);
foreach (string fileName in allFiles)
{
string extension = Path.GetExtension(fileName).ToLowerInvariant();
if (KnownBinaryExtensions.Contains(extension))
{
Console.WriteLine($"Skipping binary file: '{fileName}'");
continue;
}
Console.WriteLine($"Converting: '{fileName}'");
try
{
if (convertToUnix)
{
Dos2Unix(fileName);
}
else
{
Unix2Dos(fileName);
}
}
catch (Exception exception)
{
Console.WriteLine($"Failed to convert '{fileName}': {exception.Message}");
}
}
Console.WriteLine("Conversion complete.");
}
/// <summary>
/// Converts Windows-style line endings to Unix-style line endings in a file.
/// </summary>
/// <param name="fileName">The path to the file to convert.</param>
private static void Dos2Unix(string fileName)
{
// Check if the file exists
if (!File.Exists(fileName))
{
throw new FileNotFoundException("The specified file does not exist.", fileName);
}
// Read the content of the file
string content = File.ReadAllText(fileName);
// Replace Windows line endings (\r\n) with Unix line endings (\n)
string convertedContent = content.Replace("\r\n", "\n");
// Write the converted content back to the file
File.WriteAllText(fileName, convertedContent);
}
/// <summary>
/// Converts Unix-style line endings to Windows-style line endings in a file.
/// </summary>
/// <param name="fileName">The path to the file to convert.</param>
private static void Unix2Dos(string fileName)
{
// Check if the file exists
if (!File.Exists(fileName))
{
throw new FileNotFoundException("The specified file does not exist.", fileName);
}
// Read the content of the file
string content = File.ReadAllText(fileName);
// Replace Unix line endings (\n) with Windows line endings (\r\n)
string convertedContent = content.Replace("\r\n", "\n").Replace("\n", "\r\n");
// Write the converted content back to the file
File.WriteAllText(fileName, convertedContent);
}
}
}