|
16 | 16 |
|
17 | 17 | #include <errno.h>
|
18 | 18 | #include <stdio.h>
|
| 19 | +#include <string> |
| 20 | +#include <fstream> |
| 21 | +#include <filesystem> |
19 | 22 |
|
20 | 23 | namespace Vortex {
|
21 | 24 | namespace {
|
22 | 25 |
|
23 |
| -// ================================================================================================ |
24 |
| -// Utility functions. |
25 |
| - |
26 |
| -static FILE* OpenFile(StringRef path, bool write) |
27 |
| -{ |
28 |
| - FILE* file; |
29 |
| - WideString wpath = Widen(path); |
30 |
| - if(!(file = _wfopen(wpath.str(), write ? L"wb" : L"rb"))) |
31 |
| - { |
32 |
| - const char* reason = "file not found"; |
33 |
| - if(errno == EACCES) reason = "permission denied, file might be read only"; |
34 |
| - else if(write) reason = "unable to create file"; |
35 |
| - Debug::blockBegin(Debug::ERROR, "could not open file"); |
36 |
| - Debug::log("file: %s\n", path.str()); |
37 |
| - Debug::log("reason: %s\n", reason); |
38 |
| - Debug::blockEnd(); |
39 |
| - } |
40 |
| - return (FILE*)file; |
41 |
| -} |
42 |
| - |
43 | 26 | // ================================================================================================
|
44 | 27 | // Path iteration functions.
|
45 | 28 |
|
@@ -372,74 +355,45 @@ static bool isNewline(char c)
|
372 | 355 | return (c == '\n' || c == '\r');
|
373 | 356 | }
|
374 | 357 |
|
375 |
| -long getSize(StringRef path) |
376 |
| -{ |
377 |
| - FILE* fp = OpenFile(path, false); |
378 |
| - if(!fp) return 0; |
379 |
| - fseek(fp, 0, SEEK_END); |
380 |
| - long size = ftell(fp); |
381 |
| - fclose(fp); |
382 |
| - return size; |
383 |
| -} |
384 |
| - |
385 | 358 | String getText(StringRef path, bool* success)
|
386 | 359 | {
|
387 |
| - FILE* fp = OpenFile(path, false); |
388 |
| - if(!fp) { if(success) *success = false; return String(); } |
389 |
| - fseek(fp, 0, SEEK_END); |
390 |
| - long size = ftell(fp); |
391 |
| - String out(size, 0); |
392 |
| - fseek(fp, 0, SEEK_SET); |
393 |
| - fread(out.begin(), 1, size, fp); |
394 |
| - fclose(fp); |
395 |
| - if(success) *success = true; |
396 |
| - return out; |
| 360 | + size_t size = std::filesystem::file_size(path.str()); |
| 361 | + String str(static_cast<int>(size) + 1, '\0'); |
| 362 | + std::ifstream in(path.str()); |
| 363 | + if (in.fail()) |
| 364 | + { |
| 365 | + HudError("Failed to open file: %s", strerror(errno)); |
| 366 | + if (success != nullptr) |
| 367 | + *success = false; |
| 368 | + return {}; |
| 369 | + } |
| 370 | + |
| 371 | + in.read(str.begin(), size); |
| 372 | + if (success != nullptr) |
| 373 | + *success = true; |
| 374 | + return str; |
397 | 375 | }
|
398 | 376 |
|
399 | 377 | Vector<String> getLines(StringRef path, bool* success)
|
400 | 378 | {
|
401 |
| - constexpr size_t kBufferSize = 256; |
402 |
| - constexpr size_t kNumberOne = 1; |
403 |
| - |
404 |
| - Vector<String> out; |
405 |
| - FILE* fp = OpenFile(path, false); |
406 |
| - if(!fp) { if(success) *success = false; return out; } |
407 |
| - out.append(); |
408 |
| - std::array<char, kBufferSize> buffer; |
409 |
| - for (size_t bytesRead; bytesRead = fread(buffer.data(), kNumberOne, buffer.size(), fp);) |
410 |
| - { |
411 |
| - if(bytesRead > 0 && isNewline(buffer[0]) && out.back().len()) |
412 |
| - { |
413 |
| - out.append(); |
414 |
| - } |
415 |
| - for (size_t pos = 0, end = 0; pos < bytesRead;) |
416 |
| - { |
417 |
| - while(pos < bytesRead && isNewline(buffer[pos])) |
418 |
| - { |
419 |
| - ++pos, ++end; |
420 |
| - } |
421 |
| - while(end < bytesRead && !isNewline(buffer[end])) |
422 |
| - { |
423 |
| - ++end; |
424 |
| - } |
425 |
| - if(end > pos) |
426 |
| - { |
427 |
| - Str::append(out.back(), buffer.data() + pos, static_cast<int>(end - pos)); |
428 |
| - } |
429 |
| - if(end < bytesRead && isNewline(buffer[end])) |
430 |
| - { |
431 |
| - out.append(); |
432 |
| - } |
433 |
| - pos = end; |
434 |
| - } |
435 |
| - } |
436 |
| - if(out.back().empty()) |
| 379 | + std::ifstream in(path.str()); |
| 380 | + if (in.fail()) |
437 | 381 | {
|
438 |
| - out.pop_back(); |
| 382 | + HudError("Failed to open file: %s", strerror(errno)); |
| 383 | + if (success != nullptr) |
| 384 | + *success = false; |
| 385 | + return {}; |
439 | 386 | }
|
440 |
| - fclose(fp); |
441 |
| - if(success) *success = true; |
442 |
| - return out; |
| 387 | + |
| 388 | + Vector<String> v; |
| 389 | + std::string line; |
| 390 | + |
| 391 | + while (std::getline(in, line)) |
| 392 | + v.push_back(line.c_str()); |
| 393 | + |
| 394 | + if (success != nullptr) |
| 395 | + *success = true; |
| 396 | + return v; |
443 | 397 | }
|
444 | 398 |
|
445 | 399 | static void LogMoveFileError(StringRef path, StringRef newPath)
|
|
0 commit comments