-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwrite.cpp
More file actions
44 lines (32 loc) · 984 Bytes
/
Copy pathwrite.cpp
File metadata and controls
44 lines (32 loc) · 984 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// SPDX-FileCopyrightText: 2026 Paolo Anzani
// SPDX-License-Identifier: Apache-2.0
#include "write.h"
#include <cstddef>
#include <expected>
#include <fstream>
#include <iostream>
namespace {
bool doesFileExist(const std::string& path) {
std::ifstream file(path, std::ios::binary);
if (!file) {
return false;
}
return true;
}
} // namespace
namespace microcodex {
std::expected<int, std::string> write(const std::string& path, std::string_view content) {
if (doesFileExist(path)) {
return std::unexpected("Overwriting an existing file is not allowed");
}
std::ofstream file(path, std::ios::out | std::ios::trunc);
if (!file) {
return std::unexpected("Could not create file");
}
file << content;
if (!file) {
return std::unexpected("Failed to write to file");
}
return NULL;
}
} // namespace microcodex