From 6c36406a5dba3d390e2c98bd48c01bb1db86a985 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 11 Jul 2006 07:33:20 +0000 Subject: I added stringrep, which is a cpp replacement for some c string functions. They're all really simple, and maybe could even be optimized. I did this so that you could use the old c functions when you wanted to, but with cpp memory management. Also, some of these don't exist on other platforms, so it makes other programs more portable. The new functions are just like the c ones, only named string* instead of str*, for example stringdup instead of strdup. --- src/stringrep.cpp | 19 +++++++++++++++++++ src/stringrep.h | 17 +++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/stringrep.cpp create mode 100644 src/stringrep.h diff --git a/src/stringrep.cpp b/src/stringrep.cpp new file mode 100644 index 0000000..a505f8a --- /dev/null +++ b/src/stringrep.cpp @@ -0,0 +1,19 @@ +#include "stringrep.h" + +int32_t stringlen( const char *s ) +{ + for( int32_t i = 0;; i++ ) + if( !s[i] ) + return i; +} + +char *stringdup( const char *s ) +{ + int len = stringlen( s ); + char *r = new char[len+1]; + for( int j = 0; j <= len; j++ ) + r[j] = s[j]; + + return r; +} + diff --git a/src/stringrep.h b/src/stringrep.h new file mode 100644 index 0000000..eaa4a12 --- /dev/null +++ b/src/stringrep.h @@ -0,0 +1,17 @@ +#ifndef STRING_REP_H +#define STRING_REP_H + +#include + +/** + * Calculates the length of a string. + */ +int32_t stringlen( const char *s ); + +/** + * Identicle to strdup, which isn't available everywhere, but uses c++ memory + * facilities. Remember to use delete[] when freeing the returned string. + */ +char *stringdup( const char *s ); + +#endif -- cgit v1.2.3