Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
FileNameTimeSequence.cs
1using System;
2using System.IO;
3
4namespace Waher.Runtime.IO
5{
10 {
14 public const string DefaultYearPlaceholder = "%YEAR%";
15
19 public const string DefaultMonthPlaceholder = "%MONTH%";
20
24 public const string DefaultDayPlaceholder = "%DAY%";
25
29 public const string DefaultHourPlaceholder = "%HOUR%";
30
34 public const string DefaultMinutePlaceholder = "%MINUTE%";
35
39 public const string DefaultSecondPlaceholder = "%SECOND%";
40
41 private static readonly object systemSynchObject = new object();
42
43 private readonly bool utc;
44 private readonly string fileNamePattern;
45 private readonly string placeholderYear;
46 private readonly string placeholderMonth;
47 private readonly string placeholderDay;
48 private readonly string placeholderHour;
49 private readonly string placeholderMinute;
50 private readonly string placeholderSecond;
51 private readonly bool usesYearPlaceholder;
52 private readonly bool usesMonthPlaceholder;
53 private readonly bool usesDayPlaceholder;
54 private readonly bool usesHourPlaceholder;
55 private readonly bool usesMinutePlaceholder;
56 private readonly bool usesSecondPlaceholder;
57 private DateTime last = DateTime.MinValue;
58 private bool hasLast = false;
59
60
66 : this(FileNamePattern, true)
67 {
68 }
69
79 {
80 }
81
93 string YearPlaceholder, string MonthPlaceholder, string DayPlaceholder,
94 string HourPlaceholder, string MinutePlaceholder, string SecondPlaceholder)
95 : this(FileNamePattern, true, YearPlaceholder, MonthPlaceholder, DayPlaceholder,
96 HourPlaceholder, MinutePlaceholder, SecondPlaceholder)
97 {
98 }
99
112 string YearPlaceholder, string MonthPlaceholder, string DayPlaceholder,
113 string HourPlaceholder, string MinutePlaceholder, string SecondPlaceholder)
114 {
115 this.fileNamePattern = FileNamePattern;
116 this.utc = Utc;
117
118 this.placeholderYear = YearPlaceholder;
119 this.placeholderMonth = MonthPlaceholder;
120 this.placeholderDay = DayPlaceholder;
121 this.placeholderHour = HourPlaceholder;
122 this.placeholderMinute = MinutePlaceholder;
123 this.placeholderSecond = SecondPlaceholder;
124
125 this.usesYearPlaceholder = !string.IsNullOrEmpty(this.placeholderYear) && this.fileNamePattern.Contains(this.placeholderYear);
126 this.usesMonthPlaceholder = !string.IsNullOrEmpty(this.placeholderMonth) && this.fileNamePattern.Contains(this.placeholderMonth);
127 this.usesDayPlaceholder = !string.IsNullOrEmpty(this.placeholderDay) && this.fileNamePattern.Contains(this.placeholderDay);
128 this.usesHourPlaceholder = !string.IsNullOrEmpty(this.placeholderHour) && this.fileNamePattern.Contains(this.placeholderHour);
129 this.usesMinutePlaceholder = !string.IsNullOrEmpty(this.placeholderMinute) && this.fileNamePattern.Contains(this.placeholderMinute);
130 this.usesSecondPlaceholder = !string.IsNullOrEmpty(this.placeholderSecond) && this.fileNamePattern.Contains(this.placeholderSecond);
131 }
132
136 public bool Utc => this.utc;
137
141 public string FileNamePattern => this.fileNamePattern;
142
146 public string PlaceholderYear => this.placeholderYear;
147
151 public string PlaceholderMonth => this.placeholderMonth;
152
156 public string PlaceholderDay => this.placeholderDay;
157
161 public string PlaceholderHour => this.placeholderHour;
162
166 public string PlaceholderMinute => this.placeholderMinute;
167
171 public string PlaceholderSecond => this.placeholderSecond;
172
176 public bool UsesYearPlaceholder => this.usesYearPlaceholder;
177
181 public bool UsesMonthPlaceholder => this.usesMonthPlaceholder;
182
186 public bool UsesDayPlaceholder => this.usesDayPlaceholder;
187
191 public bool UsesHourPlaceholder => this.usesHourPlaceholder;
192
196 public bool UsesMinutePlaceholder => this.usesMinutePlaceholder;
197
201 public bool UsesSecondPlaceholder => this.usesSecondPlaceholder;
202
206 public DateTime Last => this.last;
207
213 public static bool MakeUnique(ref string FileName)
214 {
215 if (File.Exists(FileName))
216 {
217 int i = FileName.LastIndexOf('.');
218 if (i < 0)
219 i = FileName.Length;
220
221 string FileName2;
222 string Suffix;
223 int j = 1;
224
225 do
226 {
227 j++;
228 Suffix = " (" + j.ToString() + ")";
229
230 FileName2 = FileName.Insert(i, Suffix);
231 }
232 while (File.Exists(FileName2) && j < 65536);
233
234 if (j == 65536)
235 return false;
236
237 FileName = FileName2;
238 }
239
240 return true;
241 }
242
249 public bool TryGetNewFileName(out string FileName)
250 {
251 DateTime TP = this.utc ? DateTime.UtcNow : DateTime.Now;
252
253 if (!this.hasLast ||
254 (this.usesSecondPlaceholder && this.last.Second != TP.Second) ||
255 (this.usesMinutePlaceholder && this.last.Minute != TP.Minute) ||
256 (this.usesHourPlaceholder && this.last.Hour != TP.Hour) ||
257 (this.usesDayPlaceholder && this.last.Day != TP.Day) ||
258 (this.usesMonthPlaceholder && this.last.Month != TP.Month) ||
259 (this.usesYearPlaceholder && this.last.Year != TP.Year))
260 {
261 this.last = TP;
262 this.hasLast = true;
263
264 FileName = this.fileNamePattern;
265
266 if (this.usesSecondPlaceholder)
267 FileName = FileName.Replace(this.placeholderSecond, TP.Second.ToString("D2"));
268
269 if (this.usesMinutePlaceholder)
270 FileName = FileName.Replace(this.placeholderMinute, TP.Minute.ToString("D2"));
271
272 if (this.usesHourPlaceholder)
273 FileName = FileName.Replace(this.placeholderHour, TP.Hour.ToString("D2"));
274
275 if (this.usesDayPlaceholder)
276 FileName = FileName.Replace(this.placeholderDay, TP.Day.ToString("D2"));
277
278 if (this.usesMonthPlaceholder)
279 FileName = FileName.Replace(this.placeholderMonth, TP.Month.ToString("D2"));
280
281 if (this.usesYearPlaceholder)
282 FileName = FileName.Replace(this.placeholderYear, TP.Year.ToString("D4"));
283
284 lock (systemSynchObject)
285 {
286 if (!MakeUnique(ref FileName))
287 return false;
288
289 StreamWriter fs = File.CreateText(FileName); // Creates a zero-byte file, to reserve the name,
290 fs.Close();
291 fs.Dispose();
292 }
293
294 return true;
295 }
296 else
297 {
298 FileName = null;
299 return false;
300 }
301 }
302 }
303}
Class that generates a sequence of file names, based on the system time.
bool Utc
If UTC timestamps are used.
bool TryGetNewFileName(out string FileName)
Tries to get a new file name.
string PlaceholderMonth
Placeholder for the current month.
string PlaceholderDay
Placeholder for the current day.
string PlaceholderMinute
Placeholder for the current minute.
static bool MakeUnique(ref string FileName)
Returns a unique file name, if a file with the name already exists.
bool UsesMinutePlaceholder
If the placeholder for the current minute is used.
string PlaceholderHour
Placeholder for the current hour.
FileNameTimeSequence(string FileNamePattern)
Class that generates a sequence of file names, based on the system time.
bool UsesSecondPlaceholder
If the placeholder for the current second is used.
bool UsesDayPlaceholder
If the placeholder for the current day is used.
bool UsesYearPlaceholder
If the placeholder for the current year is used.
DateTime Last
Last timestamp that generated a new file name.
bool UsesHourPlaceholder
If the placeholder for the current hour is used.
FileNameTimeSequence(string FileNamePattern, bool Utc)
Class that generates a sequence of file names, based on the system time.
FileNameTimeSequence(string FileNamePattern, bool Utc, string YearPlaceholder, string MonthPlaceholder, string DayPlaceholder, string HourPlaceholder, string MinutePlaceholder, string SecondPlaceholder)
Class that generates a sequence of file names, based on the system time.
bool UsesMonthPlaceholder
If the placeholder for the current month is used.
string PlaceholderSecond
Placeholder for the current second.
string PlaceholderYear
Placeholder for the current year.
FileNameTimeSequence(string FileNamePattern, string YearPlaceholder, string MonthPlaceholder, string DayPlaceholder, string HourPlaceholder, string MinutePlaceholder, string SecondPlaceholder)
Class that generates a sequence of file names, based on the system time.