Imported Upstream version 878.70.2
[platform/upstream/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     Set(str.GetBuffer());
48     return *this;
49 }
50 bool BJString::operator==(const char* str)
51 {
52     if (buffer == NULL && str == NULL)
53         return true;
54     if (buffer == NULL || str == NULL)
55         return false;
56
57     return (strcmp(str,buffer) == 0);
58 }
59 bool BJString::operator==(const BJString& str)
60 {
61     if (buffer == NULL && str.GetBuffer() == NULL)
62         return true;
63     if (buffer == NULL || str.GetBuffer() == NULL)
64         return false;
65     return (strcmp(str.GetBuffer(),buffer) == 0);
66 }
67
68 bool BJString::operator<(const BJString& str) const
69 {
70     const char* myBuff = GetBuffer();
71     const char* otherBuff = str.GetBuffer();
72
73     if (myBuff == NULL && otherBuff == NULL)
74         return false;
75     if (myBuff != NULL && otherBuff == NULL)
76         return false;
77     if (myBuff == NULL && otherBuff != NULL)
78         return true;
79
80     int cmp = strcmp(myBuff, otherBuff);
81
82     if (cmp < 0)
83         return true;
84     else
85         return false;
86
87 }
88
89 BJ_COMPARE BJString::Compare(const BJString& str)
90 {
91     const char* myBuff = GetBuffer();
92     const char* otherBuff = str.GetBuffer();
93
94     if (myBuff == NULL && otherBuff == NULL)
95         return BJ_EQUAL;
96     if (myBuff != NULL && otherBuff == NULL)
97         return BJ_GT;
98     if (myBuff == NULL && otherBuff != NULL)
99         return BJ_LT;
100
101     int cmp = strcmp(myBuff, otherBuff);
102
103     if (cmp > 0)
104         return (BJ_GT);
105     else if (cmp < 0)
106         return (BJ_LT);
107     else
108         return (BJ_EQUAL);
109
110 }
111
112 BJString& BJString::operator+=(const char* str)
113 {
114     if (buffer == NULL)
115         return operator=(str);
116     if (str == NULL)
117         return *this;
118
119     BJString temp = buffer;
120     Create((BJ_UINT32)(strlen(buffer) + strlen(str)));
121     strcpy(buffer,temp.GetBuffer());
122     strcat(buffer,str);
123     return *this;
124 }
125 BJString& BJString::operator+=(const BJString&str)
126 {
127     operator+=(str.GetBuffer());
128     return *this;
129 }
130
131
132 const char* BJString::GetBuffer() const
133 {
134     return buffer;
135 }
136
137 void BJString::Set(const char* str)
138 {
139
140     BJ_UINT32 len = str?(BJ_UINT32)strlen(str):0;
141     if (len > 255)
142         len = 250;
143     Create(len);
144     if (buffer && str)
145            strcpy(buffer, str);
146
147 }
148 void BJString::Set(const char* str, BJ_UINT32 len)
149 {
150     Create(len);
151     if (buffer)
152     {
153         if (str)
154             strncpy(buffer, str, len);
155         else
156             memset(buffer, 0, length);
157     }
158 }
159
160 void BJString::Append(const char* str, BJ_UINT32 len)
161 {
162     if (length < (strlen(buffer) + strlen(str)))
163     {
164         BJString temp = buffer;
165         Create((BJ_UINT32)(strlen(buffer) + strlen(str)));
166         if (buffer && temp.buffer)
167             strcpy(buffer,temp.GetBuffer());
168     }
169     strncat(buffer,str,len);
170 }
171
172 bool BJString::Contains(const char* str)
173 {
174     if (buffer == NULL && str == NULL)
175         return true;
176     if (buffer == NULL ||  str == NULL)
177         return false;
178     return (strstr(buffer,str) != NULL);
179 }
180
181 BJ_UINT32 BJString::GetUINT32()
182 {
183     if (buffer == NULL)
184         return 0;
185
186     return atoi(buffer);
187 }
188
189 void BJString::Format(BJ_UINT64 number,BJ_FORMAT_STYLE style)
190 {
191     switch (style) {
192         case BJSS_BYTE:
193             Create(32);
194             sprintf(buffer,"%llu",number);
195             break;
196         case BJSS_TIME:
197         {
198             char formatedTime[24];
199             time_t timeValue = number;
200             struct tm* timeStruct = localtime(&timeValue);
201             strftime(formatedTime, sizeof(formatedTime), "%Y-%m-%d_%T_%a", timeStruct);
202             Set(formatedTime);
203             break;
204         }
205         default:
206             break;
207     }
208 }
209
210
211 void BJString::Create(BJ_UINT32 len)
212 {
213     if (length >= len)
214     {
215         if (length > 0)
216             memset(buffer, 0, len+1);
217         return;
218     }
219
220     if (buffer)
221     {
222         delete buffer;
223         buffer = NULL;
224         length = 0;
225     }
226
227     buffer = new char[len+1];
228     if (buffer)
229     {
230         memset(buffer, 0, len+1);
231         length = len;
232     }
233 }
234
235 BJ_UINT32 BJString::GetLength()
236 {
237     return  buffer?(BJ_UINT32)strlen(buffer):0;
238 }