Skip to content

Commit 8db32c6

Browse files
committed
[logo] Add program skeleton
1 parent e5fb460 commit 8db32c6

File tree

15 files changed

+287
-45
lines changed

15 files changed

+287
-45
lines changed

src/usr/include/graphics.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ void rectangle(int left, int top, int right, int bottom);
5656
void bar(int left, int top, int right, int bottom);
5757
void fillellipse(int xcenter, int ycenter, int x_radius, int y_radius);
5858

59-
int textheight(char *str);
60-
int textwidth(char *str);
61-
void outtext(char *str);
62-
int outtextxy(int x, int y, char *str);
59+
int textheight(const char *str);
60+
int textwidth(const char *str);
61+
void outtext(const char *str);
62+
int outtextxy(int x, int y, const char *str);
6363
void moveto(int x, int y);
6464
int getx();
6565
int gety();

src/usr/include/iostream.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ class istream {
2828
extern ostream cout;
2929
extern istream cin;
3030

31+
std::istream& get_line(std::istream &i, std::string &str);
32+
3133
} // namespace std end

src/usr/include/stdlib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ int abs(int a);
2020
void* malloc(size_t size);
2121
void free(void* ptr);
2222

23+
void atexit(void (*)(void));
2324
void exit(int status);
2425

2526
#ifdef __cplusplus

src/usr/include/string.h

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class basic_string {
4141
basic_string(std::size_t n, CharT c);
4242
basic_string(const CharT* str);
4343
basic_string(const CharT* str, std::size_t n);
44+
basic_string(const basic_string<CharT>& o);
4445

4546
iterator begin();
4647
iterator end();
@@ -55,17 +56,52 @@ class basic_string {
5556
CharT& operator[](std::size_t pos);
5657
const CharT *c_str() const;
5758

59+
basic_string<CharT>& operator=(const basic_string<CharT>& o);
5860
basic_string<CharT>& operator+=(const CharT c);
5961
basic_string<CharT>& operator+=(const CharT* o);
6062
basic_string<CharT>& operator+=(const basic_string<CharT> &o);
6163
};
6264

6365
using string = basic_string<char>;
6466

65-
std::istream& get_line(std::istream &i, std::string &str);
66-
6767
} // namespace std end
6868

69+
70+
template<typename CharT>
71+
inline bool operator==(const std::basic_string<CharT> &a, const CharT* b) { return std::strcmp(a.c_str(), b) == 0; }
72+
template<typename CharT>
73+
inline bool operator==(const CharT* a, const std::basic_string<CharT> &b) { return std::strcmp(a, b.c_str()) == 0; }
74+
template<typename CharT>
75+
inline bool operator==(const std::basic_string<CharT> &a, const std::basic_string<CharT> &b) { return std::strcmp(a.c_str(), b.c_str()) == 0; }
76+
77+
template<typename CharT>
78+
inline bool operator<(const std::basic_string<CharT> &a, const CharT* b) { return std::strcmp(a.c_str(), b) < 0; }
79+
template<typename CharT>
80+
inline bool operator<(const CharT* a, const std::basic_string<CharT> &b) { return std::strcmp(a, b.c_str()) < 0; }
81+
template<typename CharT>
82+
inline bool operator<(const std::basic_string<CharT> &a, const std::basic_string<CharT> &b) { return std::strcmp(a.c_str(), b.c_str()) < 0; }
83+
84+
template<typename CharT>
85+
inline bool operator>(const std::basic_string<CharT> &a, const CharT* b) { return std::strcmp(a.c_str(), b) > 0; }
86+
template<typename CharT>
87+
inline bool operator>(const CharT* a, const std::basic_string<CharT> &b) { return std::strcmp(a, b.c_str()) > 0; }
88+
template<typename CharT>
89+
inline bool operator>(const std::basic_string<CharT> &a, const std::basic_string<CharT> &b) { return std::strcmp(a.c_str(), b.c_str()) > 0; }
90+
91+
template<typename CharT>
92+
inline bool operator<=(const std::basic_string<CharT> &a, const CharT* b) { return !operator>(a,b); }
93+
template<typename CharT>
94+
inline bool operator<=(const CharT* a, const std::basic_string<CharT> &b) { return !operator>(a,b); }
95+
template<typename CharT>
96+
inline bool operator<=(const std::basic_string<CharT> &a, const std::basic_string<CharT> &b) { return !operator>(a,b); }
97+
98+
template<typename CharT>
99+
inline bool operator>=(const std::basic_string<CharT> &a, const CharT* b) { return !operator<(a,b); }
100+
template<typename CharT>
101+
inline bool operator>=(const CharT* a, const std::basic_string<CharT> &b) { return !operator<(a,b); }
102+
template<typename CharT>
103+
inline bool operator>=(const std::basic_string<CharT> &a, const std::basic_string<CharT> &b) { return !operator<(a,b); }
104+
69105
#include <string.tcc>
70106

71107
#endif

src/usr/include/string.tcc

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ basic_string<CharT>::basic_string(const CharT* str, std::size_t n) : _data(n+1,
2929
}
3030
}
3131

