BufferOutput.cpp

Go to the documentation of this file.
00001 /* vim: set ts=4 expandtab shiftwidth=4: */
00002 
00003 /******************************************************************************
00004 
00005     Copyright 2005 Digital Aggregates Corp., Arvada CO 80001-0587, USA.
00006     This file is part of the Digital Aggregates Desperado library.
00007     
00008     This library is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU Lesser General Public
00010     License as published by the Free Software Foundation; either
00011     version 2.1 of the License, or (at your option) any later version.
00012 
00013     As a special exception, if other files instantiate templates or
00014     use macros or inline functions from this file, or you compile
00015     this file and link it with other works to produce a work based on
00016     this file, this file does not by itself cause the resulting work
00017     to be covered by the GNU Lesser General Public License. However
00018     the source code for this file must still be made available in
00019     accordance with the GNU Lesser General Public License.
00020 
00021     This exception does not invalidate any other reasons why a work
00022     based on this file might be covered by the GNU Lesser General
00023     Public License.
00024 
00025     Alternative commercial licensing terms are available from the copyright
00026     holder. Contact Digital Aggregates Corporation for more information.
00027 
00028     This library is distributed in the hope that it will be useful,
00029     but WITHOUT ANY WARRANTY; without even the implied warranty of
00030     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00031     GNU Lesser General Public License for more details.
00032 
00033     You should have received a copy of the GNU Lesser General
00034     Public License along with this library; if not, write to the
00035     Free Software Foundation, Inc., 59 Temple Place, Suite 330,
00036     Boston, MA 02111-1307 USA, or http://www.gnu.org/copyleft/lesser.txt.
00037 
00038     $Name:  $
00039 
00040     $Id: BufferOutput.cpp,v 1.6 2006/02/07 00:07:02 jsloan Exp $
00041 
00042 ******************************************************************************/
00043 
00044 
00060 #include <new>
00061 #include "stdio.h"
00062 #include "errno.h"
00063 #include "target.h"
00064 #include "string.h"
00065 #include "BufferOutput.h"
00066 #include "Print.h"
00067 #include "Platform.h"
00068 
00069 
00070 //
00071 //  Constructor. The string has a specified size.
00072 //
00073 BufferOutput::BufferOutput(char* sp, size_t sz) :
00074     Output(),
00075     string(sp),
00076     size((0 != sp) ? sz : 0),
00077     offset(0)
00078 {
00079 }
00080 
00081 
00082 //
00083 //  Destructor.
00084 //
00085 BufferOutput::~BufferOutput() {
00086 }
00087 
00088 
00089 //
00090 //    Initializer.
00091 //
00092 bool BufferOutput::initialize(char* sp, size_t sz) {
00093     bool rc = false;
00094     try {
00095         this->BufferOutput::~BufferOutput();
00096         new(this) BufferOutput(sp, sz);
00097         rc = true;
00098     } catch (...) {
00099         rc = false;
00100     }
00101     return rc;
00102 }
00103 
00104 
00105 //
00106 //  Output a character.
00107 //
00108 int BufferOutput::operator() (int c) {
00109     int rc = EOF;
00110     if (0 == this->string) {
00111         errno = EINVAL;
00112     } else if (this->offset >= this->size) {
00113         errno = 0;
00114     } else {
00115         this->string[this->offset++] = rc = c;
00116     }
00117     return rc;
00118 }
00119 
00120 
00121 //
00122 //  Format and output a variable length argument list.
00123 //
00124 ssize_t BufferOutput::operator() (const char* format, va_list ap) {
00125     ssize_t rc = EOF;
00126     if (0 == this->string) {
00127         errno = EINVAL;
00128     } else if (this->offset >= this->size) {
00129         errno = 0;
00130     } else {
00131         char buffer[this->minimum_buffer_size + 1];
00132         ssize_t fc = ::vsnprintf(buffer, sizeof(buffer), format, ap);
00133         if (0 < fc) {
00134             rc = (*this)(buffer, fc);
00135         } else if (0 == fc) {
00136             rc = fc;
00137         } else if (0 == errno) {
00138             errno = EIO;
00139         }
00140     }
00141     return rc;
00142 }
00143 
00144 
00145 //
00146 //  Output a string of no more than the specified size.
00147 //
00148 ssize_t BufferOutput::operator() (const char* s, size_t size) {
00149     ssize_t rc = EOF;
00150     if (0 == this->string) {
00151         errno = EINVAL;
00152     } else if (this->offset >= this->size) {
00153         errno = 0;
00154     } else {
00155         size_t want = ::strnlen(s, size);
00156         size_t have = this->size - this->offset;
00157         if (have < want) {
00158             want = have;
00159         }
00160         if (0 < want) {
00161             std::strncpy(&(this->string[this->offset]), s, want);
00162             this->offset += want;
00163         }
00164         rc = want;
00165     }
00166     return rc;
00167 }
00168 
00169 
00170 //
00171 //  Write binary data to the string from a buffer.
00172 //
00173 ssize_t BufferOutput::operator() (
00174     const void* buffer,
00175     size_t /* minimum */,
00176     size_t maximum
00177 ) {
00178     ssize_t rc = EOF;
00179     if (0 == this->string) {
00180         errno = EINVAL;
00181     } else if (this->offset >= this->size) {
00182         errno = 0;
00183     } else if (0 == maximum) {
00184         rc = 0;
00185     } else {
00186         size_t have = this->size - this->offset;
00187         if (have < maximum) {
00188             maximum = have;
00189         }
00190         memcpy(&(this->string[this->offset]), buffer, maximum);
00191         this->offset += maximum;
00192         rc = maximum;
00193     }
00194     return rc;
00195 }
00196 
00197 
00198 //
00199 //  Flush buffered data.
00200 //
00201 int BufferOutput::operator() () {
00202     return 0;
00203 }
00204 
00205 
00206 //
00207 //  Show this object on the output object.
00208 //
00209 void BufferOutput::show(int level, Output* display, int indent) const {
00210     Platform& pl = Platform::instance();
00211     Print printf(display);
00212     const char* sp = printf.output().indentation(indent);
00213     char component[sizeof(__FILE__)];
00214     printf("%s%s(%p)[%lu]:\n",
00215         sp, pl.component(__FILE__, component, sizeof(component)),
00216         this, sizeof(*this));
00217     this->Output::show(level, display, indent + 1);
00218     printf("%s string=%p\n", sp, this->string);
00219     printf("%s size=%lu\n", sp, this->size);
00220     printf("%s offset=%lu\n", sp, this->offset);
00221 }
Copyright © 2006 Digital Aggregates Corp., Arvada CO 80001-0587, USA.
Distributed under the terms of the GNU Free Documentation License.
http://www.diag.com/navigation/downloads/Desperado.html