From 13ab86ee48bea707ffb0041659f65dac2351c6d5 Mon Sep 17 00:00:00 2001 From: cherierm Date: Fri, 11 Jul 2008 15:14:45 +0000 Subject: [PATCH] A string utility class and a new Exception (used to throw out of band exception) git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@5866 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- src/cxx/OutOfBoundsException.cxx | 81 +++ src/cxx/OutOfBoundsException.hpp | 71 +++ src/cxx/StringHelper.cxx | 824 +++++++++++++++++++++++++++++++ src/cxx/StringHelper.hpp | 245 +++++++++ 4 files changed, 1221 insertions(+) create mode 100644 src/cxx/OutOfBoundsException.cxx create mode 100644 src/cxx/OutOfBoundsException.hpp create mode 100644 src/cxx/StringHelper.cxx create mode 100644 src/cxx/StringHelper.hpp diff --git a/src/cxx/OutOfBoundsException.cxx b/src/cxx/OutOfBoundsException.cxx new file mode 100644 index 0000000000..3fb9412d82 --- /dev/null +++ b/src/cxx/OutOfBoundsException.cxx @@ -0,0 +1,81 @@ +/* + * OutOfBoundsException.cxx + * + * Copyright 2006,2007 Martin Quinson, Malek Cherier + * All right reserved. + * + * This program is free software; you can redistribute + * it and/or modify it under the terms of the license + *(GNU LGPL) which comes with this package. + * + */ + + /* OutOfBoundsException member functions implementation. + */ + +#include + +#include +#include +#include + + +namespace SimGrid +{ + namespace Msg + { + + OutOfBoundsException::OutOfBoundsException() + { + this->reason = (char*) calloc(strlen("Out of bounds") + 1, sizeof(char)); + strcpy(this->reason, "Out of bounds"); + } + + + OutOfBoundsException::OutOfBoundsException(const OutOfBoundsException& rOutOfBoundsException) + { + const char* reason = rOutOfBoundsException.toString(); + this->reason = (char*) calloc(strlen(reason) + 1, sizeof(char)); + strcpy(this->reason, reason); + + } + + OutOfBoundsException::OutOfBoundsException(int pos) + { + this->reason = (char*) calloc(strlen("Out of bounds ") + 21 + 1, sizeof(char)); + sprintf(this->reason, "Out of bounds %d", pos); + } + + OutOfBoundsException::OutOfBoundsException(int pos1, int pos2) + { + this->reason = (char*) calloc(strlen("Out of bounds ") + (2*21) + 1, sizeof(char)); + sprintf(this->reason, "Out of bounds %d : %d", pos1, pos2); + } + + + OutOfBoundsException::~OutOfBoundsException() + { + if(this->reason) + free(this->reason); + } + + const char* OutOfBoundsException::toString(void) const + { + return (const char*)(this->reason); + } + + + const OutOfBoundsException& OutOfBoundsException::operator = (const OutOfBoundsException& rOutOfBoundsException) + { + const char* reason = rOutOfBoundsException.toString(); + this->reason = (char*) calloc(strlen(reason) + 1, sizeof(char)); + + return *this; + } + + } // namespace Msg + +}// namespace SimGrid + + + diff --git a/src/cxx/OutOfBoundsException.hpp b/src/cxx/OutOfBoundsException.hpp new file mode 100644 index 0000000000..379c76856a --- /dev/null +++ b/src/cxx/OutOfBoundsException.hpp @@ -0,0 +1,71 @@ +/* + * OutOfBoundsException.hpp + * + * Copyright 2006,2007 Martin Quinson, Malek Cherier + * All right reserved. + * + * This program is free software; you can redistribute + * it and/or modify it under the terms of the license + *(GNU LGPL) which comes with this package. + * + */ + +#ifndef MSG_OUTOFBOUNDSEXCEPTION_HPP +#define MSG_OUTOFBOUNDSEXCEPTION_HPP + +#ifndef __cplusplus + #error OutOfBoundsException.hpp requires C++ compilation (use a .cxx suffix) +#endif + +#include + +namespace SimGrid +{ + namespace Msg + { + + class SIMGRIDX_EXPORT OutOfBoundsException : public Exception + { + public: + + // Default constructor. + OutOfBoundsException(); + + // Copy constructor. + OutOfBoundsException(const OutOfBoundsException& rOutOfBoundsException); + + // This constructor takes the position in the range. + OutOfBoundsException(int pos); + + OutOfBoundsException(int pos1, int pos2); + + // Destructor. + virtual ~OutOfBoundsException(); + + // Operations. + + // Returns the reason of the exception : + // the message "Out of bounds : `pos'" + const char* toString(void) const; + + // Operators. + + // Assignement. + const OutOfBoundsException& operator = (const OutOfBoundsException& rOutOfBoundsException); + + private : + + // Attributes. + + // A buffer used to build the message returned by the methode toString(). + char* reason; + }; + + + } // namespace Msg + +}// namespace SimGrid + + +#endif // !MSG_MSGEXCEPTION_HPP + diff --git a/src/cxx/StringHelper.cxx b/src/cxx/StringHelper.cxx new file mode 100644 index 0000000000..c64c6c1ce9 --- /dev/null +++ b/src/cxx/StringHelper.cxx @@ -0,0 +1,824 @@ +#include + +#include +#include +#include +#include + +#ifndef BUFF_MAX +#define BUFF_MAX ((size_t)260) +#endif // BUFF_MAX + + +// namespace SimGrid::Msg +namespace SimGrid +{ + namespace Msg + { + + #define DEFAULT_STRING_HELPER_CAPACITY ((int)128) + + + void StringHelper::init(void) + { + capacity = DEFAULT_STRING_HELPER_CAPACITY; + + if(!(content = (char*) calloc(capacity + 1, sizeof(char)))) + throw BadAllocException(); + + len = 0; + } + + // Default constructor + StringHelper::StringHelper() + { + init(); + } + + StringHelper::StringHelper(char c) + { + init(); + append(c); + + } + + StringHelper::StringHelper(char c, int n) + { + init(); + append(c, n); + } + + StringHelper::StringHelper(const char* cstr) + { + init(); + append(cstr); + } + + StringHelper::StringHelper(const char* cstr, int n) + { + init(); + append(cstr, n); + } + + StringHelper::StringHelper(const char* cstr, int pos, int n) + { + init(); + append(cstr, pos, n); + } + + StringHelper::StringHelper(const string& rstr) + { + init(); + append(rstr); + } + + StringHelper::StringHelper(const string& rstr, int n) + { + init(); + append(rstr, n); + } + + StringHelper::StringHelper(const string& rstr, int pos, int n) + { + init(); + append(rstr, pos, n); + } + + StringHelper::StringHelper(short si) + { + init(); + append(si); + } + + StringHelper::StringHelper(int i) + { + init(); + append(i); + } + + StringHelper::StringHelper(long l) + { + init(); + append(l); + } + + StringHelper::StringHelper(float f) + { + init(); + append(f); + } + + StringHelper::StringHelper(double d) + { + init(); + append(d); + } + + StringHelper::StringHelper(double d, const char* format) + { + char toAppend[BUFF_MAX + 1] = {0}; + + sprintf(toAppend,format,d); + + init(); + + append(toAppend); + } + + StringHelper::StringHelper(unsigned short usi) + { + init(); + append(usi); + } + + StringHelper::StringHelper(unsigned int ui) + { + init(); + append(ui); + } + + StringHelper::StringHelper(unsigned long ul) + { + init(); + append(ul); + } + + // Copy constructor + StringHelper::StringHelper(const StringHelper& rStringHelper) + { + if(this != &rStringHelper && rStringHelper.size()) + { + clear(); + append(rStringHelper.cstr()); + } + } + + // Destructor + StringHelper::~StringHelper() + { + if(content) + free(content); + } + + // Operations + + void StringHelper::clear(void) + { + if(len) + memset(content, 0, len); + + len = 0; + } + + bool StringHelper::empty(void) + { + return len == 0; + } + + StringHelper& StringHelper::append(unsigned char c) + { + if(capacity < len + 1) + { + int new_capacity = (capacity << 1) ; + + if(!(content = (char*) realloc(content, new_capacity))) + throw BadAllocException(); + + capacity = new_capacity; + } + + + content[len] = c; + len++; + + content[len] = '\0'; + + return *this; + } + + StringHelper& StringHelper::append(unsigned char c, int n) + { + if(n <=0) + throw InvalidArgumentException("n"); + + char* toAppend = (char*) calloc(n + 1, sizeof(char)); + + if(!toAppend) + throw BadAllocException(); + + memset(toAppend, c, n); + + append(toAppend); + + free(toAppend); + + return *this; + } + + + StringHelper& StringHelper::append(char c) + { + if(capacity < len + 1) + { + int new_capacity = (capacity << 1) ; + + if(!(content = (char*) realloc(content, new_capacity))) + throw BadAllocException(); + + capacity = new_capacity; + } + + + content[len] = c; + len++; + + content[len] = '\0'; + + return *this; + } + + StringHelper& StringHelper::append(char c, int n) + { + if(n <=0) + throw InvalidArgumentException("n"); + + char* toAppend = (char*) calloc(n + 1, sizeof(char)); + + if(!toAppend) + throw BadAllocException(); + + memset(toAppend, c, n); + + append(toAppend); + + free(toAppend); + + return *this; + } + + StringHelper& StringHelper::append(const char* cstr) + { + if(!cstr) + throw NullPointerException("cstr"); + + int l = (int) strlen(cstr); + + if(capacity < len + l) + { + int new_capacity = (capacity << 1) < (len + l) ? (len + l) << 1 : capacity << 1; + + if(!(content = (char*) realloc(content, new_capacity))) + throw BadAllocException(); + + strcat(content, cstr); + + capacity = new_capacity; + } + else + { + strcat(content, cstr); + } + + len += l; + content[len] = '\0'; + + return *this; + + } + + StringHelper& StringHelper::append(const char* cstr, int n) + { + if(!cstr) + throw NullPointerException("cstr"); + + if(n <= 0) + throw InvalidArgumentException("n"); + + + int l = ((int) strlen(cstr)) * n; + + if(capacity < len + l) + { + int new_capacity = (capacity << 1) < (len + l) ? (len + l) << 1 : capacity << 1; + + if(!(content = (char*) realloc(content, new_capacity))) + throw BadAllocException(); + + for(int i = 0; i < n; i++) + strcat(content, cstr); + + capacity = new_capacity; + } + else + { + for(int i = 0; i < n; i++) + strcat(content, cstr); + } + + len += l; + content[len] = '\0'; + + return *this; + + } + + StringHelper& StringHelper::append(const char* cstr, int pos, int n) + { + if(!cstr) + throw NullPointerException("cstr"); + + if(n <= 0) + throw InvalidArgumentException("n"); + + if(pos < 0 || pos >= (int)strlen(cstr) ) + throw OutOfBoundsException(pos); + + if(pos + n >= (int)strlen(cstr)) + throw OutOfBoundsException(pos, n); + + + char* toAppend = (char*) calloc(n + 1, sizeof(char)); + + strncpy(toAppend, cstr + pos, n); + + append(toAppend); + + free(toAppend); + + return *this; + } + + StringHelper& StringHelper::append(const string& rstr) + { + append(rstr.c_str()); + return *this; + } + + StringHelper& StringHelper::append(const string& rstr, int n) + { + if(rstr.empty()) + throw NullPointerException("rstr"); + + if(n <= 0) + throw InvalidArgumentException("n"); + + + int l = ((int) rstr.size()) * n; + + if(capacity < len + l) + { + int new_capacity = (capacity << 1) < (len + l) ? (len + l) << 1 : capacity << 1; + + if(!(content = (char*) realloc(content, new_capacity))) + throw BadAllocException(); + + for(int i = 0; i < n; i++) + strcat(content, rstr.c_str()); + + capacity = new_capacity; + } + else + { + for(int i = 0; i < n; i++) + strcat(content, rstr.c_str()); + } + + len += l; + content[len] = '\0'; + + return *this; + } + + StringHelper& StringHelper::append(const string& rstr, int pos, int n) + { + if(rstr.empty()) + throw InvalidArgumentException("rstr"); + + if(n <= 0) + throw InvalidArgumentException("n"); + + if(pos < 0 || pos >= (int) rstr.size() ) + throw OutOfBoundsException(pos); + + if(pos + n >= (int) rstr.size()) + throw OutOfBoundsException(pos, n); + + + char* toAppend = (char*) calloc(n + 1, sizeof(char)); + + strncpy(toAppend, rstr.c_str() + pos, n); + + append(toAppend); + + free(toAppend); + + return *this; + } + + StringHelper& StringHelper::append(short si) + { + char toAppend[26] = {0}; + + sprintf(toAppend, "%hd",si); + + append(toAppend); + + return *this; + } + + StringHelper& StringHelper::append(int i) + { + char toAppend[26] = {0}; + + sprintf(toAppend, "%d",i); + + append(toAppend); + + return *this; + } + + StringHelper& StringHelper::append(long l) + { + char toAppend[26] = {0}; + + sprintf(toAppend, "%ld",l); + + append(toAppend); + + return *this; + } + + StringHelper& StringHelper::append(float f) + { + char toAppend[26] = {0}; + + sprintf(toAppend, "%f",f); + + append(toAppend); + + return *this; + } + + StringHelper& StringHelper::append(double d) + { + char toAppend[26] = {0}; + + sprintf(toAppend, "%lf",d); + + append(toAppend); + + return *this; + } + + StringHelper& StringHelper::append(double d, const char* format) + { + char toAppend[BUFF_MAX + 1] = {0}; + + sprintf(toAppend, format, d); + + append(toAppend); + + return *this; + } + + StringHelper& StringHelper::append(unsigned short usi) + { + char toAppend[26] = {0}; + + sprintf(toAppend, "%hu",usi); + + append(toAppend); + + return *this; + } + + StringHelper& StringHelper::append(unsigned int ui) + { + char toAppend[26] = {0}; + + sprintf(toAppend, "%u",ui); + + append(toAppend); + + return *this; + } + + StringHelper& StringHelper::append(unsigned long ul) + { + char toAppend[26] = {0}; + + sprintf(toAppend, "%lu",ul); + + append(toAppend); + + return *this; + } + + const char& StringHelper::at(int pos) const + { + if(pos < 0 || pos >= len) + throw OutOfBoundsException(pos); + + return content[pos]; + } + + char& StringHelper::at(int pos) + { + if(pos < 0 || pos >= len) + throw OutOfBoundsException(pos); + + return content[pos]; + } + + const char* StringHelper::cstr(void) const + { + return (const char*)content; + } + + string& StringHelper::toString(void) + { + string* s = new string(); + s->append(content); + return *s; + } + + int StringHelper::size(void) const + { + return len; + } + + // Operators + + // Assignement + StringHelper& StringHelper::operator = (const StringHelper& rStringHelper) + { + + if(this !=&rStringHelper && rStringHelper.size()) + { + clear(); + append(rStringHelper.cstr()); + } + + return *this; + } + + StringHelper& StringHelper::operator = (const char* cstr) + { + clear(); + append(cstr); + return *this; + } + + StringHelper& StringHelper::operator = (const string& str) + { + clear(); + append(str); + return *this; + } + + StringHelper& StringHelper::operator = (short n) + { + clear(); + append(n); + return *this; + } + + StringHelper& StringHelper::operator = (int n) + { + clear(); + append(n); + return *this; + } + + StringHelper& StringHelper::operator = (long n) + { + clear(); + append(n); + return *this; + } + + StringHelper& StringHelper::operator = (float n) + { + clear(); + append(n); + return *this; + } + + StringHelper& StringHelper::operator = (double n) + { + clear(); + append(n); + return *this; + } + + StringHelper& StringHelper::operator = (unsigned short n) + { + clear(); + append(n); + return *this; + } + + StringHelper& StringHelper::operator = (unsigned int n) + { + clear(); + append(n); + return *this; + } + + StringHelper& StringHelper::operator = (unsigned long n) + { + clear(); + append(n); + return *this; + } + + char& StringHelper::operator[](int pos) + { + if(pos < 0 || pos >= len) + throw OutOfBoundsException(pos); + + return content[pos]; + } + + char StringHelper::operator[](int pos) const + { + if(pos < 0 || pos >= len) + throw OutOfBoundsException(pos); + + return content[pos]; + } + + StringHelper& StringHelper::operator += (short n) + { + append(n); + return *this; + } + + StringHelper& StringHelper::operator += (int n) + { + append(n); + return *this; + } + + StringHelper& StringHelper::operator += (long n) + { + append(n); + return *this; + } + + StringHelper& StringHelper::operator += (float n) + { + append(n); + return *this; + } + + StringHelper& StringHelper::operator += (double n) + { + append(n); + return *this; + } + + StringHelper& StringHelper::operator += (unsigned short n) + { + append(n); + return *this; + } + + StringHelper& StringHelper::operator += (unsigned int n) + { + append(n); + return *this; + } + + StringHelper& StringHelper::operator += (unsigned long n) + { + append(n); + return *this; + } + + StringHelper& StringHelper::operator += (const StringHelper& rStringHelper) + { + append(rStringHelper.content); + return *this; + } + + StringHelper& StringHelper::operator += (const string& rstr) + { + append(rstr.c_str()); + return *this; + } + + StringHelper& StringHelper::operator += (const char* cstr) + { + append(cstr); + return *this; + } + + StringHelper& StringHelper::operator += (char c) + { + append(c); + return *this; + } + + StringHelper& StringHelper::operator + (short n) + { + append(n); + return *this; + } + + StringHelper& StringHelper::operator + (int n) + { + append(n); + return *this; + } + + StringHelper& StringHelper::operator + (long n) + { + append(n); + return *this; + } + + StringHelper& StringHelper::operator + (float n) + { + append(n); + return *this; + } + + StringHelper& StringHelper::operator + (double n) + { + append(n); + return *this; + } + + StringHelper& StringHelper::operator + (unsigned short n) + { + append(n); + return *this; + } + + StringHelper& StringHelper::operator + (unsigned int n) + { + append(n); + return *this; + } + + StringHelper& StringHelper::operator + (unsigned long n) + { + append(n); + return *this; + } + + + StringHelper& StringHelper::operator + (const StringHelper& rStringHelper) + { + append(rStringHelper.content); + return *this; + } + + StringHelper& StringHelper::operator + (const string& rstr) + { + append(rstr.c_str()); + return *this; + } + + StringHelper& StringHelper::operator + (const char* cstr) + { + append(cstr); + return *this; + } + + StringHelper& StringHelper::operator + (char c) + { + append(c); + return *this; + } + + StringHelper::operator char *() + { + return content; + } + + StringHelper::operator const char *() + { + return content; + } + + ostream& operator<<(ostream& stream, const StringHelper& s) + { + stream << s.cstr(); + return stream; + } + + istream& operator<<(istream& stream, StringHelper& s) + { + char buff[256] = {0}; + + stream >> buff; + + s.append(buff); + + return stream; + } + + } // namespace Msg +} // namespace SimGrid \ No newline at end of file diff --git a/src/cxx/StringHelper.hpp b/src/cxx/StringHelper.hpp new file mode 100644 index 0000000000..a1675d327c --- /dev/null +++ b/src/cxx/StringHelper.hpp @@ -0,0 +1,245 @@ +/* + * StringHelper.hpp + * + * Copyright 2006,2007 Martin Quinson, Malek Cherier + * All right reserved. + * + * This program is free software; you can redistribute + * it and/or modify it under the terms of the license + *(GNU LGPL) which comes with this package. + * + */ + +#ifndef STRING_HELPER_HPP +#define STRING_HELPER_HPP + + #ifndef __cplusplus + #error StringHelper.hpp requires C++ compilation (use a .cxx suffix) +#endif + +#include +using std::string; + +#include +using std::ostream; +using std::istream; + +#include + +// namespace SimGrid::Msg +namespace SimGrid +{ + namespace Msg + { + class SIMGRIDX_EXPORT StringHelper + { + public: + + // Default constructor + StringHelper(); + + StringHelper(unsigned char c); + + StringHelper(unsigned char c, int n); + + StringHelper(char c); + + StringHelper(char c, int n); + + StringHelper(const char* cstr); + + StringHelper(const char* cstr, int n); + + StringHelper(const char* cstr, int pos, int n); + + StringHelper(const string& rstr); + + StringHelper(const string& rstr, int n); + + StringHelper(const string& rstr, int pos, int n); + + StringHelper(short si); + + StringHelper(int i); + + StringHelper(long l); + + StringHelper(float f); + + StringHelper(double d); + + StringHelper(double d, const char* format); + + StringHelper(unsigned short usi); + + StringHelper(unsigned int ui); + + StringHelper(unsigned long ul); + + // Copy constructor + StringHelper(const StringHelper& rStringHelper); + + // Destructor + ~StringHelper(); + + // Operations + + StringHelper& append(unsigned char c); + + StringHelper& append(unsigned char c, int n); + + StringHelper& append(char c); + + StringHelper& append(char c, int n); + + StringHelper& append(const char* cstr); + + StringHelper& append(const char* cstr, int n); + + StringHelper& append(const char* cstr, int pos, int n); + + StringHelper& append(const string& rstr); + + StringHelper& append(const string& rstr, int n); + + StringHelper& append(const string& rstr, int pos, int n); + + StringHelper& append(short si); + + StringHelper& append(int i); + + StringHelper& append(long l); + + StringHelper& append(float f); + + StringHelper& append(double d); + + StringHelper& append(double d, const char* format); + + StringHelper& append(unsigned short usi); + + StringHelper& append(unsigned int ui); + + StringHelper& append(unsigned long ul); + + const char& at(int pos) const; + + char& at(int pos); + + const char* cstr(void) const; + + string& toString(void); + + int size(void) const; + + void clear(void); + + bool empty(void); + + // Operators + + // Assignement + StringHelper& operator = (const StringHelper& rStringHelper); + + StringHelper& operator = (const char* cstr); + + StringHelper& operator = (const string& str); + + StringHelper& operator = (short n); + + StringHelper& operator = (int n); + + StringHelper& operator = (long n); + + StringHelper& operator = (float n); + + StringHelper& operator = (double n); + + StringHelper& operator = (unsigned short n); + + StringHelper& operator = (unsigned int n); + + StringHelper& operator = (unsigned long n); + + char& operator[](int pos); + + char operator[](int pos) const; + + StringHelper& operator += (short n); + + StringHelper& operator += (int n); + + StringHelper& operator += (long n); + + StringHelper& operator += (float n); + + StringHelper& operator += (double n); + + StringHelper& operator += (unsigned short n); + + StringHelper& operator += (unsigned int n); + + StringHelper& operator += (unsigned long n); + + StringHelper& operator += (const StringHelper& rStringHelper); + + StringHelper& operator += (const string& rstr); + + StringHelper& operator += (const char* cstr); + + StringHelper& operator += (char c); + + StringHelper& operator + (short n); + + StringHelper& operator + (int n); + + StringHelper& operator + (long n); + + StringHelper& operator + (float n); + + StringHelper& operator + (double n); + + StringHelper& operator + (unsigned short n); + + StringHelper& operator + (unsigned int n); + + StringHelper& operator + (unsigned long n); + + StringHelper& operator + (const StringHelper& rStringHelper); + + StringHelper& operator + (const string& rstr); + + StringHelper& operator + (const char* cstr); + + StringHelper& operator + (char c); + + operator char *(); + + operator const char *(); + + friend SIMGRIDX_EXPORT ostream& operator<<(ostream& stream, const StringHelper& s); + + friend SIMGRIDX_EXPORT istream& operator<<(istream& stream, StringHelper& s); + + private: + + // Methods + void init(void); + + // Attributes + + char* content; + int capacity; + int len; + }; + + typedef class StringHelper* StringHelperPtr; + + #define TEXT_(___x) StringHelper((___x)) + + + } // namespace Msg +} // namespace SimGrid + + +#endif // !STRING_HELPER_HPP \ No newline at end of file -- 2.20.1