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
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
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
00084
00085 BufferOutput::~BufferOutput() {
00086 }
00087
00088
00089
00090
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
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
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
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
00172
00173 ssize_t BufferOutput::operator() (
00174 const void* buffer,
00175 size_t ,
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
00200
00201 int BufferOutput::operator() () {
00202 return 0;
00203 }
00204
00205
00206
00207
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 }