codice:#include "StringBuffer.h" #include <stdlib.h> #include <string.h> #include <stdio.h> #include <malloc.h> StringBuffer::StringBuffer() throw() { this->capacity = 32; this->factor = 50; this->length = 0; this->original_capacity = 32; buffer = (char*)malloc(32); if (buffer==NULL)throw; buffer[0]='\0'; } StringBuffer::StringBuffer(int capacity, unsigned int factor) throw() { this->capacity = capacity; this->factor = factor; this->length = 0; this->original_capacity = capacity; buffer = (char*)malloc(capacity+1); if (buffer==NULL)throw; buffer[0]='\0'; } //these Append method must be protected against multithread collisions //if necessary void StringBuffer::Append(int n) throw() { char num[80]; itoa( n, num, 10); try{ Append(num); } catch(void) { throw; } } void StringBuffer::Append(int n, int radix) throw() { char num[80]; itoa( n, num, radix); try{ this->Append(num); } catch(void) { throw; } } void StringBuffer::Append(char c) throw() { try{ EnsureCapacity(length + 2); } catch(void) { throw; } buffer[length++]=c; buffer[length]='\0'; } void StringBuffer::Append(double n) throw() { char dbl[256]; sprintf(dbl, "%f", n); try{ Append(dbl); } catch(void) { throw; } } //Assume that c is terminated by zero void StringBuffer::Append(char* c) throw() { try{ EnsureCapacity(length + strlen(c) + 1);} catch(void){ throw;} strcat(buffer, c); length += strlen(c); } //return the actual capacity of the buffer int StringBuffer::Capacity() { return capacity; } //return the size of the actually used buffer (excluded the terminator) //that is equal to the length of the contained string int StringBuffer::Length() { return length; } //if n is greater than Capacity() then the capacity is increased by //a factor while internal capacity is not greater than n void StringBuffer::EnsureCapacity(unsigned int n) throw() { unsigned int oldcapacity = capacity; while (capacity < n) { capacity += capacity * factor / 100; if(capacity <= oldcapacity)capacity=oldcapacity+1; } if(oldcapacity != capacity) { char * newbuffer; newbuffer =(char*) realloc(buffer, capacity); if(newbuffer == NULL) throw; buffer = newbuffer; } } //frees the unused portion of the buffer void StringBuffer::TrimBuffer() { char * newbuffer; newbuffer = (char*)realloc(buffer, length + 1); buffer = newbuffer; } //return a pointer to the buffer. The buffer is returned as string //of chars terminated by zero char * StringBuffer::GetBuffer() { return buffer; } //Allocates new memory and copies the content of buffer into the new location //the original buffer remains unaltered. Note that the dimension //of the newly allocated memory is trimmed to the length (not capacity) of //the original buffer char * StringBuffer::CopyBuffer() throw() { char * p; p = (char*) malloc(length + 1); if (p==NULL) throw; strcpy(p, buffer); //buffer is certainly terminated by zero return p; } //creates a new buffer with 32 byte of capacity //mantaining original increment //the original buffer is cleanly destroyed void StringBuffer::ReInit() throw() { Free(); capacity = original_capacity; buffer =(char*) malloc(capacity); if (buffer==NULL)throw; } //the original buffer is destroyed and capacity is set to 0 void StringBuffer::Free() { if(buffer) free(buffer); length=0; capacity=0; } void StringBuffer::Empty() { length=0; buffer[0]='\0'; } int StringBuffer::GetCharAt (unsigned int pos) { if (pos > length) return -1; return buffer[pos]; }