DocWire SDK
DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, document analysis. Offline processing possible for security and confidentiality
memorystream.h
1 /*********************************************************************************************************************************************/
2 /* DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. */
3 /* Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, */
4 /* document analysis. Offline processing possible for security and confidentiality */
5 /* */
6 /* Copyright (c) SILVERCODERS Ltd, http://silvercoders.com */
7 /* Project homepage: https://github.com/docwire/docwire */
8 /* */
9 /* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-DocWire-Commercial */
10 /*********************************************************************************************************************************************/
11 
12 #ifndef DOCWIRE_MEMORY_STREAM_H
13 #define DOCWIRE_MEMORY_STREAM_H
14 
15 #include <istream>
16 #include <span>
17 
18 namespace docwire
19 {
20 
37 class imemorystreambuf : public std::streambuf
38 {
39  std::span<const std::byte> m_source;
40 
41 public:
47  explicit imemorystreambuf(std::span<const std::byte> source) :
48  m_source(source)
49  {
50  char* p = const_cast<char*>(reinterpret_cast<const char*>(m_source.data()));
51  setg(p, p, p + m_source.size());
52  }
53 
61  std::streampos seekpos(std::streampos sp, std::ios_base::openmode which = std::ios_base::in) override
62  {
63  if (which != std::ios_base::in || sp < 0 || sp > m_source.size())
64  return -1;
65  char* p = const_cast<char*>(reinterpret_cast<const char*>(m_source.data()));
66  setg(p, p + sp, p + m_source.size());
67  return sp;
68  }
69 
78  std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way, std::ios_base::openmode which = std::ios_base::in) override
79  {
80  if (which != std::ios_base::in)
81  return -1;
82  switch (way)
83  {
84  case std::ios_base::beg:
85  return seekpos(off, which);
86  case std::ios_base::cur:
87  return seekpos(gptr() - eback() + off, which);
88  case std::ios_base::end:
89  return seekpos(m_source.size() + off , which);
90  default:
91  return -1;
92  }
93  }
94 };
95 
112 class imemorystream : public std::istream
113 {
114 public:
120  explicit imemorystream(std::span<const std::byte> source)
121  : std::istream(&m_streambuf), m_streambuf(source)
122  {}
123 
124 private:
128  imemorystreambuf m_streambuf;
129 };
130 
131 } // namespace docwire
132 
133 #endif // DOCWIRE_MEMORY_STREAM_H
imemorystream is a stream that wraps a std::span of const std::byte and provides a compatible interfa...
Definition: memorystream.h:113
imemorystream(std::span< const std::byte > source)
Construct a new imemorystream object.
Definition: memorystream.h:120
imemorystreambuf is a stream buffer that wraps std::span<const std::byte> and provides a compatible i...
Definition: memorystream.h:38
std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way, std::ios_base::openmode which=std::ios_base::in) override
Move the position of the stream buffer.
Definition: memorystream.h:78
imemorystreambuf(std::span< const std::byte > source)
Construct a new imemorystreambuf object.
Definition: memorystream.h:47
std::streampos seekpos(std::streampos sp, std::ios_base::openmode which=std::ios_base::in) override
Set the position of the stream buffer.
Definition: memorystream.h:61
The main namespace for the DocWire SDK.
Definition: ai_elements.h:19