Synesis Software STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ...

fastformat/sinks/char_buffer.hpp

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////
00002  * File:        fastformat/sinks/char_buffer.hpp
00003  *
00004  * Purpose:     A FastFormat sink for fixed length character buffers.
00005  *
00006  * Created:     14th April 2008
00007  * Updated:     30th April 2010
00008  *
00009  * Home:        http://www.fastformat.org/
00010  *
00011  * Copyright (c) 2008-2010, Matthew Wilson and Synesis Software
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are
00016  * met:
00017  *
00018  * - Redistributions of source code must retain the above copyright notice,
00019  *   this list of conditions and the following disclaimer.
00020  * - Redistributions in binary form must reproduce the above copyright
00021  *   notice, this list of conditions and the following disclaimer in the
00022  *   documentation and/or other materials provided with the distribution.
00023  * - Neither the names of Matthew Wilson and Synesis Software nor the names
00024  *   of any contributors may be used to endorse or promote products derived
00025  *   from this software without specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
00028  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
00029  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00030  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00031  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00032  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00033  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00034  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00035  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00036  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00037  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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  * Version information
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        12
00059 #endif /* !FASTFORMAT_DOCUMENTATION_SKIP_SECTION */
00060 
00061 /* /////////////////////////////////////////////////////////////////////////
00062  * Language
00063  */
00064 
00065 #ifndef __cplusplus
00066 # error This file can only be included in C++ compilation units
00067 #endif /* !__cplusplus */
00068 
00069 /* /////////////////////////////////////////////////////////////////////////
00070  * Includes
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  * Namespace
00082  */
00083 
00084 #if !defined(FASTFORMAT_NO_NAMESPACE)
00085 namespace fastformat
00086 {
00087 namespace sinks
00088 {
00089 #endif /* !FASTFORMAT_NO_NAMESPACE */
00090 
00091 /* /////////////////////////////////////////////////////////////////////////
00092  * Classes
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 /* STLSOFT_CF_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT */
00123 
00124 public: // Attributes
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: // Operations
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             // next we concatenate all the slices
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             // then append the new line, if required
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: // Member variables
00184     const size_type     m_capacity;
00185     size_t              m_len;
00186     char_type* const    m_buffer;
00187 
00188 private: // Not to be implemented
00189     char_buffer_sink(class_type const&);
00190     class_type& operator =(class_type const&);
00191 };
00192 
00193 /* /////////////////////////////////////////////////////////////////////////
00194  * Action Shims
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  * Namespace
00207  */
00208 
00209 #if !defined(FASTFORMAT_NO_NAMESPACE)
00210 } /* namespace sinks */
00211 } /* namespace fastformat */
00212 #endif /* !FASTFORMAT_NO_NAMESPACE */
00213 
00214 /* ////////////////////////////////////////////////////////////////////// */
00215 
00216 #endif /* FASTFORMAT_INCL_FASTFORMAT_SINK_HPP_CHAR_BUFFER */
00217 
00218 /* ///////////////////////////// end of file //////////////////////////// */

FastFormat Library documentation © Matthew Wilson, 2006-2009 SourceForge.net Logo