mystring.h
1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef __MYSTRING_H__
#define __MYSTRING_H__
class mystring
{
char *dane;
public:
mystring ():dane (0)
{
};
~mystring ()
{
delete[]dane;
};
mystring (const char *s)
{
if (s)
{
dane = new char[strlen (s) + 1];
strcpy (dane, s);
}
else
dane = 0;
};
unsigned int length () const
{
if (!dane)
return 0;
else
return strlen (dane);
}
mystring (const mystring & src)
{
if (src.dane)
{
dane = new char[strlen (src.dane) + 1];
strcpy (dane, src.dane);
}
else
dane = 0;
};
mystring & operator= (const mystring & src)
{
if (this != &src)
{
delete[]dane;
if (src.dane)
{
dane = new char[strlen (src.dane) + 1];
strcpy (dane, src.dane);
}
else
dane = 0;
};
return *this;
}
mystring & operator+= (const mystring & src)
{
char *newstr = new char[length () + src.length () + 1];
if (length ())
strcpy (newstr, dane);
else
newstr[0] = '\0';
if (src.length ())
strcat (newstr, src.dane);
delete[]dane;
dane = newstr;
return *this;
}
char operator[] (unsigned int i) const
{
if (!dane)
abort ();
if (i >= strlen (dane))
abort ();
return dane[i];
}
char &operator[] (unsigned int i)
{
if (!dane)
abort ();
if (i >= strlen (dane))
abort ();
return dane[i];
}
friend ostream & operator<< (ostream & o, const mystring & s)
{
if (s.dane)
o << s.dane;
return o;
}
};
inline mystring operator+ (const mystring & s1, const mystring & s2)
{
mystring s (s1);
return s += s2;
}
#endif /* __MYSTRING_H__ */