-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge.cs
More file actions
166 lines (151 loc) · 5.59 KB
/
Merge.cs
File metadata and controls
166 lines (151 loc) · 5.59 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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Merge
{
public partial class Merge : Form
{
#region Variables
Dictionary<int, string> firstFile = new Dictionary<int, string>();
Dictionary<int, string> secondFile = new Dictionary<int, string>();
List<string> splitLineFirstFile = new List<string>();
List<string> splitLineSecondFile = new List<string>();
int index = 0;
#endregion
public Merge()
{
InitializeComponent();
}
private void btnFirstFile_Click(object sender, EventArgs e)
{
OpenFileDialogChoiceFile((sender as Button).Text);
}
private void btnSecondFile_Click(object sender, EventArgs e)
{
OpenFileDialogChoiceFile((sender as Button).Text);
}
private void btnMerge_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(richTextBoxMerge.Text)) richTextBoxMerge.Clear();
if (!String.IsNullOrEmpty(textBoxFirstFullPath.Text) && !String.IsNullOrEmpty(textBoxSecondFullPath.Text))
{
string[] filePaths = { textBoxFirstFullPath.Text, textBoxSecondFullPath.Text };
ReadFileAndAddToDictionary(filePaths);
MergeBothFiles();
firstFile.Clear();
secondFile.Clear();
}
}
#region Methods
private void MergeBothFiles()
{
int indexMaxCount = firstFile.Count > secondFile.Count ? firstFile.Count : secondFile.Count;
int indexMinCount = firstFile.Count < secondFile.Count ? firstFile.Count : secondFile.Count;
for (int ii = 0; ii < indexMaxCount - 1; ii++)
{
string lineFromFirstFile = firstFile[ii];
if (ii > indexMinCount) return;
string lineFromSecondFile = secondFile[ii];
if (lineFromFirstFile.Equals(lineFromSecondFile))
{
richTextBoxMerge.Text += lineFromFirstFile + "\n";
}
else
{
MergeHelper(lineFromFirstFile, lineFromSecondFile);
}
}
}
private void MergeHelper(string lineFromFirstFile, string lineFromSecondFile)
{
string pattern = @"\w(?<!\d)[\w'-]*";
int indexWord = 0;
foreach (Match m in Regex.Matches(lineFromFirstFile, pattern))
{
if(!String.IsNullOrEmpty(m.Value))
{
splitLineFirstFile.Add(m.Value);
indexWord++;
}
}
indexWord = 0;
foreach (Match m in Regex.Matches(lineFromFirstFile, pattern))
{
if (!String.IsNullOrEmpty(m.Value))
{
splitLineSecondFile.Add(m.Value);
indexWord++;
}
}
}
private void CheckAnyChangesInBothLine()
{
}
private void ReadFileAndAddToDictionary(string[] filePaths)
{
string readLine = String.Empty;
try
{
for (int ii = 0; ii < filePaths.Length; ii++)
{
using (StreamReader sr = new StreamReader(filePaths[ii]))
{
while ((readLine = sr.ReadLine()) != null)
{
if (String.IsNullOrEmpty(readLine)) continue;
if (ii == 0)
{
firstFile.Add(index, readLine.Trim());
}
else
{
secondFile.Add(index, readLine.Trim());
}
index++;
}
index = 0;
}
}
}
catch (Exception ex)
{
MessageBox.Show(String.Format("Файл не может быть прочитан: {0}", ex.Message), "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void OpenFileDialogChoiceFile(string btnText)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "Выберите файл.";
openFileDialog.InitialDirectory = "c:\\";
openFileDialog.Filter = "txt файл (*.txt)|*.txt";
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
if (btnText == "Первый файл")
{
textBoxFirstFullPath.Text = openFileDialog.FileName;
}
if (btnText == "Второй файл")
{
textBoxSecondFullPath.Text = openFileDialog.FileName;
}
}
catch (Exception ex)
{
MessageBox.Show("Ошибка: Не могу прочитать файл с диска. Подробно: " + ex.Message);
}
}
}
#endregion
}
}