Skip to content

Commit 164fc97

Browse files
authored
Merge pull request #50 from linksplatform/FreePhoenix
FreePhoenix
2 parents 00ac8c5 + 7a2c3c0 commit 164fc97

File tree

2 files changed

+244
-0
lines changed

2 files changed

+244
-0
lines changed

csharp/Platform.IO/FileHelpers.cs

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,43 @@
77

88
namespace Platform.IO
99
{
10+
/// <summary>
11+
/// <para>Provides a set of helper methods to work with files.</para>
12+
/// <para>Предоставляет набор вспомогательных методов для работы с файлами.</para>
13+
/// </summary>
1014
public static class FileHelpers
1115
{
16+
/// <summary>
17+
/// <para>Reads all the text and returns character array from the file at the <paramref name="path"/>.</para>
18+
/// <para>Читает весь текст и возвращает массив символов из файла находящегося в <paramref name="path"/>.</para>
19+
/// </summary>
20+
/// <param name="path">
21+
/// <para>The path to the file, from which to read the character array.</para>
22+
/// <para>Путь к файлу, из которого нужно прочитать массив символов.</para>
23+
/// </param>
24+
/// <returns>
25+
/// <para>The character array from the file at the <paramref name="path"/>.</para>
26+
/// <para>Массив символов из файла находящегося в <paramref name="path"/>.</para>
27+
/// </returns>
1228
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1329
public static char[] ReadAllChars(string path) => File.ReadAllText(path).ToCharArray();
1430

31+
/// <summary>
32+
/// <para>Reads and returns all <typeparamref name="T"/> structure values from the file at the <paramref name="path"/>.</para>
33+
/// <para>Считывает и возвращает все значения структур типа <typeparamref name="T"/> из файла находящегося в <paramref name="path"/>.</para>
34+
/// </summary>
35+
/// <typeparam name="T">
36+
/// <para>The structure type.</para>
37+
/// <para>Тип структуры.</para>
38+
/// </typeparam>
39+
/// <param name="path">
40+
/// <para>The path to the file, from which to read <typeparamref name="T"/> structure values array.</para>
41+
/// <para>Путь к файлу, из которого нужно прочитать массив значений структур типа <typeparamref name="T"/>.</para>
42+
/// </param>
43+
/// <returns>
44+
/// <para>The <typeparamref name="T"/> structure values array.</para>
45+
/// <para>Массив значений структур типа <typeparamref name="T"/>.</para>
46+
/// </returns>
1547
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1648
public static T[] ReadAll<T>(string path)
1749
where T : struct
@@ -20,6 +52,22 @@ public static T[] ReadAll<T>(string path)
2052
return reader.ReadAll<T>();
2153
}
2254

55+
/// <summary>
56+
/// <para>Reads and returns the first <typeparamref name="T"/> structure value from the file at the <paramref name="path"/>.</para>
57+
/// <para>Считывает и возвращает первое значение структуры типа <typeparamref name="T"/> из файла находящегося в <paramref name="path"/>.</para>
58+
/// </summary>
59+
/// <typeparam name="T">
60+
/// <para>The structure type.</para>
61+
/// <para>Тип структуры.</para>
62+
/// </typeparam>
63+
/// <param name="path">
64+
/// <para>The path to the file, from which to read the <typeparamref name="T"/> structure value.</para>
65+
/// <para>Путь к файлу, из которого нужно прочитать значение структуры типа <typeparamref name="T"/>.</para>
66+
/// </param>
67+
/// <returns>
68+
/// <para>The <typeparamref name="T"/> structure value if read from the file at the <paramref name="path"/> is successful; otherwise the default <typeparamref name="T"/> structure value.</para>
69+
/// <para>Значение структуры типа <typeparamref name="T"/> если чтение из файла находящегося в <paramref name="path"/> прошло успешно, иначе значение структуры типа <typeparamref name="T"/> по умолчанию.</para>
70+
/// </returns>
2371
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2472
public static T ReadFirstOrDefault<T>(string path)
2573
where T : struct
@@ -28,9 +76,49 @@ public static T ReadFirstOrDefault<T>(string path)
2876
return fileStream?.ReadOrDefault<T>() ?? default;
2977
}
3078

