GCC Code Coverage Report


Directory: ../
File: src/util/FileUtil.cpp
Date: 2025-11-21 23:52:31
Coverage Exec Excl Total
Lines: 96.4% 27 0 28
Functions: 100.0% 3 0 3
Branches: 50.0% 29 0 58

Line Branch Exec Source
1 // Copyright (c) 2021-2025 ChilliBits. All rights reserved.
2
3 #include "FileUtil.h"
4
5 #include <exception/CompilerError.h>
6
7 namespace spice::compiler {
8
9 /**
10 * Creates a file and writes fileContent to it.
11 *
12 * @param filePath File path
13 * @param fileContent String to write into the file
14 */
15 2 void FileUtil::writeToFile(const std::filesystem::path &filePath, const std::string &fileContent) {
16
1/2
✓ Branch 2 → 3 taken 2 times.
✗ Branch 2 → 28 not taken.
2 std::ofstream file(filePath);
17
2/4
✓ Branch 3 → 4 taken 2 times.
✗ Branch 3 → 26 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 12 taken 2 times.
2 if (!file)
18 throw CompilerError(IO_ERROR, "Failed to open file: " + filePath.string());
19
1/2
✓ Branch 12 → 13 taken 2 times.
✗ Branch 12 → 26 not taken.
2 file << fileContent;
20
1/2
✓ Branch 13 → 14 taken 2 times.
✗ Branch 13 → 26 not taken.
2 file.flush();
21
1/2
✓ Branch 14 → 15 taken 2 times.
✗ Branch 14 → 26 not taken.
2 file.close();
22 2 }
23
24 /**
25 * Retrieve the contents of a file as a string
26 *
27 * @param filePath File path
28 * @return File contents as a string
29 */
30 612 std::string FileUtil::getFileContent(const std::filesystem::path &filePath) {
31
1/2
✓ Branch 2 → 3 taken 612 times.
✗ Branch 2 → 35 not taken.
612 std::ifstream file(filePath);
32
3/4
✓ Branch 3 → 4 taken 612 times.
✗ Branch 3 → 33 not taken.
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 12 taken 611 times.
612 if (!file)
33
3/6
✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 27 not taken.
✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 25 not taken.
✓ Branch 8 → 9 taken 1 time.
✗ Branch 8 → 22 not taken.
1 throw CompilerError(IO_ERROR, "Failed to open file: " + filePath.string());
34
1/2
✓ Branch 12 → 13 taken 611 times.
✗ Branch 12 → 33 not taken.
611 std::stringstream stringStream;
35
1/2
✓ Branch 14 → 15 taken 611 times.
✗ Branch 14 → 31 not taken.
611 stringStream << file.rdbuf();
36
1/2
✓ Branch 15 → 16 taken 611 times.
✗ Branch 15 → 31 not taken.
611 file.close();
37
1/2
✓ Branch 16 → 17 taken 611 times.
✗ Branch 16 → 31 not taken.
1222 return stringStream.str();
38 612 }
39
40 /**
41 * Retrieve the number of lines in a file
42 *
43 * @param filePath File path
44 * @return Number of lines
45 */
46 2 size_t FileUtil::getLineCount(const std::filesystem::path &filePath) {
47
1/2
✓ Branch 2 → 3 taken 2 times.
✗ Branch 2 → 36 not taken.
2 std::ifstream file(filePath);
48
3/4
✓ Branch 3 → 4 taken 2 times.
✗ Branch 3 → 34 not taken.
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 12 taken 1 time.
2 if (!file)
49
3/6
✓ Branch 6 → 7 taken 1 time.
✗ Branch 6 → 28 not taken.
✓ Branch 7 → 8 taken 1 time.
✗ Branch 7 → 26 not taken.
✓ Branch 8 → 9 taken 1 time.
✗ Branch 8 → 23 not taken.
1 throw CompilerError(IO_ERROR, "Failed to open file: " + filePath.string());
50 1 size_t lineCount = 0;
51 1 std::string line;
52
4/6
✓ Branch 15 → 16 taken 6 times.
✗ Branch 15 → 32 not taken.
✓ Branch 16 → 17 taken 6 times.
✗ Branch 16 → 32 not taken.
✓ Branch 17 → 14 taken 5 times.
✓ Branch 17 → 18 taken 1 time.
6 while (std::getline(file, line))
53 5 lineCount++;
54
1/2
✓ Branch 18 → 19 taken 1 time.
✗ Branch 18 → 32 not taken.
1 file.close();
55 1 return lineCount;
56 2 }
57
58 } // namespace spice::compiler
59