-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTextfile.cs
More file actions
199 lines (181 loc) · 7.08 KB
/
Textfile.cs
File metadata and controls
199 lines (181 loc) · 7.08 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//-----------------------------------------------------------------------
// <copyright file="Textfile.cs" company="NoteFly">
// NoteFly a note application.
// Copyright (C) 2010-2013 Tom
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// </copyright>
//-----------------------------------------------------------------------
namespace NoteFly
{
using System;
using System.IO;
using System.Text;
/// <summary>
/// What file type is being written.
/// </summary>
public enum TextfileWriteType
{
/// <summary>
/// Writing to a logfile.
/// </summary>
log,
/// <summary>
/// Writing to plain text file.
/// </summary>
exporttext,
/// <summary>
/// Writing to a rtf file.
/// </summary>
exportrtf,
/// <summary>
/// Writing to html file.
/// </summary>
exporthtml,
/// <summary>
/// Writing to php file.
/// </summary>
exportphp
}
/// <summary>
/// Textfile saving class
/// </summary>
public sealed class Textfile
{
/// <summary>
/// Initializes a new instance of the Textfile class.
/// </summary>
/// <param name="writetype">The TextfileWriteType.</param>
/// <param name="filename">The filename</param>
/// <param name="title">The title of the textfile</param>
/// <param name="content">The content of the textfile</param>
public Textfile(TextfileWriteType writetype, string filename, string title, string content)
{
if (File.Exists(filename))
{
bool filelocked = false;
int filelockchecks = 0;
while (filelocked)
{
filelockchecks++;
filelocked = this.CheckFileLocked(filename);
if (filelockchecks > 50)
{
return;
}
if (filelocked)
{
System.Threading.Thread.Sleep(200);
}
}
}
FileStream fs = null;
StreamWriter writer = null;
try
{
switch (writetype)
{
case TextfileWriteType.log:
fs = new FileStream(filename, FileMode.Append);
writer = new StreamWriter(fs, Encoding.ASCII);
writer.Write(content);
break;
case TextfileWriteType.exporttext:
fs = new FileStream(filename, FileMode.OpenOrCreate);
writer = new StreamWriter(fs, Encoding.UTF8);
writer.WriteLine(Strings.T("Title: ") + title + Environment.NewLine);
writer.Write(content);
break;
case TextfileWriteType.exportrtf:
fs = new FileStream(filename, FileMode.OpenOrCreate);
writer = new StreamWriter(fs, Encoding.ASCII);
writer.Write(content);
// add a null character to the end.of the RTF file.
writer.Write((char)0);
break;
case TextfileWriteType.exporthtml:
fs = new FileStream(filename, FileMode.OpenOrCreate);
writer = new StreamWriter(fs, Encoding.UTF8);
// trying to make turn a incompleet html fragement into a valid html document.
if (!content.Contains("<!DOCTYPE"))
{
writer.WriteLine("<!DOCTYPE html>");
}
if (!content.Contains("<html"))
{
writer.WriteLine("<html>");
}
if (!content.Contains("<head>"))
{
writer.WriteLine("<head>");
writer.WriteLine("\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
writer.WriteLine("\t<title>" + title + "</title>");
writer.WriteLine("</head>");
}
if (!content.Contains("<body"))
{
writer.WriteLine("<body>");
}
writer.WriteLine("<h1>" + title + "</h1>");
writer.WriteLine("<p>" + content + "</p>");
if (!content.Contains("</body>"))
{
writer.WriteLine("</body>");
}
if (!content.Contains("</html>"))
{
writer.WriteLine("</html>");
}
break;
case TextfileWriteType.exportphp:
fs = new FileStream(filename, FileMode.OpenOrCreate);
writer = new StreamWriter(fs, Encoding.ASCII);
writer.Write(content);
break;
}
}
finally
{
if (writer != null)
{
writer.Close();
}
if (fs != null)
{
fs.Close();
}
}
}
/// <summary>
/// Attempt to open the file exclusively.
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
private bool CheckFileLocked(string filename)
{
try
{
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.None, 32))
{
fs.ReadByte();
return false;
}
}
catch (Exception)
{
return true;
}
}
}
}