- #include "stdafx.h"
- #include <stringutil.h>
- #include <vector>
- bool StringUtil::StartsWith(std::string& s1, const char * s2)
- {
- bool result = false;
- if (s2)
- {
- int len1 = s1.length();
- int len2 = strlen(s2);
- if ((len2 > 0) && (len1 >= len2))
- {
- const char * ptr1 = s1.c_str();
- result = true;
- for (int i=0;i<len2;i++){
- if (s2[i] != ptr1[i])
- {
- result = false;
- break;
- }
- }
- }
- }
- return result;
- }
- bool StringUtil::Equals(std::string& s1, const char * s2)
- {
- std::string s = s2;
- return Equals(s1,s);
- }
- bool StringUtil::Equals(std::string& s1, std::string& s2)
- {
- bool result = false;
- int len1 = s1.length();
- int len2 = s2.length();
- if (len1 > 0)
- {
- if (len1 == len2)
- {
- const char * ptr1 = s1.c_str();
- const char * ptr2 = s2.c_str();
- result = true;
- for (int i=0;i<len1;i++){
- if (ptr1[i] != ptr2[i])
- {
- result = false;
- break;
- }
- }
- }
- }
- return result;
- }
- void StringUtil::StripControlCharacters(std::string& s)
- {
- std::string result;
- int len = s.length();
- char c;
- for (int i=0;i<len;i++){
- c = s.c_str()[i];
- if (c >= 32)
- {
- result += c;
- }
- }
- s = result;
- }
- void StringUtil::Uppercase(std::string& s)
- {
- std::string result;
- int len = s.length();
- char c;
- for (int i=0;i<len;i++){
- c = s.c_str()[i];
- if ((c > 96) && (c < 123))
- {
- c -= 32;
- }
- result += c;
- }
- s = result;
- }
- int StringUtil::CompareNoCaseOrSpace(std::string& s1, std::string& s2)
- {
- int result = 0;
- std::string tmp1 = s1;
- std::string tmp2 = s2;
- StripSpaces(tmp1);
- StripSpaces(tmp2);
- if (CalculateHashNumber(tmp1) == CalculateHashNumber(tmp2))
- {
- if (tmp1 == tmp2)
- {
- result++;
- }
- }
- return result;
- }
- int StringUtil::CalculateHashNumber(std::string& s)
- {
- int result = 0;
- int length = s.length();
- if (length > 0)
- {
- const char * cptr = s.c_str();
- char c;
- for (int i=0;i<length;i++){
- c = cptr[i];
- // we want this to be case insensitive
- if ((c > 96) && (c < 123))
- {
- c -= 32;
- }
- result += (int)c;
- }
- }
- return result;
- }
- UINT32 StringUtil::DecodeIPAddress(std::string& s)
- {
- UINT32 result = 0;
- std::string stmp = s;
- int length = stmp.length();
- const char * cptr = stmp.c_str();
- int i;
- char c;
- std::string tmp;
- int non_num = 0;
- int num = 0;
- std::vector<UINT32> numbers;
- for (i=0;i<length;i++){
- c = cptr[i];
- if (c == '.')
- {
- if (tmp.length() > 0)
- {
- if (IsIntNumber(tmp))
- {
- num++;
- UINT32 u = atoi(tmp.c_str());
- numbers.push_back(u);
- }
- else
- {
- non_num++;
- }
- }
- else
- {
- break;
- }
- }
- else
- {
- tmp += c;
- }
- }
- int size = numbers.size();
- if (size == 4)
- {
- UINT32 a = numbers[0];
- UINT32 b = numbers[1];
- UINT32 c = numbers[2];
- UINT32 d = numbers[4];
- result = (d << 24) | (c << 16) | (b << 8) | a;
- }
- return result;
- }
- int StringUtil::IsIPAddress(std::string& s)
- {
- int result = 0;
- std::string stmp = s;
- StripSpaces(stmp);
- std::string tmp;
- int length = stmp.length();
- const char * cptr = stmp.c_str();
- int i;
- int non_num = 0;
- int num = 0;
- char c;
- for (i=0;i<length;i++){
- c = cptr[i];
- if (c == '.')
- {
- if (tmp.length() > 0)
- {
- if (IsIntNumber(tmp))
- {
- num++;
- }
- else
- {
- non_num++;
- }
- tmp = "";
- }
- else
- {
- break;
- }
- }
- else
- {
- tmp += c;
- }
- }
- if ((num == 4) && (non_num == 0))
- {
- result++;
- }
- return result;
- }
- int StringUtil::IsIntNumber(std::string& s)
- {
- char c;
- int ok = 0;
- int size = s.length();
- const char * cptr = s.c_str();
- for (int i=0;i<size;i++){
- c = cptr[i];
- if (i == 0)
- {
- if (c == '-')
- {
- ok++;
- }
- else if ((c>=48) && (c<=57))
- {
- ok++;
- }
- else
- {
- break;
- }
- }
- else
- {
- if ((c>=48 && c<=57))
- {
- }
- else
- {
- ok = 0;
- break;
- }
- }
- }
- return ok;
- }
- int StringUtil::IsFloatNumber(std::string& s)
- {
- char c;
- int ok = 0;
- int size = s.length();
- int got_decimal = 0;
- const char * cptr = s.c_str();
- for (int i=0;i<size;i++){
- c = cptr[i];
- if (i == 0)
- {
- if (c == '.')
- {
- ok++;
- got_decimal++;
- }
- else if (c == '-')
- {
- ok++;
- }
- else if ((c>=48) && (c<=57))
- {
- ok++;
- }
- else
- {
- break;
- }
- }
- else
- {
- if (c == '.' && !got_decimal)
- {
- got_decimal++;
- }
- else if ((c>=48 && c<=57))
- {
- }
- else
- {
- ok = 0;
- break;
- }
- }
- }
- return ok;
- }
- int StringUtil::IsPositiveIntNumber(std::string& s)
- {
- char c;
- int ok = 0;
- int size = s.length();
- const char * cptr = s.c_str();
- for (int i=0;i<size;i++){
- c = cptr[i];
- if (i == 0)
- {
- if ((c>=48) && (c<=57))
- {
- ok++;
- }
- else
- {
- break;
- }
- }
- else
- {
- if ((c>=48 && c<=57))
- {
- }
- else
- {
- ok = 0;
- break;
- }
- }
- }
- return ok;
- }
- int StringUtil::IsPositiveFloatNumber(std::string& s)
- {
- char c;
- int ok = 0;
- int size = s.length();
- int got_decimal = 0;
- const char * cptr = s.c_str();
- for (int i=0;i<size;i++){
- c = cptr[i];
- if (i == 0)
- {
- if (c == '.')
- {
- ok++;
- got_decimal++;
- }
- else if ((c>=48) && (c<=57))
- {
- ok++;
- }
- else
- {
- break;
- }
- }
- else
- {
- if (c == '.' && !got_decimal)
- {
- got_decimal++;
- }
- else if ((c>=48 && c<=57))
- {
- }
- else
- {
- ok = 0;
- break;
- }
- }
- }
- return ok;
- }
- void StringUtil::StripLeadingSpaces(std::string& s)
- {
- std::string result = "";
- const char * ptr = s.c_str();
- int length = s.length();
- int start = 0;
- int i;
- char c;
- for (i=0;i<length;i++){
- c = ptr[i];
- if (c != ' ')
- {
- start = i;
- break;
- }
- }
- for (i=start;i<length;i++){
- c = ptr[i];
- result += c;
- }
- s = result.c_str();
- }
- void StringUtil::StripTrailingSpaces(std::string& s)
- {
- std::string result = "";
- const char * ptr = s.c_str();
- int length = s.length();
- int end = length-1;
- int i;
- char c;
- for (i=end;i>=0;i--){
- c = ptr[i];
- if (c != ' ')
- {
- end = i;
- break;
- }
- }
- for (i=0;i<=end;i++){
- c = ptr[i];
- result += c;
- }
- s = result.c_str();
- }
- void StringUtil::StripSpaces(std::string& s)
- {
- StripLeadingSpaces(s);
- StripTrailingSpaces(s);
- }