Fix include style
[bcusdk.git] / common / my_strings.h
blob140b9bbbb8a70d4322d36f54a5d0b9fd310462bd
1 /*
2 EIBD eib bus access and management daemon
3 Copyright (C) 2005-2010 Martin Koegler <mkoegler@auto.tuwien.ac.at>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #ifndef MY_STRING_H
21 #define MY_STRING_H
23 #include <string.h>
25 /** implements a string */
26 class String
28 /** content */
29 char *data;
30 /** length */
31 unsigned len;
32 public:
33 /** free memory */
34 virtual ~ String ()
36 if (data)
37 delete[]data;
40 /** initialize with the empty string */
41 String ()
43 data = 0;
44 len = 0;
47 /** initialize with a string */
48 String (const String & a)
50 len = a.len;
51 if (len)
53 data = new char[len];
54 memcpy (data, a.data, len);
56 else
57 data = 0;
59 /** assign a String */
60 const String & operator = (const String & a)
62 if (data)
63 delete[]data;
64 len = a.len;
65 if (len)
67 data = new char[len];
68 memcpy (data, a.data, len);
70 else
71 data = 0;
72 return a;
75 /** initialize with a character constant */
76 String (const char *msg)
78 if (msg)
80 len = strlen (msg) + 1;
81 data = new char[len];
82 strcpy (data, msg);
84 else
86 data = 0;
87 len = 0;
91 /** assign a character constant */
92 const String & operator = (const char *msg)
94 if (data)
95 delete[]data;
96 if (msg)
98 len = strlen (msg) + 1;
99 data = new char[len];
100 strcpy (data, msg);
102 else
104 data = 0;
105 len = 0;
107 return *this;
110 /** concats two strings*/
111 String operator + (const String & a)
113 String b;
114 if (!len)
115 return a;
116 if (!a.len)
117 return *this;
118 b.len = a.len + len - 1;
119 if (b.len > 0)
121 b.data = new char[b.len];
122 if (data)
123 strcpy (b.data, data);
124 else
125 b.data[0] = 0;
126 if (a.data)
127 strcat (b.data, a.data);
129 return b;
132 bool operator == (const String & a) const
134 if (!a.len && !len)
135 return 1;
136 if (a.len != len)
137 return 0;
138 return (!strcmp (data, a.data));
141 bool operator!= (const String & a) const
143 return !(*this == a);
146 /** returns the content as char* */
147 const char *operator () () const
149 return data;
154 /** adds a text to a string */
155 inline const String &
156 operator += (String & a, const String b)
158 String c = a + b;
159 return (a = c);
162 #endif