79+
/// <summary>
80+
/// <para>Returns the <see cref="FileStream"/> opened for reading from the file at the <paramref name="path"/> if the file exists, not empty and its size is a multiple of the <typeparamref name="TStruct"/> structure size; otherwise <see langword="null"/>.</para>
81+
/// <para>Возвращает <see cref="FileStream"/> открытый для чтения из файла находящегося в <paramref name="path"/>, если файл существует, не пуст и его размер кратен размеру структуры типа <typeparamref name="TStruct"/>, а иначе <see langword="null"/>.</para>
82+
/// </summary>
83+
/// <typeparam name="TStruct">
84+
/// <para>The structure type.</para>
85+
/// <para>Тип структуры.</para>
86+
/// </typeparam>
87+
/// <param name="path">
88+
/// <para>The path to the file to validate.</para>
89+
/// <para>Путь к проверяемому файлу.</para>
90+
/// </param>
91+
/// <returns>
92+
/// <para>A <see cref="FileStream"/> opened for reading in the case of successful check; otherwise <see langword="null"/>.</para>
93+
/// <para><see cref="FileStream"/> открытый для чтения в случае успешной проверки, а иначе <see langword="null"/>.</para>
94+
/// </returns>
95+
/// <exception cref="InvalidOperationException">
96+
/// <para>The size of the file at the <paramref name="path"/> is not a multiple of the required <paramref name="elementSize"/>.</para>
97+
/// <para>Размер файла находящегося в <paramref name="path"/> не кратен требуемому <paramref name="elementSize"/>.</para>
98+
/// </exception>
3199
[MethodImpl(MethodImplOptions.AggressiveInlining)]
32100
private static FileStream GetValidFileStreamOrDefault<TStruct>(string path) where TStruct : struct => GetValidFileStreamOrDefault(path, Structure<TStruct>.Size);
33101

102+
/// <summary>
103+
/// <para>Returns the <see cref="FileStream"/> opened for reading from the file at the <paramref name="path"/> if the file exists, not empty and its size is a multiple of the required <paramref name="elementSize"/>; otherwise <see langword="null"/>.</para>
104+
/// <para>Возвращает <see cref="FileStream"/> открытый для чтения из файла находящегося в <paramref name="path"/>, если файл существует, не пуст и его размер кратен <paramref name="elementSize"/>, а иначе <see langword="null"/>.</para>
105+
/// </summary>
106+
/// <param name="path">
107+
/// <para>The path to the file to validate.</para>
108+
/// <para>Путь к проверяемому файлу.</para>
109+
/// </param>
110+
/// <param name="elementSize">
111+
/// <para>Required size of elements located in the file at the <paramref name="path"/>.</para>
112+
/// <para>Требуемый размер элементов, находящихся в файле находящегося в <paramref name="path"/>.</para>
113+
/// </param>
114+
/// <returns>
115+
/// <para>A <see cref="FileStream"/> opened for reading in the case of successful check; otherwise <see langword="null"/>.</para>
116+
/// <para><see cref="FileStream"/> открытый для чтения в случае успешной проверки, а иначе <see langword="null"/>.</para>
117+
/// </returns>
118+
/// <exception cref="InvalidOperationException">
119+
/// <para>The size of the file at the <paramref name="path"/> is not a multiple of the required <paramref name="elementSize"/>.</para>
120+
/// <para>Размер файла находящегося в <paramref name="path"/> не кратен требуемому <paramref name="elementSize"/>.</para>
121+
/// </exception>
34122
[MethodImpl(MethodImplOptions.AggressiveInlining)]
35123
private static FileStream GetValidFileStreamOrDefault(string path, int elementSize)
36124
{
@@ -46,6 +134,22 @@ private static FileStream GetValidFileStreamOrDefault(string path, int elementSi
46134
return fileSize > 0 ? File.OpenRead(path) : null;
47135
}
48136

137+
/// <summary>
138+
/// <para>Reads and returns the last <typeparamref name="T"/> structure value from the file at the <paramref name="path"/>.</para>
139+
/// <para>Считывает и возвращает последнее значение структуры типа <typeparamref name="T"/> из файла находящегося в <paramref name="path"/>.</para>
140+
/// </summary>
141+
/// <typeparam name="T">
142+
/// <para>The structure type.</para>
143+
/// <para>Тип структуры.</para>
144+
/// </typeparam>
145+
/// <param name="path">
146+
/// <para>The path to the <typeparamref name="T"/> structure values.</para>
147+
/// <para>Путь к файлу с значениями структур типа <typeparamref name="T"/>.</para>
148+
/// </param>
149+
/// <returns>
150+
/// <para>The <typeparamref name="T"/> structure value from the file at the <paramref name="path"/> in the case of successful read; otherwise the default <typeparamref name="T"/> structure value.</para>
151+
/// <para>Значение структуры типа <typeparamref name="T"/> из файла находящегося в <paramref name="path"/> в случае успешного чтения, иначе значение по умолчанию структуры типа <typeparamref name="T"/>.</para>
152+
/// </returns>
49153
[MethodImpl(MethodImplOptions.AggressiveInlining)]
50154
public static T ReadLastOrDefault<T>(string path)
51155
where T : struct
@@ -61,6 +165,22 @@ public static T ReadLastOrDefault<T>(string path)
61165
return reader.ReadOrDefault<T>();
62166
}
63167

