libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
stdio.cpp
1#include "stdio.hpp"
2
3#include <limits>
4
5using namespace LibXR;
6
7// STDIO compiled-format bridge.
8// STDIO 编译格式桥接层。
10
12{
13 if (saturated_)
14 {
15 return ErrorCode::OK;
16 }
17
18 auto ec = stream_.Acquire();
19 if (ec != ErrorCode::OK)
20 {
21 return ec;
22 }
23
24 size_t copy_size = chunk.size();
25 size_t stream_writable = stream_.EmptySize();
26 if (copy_size > stream_writable)
27 {
28 copy_size = stream_writable;
29 }
30
31 if (copy_size == 0)
32 {
33 saturated_ = true;
34 return ErrorCode::OK;
35 }
36
37 ec = stream_.Write(chunk.substr(0, copy_size));
38 if (ec != ErrorCode::OK)
39 {
40 return ec;
41 }
42
43 retained_size_ += copy_size;
44 if (copy_size != chunk.size() || stream_.EmptySize() == 0)
45 {
46 saturated_ = true;
47 }
48 return ErrorCode::OK;
49}
50
52{
53 if (!STDIO::write_ || !STDIO::write_->Writable())
54 {
55 return false;
56 }
57
58 if (!write_mutex_)
59 {
61 }
62
63 return write_mutex_->Lock() == ErrorCode::OK;
64}
65
67 CompiledWriteFun write_fun)
68{
69 CompiledSink sink(stream);
70 auto ec = write_fun(context, sink);
71 return FinishWriteSession(stream, sink.RetainedSize(), ec);
72}
73
74int STDIO::WriteCompiledSession(void* context, CompiledWriteFun write_fun)
75{
76 ASSERT(write_mutex_ != nullptr);
77 ASSERT(write_fun != nullptr);
78
79 if (write_stream_ != nullptr)
80 {
81 return WriteCompiledToStream(*write_stream_, context, write_fun);
82 }
83
84 static WriteOperation op; // NOLINT
85 WritePort::Stream stream(write_, op);
86 return WriteCompiledToStream(stream, context, write_fun);
87}
88
89int STDIO::FinishWriteSession(WritePort::Stream& stream, size_t retained_size,
90 ErrorCode format_result)
91{
92 ASSERT(write_mutex_ != nullptr);
93
94 // Stream-backed STDIO is now explicitly prefix-preserving on formatting
95 // failure: any bytes already retained in this session are committed instead
96 // of being pseudo-rolled-back.
97 // 基于 Stream 的 STDIO 现在明确采用“前缀保留”语义:格式化失败时,
98 // 本会话已保留的字节仍然提交,不再尝试伪回滚。
99 auto ec = stream.Commit();
100
102
103 if (format_result != ErrorCode::OK || ec != ErrorCode::OK ||
104 retained_size > static_cast<size_t>(std::numeric_limits<int>::max()))
105 {
106 return -1;
107 }
108
109 return static_cast<int>(retained_size);
110}
互斥锁类,提供线程同步机制 (Mutex class providing thread synchronization mechanisms).
Definition mutex.hpp:18
ErrorCode Lock()
加锁,如果锁已被占用,则阻塞等待 (Lock the mutex, blocking if it is already locked).
Definition mutex.cpp:16
void Unlock()
解锁互斥锁 (Unlock the mutex).
Definition mutex.cpp:40
STDIO 编译格式会话使用的流式截断输出端 / Stream-backed truncating sink used by one STDIO compiled-format session.
Definition stdio.hpp:41
ErrorCode Write(std::string_view chunk)
追加一个文本片段;必要时按会话剩余空间直接截断 / Append one text chunk and truncate directly to the remaining session capaci...
Definition stdio.cpp:11
size_t RetainedSize() const
返回当前会话最终保留下来的字节数 / Return the retained byte count of the current session
Definition stdio.hpp:60
CompiledSink(WritePort::Stream &stream)
构造一个绑定到指定流的编译格式输出端 / Construct one compiled-format sink bound to the given stream
Definition stdio.cpp:9
static LibXR::WritePort::Stream * write_stream_
Optional externally owned write stream. 可选的外部托管写流。
Definition stdio.hpp:29
static LibXR::Mutex * write_mutex_
Write port mutex. 写入端口互斥锁。
Definition stdio.hpp:27
static int WriteCompiledToStream(WritePort::Stream &stream, void *context, CompiledWriteFun write_fun)
在指定 Stream 上执行一次完整的 STDIO 编译格式写入与收尾 / Run one complete STDIO compiled-format write and finalize pass ...
Definition stdio.cpp:66
static int FinishWriteSession(WritePort::Stream &stream, size_t retained_size, ErrorCode format_result)
提交当前编译格式会话的写入流并释放共享会话 / Commit the current compiled-format session stream and release the shared sess...
Definition stdio.cpp:89
static int WriteCompiledSession(void *context, CompiledWriteFun write_fun)
执行一次完整的 STDIO 编译格式流会话选择、写入与收尾 / Run one complete STDIO compiled-format stream session: stream selecti...
Definition stdio.cpp:74
static WritePort * write_
Write port instance. 写入端口。
Definition stdio.hpp:26
static bool BeginWriteSession()
获取一个共享的 STDIO 写入会话 / Acquire one shared STDIO write session
Definition stdio.cpp:51
ErrorCode Commit()
手动提交已写入的数据到队列,并释放当前锁。
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
@ OK
操作成功 | Operation successful