silver-ymz commented on code in PR #3004:
URL:
https://github.com/apache/incubator-opendal/pull/3004#discussion_r1314456551
##########
bindings/cpp/src/opendal.cpp:
##########
@@ -85,6 +87,70 @@ std::vector<Entry> Operator::list(std::string_view path) {
return entries;
}
+ReaderStream Operator::reader(std::string_view path) {
+ return {operator_.value()->reader(RUST_STR(path))};
+}
+
+// Reader
+
+// Development Note:
+// Because rust side can't get current pointer info of c++, so we delay the
+// `consume` operation to the next `fill_buf`. Please pay attention to call
+// `consume` and update c++ pointers before each `seek` and `fill_buf`
+// operation.
+
+ffi::SeekDir to_rust_seek_dir(std::ios_base::seekdir dir);
+
+ReaderStreamBuf::pos_type
+ReaderStreamBuf::seekoff(ReaderStreamBuf::off_type off,
+ std::ios_base::seekdir dir,
+ std::ios_base::openmode which) {
+ if (!(which & std::ios_base::in)) {
+ return -1;
+ }
+
+ if (gptr() != nullptr) {
+ reader_->consume(gptr() - eback());
+ setg(gptr(), gptr(), egptr());
+ }
+
+ if (dir == std::ios_base::cur) {
+ off += gptr() - eback();
+ }
+
+ auto res = reader_->seek(off, to_rust_seek_dir(dir));
+
+ auto buffer = reader_->buffer();
+ auto gbeg = (char *)(buffer.data());
+ auto gcurr = gbeg;
+ auto gend = gbeg + buffer.size();
+ setg(gbeg, gcurr, gend);
+
+ return res;
+}
+
+ReaderStreamBuf::pos_type
+ReaderStreamBuf::seekpos(ReaderStreamBuf::pos_type pos,
+ std::ios_base::openmode which) {
+ return seekoff(pos, std::ios_base::beg, which);
+}
+
+ReaderStreamBuf::int_type ReaderStreamBuf::underflow() {
+ if (gptr() != nullptr) {
+ reader_->consume(gptr() - eback());
+ setg(gptr(), gptr(), egptr());
+ }
+ auto buffer = reader_->fill_buf();
+ auto gbeg = (char *)(buffer.data());
+ auto gcurr = gbeg;
+ auto gend = gbeg + buffer.size();
+ setg(gbeg, gcurr, gend);
+
+ return gcurr == gend ? traits_type::eof() : traits_type::to_int_type(*gcurr);
Review Comment:
> Maybe we polish this part by giving gbeg, gcurr and gend a more meaningful
name?
`gbeg`, `gcurr`, `gend` is the name used in cpp document of
[setg](https://en.cppreference.com/w/cpp/io/basic_streambuf/setg).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]