-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourseFile.cs
More file actions
109 lines (99 loc) · 3.09 KB
/
SourseFile.cs
File metadata and controls
109 lines (99 loc) · 3.09 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Merger
{
public class SourseFile
{
public FileInfo File_ { get; }
public HashSet<string> Imports { get; set; }
public List<Entity> Entities { get; }
protected static HashSet<string> EntityTypes = new HashSet<string>() { "class", "enum", "interface" };
private static readonly string importView = "import";
public SourseFile(FileInfo file)
{
File_ = file;
Imports = new HashSet<string>();
Entities = new List<Entity>();
}
public void Read()
{
using (var sr = new StreamReader(File_.OpenRead()))
{
string line = "";
while (!isEntity(line))
{
line = sr.ReadLine();
if (line == null) return;
if (line.Contains(importView))
{
Imports.Add(line);
}
}
while (!sr.EndOfStream)
{
Entities.Add(new Entity(line));
Entities[Entities.Count - 1].Read(sr);
line = sr.ReadLine();
while (!isEntity(line) && !sr.EndOfStream)
line = sr.ReadLine();
}
}
}
public void Merge(SourseFile another, string mainEntityName)
{
foreach (var import in another.Imports)
{
this.Imports.Add(import);
}
foreach (var entity in another.Entities)
{
if (entity.Signature.Contains(mainEntityName))
{
this.Entities.Insert(0, entity);
}
else
{
if (entity.Signature.StartsWith("public "))
{
entity.Code[0] = entity.Signature.Remove(0, "public ".Length);
}
this.Entities.Add(entity);
}
}
}
public void Write()
{
using (StreamWriter writter = new StreamWriter(File_.FullName))
{
foreach (string import in this.Imports)
{
writter.WriteLine(import);
}
foreach (var entity in this.Entities)
{
writter.Write("\n\n");
foreach (var line in entity.Code)
{
writter.WriteLine(line);
}
}
}
}
private static bool isEntity(string line)
{
if (line == null) return false;
foreach (var entity in EntityTypes)
{
if (line.Contains(entity)) return true;
}
return false;
}
public override string ToString()
{
return File_.Name;
}
}
}