168+
/// <summary>
169+
/// <para>Writes <typeparamref name="T"/> structure value at the beginning of the file at the <paramref name="path"/>.</para>
170+
/// <para>Записывает значение структуры типа <typeparamref name="T"/> в начало файла находящегося в <paramref name="path"/>.</para>
171+
/// </summary>
172+
/// <typeparam name="T">
173+
/// <para>The structure type.</para>
174+
/// <para>Тип структуры.</para>
175+
/// </typeparam>
176+
/// <param name="path">
177+
/// <para>The path to the file to be changed or created.</para>
178+
/// <para>Путь к файлу, который будет изменён или создан.</para>
179+
/// </param>
180+
/// <param name="value">
181+
/// <para><typeparamref name="T"/> structure value to be written at the beginning of the file at the <paramref name="path"/>.</para>
182+
/// <para>Значение структуры типа <typeparamref name="T"/>, записываемое в начало файла находящегося в <paramref name="path"/>.</para>
183+
/// </param>
64184
[MethodImpl(MethodImplOptions.AggressiveInlining)]
65185
public static void WriteFirst<T>(string path, T value)
66186
where T : struct
@@ -70,12 +190,48 @@ public static void WriteFirst<T>(string path, T value)
70190
writer.Write(value);
71191
}
72192

193+
/// <summary>
194+
/// <para>Opens or creates the file at the <paramref name="path"/> and returns its <see cref="FileStream"/> with append mode and write access.</para>
195+
/// <para>Открывает или создает файл находящегося в <paramref name="path"/> и возвращает его <see cref="FileStream"/> с режимом дополнения и доступом на запись.</para>
196+
/// </summary>
197+
/// <param name="path">
198+
/// <para>The path to the file to open or create.</para>
199+
/// <para>Путь к файлу, который нужно открыть или создать.</para>
200+
/// </param>
201+
/// <returns>
202+
/// <para>The <see cref="FileStream"/> with append mode and write access.</para>
203+
/// <para><see cref="FileStream"/> с режимом дополнения и доступом на запись.</para>
204+
/// </returns>
73205
[MethodImpl(MethodImplOptions.AggressiveInlining)]
74206
public static FileStream Append(string path) => File.Open(path, FileMode.Append, FileAccess.Write);
75207

208+
/// <summary>
209+
/// <para>Returns the size of file at the <paramref name="path"/> if file exists; otherwise 0.</para>
210+
/// <para>Возвращает размер файла находящегося в <paramref name="path"/> если тот существует, иначе 0.</para>
211+
/// </summary>
212+
/// <param name="path">
213+
/// <para>The path to the file to get size.</para>
214+
/// <para>Путь к файлу, размер которого нужно получить.</para>
215+
/// </param>
216+
/// <returns>
217+
/// <para>Size of file at the <paramref name="path"/> if it exists; otherwise 0.</para>
218+
/// <para>Размер файла если файл находящийся в <paramref name="path"/> существует, либо 0.</para>
219+
/// </returns>
76220
[MethodImpl(MethodImplOptions.AggressiveInlining)]
77221
public static long GetSize(string path) => File.Exists(path) ? new FileInfo(path).Length : 0;
78222

