-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetSim.Data.Files.cs
More file actions
247 lines (226 loc) · 8.31 KB
/
NetSim.Data.Files.cs
File metadata and controls
247 lines (226 loc) · 8.31 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace NetSim.Data.Files
{
/// <summary>
/// Creates a text file for writing
/// </summary>
public class OutputFile : FileStream
{
/// <summary>
/// the streamwriter
/// </summary>
private StreamWriter sw { get; set; }
/// <summary>
/// Creates a text file for writing
/// </summary>
/// <param name="fileName">name of the file</param>
public OutputFile(String fileName)
: base(OutputFile.CheckAndGetFileName(fileName), FileMode.OpenOrCreate, FileAccess.Write)
{
this.sw = new StreamWriter(this);
}
/// <summary>
/// Writes matrix data into file with given format for each number
/// </summary>
/// <typeparam name="T">Type of the data in the matrix</typeparam>
/// <param name="data">the columns of the data matrix, each of wich should be entered as a different parameter for this method</param>
/// <param name="format">format for writing data from the matrix</param>
/// <param name="separator">column separator character</param>
/// <param name="header">header of the file</param>
/// <param name="transpose">transpose data matrix before writing</param>
public void WriteData<T>(String format, String separator, String header, Boolean transpose, params T[][] data)
{
format = "{0:" + format + "}";
if (header != String.Empty)
{
this.WriteLine(header);
}
if (transpose)
{
data = this.Transpose(data);
}
Int32 n = data[0].Length;
if (data.Any(e => e.Length != n))
{
throw new ArgumentOutOfRangeException("All data vectors supplied to this method must have the same length!");
}
Int32 j, i = 0, nCol = data.Length - 1;
while (i < n)
{
j = 0;
while (j < nCol)
{
this.Write(format + separator, data[j][i]);
j++;
}
this.Write(format + Environment.NewLine, data[nCol][i]);
i++;
}
}
private T[][] Transpose<T>(T[][] A)
{
Int32 i = 0, j, n = A.Length, m = A[0].Length;
T[][] B = new T[m][];
while (i < m)
{
B[i] = new T[n];
i++;
}
i = 0;
while (i < n)
{
j = 0;
while (j < m)
{
B[j][i] = A[i][j];
j++;
}
i++;
}
return B;
}
/// <summary>
/// writes formatted string
/// </summary>
/// <param name="format">format of the string</param>
/// <param name="args">arguments (does not need an array)</param>
public void WriteLine(String format, params Object[] args)
{
this.sw.WriteLine(format, args);
}
/// <summary>
/// writes text to the file
/// </summary>
/// <param name="text">text to be written</param>
public void WriteLine(String text)
{
this.sw.WriteLine(text);
}
/// <summary>
/// writes formatted string to the file without breaking line
/// </summary>
/// <param name="format">format string</param>
/// <param name="args">arguments (does not need an array)</param>
public void Write(String format, params Object[] args)
{
this.sw.Write(format, args);
}
/// <summary>
/// writes text to the file without breaking line
/// </summary>
/// <param name="text">text to be written</param>
public void Write(String text)
{
this.sw.Write(text);
}
/// <summary>
/// closes file and streamwriter
/// </summary>
public new void Close()
{
this.sw.Close();
base.Close();
}
/// <summary>
/// checks if filename is available in the directory it was passed and returns a free filename
/// </summary>
/// <param name="fileNameWithExtension">complete filename (relative or absolute)</param>
/// <returns>a free filename based on the passed one</returns>
public static String CheckAndGetFileName(String fileNameWithExtension)
{
return OutputFile.CheckAndGetFileName(Path.GetFileNameWithoutExtension(fileNameWithExtension), Path.GetExtension(fileNameWithExtension).Substring(1));
}
/// <summary>
/// checks if filename is available in the directory it was passed and returns a free filename
/// </summary>
/// <param name="fileName">no extension filename (relative or absolute)</param>
/// <param name="ext">file extension</param>
/// <returns>a free filename based on the passed one</returns>
private static String CheckAndGetFileName(String fileName, String ext)
{
// checking whether filename already exists
String patternStr1 = "^" + fileName.Replace(Path.DirectorySeparatorChar, '%').Replace('/', '%') + @"_*[0-9]*\." + ext + @"$";
String patternStr2 = fileName.Replace(Path.DirectorySeparatorChar, '%').Replace('/', '%') + @"_*[0-9]*\." + ext + @"$";
String temp;
String dir = Path.GetDirectoryName(fileName);
dir = (dir == "" ? Directory.GetCurrentDirectory() : dir);
List<String> currFilesList = Directory.GetFiles(dir, "*" + ext, SearchOption.TopDirectoryOnly).ToList<String>();//.Select(e => e.Replace(Path.DirectorySeparatorChar, '%')).ToList<String>();
Int32 i = 0;
while (i < currFilesList.Count)
{
currFilesList[i] = currFilesList[i].Replace(Path.DirectorySeparatorChar, '%').Replace('/', '%');
currFilesList[i] = System.Text.RegularExpressions.Regex.Match(currFilesList[i], patternStr2, System.Text.RegularExpressions.RegexOptions.Compiled).Value;
i++;
}
try
{
i = 0;
while (true)
{
temp = currFilesList.First<String>(s => System.Text.RegularExpressions.Regex.IsMatch(s, patternStr1, System.Text.RegularExpressions.RegexOptions.Compiled));
if (!String.IsNullOrEmpty(temp))
{
currFilesList.Remove(temp);
i++;
continue;
}
else
{
break;
}
}
}
catch (InvalidOperationException)
{
temp = (i == 0 ? String.Empty : "_" + i.ToString());
fileName = fileName + temp + "." + ext;
}
return fileName;
}
}
/// <summary>
/// opens a text file for reading
/// </summary>
public class InputFile : FileStream
{
/// <summary>
/// the streamreader
/// </summary>
private StreamReader sr { get; set; }
/// <summary>
/// indicates whether the file is at the end of the stream
/// </summary>
public Boolean EndOfStream
{
get
{
return this.sr.EndOfStream;
}
}
/// <summary>
/// reads a line from the file
/// </summary>
public Func<String> ReadLine;
/// <summary>
/// opens a text file for reading
/// </summary>
/// <param name="fileName">the name of the file</param>
public InputFile(String fileName)
: base(fileName, FileMode.Open, FileAccess.Read)
{
this.sr = new StreamReader(this);
this.ReadLine = this.sr.ReadLine;
}
/// <summary>
/// closes the file and the streamreader
/// </summary>
public new void Close()
{
this.sr.Close();
base.Close();
}
}
}