diff options
Diffstat (limited to 'mDNSResponder/mDNSMacOSX/BonjourTop/source/bjstring.cpp')
-rw-r--r-- | mDNSResponder/mDNSMacOSX/BonjourTop/source/bjstring.cpp | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/mDNSResponder/mDNSMacOSX/BonjourTop/source/bjstring.cpp b/mDNSResponder/mDNSMacOSX/BonjourTop/source/bjstring.cpp new file mode 100644 index 00000000..5a6fcdf1 --- /dev/null +++ b/mDNSResponder/mDNSMacOSX/BonjourTop/source/bjstring.cpp @@ -0,0 +1,238 @@ +// +// bjstring.cpp +// TestTB +// +// Created by Terrin Eager on 9/26/12. +// +// + +#include "bjstring.h" +#include <time.h> + + +BJString::BJString() +{ + buffer = NULL; + length = 0; +} + +BJString::BJString(const BJString& scr) +{ + buffer = NULL; + length = 0; + Set(scr.GetBuffer()); +} +BJString::BJString(const char* str) +{ + buffer = NULL; + length = 0; + Set(str); +} + +BJString::~BJString() +{ + delete[] buffer; + buffer = NULL; +} + + +BJString& BJString::operator=(const char* str) +{ + Set(str); + return *this; +} + +BJString& BJString::operator=(const BJString& str) +{ + Set(str.GetBuffer()); + return *this; +} +bool BJString::operator==(const char* str) +{ + if (buffer == NULL && str == NULL) + return true; + if (buffer == NULL || str == NULL) + return false; + + return (strcmp(str,buffer) == 0); +} +bool BJString::operator==(const BJString& str) +{ + if (buffer == NULL && str.GetBuffer() == NULL) + return true; + if (buffer == NULL || str.GetBuffer() == NULL) + return false; + return (strcmp(str.GetBuffer(),buffer) == 0); +} + +bool BJString::operator<(const BJString& str) const +{ + const char* myBuff = GetBuffer(); + const char* otherBuff = str.GetBuffer(); + + if (myBuff == NULL && otherBuff == NULL) + return false; + if (myBuff != NULL && otherBuff == NULL) + return false; + if (myBuff == NULL && otherBuff != NULL) + return true; + + int cmp = strcmp(myBuff, otherBuff); + + if (cmp < 0) + return true; + else + return false; + +} + +BJ_COMPARE BJString::Compare(const BJString& str) +{ + const char* myBuff = GetBuffer(); + const char* otherBuff = str.GetBuffer(); + + if (myBuff == NULL && otherBuff == NULL) + return BJ_EQUAL; + if (myBuff != NULL && otherBuff == NULL) + return BJ_GT; + if (myBuff == NULL && otherBuff != NULL) + return BJ_LT; + + int cmp = strcmp(myBuff, otherBuff); + + if (cmp > 0) + return (BJ_GT); + else if (cmp < 0) + return (BJ_LT); + else + return (BJ_EQUAL); + +} + +BJString& BJString::operator+=(const char* str) +{ + if (buffer == NULL) + return operator=(str); + if (str == NULL) + return *this; + + BJString temp = buffer; + Create((BJ_UINT32)(strlen(buffer) + strlen(str))); + strcpy(buffer,temp.GetBuffer()); + strcat(buffer,str); + return *this; +} +BJString& BJString::operator+=(const BJString&str) +{ + operator+=(str.GetBuffer()); + return *this; +} + + +const char* BJString::GetBuffer() const +{ + return buffer; +} + +void BJString::Set(const char* str) +{ + + BJ_UINT32 len = str?(BJ_UINT32)strlen(str):0; + if (len > 255) + len = 250; + Create(len); + if (buffer && str) + strcpy(buffer, str); + +} +void BJString::Set(const char* str, BJ_UINT32 len) +{ + Create(len); + if (buffer) + { + if (str) + strncpy(buffer, str, len); + else + memset(buffer, 0, length); + } +} + +void BJString::Append(const char* str, BJ_UINT32 len) +{ + if (length < (strlen(buffer) + strlen(str))) + { + BJString temp = buffer; + Create((BJ_UINT32)(strlen(buffer) + strlen(str))); + if (buffer && temp.buffer) + strcpy(buffer,temp.GetBuffer()); + } + strncat(buffer,str,len); +} + +bool BJString::Contains(const char* str) +{ + if (buffer == NULL && str == NULL) + return true; + if (buffer == NULL || str == NULL) + return false; + return (strstr(buffer,str) != NULL); +} + +BJ_UINT32 BJString::GetUINT32() +{ + if (buffer == NULL) + return 0; + + return atoi(buffer); +} + +void BJString::Format(BJ_UINT64 number,BJ_FORMAT_STYLE style) +{ + switch (style) { + case BJSS_BYTE: + Create(32); + sprintf(buffer,"%llu",number); + break; + case BJSS_TIME: + { + char formatedTime[24]; + time_t timeValue = number; + struct tm* timeStruct = localtime(&timeValue); + strftime(formatedTime, sizeof(formatedTime), "%Y-%m-%d_%T_%a", timeStruct); + Set(formatedTime); + break; + } + default: + break; + } +} + + +void BJString::Create(BJ_UINT32 len) +{ + if (length >= len) + { + if (length > 0) + memset(buffer, 0, len+1); + return; + } + + if (buffer) + { + delete buffer; + buffer = NULL; + length = 0; + } + + buffer = new char[len+1]; + if (buffer) + { + memset(buffer, 0, len+1); + length = len; + } +} + +BJ_UINT32 BJString::GetLength() +{ + return buffer?(BJ_UINT32)strlen(buffer):0; +} |