string_buffer.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #ifndef dap_string_buffer_h
  15. #define dap_string_buffer_h
  16. #include "dap/io.h"
  17. #include <algorithm> // std::min
  18. #include <cstring> // memcpy
  19. #include <deque>
  20. #include <memory> // std::unique_ptr
  21. #include <string>
  22. namespace dap {
  23. class StringBuffer : public virtual Reader, public virtual Writer {
  24. public:
  25. static inline std::unique_ptr<StringBuffer> create();
  26. inline bool write(const std::string& s);
  27. inline std::string string() const;
  28. // Reader / Writer compilance
  29. inline bool isOpen() override;
  30. inline void close() override;
  31. inline size_t read(void* buffer, size_t bytes) override;
  32. inline bool write(const void* buffer, size_t bytes) override;
  33. private:
  34. std::string str;
  35. std::deque<size_t> chunk_lengths;
  36. bool closed = false;
  37. };
  38. bool StringBuffer::isOpen() {
  39. return !closed;
  40. }
  41. void StringBuffer::close() {
  42. closed = true;
  43. }
  44. std::unique_ptr<StringBuffer> StringBuffer::create() {
  45. return std::unique_ptr<StringBuffer>(new StringBuffer());
  46. }
  47. bool StringBuffer::write(const std::string& s) {
  48. return write(s.data(), s.size());
  49. }
  50. std::string StringBuffer::string() const {
  51. return str;
  52. }
  53. size_t StringBuffer::read(void* buffer, size_t bytes) {
  54. if (closed || bytes == 0 || str.size() == 0 || chunk_lengths.size() == 0) {
  55. return 0;
  56. }
  57. size_t& chunk_length = chunk_lengths.front();
  58. auto len = std::min(bytes, chunk_length);
  59. memcpy(buffer, str.data(), len);
  60. str = std::string(str.begin() + len, str.end());
  61. if (bytes < chunk_length) {
  62. chunk_length -= bytes;
  63. } else {
  64. chunk_lengths.pop_front();
  65. }
  66. return len;
  67. }
  68. bool StringBuffer::write(const void* buffer, size_t bytes) {
  69. if (closed) {
  70. return false;
  71. }
  72. auto chars = reinterpret_cast<const char*>(buffer);
  73. str.append(chars, chars + bytes);
  74. chunk_lengths.push_back(bytes);
  75. return true;
  76. }
  77. } // namespace dap
  78. #endif // dap_string_buffer_h