223+
/// <summary>
224+
/// <para>Sets the <paramref name="size"/> for the file at the <paramref name="path"/>.</para>
225+
/// <para>Устанавливает <paramref name="size"/> файлу находящемуся в <paramref name="path"/>.</para>
226+
/// </summary>
227+
/// <param name="path">
228+
/// <para>The path to the file to be resized.</para>
229+
/// <para>Путь к файлу, размер которого нужно изменить.</para>
230+
/// </param>
231+
/// <param name="size">
232+
/// <para>The size to assign to the file at the <paramref name="path"/>.</para>
233+
/// <para>Размер который будет присвоен файлу находящемуся в <paramref name="path"/>.</para>
234+
/// </param>
79235
[MethodImpl(MethodImplOptions.AggressiveInlining)]
80236
public static void SetSize(string path, long size)
81237
{
@@ -86,12 +242,48 @@ public static void SetSize(string path, long size)
86242
}
87243
}
88244

245+
/// <summary>
246+
/// <para>Removes all files from the directory at the path <paramref name="directory"/>.</para>
247+
/// <para>Удаляет все файлы из директории находящейся по пути <paramref name="directory"/>.</para>
248+
/// </summary>
249+
/// <param name="directory">
250+
/// <para>The path to the directory to be cleaned.</para>
251+
/// <para>Путь к директории для очистки.</para>
252+
/// </param>
89253
[MethodImpl(MethodImplOptions.AggressiveInlining)]
90254
public static void DeleteAll(string directory) => DeleteAll(directory, "*");
91255

256+
/// <summary>
257+
/// <para>Removes files from the directory at the path <paramref name="directory"/> according to the <paramref name="searchPattern"/>.</para>
258+
/// <para>Удаляет файлы из директории находящейся по пути <paramref name="directory"/> в соотвествии с <paramref name="searchPattern"/>.</para>
259+
/// </summary>
260+
/// <param name="directory">
261+
/// <para>The path to the directory to be cleaned.</para>
262+
/// <para>Путь к директории для очистки.</para>
263+
/// </param>
264+
/// <param name="searchPattern">
265+
/// <para>A search pattern for files to be deleted in the directory at the path <paramref name="directory"/>.</para>
266+
/// <para>Шаблон поиска для удаляемых файлов в директории находящейся по пути <paramref name="directory"/>.</para>
267+
/// </param>
92268
[MethodImpl(MethodImplOptions.AggressiveInlining)]
93269
public static void DeleteAll(string directory, string searchPattern) => DeleteAll(directory, searchPattern, SearchOption.TopDirectoryOnly);
94270

271+
/// <summary>
272+
/// <para>Removes files from the directory at the path <paramref name="directory"/> according to the <paramref name="searchPattern"/> and the <paramref name="searchOption"/>.</para>
273+
/// <para>Удаляет файлы из директории находящейся по пути <paramref name="directory"/> в соотвествии с <paramref name="searchPattern"/> и <paramref name="searchOption"/>.</para>
274+
/// </summary>
275+
/// <param name="directory">
276+
/// <para>The path to the directory to be cleaned.</para>
277+
/// <para>Путь к директории для очистки.</para>
278+
/// </param>
279+
/// <param name="searchPattern">
280+
/// <para>A search pattern for files to be deleted in the directory at the path <paramref name="directory"/>.</para>
281+
/// <para>Шаблон поиска для удаляемых файлов в директории находящейся по пути <paramref name="directory"/> .</para>
282+
/// </param>
283+
/// <param name="searchOption">
284+
/// <para>A <see cref="SearchOption"/> value that determines whether to search only in the current the directory at the path <paramref name="directory"/>, or also in all subdirectories.</para>
285+
/// <para>Значение <see cref="SearchOption"/> определяющее искать ли только в текущей директории находящейся по пути <paramref name="directory"/>, или также во всех субдиректориях.</para>
286+
/// </param>
95287
[MethodImpl(MethodImplOptions.AggressiveInlining)]
96288
public static void DeleteAll(string directory, string searchPattern, SearchOption searchOption)
97289
{

0 commit comments

Comments
 (0)