-
Notifications
You must be signed in to change notification settings - Fork 0
/
Brain.cs
152 lines (144 loc) · 4.18 KB
/
Brain.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
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
/*
* Created by SharpDevelop.
* User: novalis78
* Date: 17.11.2004
* Time: 20:16
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.IO;
using System.Windows.Forms;
using System.Collections.Specialized;
namespace PreludeEngine
{
/// <summary>
/// Description of Brain
/// </summary>
public class Brain : StringBase
{
private static string filePath = "";
private static string currDir = "";
protected static string fullPathName = "";
#region brain constructor
public Brain(string fileName, bool fullPath)
{
currDir = Directory.GetCurrentDirectory();
filePath = currDir + "\\" + fileName;
if(fullPath)
{
readBrainFile(fileName);
fullPathName = fileName;
}
else
readBrainFile();
}
#endregion
#region write/read brain files
//read brain file from disc
protected StringCollection readBrainFile()
{
StringCollection sc = new StringCollection();
if(File.Exists(filePath))
{
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
StreamReader rs = new StreamReader(fs);
string line;
while ((line = rs.ReadLine()) != null)
{
sc.Add(line);
}
rs.Close();
fs.Close();
}
else
{
MessageBox.Show("No mind file found, creating new one");
FileStream cs = File.Create(filePath);
cs.Close();
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
StreamReader rs = new StreamReader(fs);
string line;
while ((line = rs.ReadLine()) != null)
{
sc.Add(line);
}
rs.Close();
fs.Close();
}
return sc;
}
protected StringCollection readBrainFile(string openFrom)
{
StringCollection sc = new StringCollection();
if(File.Exists(openFrom))
{
FileStream fs = new FileStream(openFrom, FileMode.Open, FileAccess.Read);
StreamReader rs = new StreamReader(fs);
string line;
while ((line = rs.ReadLine()) != null)
{
sc.Add(line);
}
rs.Close();
fs.Close();
}
return sc;
}
//write brain file to disc
protected void writeBrainFile(StringCollection memoryBuffer)
{
if(fullPathName != "")
{
writeBrainFile(memoryBuffer, fullPathName);
return;
}
File.Delete(filePath);
FileStream fs = new FileStream(filePath , FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
System.Collections.Specialized.StringEnumerator ii = memoryBuffer.GetEnumerator();
while(ii.MoveNext())
{
string dat = ii.Current;
sw.WriteLine(dat);
}
sw.Flush();
sw.Close();
fs.Close();
}
//for saving as with custom location
protected void writeBrainFile(StringCollection memoryBuffer, string saveAs)
{
/*File.Delete(saveAs);
FileStream fs = new FileStream(saveAs , FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
System.Collections.Specialized.StringEnumerator ii = memoryBuffer.GetEnumerator();
while(ii.MoveNext())
{
string dat = ii.Current;
sw.WriteLine(dat);
}
sw.Flush();
sw.Close();
fs.Close(); */
}
#endregion
#region logging
protected void writeToLogFile(string header, string message)
{
/*DateTime dtNow;
dtNow = DateTime.Now;
string logFile = currDir + "\\" + dtNow.ToShortDateString() + "_" + "log.txt";
FileStream fs = new FileStream(logFile , FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine(dtNow.ToShortTimeString() + " [" + header + "] : " + message);
sw.Flush();
sw.Close();
fs.Close(); */
}
#endregion
}
}