| 1 |
// Copyright 2019 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#ifndef dap_string_buffer_h |
| 16 |
#define dap_string_buffer_h |
| 17 |
|
| 18 |
#include "dap/io.h" |
| 19 |
|
| 20 |
#include <algorithm> // std::min |
| 21 |
#include <cstring> // memcpy |
| 22 |
#include <deque> |
| 23 |
#include <memory> // std::unique_ptr |
| 24 |
#include <string> |
| 25 |
|
| 26 |
namespace dap { |
| 27 |
|
| 28 |
class StringBuffer : public virtual Reader, public virtual Writer { |
| 29 |
public: |
| 30 |
static inline std::unique_ptr<StringBuffer> create(); |
| 31 |
|
| 32 |
inline bool write(const std::string& s); |
| 33 |
inline std::string string() const; |
| 34 |
|
| 35 |
// Reader / Writer compilance |
| 36 |
inline bool isOpen() override; |
| 37 |
inline void close() override; |
| 38 |
inline size_t read(void* buffer, size_t bytes) override; |
| 39 |
inline bool write(const void* buffer, size_t bytes) override; |
| 40 |
|
| 41 |
private: |
| 42 |
std::string str; |
| 43 |
std::deque<size_t> chunk_lengths; |
| 44 |
bool closed = false; |
| 45 |
}; |
| 46 |
|
| 47 |
bool StringBuffer::isOpen() { |
| 48 |
return !closed; |
| 49 |
} |
| 50 |
void StringBuffer::close() { |
| 51 |
closed = true; |
| 52 |
} |
| 53 |
|
| 54 |
std::unique_ptr<StringBuffer> StringBuffer::create() { |
| 55 |
return std::unique_ptr<StringBuffer>(new StringBuffer()); |
| 56 |
} |
| 57 |
|
| 58 |
bool StringBuffer::write(const std::string& s) { |
| 59 |
return write(s.data(), s.size()); |
| 60 |
} |
| 61 |
|
| 62 |
std::string StringBuffer::string() const { |
| 63 |
return str; |
| 64 |
} |
| 65 |
|
| 66 |
size_t StringBuffer::read(void* buffer, size_t bytes) { |
| 67 |
if (closed || bytes == 0 || str.size() == 0 || chunk_lengths.size() == 0) { |
| 68 |
return 0; |
| 69 |
} |
| 70 |
size_t& chunk_length = chunk_lengths.front(); |
| 71 |
|
| 72 |
auto len = std::min(bytes, chunk_length); |
| 73 |
memcpy(buffer, str.data(), len); |
| 74 |
str = std::string(str.begin() + len, str.end()); |
| 75 |
if (bytes < chunk_length) { |
| 76 |
chunk_length -= bytes; |
| 77 |
} else { |
| 78 |
chunk_lengths.pop_front(); |
| 79 |
} |
| 80 |
return len; |
| 81 |
} |
| 82 |
|
| 83 |
bool StringBuffer::write(const void* buffer, size_t bytes) { |
| 84 |
if (closed) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
auto chars = reinterpret_cast<const char*>(buffer); |
| 88 |
str.append(chars, chars + bytes); |
| 89 |
chunk_lengths.push_back(bytes); |
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
} // namespace dap |
| 94 |
|
| 95 |
#endif // dap_string_buffer_h |