|
12 | 12 |
|
13 | 13 | #include "file_handler.hpp"
|
14 | 14 |
|
15 |
| -namespace TINY |
16 |
| -{ |
17 |
| - /** |
18 |
| - * @namespace SCANNER |
19 |
| - * @brief Contains all components related to the lexical analysis (scanning) of TINY language. |
20 |
| - */ |
21 |
| - namespace SCANNER |
22 |
| - { |
23 |
| - |
24 |
| - std::string FileHandler::readFile(const std::string &filePath) |
25 |
| - { |
26 |
| - // Open the file at the given path |
27 |
| - std::ifstream file(filePath); |
28 |
| - |
29 |
| - // Check if the file was opened successfully |
30 |
| - if (!file.is_open()) |
31 |
| - { |
32 |
| - throw std::runtime_error("Failed to open file: " + filePath); |
33 |
| - } |
34 |
| - |
35 |
| - // Read the entire file content into a string |
36 |
| - return std::string( |
37 |
| - (std::istreambuf_iterator<char>(file)), |
38 |
| - std::istreambuf_iterator<char>()); |
39 |
| - } |
40 |
| - |
41 |
| - void FileHandler::writeTokens(const std::string &filePath, const std::vector<Token> &tokens, |
42 |
| - bool includePosition) |
43 |
| - { |
44 |
| - // Create a string file content from the tokens |
45 |
| - std::ostringstream contentStream; |
46 |
| - for (const Token &token : tokens) |
47 |
| - { |
48 |
| - contentStream << token.toString(includePosition) << "\n"; |
49 |
| - } |
50 |
| - |
51 |
| - // Write the file content to the file at the given path |
52 |
| - FileHandler::writeFile(filePath, contentStream.str()); |
53 |
| - } |
54 |
| - |
55 |
| - void FileHandler::writeFile(const std::string &filePath, const std::string &content) |
56 |
| - { |
57 |
| - // Open the file at the given path |
58 |
| - std::ofstream file(filePath); |
59 |
| - |
60 |
| - // Check if the file was opened successfully |
61 |
| - if (!file.is_open()) |
62 |
| - { |
63 |
| - throw std::runtime_error("Failed to open file: " + filePath); |
64 |
| - } |
65 |
| - |
66 |
| - // Write the content to the file |
67 |
| - file << content; |
68 |
| - } |
69 |
| - } // namespace SCANNER |
70 |
| -} // namespace TINY |
| 15 | +namespace TINY { |
| 16 | +/** |
| 17 | + * @namespace SCANNER |
| 18 | + * @brief Contains all components related to the lexical analysis (scanning) of TINY language. |
| 19 | + */ |
| 20 | +namespace SCANNER { |
| 21 | + |
| 22 | +std::string FileHandler::readFile(const std::string &filePath) { |
| 23 | + // file path cannot be empty |
| 24 | + if (filePath.empty()) { |
| 25 | + throw std::invalid_argument("File path cannot be empty."); |
| 26 | + } |
| 27 | + |
| 28 | + // file path must exist |
| 29 | + if (!std::filesystem::exists(filePath)) { |
| 30 | + throw std::invalid_argument("File does not exist: " + filePath); |
| 31 | + } |
| 32 | + |
| 33 | + // Open the file at the given path |
| 34 | + std::ifstream file(filePath); |
| 35 | + |
| 36 | + // Check if the file was opened successfully |
| 37 | + if (!file.is_open()) { |
| 38 | + throw std::runtime_error("Failed to open file: " + filePath); |
| 39 | + } |
| 40 | + |
| 41 | + // Read the entire file content into a string |
| 42 | + return std::string( |
| 43 | + (std::istreambuf_iterator<char>(file)), |
| 44 | + std::istreambuf_iterator<char>()); |
| 45 | +} |
| 46 | + |
| 47 | +void FileHandler::writeTokens(const std::string &filePath, const std::vector<Token> &tokens, |
| 48 | + bool includePosition) { |
| 49 | + // Create a string file content from the tokens |
| 50 | + std::ostringstream contentStream; |
| 51 | + for (const Token &token : tokens) { |
| 52 | + contentStream << token.toString(includePosition) << "\n"; |
| 53 | + } |
| 54 | + |
| 55 | + // Write the file content to the file at the given path |
| 56 | + FileHandler::writeFile(filePath, contentStream.str()); |
| 57 | +} |
| 58 | + |
| 59 | +void FileHandler::writeFile(const std::string &filePath, const std::string &content) { |
| 60 | + // if the file path is empty, throw an invalid argument exception |
| 61 | + if (filePath.empty()) { |
| 62 | + throw std::invalid_argument("File path cannot be empty."); |
| 63 | + } |
| 64 | + |
| 65 | + // if the path is a directory, throw an invalid argument exception |
| 66 | + if (std::filesystem::is_directory(filePath)) { |
| 67 | + throw std::invalid_argument("File path is a directory: " + filePath); |
| 68 | + } |
| 69 | + |
| 70 | + // Extract the parent directory from the file path |
| 71 | + std::filesystem::path fileParentPath = std::filesystem::path(filePath).parent_path(); |
| 72 | + |
| 73 | + // if the path have unmade folders, create them |
| 74 | + if (!fileParentPath.empty() && !std::filesystem::exists(fileParentPath)) { |
| 75 | + std::filesystem::create_directories(fileParentPath); |
| 76 | + // set color to orange |
| 77 | + std::cout << "\033[1;33m"; |
| 78 | + // print the message with the full path from the root |
| 79 | + std::cout << "Creating directory: " << std::filesystem::absolute(fileParentPath) << std::endl; |
| 80 | + // reset color |
| 81 | + std::cout << "\033[0m"; |
| 82 | + } |
| 83 | + |
| 84 | + // Open the file at the given path |
| 85 | + std::ofstream file(filePath); |
| 86 | + |
| 87 | + // Check if the file was opened successfully |
| 88 | + if (!file.is_open()) { |
| 89 | + throw std::runtime_error("Failed to open file: " + filePath); |
| 90 | + } |
| 91 | + |
| 92 | + // Write the content to the file |
| 93 | + file << content; |
| 94 | +} |
| 95 | +} // namespace SCANNER |
| 96 | +} // namespace TINY |
0 commit comments