output.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2017 The Abseil Authors.
  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. // Output extension hooks for the Format library.
  16. // `internal::InvokeFlush` calls the appropriate flush function for the
  17. // specified output argument.
  18. // `BufferRawSink` is a simple output sink for a char buffer. Used by SnprintF.
  19. // `FILERawSink` is a std::FILE* based sink. Used by PrintF and FprintF.
  20. #ifndef ABSL_STRINGS_INTERNAL_STR_FORMAT_OUTPUT_H_
  21. #define ABSL_STRINGS_INTERNAL_STR_FORMAT_OUTPUT_H_
  22. #include <cstdio>
  23. #include <ios>
  24. #include <ostream>
  25. #include <string>
  26. #include "absl/base/port.h"
  27. #include "absl/strings/string_view.h"
  28. namespace absl {
  29. ABSL_NAMESPACE_BEGIN
  30. namespace str_format_internal {
  31. // RawSink implementation that writes into a char* buffer.
  32. // It will not overflow the buffer, but will keep the total count of chars
  33. // that would have been written.
  34. class BufferRawSink {
  35. public:
  36. BufferRawSink(char* buffer, size_t size) : buffer_(buffer), size_(size) {}
  37. size_t total_written() const { return total_written_; }
  38. void Write(string_view v);
  39. private:
  40. char* buffer_;
  41. size_t size_;
  42. size_t total_written_ = 0;
  43. };
  44. // RawSink implementation that writes into a FILE*.
  45. // It keeps track of the total number of bytes written and any error encountered
  46. // during the writes.
  47. class FILERawSink {
  48. public:
  49. explicit FILERawSink(std::FILE* output) : output_(output) {}
  50. void Write(string_view v);
  51. size_t count() const { return count_; }
  52. int error() const { return error_; }
  53. private:
  54. std::FILE* output_;
  55. int error_ = 0;
  56. size_t count_ = 0;
  57. };
  58. // Provide RawSink integration with common types from the STL.
  59. inline void AbslFormatFlush(std::string* out, string_view s) {
  60. out->append(s.data(), s.size());
  61. }
  62. inline void AbslFormatFlush(std::ostream* out, string_view s) {
  63. out->write(s.data(), static_cast<std::streamsize>(s.size()));
  64. }
  65. inline void AbslFormatFlush(FILERawSink* sink, string_view v) {
  66. sink->Write(v);
  67. }
  68. inline void AbslFormatFlush(BufferRawSink* sink, string_view v) {
  69. sink->Write(v);
  70. }
  71. // This is a SFINAE to get a better compiler error message when the type
  72. // is not supported.
  73. template <typename T>
  74. auto InvokeFlush(T* out, string_view s) -> decltype(AbslFormatFlush(out, s)) {
  75. AbslFormatFlush(out, s);
  76. }
  77. } // namespace str_format_internal
  78. ABSL_NAMESPACE_END
  79. } // namespace absl
  80. #endif // ABSL_STRINGS_INTERNAL_STR_FORMAT_OUTPUT_H_