[Winpt-commits] r281 - in trunk: . Generic
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Mon Jan 15 23:04:00 CET 2007
Author: twoaday
Date: 2007-01-15 23:04:00 +0100 (Mon, 15 Jan 2007)
New Revision: 281
Added:
trunk/Generic/
trunk/Generic/StringBuffer.cpp
trunk/Generic/StringBuffer.h
Log:
Added: trunk/Generic/StringBuffer.cpp
===================================================================
--- trunk/Generic/StringBuffer.cpp 2007-01-15 22:03:31 UTC (rev 280)
+++ trunk/Generic/StringBuffer.cpp 2007-01-15 22:04:00 UTC (rev 281)
@@ -0,0 +1,214 @@
+/* StringBuffer.cpp - A simple string buffer implementation.
+ Copyright (C) 2006, 2007 Timo Schulz
+ Released under the GNU General Public License, Version 2. */
+
+#include <string.h>
+#include <stdio.h>
+
+#include "StringBuffer.h"
+
+/* Add the converted data at the end of the internal buffer.
+ Allocate more memory if needed, plus some extra bytes. */
+void StringBuffer::addCharacters (const char *buffer, int bufsize)
+{
+ if (size+bufsize > alloced_size) {
+ char *save = private_contents;
+
+ alloced_size += bufsize+DEFSIZE+1;
+ private_contents = new char[alloced_size+1];
+ memset (private_contents, 0, alloced_size+1);
+ if (save != NULL) {
+ strcpy (private_contents, save);
+ delete []save;
+ }
+ }
+ strcat (private_contents, buffer);
+ size += bufsize;
+}
+
+
+/* Create an empty buffer object. */
+StringBuffer::StringBuffer ()
+{
+ alloced_size = 0;
+ size = 0;
+ private_contents = NULL;
+}
+
+
+/* Create an empty buffer object with an inital size. */
+StringBuffer::StringBuffer (int bufsize)
+{
+ alloced_size = bufsize+DEFSIZE+1;
+ private_contents = new char[alloced_size+1];
+ memset (private_contents, 0, alloced_size+1);
+ size = 0;
+}
+
+
+/* Create a buffer object with the given contents. */
+StringBuffer::StringBuffer (const char *contents)
+{
+ alloced_size = strlen (contents)+1+DEFSIZE;
+ private_contents = new char[alloced_size+1];
+ memset (private_contents, 0, alloced_size+1);
+ strcpy (private_contents, contents);
+ size = strlen (contents);
+}
+
+
+/* Destructor which fres the used memory. */
+StringBuffer::~StringBuffer ()
+{
+ if (private_contents != NULL)
+ delete []private_contents;
+ private_contents = NULL;
+ size = 0;
+ alloced_size = 0;
+}
+
+
+/* Return a copy of the buffer which must be freed after use. */
+char* StringBuffer::getBufferCopy (void)
+{
+ char *p = new char[strlen (private_contents)+1];
+ strcpy (p, private_contents);
+ return p;
+}
+
+
+/* Return a constant pointer to the 'char*' data of the buffer. */
+const char *StringBuffer::getBuffer (void)
+{
+ return private_contents;
+}
+
+
+/* Return the size of the underlying string. */
+int StringBuffer::getSize (void)
+{
+ return size;
+}
+
+/* Reset the buffer contents. */
+void StringBuffer::reset (void)
+{
+ memset (private_contents, 0, alloced_size);
+ size = 0;
+}
+
+
+/* Add an integer to the buffer. */
+void StringBuffer::add (int val)
+{
+ char tmp[32];
+
+ _snprintf (tmp, 31, "%d", val);
+ addCharacters (tmp, strlen (tmp));
+}
+
+
+/* Add a single character to the buffer. */
+void StringBuffer::add (char val)
+{
+ char tmp[4];
+
+ _snprintf (tmp, 3, "%c", val);
+ addCharacters (tmp, strlen (tmp));
+}
+
+
+/* Add a long-integer to the buffer. */
+void StringBuffer::add (long val)
+{
+ char tmp[32];
+
+ _snprintf (tmp, 31, "%d", val);
+ addCharacters (tmp, strlen (tmp));
+}
+
+
+/* Add a string to the buffer. */
+void StringBuffer::add (const char *val)
+{
+ addCharacters (val, strlen (val));
+}
+
+
+/* Add an unsigned integer to the buffer. */
+void StringBuffer::add (unsigned long val)
+{
+ char tmp[32];
+
+ _snprintf (tmp, 31, "%lu", val);
+ addCharacters (tmp, strlen (tmp));
+}
+
+
+/* Add an unsigned integer in hex format to the buffer. */
+void StringBuffer::addHex (unsigned long val)
+{
+ char tmp[32];
+
+ _snprintf (tmp, 31, "%08lx", val);
+ addCharacters (tmp, strlen (tmp));
+}
+
+
+/* Overloaded operators to provide an easier interface
+ for manipulating the underlying buffer. */
+StringBuffer& StringBuffer::operator +(int val)
+{
+ add (val);
+ return *this;
+}
+
+
+StringBuffer& StringBuffer::operator +(unsigned long val)
+{
+ add (val);
+ return *this;
+}
+
+
+StringBuffer& StringBuffer::operator +(const char *val)
+{
+ addCharacters (val, strlen (val));
+ return *this;
+}
+
+
+StringBuffer& StringBuffer::operator +=(int val)
+{
+ add (val);
+ return *this;
+}
+
+
+StringBuffer& StringBuffer::operator +=(unsigned long val)
+{
+ add (val);
+ return *this;
+}
+
+
+StringBuffer& StringBuffer::operator +=(const char *val)
+{
+ addCharacters (val, strlen (val));
+ return *this;
+}
+
+
+StringBuffer& StringBuffer::operator +=(char val)
+{
+ add (val);
+ return *this;
+}
+
+
+StringBuffer& StringBuffer::operator=(const char *val)
+{
+ reset ();
+ addCharacters (val, strlen (val));
+ return *this;
+}
Added: trunk/Generic/StringBuffer.h
===================================================================
--- trunk/Generic/StringBuffer.h 2007-01-15 22:03:31 UTC (rev 280)
+++ trunk/Generic/StringBuffer.h 2007-01-15 22:04:00 UTC (rev 281)
@@ -0,0 +1,87 @@
+#ifndef __STRING_BUFFER_H
+#define __STRING_BUFFER_H
+
+/* Copyright (C) 2006, 2007 Timo Schulz
+ Released under the GNU General Public License, Version 2. */
+
+#define DEFSIZE 128
+
+/* A simple string buffer implementation which allows to manipulate
+ the underlying buffer in a unified and easy way. The code is written
+ for clarity and simplicty, _not_ speed. */
+class StringBuffer
+{
+private:
+ char *private_contents;
+ int alloced_size;
+ int size;
+
+private:
+ /* Add the converted data at the end of the internal buffer.
+ Allocate more memory if needed, plus some extra bytes. */
+ void addCharacters (const char *buffer, int bufsize);
+
+public:
+ StringBuffer ();
+
+ /* Create an empty buffer object with an inital size. */
+ StringBuffer (int bufsize);
+
+
+ /* Create a buffer object with the given contents. */
+ StringBuffer (const char *contents);
+
+ /* Destructor which fres the used memory. */
+ ~StringBuffer ();
+
+ /* Return a constant pointer to the 'char*' data of the buffer. */
+ const char *getBuffer (void);
+
+ /* Return a copy of the buffer which must be freed after use. */
+ char* getBufferCopy (void);
+
+ /* Return the size of the underlying string. */
+ int getSize (void);
+
+ /* Reset the buffer contents. */
+ void reset (void);
+
+ /* Add an integer to the buffer. */
+ void add (int val);
+
+ /* Add a single character to the buffer. */
+ void add (char val);
+
+ /* Add a long-integer to the buffer. */
+ void add (long val);
+
+ /* Add a string to the buffer. */
+ void add (const char *val);
+
+ /* Add an unsigned integer to the buffer. */
+ void add (unsigned long val);
+
+ /* Add an unsigned integer in hex format to the buffer. */
+ void addHex (unsigned long val);
+
+ /* Overloaded operators to provide an easier interface
+ for manipulating the underlying buffer. */
+ StringBuffer& operator +(int val);
+
+ StringBuffer& operator +(unsigned long val);
+
+ StringBuffer& operator +(const char *val);
+
+ StringBuffer& operator +=(int val);
+
+ StringBuffer& operator +=(unsigned long val);
+
+ StringBuffer& operator +=(const char *val);
+
+ StringBuffer& operator +=(char val);
+
+ StringBuffer& operator=(const char *val);
+};
+
+
+#endif /*__STRING_BUFFER_H*/
More information about the Winpt-commits
mailing list