commit 5b729bc0bfee1ea955b77d341b26a55b57517c4a parent 897eb49077bb65546102409c7c38f371b5e55793 Author: Christos Margiolis <christos@margiolis.net> Date: Fri, 1 May 2020 03:52:25 +0300 improved xstring Diffstat:
20 files changed, 240 insertions(+), 29 deletions(-)
diff --git a/assignment-2.4-inheritance/bin/inheritance b/assignment-2.4-inheritance/bin/inheritance Binary files differ. diff --git a/assignment-2.4-inheritance/obj/app.o b/assignment-2.4-inheritance/obj/app.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/game.o b/assignment-2.4-inheritance/obj/game.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/main.o b/assignment-2.4-inheritance/obj/main.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/manufacturer.o b/assignment-2.4-inheritance/obj/manufacturer.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/office.o b/assignment-2.4-inheritance/obj/office.o Binary files differ. diff --git a/assignment-2.4-inheritance/obj/review.o b/assignment-2.4-inheritance/obj/review.o Binary files differ. diff --git a/assignment-2.4-inheritance/res/output.csv b/assignment-2.4-inheritance/res/output.csv diff --git a/assignment-2.4-inheritance/src/app.cpp b/assignment-2.4-inheritance/src/app.cpp diff --git a/assignment-2.4-inheritance/src/app.h b/assignment-2.4-inheritance/src/app.h diff --git a/assignment-2.4-inheritance/src/main.cpp b/assignment-2.4-inheritance/src/main.cpp @@ -1,4 +1,3 @@ -#include <iostream> #include "appsystem.h" std::ostream& operator<< (std::ostream& stream, const AppSystem& sys) diff --git a/assignment-2.5-spreadsheets/bin/spreadsheets b/assignment-2.5-spreadsheets/bin/spreadsheets Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/main.o b/assignment-2.5-spreadsheets/obj/main.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/obj/xstring.o b/assignment-2.5-spreadsheets/obj/xstring.o Binary files differ. diff --git a/assignment-2.5-spreadsheets/res/errlog.csv b/assignment-2.5-spreadsheets/res/errlog.csv diff --git a/assignment-2.5-spreadsheets/res/matchings.csv b/assignment-2.5-spreadsheets/res/matchings.csv diff --git a/assignment-2.5-spreadsheets/res/report.csv b/assignment-2.5-spreadsheets/res/report.csv diff --git a/assignment-2.5-spreadsheets/src/main.cpp b/assignment-2.5-spreadsheets/src/main.cpp @@ -1,8 +1,7 @@ -#include <vector> #include "xstring.h" int main(int argc, char **argv) { - + return 0; } diff --git a/assignment-2.5-spreadsheets/src/xstring.cpp b/assignment-2.5-spreadsheets/src/xstring.cpp @@ -1,16 +1,20 @@ #include "xstring.h" +template<> const char *getformat<int>() {return "%d";} +template<> const char *getformat<float>() {return "%f";} +template<> const char *getformat<double>() {return "%f";} + xstring::xstring() - :str(nullptr), len(0) {} + :str(new char[1]), len(0) {} -xstring::xstring(const char *str) - :str(transfer(str)), len(strlen(str)) {} +xstring::xstring(const char *s) + :str(conv(s)), len(strlen(s)) {} xstring::xstring(const xstring& s) { - if (s.str != nullptr) + if (!this->empty()) { - str = transfer(s.str); + str = conv(s.str); len = strlen(str); } else @@ -22,23 +26,20 @@ xstring::xstring(const xstring& s) xstring::~xstring() { - delete[] str; -} - -void xstring::operator+= (const char *ns) -{ - if (str != nullptr) strcat(str, ns); - else str = transfer(ns); - len = strlen(str); + if (!this->empty()) + { + len = 0; + delete[] str; + } } xstring xstring::operator= (const xstring& s) { if (this == &s) return *this; - if (s.str != nullptr) + if (!s.empty()) { - if (str != nullptr) delete[] str; - str = transfer(s.str); + if (!this->empty()) delete[] str; + str = conv(s.str); len = strlen(str); } else @@ -49,27 +50,200 @@ xstring xstring::operator= (const xstring& s) return *this; } +xstring xstring::operator= (const char *s) +{ + if (!this->empty()) delete[] str; + str = conv(s); + len = strlen(str); + return *this; +} + +xstring xstring::operator+ (const xstring& s) +{ + return xstring(strcat(this->str, s.str)); +} + +xstring xstring::operator+ (const char *s) +{ + return xstring(strcat(this->str, s)); +} + +xstring& xstring::operator+= (const xstring& s) +{ + if (!this->empty()) + { + resize(s.str); + strcat(str, s.str); + } + else str = conv(s.str); + len = strlen(str); + return *this; +} + +xstring& xstring::operator+= (const char *s) +{ + if (!this->empty()) + { + resize(s); + strcat(str, s); + } + else str = conv(s); + len = strlen(str); + return *this; +} + +bool xstring::operator== (const xstring& s) const +{ + return strcmp(this->str, s.str) == 0; +} + +bool xstring::operator== (const char *s) const +{ + return strcmp(this->str, s) == 0; +} + +bool xstring::operator!= (const xstring& s) const +{ + return strcmp(this->str, s.str) != 0; +} + +bool xstring::operator!= (const char *s) const +{ + return strcmp(this->str, s) != 0; +} + +bool xstring::operator< (const xstring& s) const +{ + return strlen(this->str) < strlen(s.str); +} + +bool xstring::operator< (const char *s) const +{ + return strlen(this->str) < strlen(s); +} + +bool xstring::operator<= (const xstring& s) const +{ + return strlen(this->str) <= strlen(s.str); +} + +bool xstring::operator<= (const char *s) const +{ + return strlen(this->str) <= strlen(s); +} + +bool xstring::operator> (const xstring& s) const +{ + return strlen(this->str) > strlen(s.str); +} + +bool xstring::operator> (const char *s) const +{ + return strlen(this->str) > strlen(s); +} + +bool xstring::operator>= (const xstring& s) const +{ + return strlen(this->str) >= strlen(s.str); +} + +bool xstring::operator>= (const char *s) const +{ + return strlen(this->str) >= strlen(s); +} + +char& xstring::operator[] (int i) const +{ + if (i >= len) + { + std::cerr << "Out of bounds." << std::endl; + exit(1); + } + return str[i]; +} + std::ostream& operator<< (std::ostream& stream, const xstring& s) { return stream << s.str; } -int xstring::size() const +std::istream& operator>> (std::istream& stream, const xstring& s) +{ + return stream >> s.str; +} + +xstring& xstring::append(const xstring& s) +{ + if (!s.empty()) *this += s; + return *this; +} + +xstring& xstring::append(const xstring& s, int i) +{ + +} + +xstring& xstring::append(const char *s) { + if (!strempty(s)) *this += s; + return *this; +} + +xstring& xstring::append(const char *s, int i) +{ + +} + +char *xstring::cstr() const +{ + return str; +} + +char& xstring::last() const +{ + if (!this->empty()) return str[len-1]; +} + +std::size_t xstring::size() +{ + len = strlen(str); return len; } +bool xstring::empty() const +{ + return str == nullptr; +} + +bool xstring::strempty(const char *s) const +{ + return s == nullptr; +} + void xstring::clear() { - if (str != nullptr) delete[] str; + if (!this->empty()) delete[] str; str = nullptr; len = 0; } -char *xstring::transfer(const char *str) +char *xstring::conv(const char *s) { - int sl = strlen(str); + int sl = strlen(s); char *tmp = new char[sl+1]; - std::copy(str, str + sl+1, tmp); + std::copy(s, s + sl+1, tmp); return tmp; } + +void xstring::resize(const char *s) +{ + if (!this->empty()) + { + int l = len + strlen(s); + char *tmp = new char[l + 1]; + std::copy(str, str + len+1, tmp); + delete[] str; + str = tmp; + len = strlen(str); + } +} diff --git a/assignment-2.5-spreadsheets/src/xstring.h b/assignment-2.5-spreadsheets/src/xstring.h @@ -2,6 +2,7 @@ #define XSTRING_H #include <algorithm> +#include <cstdlib> #include <iostream> #include <string.h> @@ -11,20 +12,58 @@ class xstring char *str; int len; - char *transfer(const char *str); + char *conv(const char *s); + void resize(const char *s); + bool strempty(const char *s) const; public: xstring(); - xstring(const char *str); + xstring(const char *s); xstring(const xstring& s); ~xstring(); - void operator+= (const char *ns); - xstring operator= (const xstring& s); + xstring operator= (const xstring& s); + xstring operator= (const char *s); + xstring operator+ (const xstring& s); + xstring operator+ (const char *s); + xstring& operator+= (const xstring& s); + xstring& operator+= (const char *s); + bool operator== (const xstring& s) const; + bool operator== (const char *s) const; + bool operator!= (const xstring& s) const; + bool operator!= (const char *s) const; + bool operator< (const xstring& s) const; + bool operator< (const char *s) const; + bool operator<= (const xstring& s) const; + bool operator<= (const char *s) const; + bool operator> (const xstring& s) const; + bool operator> (const char *s) const; + bool operator>= (const xstring& s) const; + bool operator>= (const char *s) const; + char& operator[] (int i) const; friend std::ostream& operator<< (std::ostream& stream, const xstring& s); + friend std::istream& operator>> (std::istream& stream, const xstring& s); - int size() const; + xstring& append(const xstring& s); + xstring& append(const xstring& s, int i); + xstring& append(const char *s); + xstring& append(const char *s, int i); + + char *cstr() const; + char& last() const; + std::size_t size(); + bool empty() const; void clear(); }; +template<typename T> xstring tostr(T val); +template<typename T> const char *getformat(); +template<typename T> +xstring tostr(T val) +{ + char buf[25]; + snprintf(buf, sizeof(buf), getformat<T>(), val); + return xstring(buf); +} + #endif /* XSTRING_H */