Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 #include "libts.h"
00025 #include "TextBuffer.h"
00026 
00027 
00028 
00029 
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     
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;     
00051     nextAdd[0] = '\0';
00052   }
00053 }
00054 
00055 textBuffer::~textBuffer()
00056 {
00057   ats_free(bufferStart);
00058 }
00059 
00060 
00061 
00062 
00063 
00064 
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 
00076 
00077 
00078 
00079 
00080 
00081 
00082 
00083 int
00084 textBuffer::copyFrom(const void *source, int num_bytes)
00085 {
00086 
00087   
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 
00104 
00105 
00106 
00107 
00108 
00109 
00110 
00111 
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       
00134       return -1;
00135     }
00136   }
00137 
00138   return 0;
00139 }
00140 
00141 
00142 
00143 
00144 
00145 
00146 
00147 
00148 
00149 int
00150 textBuffer::rawReadFromFile(int fd)
00151 {
00152   int readSize;
00153 
00154   
00155   
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) {          
00165     return 0;
00166   } else if (readSize < 0) {
00167     
00168     return readSize;
00169   } else {
00170     nextAdd = nextAdd + readSize;
00171     spaceLeft -= readSize;
00172     return readSize;
00173   }
00174 }
00175 
00176 
00177 
00178 
00179 
00180 
00181 int
00182 textBuffer::readFromFD(int fd)
00183 {
00184   int readSize;
00185 
00186   
00187   
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     
00198     return 0;
00199   } else if (readSize < 0) {
00200     
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 }