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
00042
00049 #ifndef FASTFORMAT_INCL_FASTFORMAT_SINK_HPP_C_STRING
00050 #define FASTFORMAT_INCL_FASTFORMAT_SINK_HPP_C_STRING
00051
00052
00053
00054
00055
00056 #ifndef FASTFORMAT_DOCUMENTATION_SKIP_SECTION
00057 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_C_STRING_MAJOR 1
00058 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_C_STRING_MINOR 2
00059 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_C_STRING_REVISION 1
00060 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_C_STRING_EDIT 10
00061 #endif
00062
00063
00064
00065
00066
00067 #ifndef __cplusplus
00068 # error This file can only be included in C++ compilation units
00069 #endif
00070
00071
00072
00073
00074
00075 #include <fastformat/fastformat.h>
00076 #include <fastformat/quality/contract.h>
00077 #include <fastformat/util/sinks/helpers.hpp>
00078 #include <fastformat/format/standard_flags.hpp>
00079
00080 #include <stdexcept>
00081
00082
00083
00084
00085
00086 #if !defined(FASTFORMAT_NO_NAMESPACE)
00087 namespace fastformat
00088 {
00089 namespace sinks
00090 {
00091 #endif
00092
00093
00094
00095
00096
00099 class c_string_sink
00100 {
00101 public:
00103 typedef c_string_sink class_type;
00105 typedef ff_char_t char_type;
00107 typedef size_t size_type;
00108
00109 public:
00111 c_string_sink(size_type n, char_type* buffer)
00112 : m_capacity(n)
00113 , m_len(0)
00114 , m_buffer(buffer)
00115 {}
00116 #ifdef STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT
00118 template <size_t N>
00119 c_string_sink(char_type (&ar)[N])
00120 : m_capacity(N)
00121 , m_len(0)
00122 , m_buffer(&ar[0])
00123 {}
00124 #endif
00125
00126 public:
00127
00128 size_type size() const
00129 {
00130 return m_len;
00131 }
00133 size_type capacity() const
00134 {
00135 return m_capacity;
00136 }
00137
00138 public:
00139
00140 class_type& write(size_type cchTotal, size_type numResults, ff_string_slice_t const* results, int flags)
00141 {
00142 const ff_string_slice_t crlf = fastformat_getNewlineForPlatform();
00143
00144 const size_type requiredSize = size()
00145 + cchTotal
00146 + ((flags::ff_newLine & flags) ? crlf.len : 0)
00147 + 1
00148 ;
00149
00150 if(requiredSize > capacity())
00151 {
00152 throw std::out_of_range("character buffer sink capacity exceeded");
00153 }
00154 else
00155 {
00156 char_type* p = &m_buffer[0] + size();
00157
00158
00159
00160 { for(size_type i = 0; i < numResults; ++i)
00161 {
00162 ff_string_slice_t const& slice = results[i];
00163
00164 ::memcpy(p, slice.ptr, slice.len * sizeof(char_type));
00165 p += slice.len;
00166 }}
00167
00168
00169
00170 if(flags::ff_newLine & flags)
00171 {
00172 const ff_string_slice_t crlf = fastformat_getNewlineForPlatform();
00173
00174 ::memcpy(p, crlf.ptr, crlf.len * sizeof(char_type));
00175 p += crlf.len;
00176 }
00177
00178 *p++ = '\0';
00179
00180 m_len += cchTotal;
00181
00182 FASTFORMAT_CONTRACT_ENFORCE_POSTCONDITION_STATE_APPL_LAYER(p == &m_buffer[0] + size() + 1, "c_string sink writing logic failed: write pointer in wrong place");
00183 }
00184
00185 return *this;
00186 }
00187
00188 private:
00189 const size_type m_capacity;
00190 size_t m_len;
00191 char_type* const m_buffer;
00192
00193 private:
00194 c_string_sink(class_type const&);
00195 class_type& operator =(class_type const&);
00196 };
00197
00198
00199
00200
00201
00205 inline c_string_sink& fmt_slices(c_string_sink& sink, int flags, size_t cchTotal, size_t numResults, ff_string_slice_t const* results)
00206 {
00207 return sink.write(cchTotal, numResults, results, flags);
00208 }
00209
00210
00211
00212
00213
00214 #if !defined(FASTFORMAT_NO_NAMESPACE)
00215 }
00216 }
00217 #endif
00218
00219
00220
00221 #endif
00222
00223