• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

TextBuffer.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003   A brief file description
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 #include "libts.h"
00025 #include "TextBuffer.h"
00026 
00027 /****************************************************************************
00028  *
00029  *  TextBuffer.cc - A self-expanding buffer, primarly meant for strings
00030  *
00031  *
00032  *
00033  ****************************************************************************/
00034 
00035 textBuffer::textBuffer(int size)
00036 {
00037   bufferStart = NULL;
00038   nextAdd = NULL;
00039   currentSize = spaceLeft = 0;
00040   if (size > 0) {
00041 
00042     // Insitute a minimum size
00043     if (size < 1024) {
00044       size = 1024;
00045     }
00046 
00047     bufferStart = (char *)ats_malloc(size);
00048     nextAdd = bufferStart;
00049     currentSize = size;
00050     spaceLeft = size - 1;     // Leave room for a terminator;
00051     nextAdd[0] = '\0';
00052   }
00053 }
00054 
00055 textBuffer::~textBuffer()
00056 {
00057   ats_free(bufferStart);
00058 }
00059 
00060 // void textBuffer::reUse()
00061 //
00062 //   Sets the text buffer for reuse by repositioning the
00063 //     ptrs to beginning of buffer.  The buffer space is
00064 //     reused
00065 void
00066 textBuffer::reUse()
00067 {
00068   if (bufferStart != NULL) {
00069     nextAdd = bufferStart;
00070     spaceLeft = currentSize - 1;
00071     nextAdd[0] = '\0';
00072   }
00073 }
00074 
00075 // int textBuffer::copyFrom(void*,int num_bytes)
00076 //
00077 //
00078 //  Copy N bytes (determined by num_bytes) on to the
00079 //  end of the buffer.
00080 //
00081 //  Returns the number of bytes copies or
00082 //  -1 if there was insufficient memory
00083 int
00084 textBuffer::copyFrom(const void *source, int num_bytes)
00085 {
00086 
00087   // Get more space if necessary
00088   if (spaceLeft < num_bytes) {
00089     if (enlargeBuffer(num_bytes) == -1) {
00090       return -1;
00091     }
00092   }
00093 
00094   memcpy(nextAdd, source, num_bytes);
00095   spaceLeft -= num_bytes;
00096 
00097   nextAdd += num_bytes;
00098   nextAdd[0] = '\0';
00099 
00100   return num_bytes;
00101 }
00102 
00103 //  textBuffer::enlargeBuffer(int n)
00104 //
00105 //  Enlarge the buffer so at least at N
00106 //    bytes are free in the buffer.
00107 //
00108 //  Always enlarges by a power of two.
00109 //
00110 //  Returns -1 if insufficient memory,
00111 //    zero otherwise
00112 int
00113 textBuffer::enlargeBuffer(int N)
00114 {
00115   int addedSize = currentSize;
00116   int newSize = currentSize * 2;
00117   char *newSpace;
00118 
00119   if (spaceLeft < N) {
00120 
00121     while (addedSize < N) {
00122       addedSize += newSize;
00123       newSize *= 2;
00124     }
00125 
00126     newSpace = (char *)ats_realloc(bufferStart, newSize);
00127     if (newSpace != NULL) {
00128       nextAdd = newSpace + (unsigned int) (nextAdd - bufferStart);
00129       bufferStart = newSpace;
00130       spaceLeft += addedSize;
00131       currentSize = newSize;
00132     } else {
00133       // Out of Memory, Sigh
00134       return -1;
00135     }
00136   }
00137 
00138   return 0;
00139 }
00140 
00141 // int textBuffer::rawReadFromFile
00142 //
00143 // - Issues a single read command on the file descriptor or handle
00144 //   passed in and reads in raw data (not assumed to be text, no
00145 //   string terminators added).
00146 // - Cannot read from file descriptor on win32 because the win32
00147 //   read() function replaces CR-LF with LF if the file is not
00148 //   opened in binary mode.
00149 int
00150 textBuffer::rawReadFromFile(int fd)
00151 {
00152   int readSize;
00153 
00154   // Check to see if we have got a resonable amount of space left in our
00155   //   buffer, if not try to get somemore
00156   if (spaceLeft < 4096) {
00157     if (enlargeBuffer(4096) == -1) {
00158       return -1;
00159     }
00160   }
00161 
00162   readSize = read(fd, nextAdd, spaceLeft - 1);
00163 
00164   if (readSize == 0) {          //EOF
00165     return 0;
00166   } else if (readSize < 0) {
00167     // Error on read
00168     return readSize;
00169   } else {
00170     nextAdd = nextAdd + readSize;
00171     spaceLeft -= readSize;
00172     return readSize;
00173   }
00174 }
00175 
00176 // int textBuffer::readFromFD(int fd)
00177 //
00178 // Issues a single read command on the file
00179 // descritor passed in.  Attempts to read a minimum of
00180 // 512 bytes from file descriptor passed.
00181 int
00182 textBuffer::readFromFD(int fd)
00183 {
00184   int readSize;
00185 
00186   // Check to see if we have got a resonable amount of space left in our
00187   //   buffer, if not try to get somemore
00188   if (spaceLeft < 512) {
00189     if (enlargeBuffer(512) == -1) {
00190       return -1;
00191     }
00192   }
00193 
00194   readSize = read(fd, nextAdd, spaceLeft - 1);
00195 
00196   if (readSize == 0) {
00197     // Socket is empty so we are done
00198     return 0;
00199   } else if (readSize < 0) {
00200     // Error on read
00201     return readSize;
00202   } else {
00203     nextAdd = nextAdd + readSize;
00204     nextAdd[0] = '\0';
00205     spaceLeft -= readSize + 1;
00206     return readSize;
00207   }
00208 }
00209 
00210 char *
00211 textBuffer::bufPtr()
00212 {
00213   return bufferStart;
00214 }

Generated by  doxygen 1.7.1