| 93 | } |
| 94 | |
| 95 | bool FileBase::ReadAllText(const StringView& path, String& data) |
| 96 | { |
| 97 | PROFILE_CPU_NAMED("File::ReadAllText"); |
| 98 | ZoneText(*path, path.Length()); |
| 99 | data.Clear(); |
| 100 | |
| 101 | // Read data |
| 102 | Array<byte> bytes; |
| 103 | if (ReadAllBytes(path, bytes)) |
| 104 | return true; |
| 105 | |
| 106 | // Check for empty data |
| 107 | if (bytes.IsEmpty()) |
| 108 | return false; |
| 109 | |
| 110 | // Add null terminator char |
| 111 | int32 count = bytes.Count(); |
| 112 | bytes.Add('\0'); |
| 113 | |
| 114 | // Check BOM char |
| 115 | // TODO: maybe let's check whole BOM???? |
| 116 | switch (bytes[0]) |
| 117 | { |
| 118 | case 0xEF: // UTF-8 |
| 119 | { |
| 120 | // BOM: EF BB BF |
| 121 | |
| 122 | // Eat BOM |
| 123 | count -= 3; |
| 124 | if (count < 0) |
| 125 | { |
| 126 | // Invalid data |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | // Convert to UTF-16 |
| 131 | int32 utf16Length; |
| 132 | Char* utf16Data = StringUtils::ConvertUTF82UTF16(reinterpret_cast<char*>(bytes.Get()), count, utf16Length); |
| 133 | data.Set(utf16Data, utf16Length); |
| 134 | Allocator::Free(utf16Data); |
| 135 | } |
| 136 | break; |
| 137 | case 0xFE: // UTF-16 (BE) |
| 138 | { |
| 139 | // BOM: FE FF |
| 140 | |
| 141 | // Eat BOM |
| 142 | count -= 2; |
| 143 | if (count < 0) |
| 144 | { |
| 145 | // Invalid data |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | data = (Char*)bytes.Get(); |
| 150 | } |
| 151 | break; |
| 152 | case 0xFF: // UTF-16 (LE) |
no test coverage detected