Repository navigation
Expand file tree
/
Copy pathVFS_Streams.cpp
More file actions
61 lines (51 loc) · 2.25 KB
/
Copy pathVFS_Streams.cpp
File metadata and controls
61 lines (51 loc) · 2.25 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <cstring>
#include <filesystem>
#include <vector>
#include "Concurrency/WorkContractGroup.h"
#include "Concurrency/WorkService.h"
#include "EntropyCore.h"
#include "VirtualFileSystem/FileHandle.h"
#include "VirtualFileSystem/FileStream.h"
#include "VirtualFileSystem/VirtualFileSystem.h"
using namespace EntropyEngine::Core;
using namespace EntropyEngine::Core::Concurrency;
using namespace EntropyEngine::Core::IO;
int main() {
WorkService service({});
service.start();
WorkContractGroup group(128, "VFS_Streams");
service.addWorkContractGroup(&group);
VirtualFileSystem vfs(&group);
auto handle = vfs.createFileHandle((std::filesystem::temp_directory_path() / "vfs_streams_demo.txt").string());
handle.createEmpty().wait();
// Unbuffered read-write stream
if (auto stream = handle.openReadWriteStream()) {
const char* msg = "Streaming API demo\n";
stream->seek(0, std::ios::end);
auto wrote = stream->write(std::span<const std::byte>(reinterpret_cast<const std::byte*>(msg), strlen(msg)));
stream->flush();
ENTROPY_LOG_INFO(std::string("Unbuffered wrote: ") + std::to_string(wrote.bytesTransferred) + " bytes");
stream->seek(0, std::ios::beg);
std::vector<std::byte> buf(256);
auto read = stream->read(buf);
ENTROPY_LOG_INFO(std::string("Unbuffered read: ") + std::to_string(read.bytesTransferred) + " bytes");
} else {
ENTROPY_LOG_ERROR("Failed to open unbuffered stream");
}
// Buffered stream wrapper
if (auto base = handle.openReadWriteStream()) {
BufferedFileStream buffered(std::move(base), 4096);
const char* msgB = "Buffered block\n";
buffered.seek(0, std::ios::end);
auto wroteB =
buffered.write(std::span<const std::byte>(reinterpret_cast<const std::byte*>(msgB), strlen(msgB)));
buffered.flush();
ENTROPY_LOG_INFO(std::string("Buffered wrote: ") + std::to_string(wroteB.bytesTransferred) + " bytes");
buffered.seek(0, std::ios::beg);
std::vector<std::byte> buf2(512);
auto read2 = buffered.read(buf2);
ENTROPY_LOG_INFO(std::string("Buffered read: ") + std::to_string(read2.bytesTransferred) + " bytes");
}
service.stop();
return 0;
}