output.h 2.8 KB

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