MCPcopy Create free account
hub / github.com/Pagghiu/SaneCppLibraries / runAwaitFilePatch

Function runAwaitFilePatch

Examples/AwaitFilePatch/AwaitFilePatch.cpp:41–99  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

39}
40
41static Result runAwaitFilePatch()
42{
43 Console console;
44 Console::tryAttachingToParentConsole();
45
46 StringPath workingDirectory;
47 StringSpan cwd = FileSystem::Operations::getCurrentWorkingDirectory(workingDirectory);
48 if (cwd.isEmpty())
49 {
50 return Result::Error("AwaitFilePatch could not resolve current working directory");
51 }
52
53 StringPath path;
54 SC_TRY(Path::join(path, {cwd, "await-file-patch.txt"}));
55
56 FileSystem fs;
57 SC_TRY(fs.writeString(path.view(), "status=____\n"));
58 auto cleanup = MakeDeferred([&fs, &path] { (void)fs.removeFile(path.view()); });
59
60 ThreadPool threadPool;
61 SC_TRY(threadPool.create(2));
62 auto destroyThreadPool = MakeDeferred([&threadPool] { (void)threadPool.destroy(); });
63
64 AsyncEventLoop async;
65 SC_TRY(async.create());
66 auto closeAsync = MakeDeferred([&async] { (void)async.close(); });
67
68 FileDescriptor file;
69 SC_TRY(file.open(path.view(), FileOpen::ReadWrite));
70 auto closeFile = MakeDeferred([&file] { (void)file.close(); });
71
72 char allocatorStorage[12 * 1024] = {};
73 AwaitAllocator allocator;
74 SC_TRY(allocator.createFixed(allocatorStorage));
75 AwaitEventLoop await(async, allocator);
76
77 char readBuffer[12] = {};
78 AwaitFileReadResult readResult;
79 AwaitFileWriteResult writeResult;
80 AwaitTask task = patchStatus(await, threadPool, file, {readBuffer, sizeof(readBuffer)}, readResult, writeResult);
81
82 SC_TRY(await.spawn(task));
83 Result runResult = await.run();
84 if (not runResult)
85 {
86 return task.isCompleted() ? task.result()
87 : Result::Error("AwaitFilePatch event loop stopped before task completed");
88 }
89 SC_TRY(task.result());
90
91 if (writeResult.numBytes != 4)
92 {
93 return Result::Error("AwaitFilePatch wrote an unexpected number of bytes");
94 }
95
96 StringView text({readResult.data.data(), readResult.data.sizeInBytes()}, false, StringEncoding::Ascii);
97 console.print("AwaitFilePatch patched invoice line: {}", text);
98 return Result(true);

Callers 1

mainFunction · 0.85

Calls 15

MakeDeferredFunction · 0.85
patchStatusFunction · 0.85
createFixedMethod · 0.80
spawnMethod · 0.80
isCompletedMethod · 0.80
resultMethod · 0.80
printMethod · 0.80
ErrorEnum · 0.50
joinFunction · 0.50
ResultClass · 0.50
isEmptyMethod · 0.45

Tested by

no test coverage detected