32+
template <typename CharT>
33+
basic_string<CharT>::basic_string(const basic_string<CharT> &o) : _data(o._data) {}
34+
3235
template <typename CharT>
3336
typename basic_string<CharT>::iterator basic_string<CharT>::begin() { return this->_data.begin(); }
3437

@@ -80,6 +83,12 @@ const CharT *basic_string<CharT>::c_str() const {
8083
return this->_data.begin();
8184
}
8285

86+
template <typename CharT>
87+
basic_string<CharT>& basic_string<CharT>::operator=(const basic_string<CharT> &o) {
88+
this->_data = o._data;
89+
return *this;
90+
}
91+
8392
template <typename CharT>
8493
basic_string<CharT>& basic_string<CharT>::operator+=(const CharT c) {
8594
this->_data.back() = c; // replace null character
@@ -107,14 +116,4 @@ basic_string<CharT>& basic_string<CharT>::operator+=(const basic_string<CharT> &
107116
return *this;
108117
}
109118

110-
std::istream& get_line(std::istream &i, std::string &str) {
111-
char ch;
112-
str.clear();
113-
while (1) {
114-
i.get(ch);
115-
if (ch == '\n') break;
116-
str += ch;
117-
}
118-
}
119-
120119
} // namespace std end

src/usr/include/utility.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
namespace std {
4+
5+
template<typename T1, typename T2>
6+
class pair {
7+
public:
8+
T1 first;
9+
T2 second;
10+
pair();
11+
pair(const T1 &first, const T2 &second);
12+
};
13+
14+
template<typename T1, typename T2>
15+
inline pair<T1,T2> make_pair(const T1 &a, const T2& b) {
16+
return pair<T1,T2>(a, b);
17+
}
18+
19+
} // namespace std end
20+
21+
#include <utility.tcc>

src/usr/include/utility.tcc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
// This is internal headerfile to be included by utility.h only
4+
5+
#include <utility.h>
6+
7+
namespace std {
8+
9+
template<typename T1, typename T2>
10+
pair<T1,T2>::pair() {}
11+
12+
template<typename T1, typename T2>
13+
pair<T1,T2>::pair(const T1 &first, const T2 &second) : first(first), second(second) {}
14+
15+
} // namespace std end

src/usr/include/vector.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ class vector {
1414
typedef T* iterator;
1515
typedef const T* const_iterator;
1616

17-
vector<T>();
18-
vector<T>(std::size_t size, const T &_default);
17+
vector();
18+
vector(std::size_t size, const T &_default);
19+
vector(const vector<T> &o);
20+
vector& operator=(const vector<T> &o);
21+
~vector();
1922

2023
iterator begin();
2124
iterator end();

src/usr/include/vector.tcc

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,25 @@ vector<T>::vector(std::size_t size, const T &_default): _size(size) {
3030
}
3131
}
3232

33+
template <typename T>
34+
vector<T>::vector(const vector<T> &o) : _size(o._size) {
35+
resize_capacity(o._capacity);
36+
std::memcpy(_data, o._data, _capacity*sizeof(T));
37+
}
38+
39+
template <typename T>
40+
vector<T>& vector<T>::operator=(const vector<T> &o) {
41+
resize_capacity(o._capacity);
42+
_size = o._size;
43+
std::memcpy(_data, o._data, _capacity*sizeof(T));
44+
return *this;
45+
}
46+
47+
template <typename T>
48+
vector<T>::~vector() {
49+
delete[] _data;
50+
}
51+
3352
template <typename T>
3453
typename vector<T>::iterator vector<T>::begin() { return this->_data; }
3554

@@ -48,10 +67,10 @@ void vector<T>::resize_capacity(std::size_t capacity) {
4867
// and will also make a copy of array and move _data pointer.
4968
// assumes size<=current_capacity and size<=new_capacity
5069
const std::size_t data_size = capacity*sizeof(T);
51-
T *_new_data = (T*) std::malloc(data_size);
70+
T *_new_data = new T[capacity];
5271
if (!_new_data) return; // malloc failed
5372
std::memcpy(_new_data, this->_data, data_size);
54-
std::free(this->_data); // should be no-op if _data == NULL
73+
delete[] this->_data; // should be no-op if _data == NULL
5574
this->_data = _new_data;
5675
this->_capacity = capacity;
5776
}

src/usr/lib/graphics.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,19 +346,19 @@ static void draw_font(char c, int x, int y) {
346346
}
347347
}
348348

349-
int textheight(char *str) {
349+
int textheight(const char *str) {
350350
return gfont.size;
351351
}
352352

353-
int textwidth(char *str) {
353+
int textwidth(const char *str) {
354354
return gfont.size * strlen(str);
355355
}
356356

357-
void outtext(char *str) {
357+
void outtext(const char *str) {
358358
gstate.x += outtextxy(gstate.x, gstate.y, str);
359359
}
360360

361-
int outtextxy(int x, int y, char *str) {
361+
int outtextxy(int x, int y, const char *str) {
362362
// returns textwidth
363363
int len = 0;
364364
while (str!=NULL && (*str)!=NULL) {

0 commit comments

Comments
 (0)