|
1 |
| -// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved. |
| 1 | +// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved. |
2 | 2 | // For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
|
3 | 3 |
|
4 | 4 | #include "Helpers.hpp"
|
|
8 | 8 | #include <set>
|
9 | 9 | #include <algorithm>
|
10 | 10 | #include <utility>
|
| 11 | +#include <fstream> |
11 | 12 |
|
12 | 13 | namespace
|
13 | 14 | {
|
14 |
| - std::string readContent(const std::string &filename) noexcept |
| 15 | + std::string readContent(const std::string &filename) |
15 | 16 | {
|
16 |
| - auto getFileSize = [](FILE *fd) -> std::size_t { |
17 |
| - std::fseek(fd, 0, SEEK_END); |
18 |
| - const auto size = std::ftell(fd); |
19 |
| - std::rewind(fd); |
20 |
| - return size; |
21 |
| - }; |
22 |
| - |
23 |
| - std::vector<char> fileContent; |
24 |
| - if (const auto fp = std::fopen(filename.c_str(), "r")) { |
25 |
| - const auto fileSize = getFileSize(fp); |
26 |
| - |
27 |
| - fileContent.reserve(fileSize + 1); |
28 |
| - std::fread(fileContent.data(), 1, fileSize, fp); |
29 |
| - std::fclose(fp); |
30 |
| - fileContent[fileSize] = '\0'; |
31 |
| - return fileContent.data(); |
| 17 | + std::ifstream file{filename}; |
| 18 | + if (!file.is_open()) { |
| 19 | + return {}; |
32 | 20 | }
|
33 |
| - return {}; |
| 21 | + |
| 22 | + const auto fileSize = std::filesystem::file_size(filename); |
| 23 | + auto fileContent = std::make_unique<char[]>(fileSize + 1); |
| 24 | + |
| 25 | + file.read(fileContent.get(), fileSize); |
| 26 | + fileContent[fileSize] = '\0'; |
| 27 | + |
| 28 | + return std::string{fileContent.get()}; |
34 | 29 | }
|
35 | 30 |
|
36 | 31 | std::vector<std::string> readCommands(const std::filesystem::path &filePath)
|
37 | 32 | {
|
38 |
| - const auto fileContent = readContent(filePath.c_str()); |
| 33 | + const auto &fileContent = readContent(filePath.c_str()); |
39 | 34 | std::string currentStatement{};
|
40 | 35 | std::vector<std::string> statements{};
|
41 | 36 |
|
42 | 37 | std::string line{};
|
43 |
| - for (const auto &c : fileContent) { |
| 38 | + for (const auto c : fileContent) { |
44 | 39 | if (c != '\n') {
|
45 | 40 | line += c;
|
46 | 41 | }
|
|
0 commit comments