commit 52138f3c391a49cf7442f80a60d506f6857d0acb
parent 5b729bc0bfee1ea955b77d341b26a55b57517c4a
Author: Christos Margiolis <christos@margiolis.net>
Date: Fri, 1 May 2020 19:21:01 +0300
pending possible bug fix on line 96 in xstring.cpp
Diffstat:
9 files changed, 128 insertions(+), 33 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/appsystem.o b/assignment-2.4-inheritance/obj/appsystem.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.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/src/main.cpp b/assignment-2.5-spreadsheets/src/main.cpp
@@ -1,7 +1,16 @@
#include "xstring.h"
+#include <fstream>
int main(int argc, char **argv)
{
+ std::ifstream f("src/main.cpp");
+ while (f.good())
+ {
+ lab::xstring a;
+ lab::getline(f, a);
+ std::cout << a << std::endl;
+ }
+ f.close();
return 0;
}
diff --git a/assignment-2.5-spreadsheets/src/xstring.cpp b/assignment-2.5-spreadsheets/src/xstring.cpp
@@ -1,9 +1,6 @@
#include "xstring.h"
-template<> const char *getformat<int>() {return "%d";}
-template<> const char *getformat<float>() {return "%f";}
-template<> const char *getformat<double>() {return "%f";}
-
+namespace lab {
xstring::xstring()
:str(new char[1]), len(0) {}
@@ -15,22 +12,18 @@ xstring::xstring(const xstring& s)
if (!this->empty())
{
str = conv(s.str);
- len = strlen(str);
+ len = size();
}
else
{
- str = nullptr;
+ str = new char[1];
len = 0;
}
}
xstring::~xstring()
{
- if (!this->empty())
- {
- len = 0;
- delete[] str;
- }
+ delete[] str;
}
xstring xstring::operator= (const xstring& s)
@@ -40,11 +33,11 @@ xstring xstring::operator= (const xstring& s)
{
if (!this->empty()) delete[] str;
str = conv(s.str);
- len = strlen(str);
+ len = size();
}
else
{
- str = nullptr;
+ str = new char[1];
len = 0;
}
return *this;
@@ -54,7 +47,7 @@ xstring xstring::operator= (const char *s)
{
if (!this->empty()) delete[] str;
str = conv(s);
- len = strlen(str);
+ len = size();
return *this;
}
@@ -76,7 +69,7 @@ xstring& xstring::operator+= (const xstring& s)
strcat(str, s.str);
}
else str = conv(s.str);
- len = strlen(str);
+ len = size();
return *this;
}
@@ -88,7 +81,20 @@ xstring& xstring::operator+= (const char *s)
strcat(str, s);
}
else str = conv(s);
- len = strlen(str);
+ len = size();
+ return *this;
+}
+
+xstring& xstring::operator+= (char c)
+{
+ if (!this->empty())
+ {
+ resize(1);
+ str[len] = c;
+ }
+ else str = conv(c);
+ // wtf
+ len += size() + 1;
return *this;
}
@@ -152,7 +158,7 @@ bool xstring::operator>= (const char *s) const
return strlen(this->str) >= strlen(s);
}
-char& xstring::operator[] (int i) const
+char& xstring::operator[] (std::size_t i) const
{
if (i >= len)
{
@@ -178,7 +184,7 @@ xstring& xstring::append(const xstring& s)
return *this;
}
-xstring& xstring::append(const xstring& s, int i)
+xstring& xstring::append(const xstring& s, std::size_t i)
{
}
@@ -189,11 +195,17 @@ xstring& xstring::append(const char *s)
return *this;
}
-xstring& xstring::append(const char *s, int i)
+xstring& xstring::append(const char *s, std::size_t i)
{
}
+xstring& xstring::append(char c)
+{
+ *this += c;
+ return *this;
+}
+
char *xstring::cstr() const
{
return str;
@@ -212,26 +224,34 @@ std::size_t xstring::size()
bool xstring::empty() const
{
- return str == nullptr;
+ return len == 0;
}
bool xstring::strempty(const char *s) const
{
- return s == nullptr;
+ return strlen(s) == 0;
}
void xstring::clear()
{
if (!this->empty()) delete[] str;
- str = nullptr;
+ str = new char[1];
len = 0;
}
char *xstring::conv(const char *s)
{
- int sl = strlen(s);
- char *tmp = new char[sl+1];
- std::copy(s, s + sl+1, tmp);
+ std::size_t l = strlen(s);
+ char *tmp = new char[l + 1];
+ std::copy(s, s + l+1, tmp);
+ return tmp;
+}
+
+char *xstring::conv(char c)
+{
+ // 2 for '\0'
+ char *tmp = new char[1];
+ tmp[0] = c;
return tmp;
}
@@ -239,11 +259,59 @@ void xstring::resize(const char *s)
{
if (!this->empty())
{
- int l = len + strlen(s);
+ std::size_t l = len + strlen(s);
char *tmp = new char[l + 1];
std::copy(str, str + len+1, tmp);
delete[] str;
str = tmp;
- len = strlen(str);
+ len = size();
+ }
+}
+
+void xstring::resize(std::size_t n)
+{
+ if (!this->empty())
+ {
+ std::size_t l = len + n;
+ char *tmp = new char[l + 1];
+ std::copy(str, str + len+1, tmp);
+ delete[] str;
+ str = tmp;
+ // wtf
+ len = size();
+ }
+}
+
+std::istream& getline(std::istream& stream, xstring& s)
+{
+ char c;
+ s.clear();
+ while (stream.get(c) && c != '\n')
+ s.append(c);
+ return stream;
+}
+
+std::istream& getline(std::istream& stream, xstring& s, char delim)
+{
+ char c;
+ s.clear();
+ while (stream.get(c) && c != '\n')
+ {
+ if (c == delim) break;
+ else s.append(c);
}
+ return stream;
+}
+
+template<> const char *getformat<short>() {return "%hi";}
+template<> const char *getformat<int>() {return "%d";}
+template<> const char *getformat<long>() {return "%ld";}
+template<> const char *getformat<long long>() {return "%lld";}
+template<> const char *getformat<unsigned short>() {return "%hu";}
+template<> const char *getformat<unsigned int>() {return "%u";}
+template<> const char *getformat<unsigned long>() {return "%lu";}
+template<> const char *getformat<unsigned long long>() {return "%llu";}
+template<> const char *getformat<float>() {return "%f";}
+template<> const char *getformat<double>() {return "%f";}
+template<> const char *getformat<long double>() {return "%Lf";}
}
diff --git a/assignment-2.5-spreadsheets/src/xstring.h b/assignment-2.5-spreadsheets/src/xstring.h
@@ -6,14 +6,17 @@
#include <iostream>
#include <string.h>
+namespace lab {
class xstring
{
private:
char *str;
- int len;
+ std::size_t len;
char *conv(const char *s);
+ char *conv(char c);
void resize(const char *s);
+ void resize(std::size_t n);
bool strempty(const char *s) const;
public:
@@ -28,6 +31,7 @@ class xstring
xstring operator+ (const char *s);
xstring& operator+= (const xstring& s);
xstring& operator+= (const char *s);
+ xstring& operator+= (char c);
bool operator== (const xstring& s) const;
bool operator== (const char *s) const;
bool operator!= (const xstring& s) const;
@@ -40,14 +44,15 @@ class xstring
bool operator> (const char *s) const;
bool operator>= (const xstring& s) const;
bool operator>= (const char *s) const;
- char& operator[] (int i) const;
+ char& operator[] (std::size_t i) const;
friend std::ostream& operator<< (std::ostream& stream, const xstring& s);
friend std::istream& operator>> (std::istream& stream, const xstring& s);
xstring& append(const xstring& s);
- xstring& append(const xstring& s, int i);
+ xstring& append(const xstring& s, std::size_t i);
xstring& append(const char *s);
- xstring& append(const char *s, int i);
+ xstring& append(const char *s, std::size_t i);
+ xstring& append(char c);
char *cstr() const;
char& last() const;
@@ -56,14 +61,27 @@ class xstring
void clear();
};
-template<typename T> xstring tostr(T val);
+std::istream& getline(std::istream& stream, xstring& s);
+std::istream& getline(std::istream& stream, xstring& s, char delim);
+
+template<typename T> xstring to_xstr(T val);
template<typename T> const char *getformat();
template<typename T>
-xstring tostr(T val)
+xstring to_xstr(T val)
{
char buf[25];
snprintf(buf, sizeof(buf), getformat<T>(), val);
return xstring(buf);
}
+// set a default T
+template<typename T>
+xstring to_xstr(const char *fs, T val)
+{
+ char buf[25];
+ snprintf(buf, sizeof(buf), fs, val);
+ return xstring(buf);
+}
+}
+
#endif /* XSTRING_H */