33#include " file.hpp"
44#include < cassert>
55#include < format>
6+ #include < mutex>
7+ #include < map>
68
79namespace hk {
810
9- file_buffer::file_buffer (hk::path_id path_id)
10- : file(path_id)
11+ std::mutex file_buffer::_mutex;
12+ std::map<std::filesystem::path, std::vector<char >> file_buffer::_content_by_path;
13+
14+ file_buffer::file_buffer (std::filesystem::path path)
15+ : file(), _path(std::move(path))
1116{
1217}
1318
@@ -18,13 +23,18 @@ file_buffer::file_buffer(hk::path_id path_id)
1823 }
1924
2025 auto const _ = std::scoped_lock (_mutex);
26+ auto it = _content_by_path.lower_bound (_path);
27+ if (it == _content_by_path.end () or it->first != _path) {
28+ it = _content_by_path.emplace_hint (it, _path, std::vector<char >{});
29+ }
30+ auto const &content = it->second ;
2131
22- if (position >= _content .size ()) {
32+ if (position >= content .size ()) {
2333 return 0 ; // Position is beyond the end of the content
2434 }
2535
26- auto bytes_read = std::min (buffer.size (), _content .size () - position);
27- std::copy_n (_content .data () + position, bytes_read, buffer.data ());
36+ auto bytes_read = std::min (buffer.size (), content .size () - position);
37+ std::copy_n (content .data () + position, bytes_read, buffer.data ());
2838
2939 if (bytes_read < buffer.size ()) {
3040 // If we read less than the buffer size, fill the rest with null characters.
@@ -36,10 +46,17 @@ file_buffer::file_buffer(hk::path_id path_id)
3646
3747std::size_t file_buffer::write (std::size_t position, std::span<char const > buffer)
3848{
39- if (position >= _content.size ()) {
40- _content.resize (position + buffer.size (), ' \0 ' );
49+ auto const _ = std::scoped_lock (_mutex);
50+ auto it = _content_by_path.lower_bound (_path);
51+ if (it == _content_by_path.end () or it->first != _path) {
52+ it = _content_by_path.emplace_hint (it, _path, std::vector<char >{});
53+ }
54+ auto &content = it->second ;
55+
56+ if (position >= content.size ()) {
57+ content.resize (position + buffer.size (), ' \0 ' );
4158 }
42- std::copy (buffer.begin (), buffer.end (), _content .begin () + position);
59+ std::copy (buffer.begin (), buffer.end (), content .begin () + position);
4360 return buffer.size ();
4461}
4562
@@ -49,20 +66,19 @@ std::size_t file_buffer::write(std::size_t position, std::span<char const> buffe
4966
5067 // Generate a unique path for the file buffer.
5168 // This could be a temporary file or a specific directory for file buffers.
52- return std::format (" /tmp/hic-buffer-{}.bin" , ++counter);
69+ return std::filesystem::path{ std:: format (" /tmp/hic-buffer-{}.bin" , ++counter)} ;
5370}
5471
55- [[nodiscard]] path_id make_file_buffer (std::span<char const > content)
72+ [[nodiscard]] std::filesystem::path make_file_buffer (std::span<char const > content)
5673{
57- auto path = path_id{make_file_buffer_path ()};
58- auto & file = get_file (path);
59- assert (dynamic_cast <file_buffer*>(&file) != nullptr );
74+ auto path = make_file_buffer_path ();
75+ auto file = file_buffer{path};
6076
6177 file.write (0 , content);
6278 return path;
6379}
6480
65- [[nodiscard]] path_id make_file_buffer (std::string_view content)
81+ [[nodiscard]] std::filesystem::path make_file_buffer (std::string_view content)
6682{
6783 return make_file_buffer (std::span<char const >{content.data (), content.size ()});
6884}
0 commit comments