-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileAggregator.cs
67 lines (60 loc) · 2.19 KB
/
FileAggregator.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace CodeAggregatorGtk
{
public class FileAggregator
{
public static void AggregateFiles(string sourceFolder, string outputFile, List<string> selectedNodes, Action<double> progressCallback)
{
int totalNodes = selectedNodes.Count;
int processedNodes = 0;
using (var output = new StreamWriter(outputFile, false, Encoding.UTF8))
{
output.WriteLine($"Source Folder: {sourceFolder}");
foreach (var node in selectedNodes)
{
string relativePath = Path.GetRelativePath(sourceFolder, node);
bool isTextFile = IsTextFile(node);
if (isTextFile)
{
output.WriteLine($"\n--- Start of File: {relativePath} ---\n");
foreach (var line in File.ReadLines(node))
{
output.WriteLine(line);
}
output.WriteLine($"\n--- End of File: {relativePath} ---\n");
}
processedNodes++;
progressCallback((double)processedNodes / totalNodes);
}
}
}
private static bool IsTextFile(string filePath)
{
try
{
using (var stream = new StreamReader(filePath, detectEncodingFromByteOrderMarks: true))
{
char[] buffer = new char[512];
int charsRead = stream.Read(buffer, 0, buffer.Length);
if (charsRead == 0)
return false;
for (int i = 0; i < charsRead; i++)
{
if (char.IsControl(buffer[i]) && buffer[i] != '\r' && buffer[i] != '\n' && buffer[i] != '\t')
{
return false;
}
}
return true;
}
}
catch
{
return false;
}
}
}
}