- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages
- Examples

# fastformat/sinks/vectored_file.hpp

Go to the documentation of this file.

    00001 /* /////////////////////////////////////////////////////////////////////////
    00002  * File:        fastformat/sinks/vectored_file.hpp
    00003  *
    00004  * Purpose:     A FastFormat sink for the UNIX writev() vector I/O function.
    00005  *
    00006  * Created:     24th November 2007
    00007  * Updated:     7th March 2010
    00008  *
    00009  * Home:        http://www.fastformat.org/
    00010  *
    00011  * Copyright (c) 2007-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_VECTORED_FILE
    00048 #define FASTFORMAT_INCL_FASTFORMAT_SINK_HPP_VECTORED_FILE
    00049
    00050 /* /////////////////////////////////////////////////////////////////////////
    00051  * Version information
    00052  */
    00053
    00054 #ifndef FASTFORMAT_DOCUMENTATION_SKIP_SECTION
    00055 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_VECTORED_FILE_MAJOR     1
    00056 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_VECTORED_FILE_MINOR     1
    00057 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_VECTORED_FILE_REVISION  3
    00058 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_VECTORED_FILE_EDIT      20
    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/internal/stlsoft.h>
    00075 #include <fastformat/quality/contract.h>
    00076 #include <fastformat/format/standard_flags.hpp>
    00077
    00078 #include <platformstl/error/exceptions.hpp>
    00079 #include <stlsoft/memory/auto_buffer.hpp>
    00080
    00081 #include <string>
    00082 #include <sys/uio.h>
    00083
    00084 /* /////////////////////////////////////////////////////////////////////////
    00085  * Namespace
    00086  */
    00087
    00088 #if !defined(FASTFORMAT_NO_NAMESPACE)
    00089 namespace fastformat
    00090 {
    00091 namespace sinks
    00092 {
    00093 #endif /* !FASTFORMAT_NO_NAMESPACE */
    00094
    00095 /* /////////////////////////////////////////////////////////////////////////
    00096  * Classes
    00097  */
    00098
    00101 class vectored_file_sink
    00102 {
    00103 public:
    00105     typedef vectored_file_sink     class_type;
    00106
    00107 public:
    00109     explicit vectored_file_sink(int fh)
    00110         : m_fh(fh)
    00111         , m_delim()
    00112     {}
    00114     vectored_file_sink(int fh, char const* delim)
    00115         : m_fh(fh)
    00116         , m_delim(delim)
    00117     {}
    00118
    00119 public:
    00121     class_type& write(size_t numResults, ff_string_slice_t const* results, int flags)
    00122     {
    00123         stlsoft::auto_buffer<iovec>     vectors(numResults + 2);
    00124
    00125 #ifdef STLSOFT_CF_THROW_BAD_ALLOC
    00126         if(!vectors.empty())
    00127 #endif /* STLSOFT_CF_THROW_BAD_ALLOC */
    00128         {
    00129             FASTFORMAT_CONTRACT_ENFORCE_PRECONDITION_PARAMS_APPL_LAYER(vectors.size() == numResults + 2, "vec array must be equal to 2 + the number of results");
    00130
    00131             { for(size_t i = 0; i != vectors.size() - 2; ++i)
    00132             {
    00133                 vectors[i].iov_base =   const_cast<char*>(results[i].ptr);
    00134                 vectors[i].iov_len  =   results[i].len;
    00135             }}
    00136
    00137             if(!m_delim.empty())
    00138             {
    00139                 vectors[vectors.size() - 2].iov_base    =   const_cast<char*>(m_delim.data());
    00140                 vectors[vectors.size() - 2].iov_len     =   m_delim.size();
    00141             }
    00142             else
    00143             {
    00144                 vectors.resize(vectors.size() - 1);
    00145             }
    00146
    00147             if(flags::ff_newLine & flags)
    00148             {
    00149                 const ff_string_slice_t crlf = fastformat_getNewlineForPlatform();
    00150
    00151                 vectors[vectors.size() - 1].iov_base    =   const_cast<char*>(crlf.ptr);
    00152                 vectors[vectors.size() - 1].iov_len     =   crlf.len;
    00153             }
    00154             else
    00155             {
    00156                 vectors.resize(vectors.size() - 1);
    00157             }
    00158
    00159             if(::writev(m_fh, &vectors[0], static_cast<int>(vectors.size())) < 0)
    00160             {
    00161 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
    00162                 throw platformstl::platform_exception("failed to write vectored payload", errno);
    00163 #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
    00164             }
    00165
    00166 #if 0
    00167             if(flags::ff_flush & flags)
    00168             {
    00169                 ::flush(m_fh);
    00170             }
    00171 #endif /* 0 */
    00172         }
    00173
    00174         return *this;
    00175     }
    00176
    00177 private:
    00178     int         m_fh;
    00179     std::string m_delim;
    00180 };
    00181
    00182 /* /////////////////////////////////////////////////////////////////////////
    00183  * Action Shims
    00184  */
    00185
    00189 inline vectored_file_sink& fmt_slices(vectored_file_sink& sink, int flags, size_t /* cchTotal */, size_t numResults, ff_string_slice_t const* results)
    00190 {
    00191     return sink.write(numResults, results, flags);
    00192 }
    00193
    00194 /* /////////////////////////////////////////////////////////////////////////
    00195  * Namespace
    00196  */
    00197
    00198 #if !defined(FASTFORMAT_NO_NAMESPACE)
    00199 } /* namespace sinks */
    00200 } /* namespace fastformat */
    00201 #endif /* !FASTFORMAT_NO_NAMESPACE */
    00202
    00203 /* /////////////////////////////////////////////////////////////////////////
    00204  * Inclusion
    00205  */
    00206
    00207 #ifdef STLSOFT_PPF_pragma_once_SUPPORT
    00208 # pragma once
    00209 #endif /* STLSOFT_PPF_pragma_once_SUPPORT */
    00210
    00211 /* ////////////////////////////////////////////////////////////////////// */
    00212
    00213 #endif /* FASTFORMAT_INCL_FASTFORMAT_SINK_HPP_VECTORED_FILE */
    00214
    00215 /* ///////////////////////////// end of file //////////////////////////// */

FastFormat Library documentation © Matthew Wilson, 2006-2009 |