00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00047 #ifndef FASTFORMAT_INCL_FASTFORMAT_SINK_HPP_CHAR_BUFFER
00048 #define FASTFORMAT_INCL_FASTFORMAT_SINK_HPP_CHAR_BUFFER
00049
00050
00051
00052
00053
00054 #ifndef FASTFORMAT_DOCUMENTATION_SKIP_SECTION
00055 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_CHAR_BUFFER_MAJOR 1
00056 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_CHAR_BUFFER_MINOR 2
00057 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_CHAR_BUFFER_REVISION 1
00058 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_CHAR_BUFFER_EDIT 10
00059 #endif
00060
00061
00062
00063
00064
00065 #ifndef __cplusplus
00066 # error This file can only be included in C++ compilation units
00067 #endif
00068
00069
00070
00071
00072
00073 #include <fastformat/fastformat.h>
00074 #include <fastformat/quality/contract.h>
00075 #include <fastformat/util/sinks/helpers.hpp>
00076 #include <fastformat/format/standard_flags.hpp>
00077
00078 #include <stdexcept>
00079
00080
00081
00082
00083
00084 #if !defined(FASTFORMAT_NO_NAMESPACE)
00085 namespace fastformat
00086 {
00087 namespace sinks
00088 {
00089 #endif
00090
00091
00092
00093
00094
00097 class char_buffer_sink
00098 {
00099 public:
00101 typedef char_buffer_sink class_type;
00103 typedef ff_char_t char_type;
00105 typedef size_t size_type;
00106
00107 public:
00109 char_buffer_sink(size_type n, char_type* buffer)
00110 : m_capacity(n)
00111 , m_len(0)
00112 , m_buffer(buffer)
00113 {}
00114 #ifdef STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT
00116 template <size_t N>
00117 char_buffer_sink(char_type (&ar)[N])
00118 : m_capacity(N)
00119 , m_len(0)
00120 , m_buffer(&ar[0])
00121 {}
00122 #endif
00123
00124 public:
00125
00126 size_type size() const
00127 {
00128 return m_len;
00129 }
00131 size_type capacity() const
00132 {
00133 return m_capacity;
00134 }
00135
00136 public:
00137
00138 class_type& write(size_type cchTotal, size_type numResults, ff_string_slice_t const* results, int flags)
00139 {
00140 const ff_string_slice_t crlf = fastformat_getNewlineForPlatform();
00141
00142 const size_type requiredSize = size()
00143 + cchTotal
00144 + ((flags::ff_newLine & flags) ? crlf.len : 0)
00145 ;
00146
00147 if(requiredSize > capacity())
00148 {
00149 throw std::out_of_range("character buffer sink capacity exceeded");
00150 }
00151 else
00152 {
00153 char_type* p = &m_buffer[0] + size();
00154
00155
00156
00157 { for(size_type i = 0; i < numResults; ++i)
00158 {
00159 ff_string_slice_t const& slice = results[i];
00160
00161 ::memcpy(p, slice.ptr, slice.len * sizeof(char_type));
00162 p += slice.len;
00163 }}
00164
00165
00166
00167 if(flags::ff_newLine & flags)
00168 {
00169 const ff_string_slice_t crlf = fastformat_getNewlineForPlatform();
00170
00171 ::memcpy(p, crlf.ptr, crlf.len * sizeof(char_type));
00172 p += crlf.len;
00173 }
00174
00175 m_len += cchTotal;
00176
00177 FASTFORMAT_CONTRACT_ENFORCE_POSTCONDITION_STATE_APPL_LAYER(p == &m_buffer[0] + size(), "char_buffer sink writing logic failed: write pointer in wrong place");
00178 }
00179
00180 return *this;
00181 }
00182
00183 private:
00184 const size_type m_capacity;
00185 size_t m_len;
00186 char_type* const m_buffer;
00187
00188 private:
00189 char_buffer_sink(class_type const&);
00190 class_type& operator =(class_type const&);
00191 };
00192
00193
00194
00195
00196
00200 inline char_buffer_sink& fmt_slices(char_buffer_sink& sink, int flags, size_t cchTotal, size_t numResults, ff_string_slice_t const* results)
00201 {
00202 return sink.write(cchTotal, numResults, results, flags);
00203 }
00204
00205
00206
00207
00208
00209 #if !defined(FASTFORMAT_NO_NAMESPACE)
00210 }
00211 }
00212 #endif
00213
00214
00215
00216 #endif
00217
00218