1 // =================================
  2 // Copyright (c) 2021 Seppo Laakko
  3 // Distributed under the MIT license
  4 // =================================
  5 
  6 using System;
  7 using System.Collections;
  8 
  9 namespace System.IO
 10 {
 11     public static class File
 12     {
 13         public static nothrow bool Exists(const string& filePath)
 14         {
 15             return RtFileExists(filePath.Chars());
 16         }
 17         public static long Size(const string& filePath)
 18         {
 19             int errorStringHandle = -1;
 20             long fileSize = RtGetFileSize(filePath.Chars()errorStringHandle);
 21             if (fileSize == -1)
 22             {
 23                 string errorMessage = RtGetError(errorStringHandle);
 24                 RtDisposeError(errorStringHandle);
 25                 throw FileSystemException(errorMessage);
 26             }
 27             else
 28             {
 29                 return fileSize;
 30             }
 31         }
 32         public static nothrow bool LastWriteTimeLess(const string& filePath1const string& filePath2)
 33         {
 34             return RtLastWriteTimeLess(filePath1.Chars()filePath2.Chars());
 35         }
 36         public static StreamWriter CreateText(const string& filePath)
 37         {
 38             return StreamWriter(SharedPtr<ByteStream>(new BufferedByteStream(SharedPtr<ByteStream>(
 39                 new FileByteStream(filePathOpenMode.write)))));
 40         }
 41         public static BinaryWriter CreateBinary(const string& filePath)
 42         {
 43             return BinaryWriter(SharedPtr<ByteStream>(new BufferedByteStream(SharedPtr<ByteStream>(
 44                 new FileByteStream(filePathcast<OpenMode>(OpenMode.write | OpenMode.binary))))));
 45         }
 46         public static StreamWriter AppendText(const string& filePath)
 47         {
 48             return StreamWriter(SharedPtr<ByteStream>(new BufferedByteStream(SharedPtr<ByteStream>(
 49                 new FileByteStream(filePathOpenMode.append)))));
 50         }
 51         public static StreamReader OpenRead(const string& filePath)
 52         {
 53             return StreamReader(SharedPtr<ByteStream>(new BufferedByteStream(SharedPtr<ByteStream>(
 54                 new FileByteStream(filePathOpenMode.read)))));
 55         }
 56         public static BinaryReader OpenBinary(const string& filePath)
 57         {
 58             return BinaryReader(SharedPtr<ByteStream>(new BufferedByteStream(SharedPtr<ByteStream>(
 59                 new FileByteStream(filePathcast<OpenMode>(OpenMode.read | OpenMode.binary))))));
 60         }
 61         public static string ReadAllText(const string& filePath)
 62         {
 63             StreamReader reader = OpenRead(filePath);
 64             string content = reader.ReadToEnd();
 65             if (content.Length() >= 3 && cast<byte>(content[0]) == 0xEFu && cast<byte>(content[1]) == 0xBBu && cast<byte>(content[2]) == 0xBFu)
 66             {
 67                 return content.Substring(3);
 68             }
 69             else
 70             {
 71                 return content;
 72             }
 73         }
 74         public static List<string> ReadAllLines(const string& filePath)
 75         {
 76             List<string> lines;
 77             bool start = true;
 78             StreamReader reader = OpenRead(filePath);
 79             string line = reader.ReadLine();
 80             while (!reader.EndOfStream())
 81             {
 82                 if (start)
 83                 {
 84                     if (line.Length() >= 3 && cast<byte>(line[0]) == 0xEFu && cast<byte>(line[1]) == 0xBBu && cast<byte>(line[2]) == 0xBFu)
 85                     {
 86                         line = line.Substring(3);
 87                     }
 88                     start = false;
 89                 }
 90                 lines.Add(line);
 91                 line = reader.ReadLine();
 92             }
 93             if (!line.IsEmpty())
 94             {
 95                 lines.Add(line);
 96             }
 97             return lines;
 98         }
 99         public static void Remove(const string& filePath)
100         {
101             int errorStringHandle = -1;
102             if (!RtRemoveFile(filePath.Chars()errorStringHandle))
103             {
104                 string errorMessage = RtGetError(errorStringHandle);
105                 RtDisposeError(errorStringHandle);
106                 throw FileSystemException(errorMessage);
107             }
108         }
109         public static void Copy(const string& sourceFilePathconst string& targetFilePath)
110         {
111             int errorStringHandle = -1;
112             if (!RtCopyFile(sourceFilePath.Chars()targetFilePath.Chars()errorStringHandle))
113             {
114                 string errorMessage = RtGetError(errorStringHandle);
115                 RtDisposeError(errorStringHandle);
116                 throw FileSystemException(errorMessage);
117             }
118         }
119         public static void Move(const string& sourceFilePathconst string& targetFilePath)
120         {
121             int errorStringHandle = -1;
122             if (!RtMoveFile(sourceFilePath.Chars()targetFilePath.Chars()errorStringHandle))
123             {
124                 string errorMessage = RtGetError(errorStringHandle);
125                 RtDisposeError(errorStringHandle);
126                 throw FileSystemException(errorMessage);
127             }
128         }
129     }
130 }