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

fastformat/sinks/string_array.hpp

Go to the documentation of this file.
00001 /* /////////////////////////////////////////////////////////////////////////
00002  * File:        fastformat/sinks/string_array.hpp
00003  *
00004  * Purpose:     A FastFormat sink for string sequences.
00005  *
00006  * Created:     24th December 2007
00007  * Updated:     11th August 2009
00008  *
00009  * Home:        http://www.fastformat.org/
00010  *
00011  * Copyright (c) 2007-2009, 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 
00042 
00043 #error This file is not ready
00044 
00050 #ifndef FASTFORMAT_INCL_FASTFORMAT_SINK_HPP_STRING_ARRAY
00051 #define FASTFORMAT_INCL_FASTFORMAT_SINK_HPP_STRING_ARRAY
00052 
00053 /* /////////////////////////////////////////////////////////////////////////
00054  * Version information
00055  */
00056 
00057 #ifndef FASTFORMAT_DOCUMENTATION_SKIP_SECTION
00058 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_STRING_ARRAY_MAJOR      0
00059 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_STRING_ARRAY_MINOR      0
00060 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_STRING_ARRAY_REVISION   1
00061 # define FASTFORMAT_VER_FASTFORMAT_SINK_HPP_STRING_ARRAY_EDIT       4
00062 #endif /* !FASTFORMAT_DOCUMENTATION_SKIP_SECTION */
00063 
00064 /* /////////////////////////////////////////////////////////////////////////
00065  * Language
00066  */
00067 
00068 #ifndef __cplusplus
00069 # error This file can only be included in C++ compilation units
00070 #endif /* !__cplusplus */
00071 
00072 /* /////////////////////////////////////////////////////////////////////////
00073  * Includes
00074  */
00075 
00076 #include <fastformat/fastformat.h>
00077 #include <fastformat/internal/stlsoft.h>
00078 #include <fastformat/quality/contract.h>
00079 #include <fastformat/format/standard_flags.hpp>
00080 
00081 #include <platformstl/error/exceptions.hpp>
00082 #include <stlsoft/memory/auto_buffer.hpp>
00083 
00084 #include <string>
00085 
00086 /* /////////////////////////////////////////////////////////////////////////
00087  * Namespace
00088  */
00089 
00090 #if !defined(FASTFORMAT_NO_NAMESPACE)
00091 namespace fastformat
00092 {
00093 namespace sinks
00094 {
00095 #endif /* !FASTFORMAT_NO_NAMESPACE */
00096 
00097 /* /////////////////////////////////////////////////////////////////////////
00098  * Classes
00099  */
00100 
00103 class string_array_sink
00104 {
00105 public:
00107     typedef string_array_sink     class_type;
00108 
00109 public:
00111     string_array_sink(int fh)
00112         : m_fh(fh)
00113         , m_delim()
00114     {}
00116     string_array_sink(int fh, char const* delim)
00117         : m_fh(fh)
00118         , m_delim(delim)
00119     {}
00120 
00121 public:
00123     class_type& write(size_t numResults, ff_string_slice_t const* results, int flags)
00124     {
00125         stlsoft::auto_buffer<iovec>     vectors(numResults + 2);
00126         ff_string_slice_t               crlf = fastformat_getNewlineForPlatform();
00127 
00128 #ifdef STLSOFT_CF_THROW_BAD_ALLOC
00129         if(!vectors.empty())
00130 #endif /* STLSOFT_CF_THROW_BAD_ALLOC */
00131         {
00132             STLSOFT_ASSERT(vectors.size() == numResults + 2);
00133 
00134             { for(size_t i = 0; i != vectors.size() - 2; ++i)
00135             {
00136                 vectors[i].iov_base =   const_cast<char*>(results[i].ptr);
00137                 vectors[i].iov_len  =   results[i].len;
00138             }}
00139 
00140             if(!m_delim.empty())
00141             {
00142                 vectors[vectors.size() - 2].iov_base    =   const_cast<char*>(m_delim.data());
00143                 vectors[vectors.size() - 2].iov_len     =   m_delim.size();
00144             }
00145             else
00146             {
00147                 vectors.resize(vectors.size() - 1);
00148             }
00149 
00150             if(flags::ff_newLine & flags)
00151             {
00152                 vectors[vectors.size() - 1].iov_base    =   const_cast<char*>(crlf.ptr);
00153                 vectors[vectors.size() - 1].iov_len     =   crlf.len;
00154             }
00155             else
00156             {
00157                 vectors.resize(vectors.size() - 1);
00158             }
00159 
00160             if(::writev(m_fh, &vectors[0], static_cast<int>(vectors.size())) < 0)
00161             {
00162 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00163                 throw platformstl::platform_exception("failed to write vector payload", errno);
00164 #endif /* STLSOFT_CF_EXCEPTION_SUPPORT */
00165             }
00166 
00167 #if 0
00168             if(flags::ff_flush & flags)
00169             {
00170                 ::flush(m_fh);
00171             }
00172 #endif /* 0 */
00173         }
00174 
00175         return *this;
00176     }
00177 
00178 private:
00179     int         m_fh;
00180     std::string m_delim;
00181 };
00182 
00183 /* /////////////////////////////////////////////////////////////////////////
00184  * Action Shims
00185  */
00186 
00190 inline string_array_sink& fmt_slices(string_array_sink& sink, int flags, size_t /* cchTotal */, size_t numResults, ff_string_slice_t const* results)
00191 {
00192     return sink.write(numResults, results, flags);
00193 }
00194 
00195 /* /////////////////////////////////////////////////////////////////////////
00196  * Namespace
00197  */
00198 
00199 #if !defined(FASTFORMAT_NO_NAMESPACE)
00200 } /* namespace sinks */
00201 } /* namespace fastformat */
00202 #endif /* !FASTFORMAT_NO_NAMESPACE */
00203 
00204 /* ////////////////////////////////////////////////////////////////////// */
00205 
00206 #endif /* FASTFORMAT_INCL_FASTFORMAT_SINK_HPP_STRING_ARRAY */
00207 
00208 /* ///////////////////////////// end of file //////////////////////////// */

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