]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/BonjourTop/source/bjstring.cpp
mDNSResponder-1096.0.2.tar.gz
[apple/mdnsresponder.git] / mDNSMacOSX / BonjourTop / source / bjstring.cpp
1 //
2 // bjstring.cpp
3 // TestTB
4 //
5 // Created by Terrin Eager on 9/26/12.
6 //
7 //
8
9 #include "bjstring.h"
10 #include <time.h>
11
12
13 BJString::BJString()
14 {
15 buffer = NULL;
16 length = 0;
17 }
18
19 BJString::BJString(const BJString& scr)
20 {
21 buffer = NULL;
22 length = 0;
23 Set(scr.GetBuffer());
24 }
25 BJString::BJString(const char* str)
26 {
27 buffer = NULL;
28 length = 0;
29 Set(str);
30 }
31
32 BJString::~BJString()
33 {
34 delete[] buffer;
35 buffer = NULL;
36 }
37
38
39 BJString& BJString::operator=(const char* str)
40 {
41 Set(str);
42 return *this;
43 }
44
45 BJString& BJString::operator=(const BJString& str)
46 {
47 if (&str != this)
48 Set(str.GetBuffer());
49 return *this;
50 }
51 bool BJString::operator==(const char* str)
52 {
53 if (buffer == NULL && str == NULL)
54 return true;
55 if (buffer == NULL || str == NULL)
56 return false;
57
58 return (strcmp(str,buffer) == 0);
59 }
60 bool BJString::operator==(const BJString& str)
61 {
62 if (buffer == NULL && str.GetBuffer() == NULL)
63 return true;
64 if (buffer == NULL || str.GetBuffer() == NULL)
65 return false;
66 return (strcmp(str.GetBuffer(),buffer) == 0);
67 }
68
69 bool BJString::operator<(const BJString& str) const
70 {
71 const char* myBuff = GetBuffer();
72 const char* otherBuff = str.GetBuffer();
73
74 if (myBuff == NULL && otherBuff == NULL)
75 return false;
76 if (myBuff != NULL && otherBuff == NULL)
77 return false;
78 if (myBuff == NULL && otherBuff != NULL)
79 return true;
80
81 int cmp = strcmp(myBuff, otherBuff);
82
83 if (cmp < 0)
84 return true;
85 else
86 return false;
87
88 }
89
90 BJ_COMPARE BJString::Compare(const BJString& str)
91 {
92 const char* myBuff = GetBuffer();
93 const char* otherBuff = str.GetBuffer();
94
95 if (myBuff == NULL && otherBuff == NULL)
96 return BJ_EQUAL;
97 if (myBuff != NULL && otherBuff == NULL)
98 return BJ_GT;
99 if (myBuff == NULL && otherBuff != NULL)
100 return BJ_LT;
101
102 int cmp = strcmp(myBuff, otherBuff);
103
104 if (cmp > 0)
105 return (BJ_GT);
106 else if (cmp < 0)
107 return (BJ_LT);
108 else
109 return (BJ_EQUAL);
110
111 }
112
113 BJString& BJString::operator+=(const char* str)
114 {
115 if (buffer == NULL)
116 return operator=(str);
117 if (str == NULL)
118 return *this;
119
120 BJString temp = buffer;
121 Create((BJ_UINT32)(strlen(buffer) + strlen(str)));
122 strlcpy(buffer, temp.GetBuffer(), length + 1);
123 strlcat(buffer, str, length + 1);
124 return *this;
125 }
126 BJString& BJString::operator+=(const BJString&str)
127 {
128 operator+=(str.GetBuffer());
129 return *this;
130 }
131
132
133 const char* BJString::GetBuffer() const
134 {
135 return buffer;
136 }
137
138 void BJString::Set(const char* str)
139 {
140
141 BJ_UINT32 len = str?(BJ_UINT32)strlen(str):0;
142 if (len > 255)
143 len = 250;
144 Create(len);
145 if (buffer && str)
146 strlcpy(buffer, str, length + 1);
147
148 }
149 void BJString::Set(const char* str, BJ_UINT32 len)
150 {
151 Create(len);
152 if (buffer)
153 {
154 if (str)
155 strncpy(buffer, str, len);
156 else
157 memset(buffer, 0, length);
158 }
159 }
160
161 void BJString::Append(const char* str, BJ_UINT32 len)
162 {
163 if (length < (strlen(buffer) + strlen(str)))
164 {
165 BJString temp = buffer;
166 Create((BJ_UINT32)(strlen(buffer) + strlen(str)));
167 if (buffer && temp.buffer)
168 strlcpy(buffer, temp.GetBuffer(), length + 1);
169 }
170 strncat(buffer,str,len);
171 }
172
173 bool BJString::Contains(const char* str)
174 {
175 if (buffer == NULL && str == NULL)
176 return true;
177 if (buffer == NULL || str == NULL)
178 return false;
179 return (strstr(buffer,str) != NULL);
180 }
181
182 BJ_UINT32 BJString::GetUINT32()
183 {
184 if (buffer == NULL)
185 return 0;
186
187 return atoi(buffer);
188 }
189
190 void BJString::Format(BJ_UINT64 number,BJ_FORMAT_STYLE style)
191 {
192 switch (style) {
193 case BJSS_BYTE:
194 Create(32);
195 sprintf(buffer,"%llu",number);
196 break;
197 case BJSS_TIME:
198 {
199 char formatedTime[24];
200 time_t timeValue = number;
201 struct tm* timeStruct = localtime(&timeValue);
202 strftime(formatedTime, sizeof(formatedTime), "%Y-%m-%d_%T_%a", timeStruct);
203 Set(formatedTime);
204 break;
205 }
206 default:
207 break;
208 }
209 }
210
211
212 void BJString::Create(BJ_UINT32 len)
213 {
214 if (buffer)
215 {
216 if (length >= len)
217 {
218 memset(buffer, 0, len + 1);
219 return;
220 }
221 delete buffer;
222 buffer = NULL;
223 length = 0;
224 }
225
226 buffer = new char[len+1];
227 if (buffer)
228 {
229 memset(buffer, 0, len+1);
230 length = len;
231 }
232 }
233
234 BJ_UINT32 BJString::GetLength()
235 {
236 return buffer?(BJ_UINT32)strlen(buffer):0;
237 }