1
0
Fork 0

DBG: removed crappy UString class and moved string manipulation/conversion functions in a StringUtils class

This commit is contained in:
Mr. eXoDia 2014-11-15 14:56:16 +01:00
parent 51401255d3
commit 021bc4d778
27 changed files with 178 additions and 1463 deletions

View File

@ -1,50 +0,0 @@
/**
* UTF8 string library.
*
* Allows to use native UTF8 sequences as a string class. Has many overloaded
* operators that provides such features as concatenation, types converting and
* much more.
*
* Distributed under GPL v3
*
* Author:
* Grigory Gorelov (gorelov@grigory.info)
* See more information on grigory.info
*/
#include "Exception.h"
UTF8::Exception::Exception(const std::string & error, const int & StatusCode)
{
this->error = error;
this->StatusCode = StatusCode;
}
UTF8::Exception::Exception(std::string error)
{
this->error = error;
this->StatusCode = UnspecifiedException;
}
UTF8::Exception::Exception(const UTF8::Exception & e)
{
error = e.error;
StatusCode = e.StatusCode;
}
std::string UTF8::Exception::GetErrorString() const
{
return error;
}
int UTF8::Exception::GetErrorCode() const
{
return StatusCode;
}
UTF8::Exception & UTF8::Exception::operator =(const UTF8::Exception & e)
{
error = e.error;
error = e.StatusCode;
return *this;
}

View File

@ -1,64 +0,0 @@
/**
* UTF8 string library.
*
* Allows to use native UTF8 sequences as a string class. Has many overloaded
* operators that provides such features as concatenation, types converting and
* much more.
*
* Distributed under GPL v3
*
* Author:
* Grigory Gorelov (gorelov@grigory.info)
* See more information on grigory.info
*/
#ifndef _UTF8_Exception_H
#define _UTF8_Exception_H
#include <string>
namespace UTF8
{
/**
* Exception class. When something bad happens it is thowed by UTF8::String.
*/
class Exception
{
public:
enum
{
UnspecifiedException = 1,
StringToIntConversionError = 2,
StringToDoubleConversionError = 3,
FileNotFound = 4,
StringIsNotACharacter = 5
};
/**
* Just a constructor
*/
Exception(std::string error);
/// Just a constructor
Exception(const std::string & error, const int & StatusCode);
/// Copying constructor
Exception(const Exception & e);
/// Returns error string
std::string GetErrorString() const;
/// Returns error code
int GetErrorCode() const;
/// Assing operator
Exception & operator =(const Exception &);
private:
std::string error;
int StatusCode;
};
}
#endif /* _EXCEPTION_H */

View File

@ -1,883 +0,0 @@
/**
* UTF8 string library.
*
* Allows to use native UTF8 sequences as a string class. Has many overloaded
* operators that provides such features as concatenation, types converting and
* much more.
*
* Distributed under GPL v3
*
* Author:
* Grigory Gorelov (gorelov@grigory.info)
* See more information on grigory.info
*/
#include "String.h"
#include <string>
#include <sstream>
#include <iostream>
#include <stdlib.h>
#include <ostream>
#include <stdint.h>
#include <errno.h>
#include "Exception.h"
bool UTF8::String::HasThisString(const UTF8::String & Str) const
{
return GetSubstringPosition(Str) != -1;
}
bool UTF8::String::CharacterIsOneOfThese(const UTF8::String & Characters) const
{
if(Length() == 1)
{
for(unsigned int i = 0; i < Characters.Length(); i++)
{
if(Characters[i] == *this)
{
return true;
}
}
return false;
}
else
{
throw Exception("[CharacterIsOneOfThese] String is more then one character length: \"" + ToString() + "\"", UTF8::Exception::StringIsNotACharacter);
}
}
UTF8::String UTF8::String::FromFile(const UTF8::String & Path)
{
UTF8::String s;
std::ifstream File;
File.open(Path.ToConstCharPtr());
if(File.is_open())
{
File.seekg(0, std::ios::end);
size_t Length = (size_t)File.tellg();
File.seekg(0, std::ios::beg);
char* buf = new char[Length + 1];
memset(buf, 0, Length + 1);
File.read(buf, Length);
s.AppendString(buf);
delete[] buf;
}
else
{
throw Exception("Cannot open file \"" + Path.ToString() + "\"", UTF8::Exception::FileNotFound);
}
File.close();
return s;
}
long UTF8::String::Search(const UTF8::String & SubString, unsigned int StartPosition, int Direction) const
{
unsigned int SubstringLength = SubString.Length();
unsigned int n = StartPosition;
if(n > Length() - SubstringLength)
{
if(Direction == SearchDirectionFromLeftToRight)
{
return -1;
}
else
{
n = Length() - SubstringLength;
}
}
if((int)n < 0)
{
if(Direction == SearchDirectionFromRightToLeft)
{
return -1;
}
else
{
n = 0;
}
}
while(((Direction == SearchDirectionFromLeftToRight) && (n < Length() - SubstringLength + 1)) || ((Direction == SearchDirectionFromRightToLeft) && ((int)n >= 0)))
{
if(this->Substring(n, SubstringLength) == SubString)
{
return n;
}
n += Direction == SearchDirectionFromLeftToRight ? 1 : -1;
}
return -1;
}
std::ostream & operator<<(std::ostream & os, const UTF8::String & s)
{
os << s.ToString();
return os;
}
bool operator==(const char* str, const UTF8::String & StringObj)
{
return StringObj == str;
}
bool operator==(const std::string & str, const UTF8::String & StringObj)
{
return StringObj == str;
}
bool operator!=(const char* str, const UTF8::String & StringObj)
{
return StringObj != str;
}
bool operator!=(const std::string & str, const UTF8::String & StringObj)
{
return StringObj != str;
}
UTF8::String UTF8::String::Quote() const
{
return "\"" + (*this) + "\"";
}
UTF8::String UTF8::String::Trim() const
{
UTF8::String result = *this;
unsigned int i = 0;
while((result[i] == " ") || (result[i] == "\n") || (result[i] == "\r") || (result[i] == "\t"))
{
i++;
}
if(i == result.Length())
{
return UTF8::String();
}
long j = result.Length();
while((result[j - 1] == " ") || (result[j - 1] == "\n") || (result[j - 1] == "\r") || (result[j - 1] == "\t"))
{
j--;
}
result = result.Substring(i, j - i);
return result;
}
UTF8::String UTF8::String::Replace(const UTF8::String & Search, const UTF8::String & Replace) const
{
UTF8::String result = *this;
// Long to cover unsigned int and -1
long pos = 0;
while((pos = result.Search(Search, pos)) != -1)
{
result = result.SubstringReplace(pos, Search.Length(), Replace);
// Next time we search after replacement
pos += Replace.Length();
}
return result;
}
UTF8::String UTF8::String::SubstringReplace(unsigned int Start, unsigned int Count, const UTF8::String & Replace) const
{
if(Start < Length())
{
return (Start ? Substring(0, Start) : UTF8::String()) + Replace + Substring(Start + Count);
}
else
{
return *this;
}
}
UTF8::String UTF8::String::Implode(const std::vector <UTF8::String> & Strings, const UTF8::String & Separator)
{
if(Strings.size())
{
UTF8::String Result;
for(unsigned int i = 0; i < Strings.size(); i++)
{
if(Result.Length())
{
Result += Separator;
}
Result += Strings[i];
}
return Result;
}
else
{
return UTF8::String();
}
}
std::vector <UTF8::String> UTF8::String::Explode(const String & Separator) const
{
std::vector <UTF8::String> v;
unsigned int prev = 0;
unsigned int i = 0;
while(i < Length() - Separator.Length() + 1)
{
if(Substring(i, Separator.Length()) == Separator)
{
if(i - prev > 0)
{
v.push_back(Substring(prev, i - prev));
}
i += Separator.Length();
prev = i;
}
else
{
i++;
}
}
if(prev < Length())
{
v.push_back(Substring(prev, Length() - prev));
}
return v;
}
UTF8::String operator+(const char* CharPtr, const UTF8::String & StringObj)
{
UTF8::String s(CharPtr);
s += StringObj;
return s;
}
UTF8::String operator+(const std::string & str, const UTF8::String & StringObj)
{
UTF8::String s(str);
s += StringObj;
return s;
}
UTF8::String UTF8::String::operator+(const UTF8::String & s) const
{
UTF8::String res(*this);
res.AppendString(s.Data);
return res;
}
UTF8::String & UTF8::String::operator+=(const UTF8::String & s)
{
AppendString(s.Data);
return *this;
}
void UTF8::String::AppendString(const char* str)
{
// The functions that can fill buffer directly:
//
// SetString AppendString
//
// Make sure all preparations are done there
if(str && strlen(str))
{
if(DataArrayLength)
{
CheckIfStringIsCorrect(str);
unsigned int StrLength = (unsigned int)strlen(str);
Data = (char*) realloc(Data, DataArrayLength + StrLength + 1);
if(Data != NULL)
{
memcpy(Data + DataArrayLength, str, StrLength);
DataArrayLength += StrLength;
Data[DataArrayLength] = 0;
CalculateStringLength();
}
else
{
throw Exception("[AppendString] Cannot realloc any more memory");
}
}
else
{
SetString(str);
}
}
}
void UTF8::String::SetString(const char* str)
{
// The functions that can fill buffer directly:
//
// SetString AppendString
//
// Make sure all preparations are done there
if(str && strlen(str))
{
CheckIfStringIsCorrect(str);
Empty();
DataArrayLength = (unsigned int)strlen(str);
Data = new char[DataArrayLength + 1];
Data[DataArrayLength] = 0;
memcpy(Data, str, DataArrayLength);
CalculateStringLength();
}
else
{
Empty();
}
}
void UTF8::String::ConvertFromInt64(int64_t n)
{
Empty();
if(n)
{
bool minus;
if(n < 0)
{
n = -n;
minus = true;
}
else
{
minus = false;
}
char tmp[32] = "0";
const char* num = "0123456789";
memset(tmp, 0, 32);
unsigned int i = 30;
while(n)
{
tmp[i] = num[n % 10];
n /= 10;
i--;
if(((int)i < 0) || ((i < 1) && minus))
{
throw Exception("[ConvertFromInt] Cycle terminated, buffer overflow.");
}
}
if(minus)
{
tmp[i] = '-';
i--;
}
SetString(tmp + i + 1);
}
else
{
SetString("0");
}
CalculateStringLength();
}
void UTF8::String::InitString()
{
Data = NULL;
DataArrayLength = 0;
StringLength = 0;
}
UTF8::String::String()
{
InitString();
}
UTF8::String::String(const std::string & s)
{
InitString();
CheckIfStringIsCorrect(s.c_str());
AppendString(s.c_str());
CalculateStringLength();
}
int UTF8::String::GetSymbolIndexInDataArray(unsigned int Position) const
{
if(Position >= StringLength)
{
throw Exception(UTF8::String("[GetSymbolIndexInDataArray] trying to get position beyond the end of string."));
}
unsigned int n = 0;
for(unsigned int i = 0; i < Position; i++)
{
n += GetSequenceLength(Data + n);
}
return n;
}
long UTF8::String::GetSubstringPosition(const UTF8::String & SubString, unsigned int Start) const
{
if(SubString.Length() > StringLength)
{
return -1;
}
unsigned int ScansCount = StringLength - SubString.StringLength + 1 - Start;
for(unsigned int i = 0; i < ScansCount; i++)
{
if(this->Substring(i + Start, SubString.StringLength) == SubString)
{
return i + Start;
}
}
return -1;
}
UTF8::String UTF8::String::Substring(unsigned int Start, unsigned int Count) const
{
if(Start >= StringLength)
{
return UTF8::String();
}
if((Start + Count > StringLength) || (Count == 0))
{
Count = StringLength - Start;
}
unsigned int StartIndex = GetSymbolIndexInDataArray(Start);
unsigned int CopyAmount = 0;
for(unsigned int i = 0; i < Count; i++)
{
CopyAmount += GetSequenceLength(Data + StartIndex + CopyAmount);
}
char* tmp = new char[CopyAmount + 1];
memcpy(tmp, Data + StartIndex, CopyAmount);
tmp[CopyAmount] = 0;
UTF8::String r(tmp);
delete[] tmp;
return r;
}
UTF8::String::String(const char* str)
{
InitString();
SetString(str);
}
UTF8::String::String(const uint32_t* str)
{
InitString();
ConvertFromUTF32(str);
}
void UTF8::String::ConvertFromUTF32(const uint32_t* s)
{
if(s)
{
unsigned int WideStringLength = 0;
do
{
WideStringLength++;
if(WideStringLength == 4294967295UL)
{
throw Exception("[ConvertFromUTF32] Cannot find termination symbol in incoming string.");
}
}
while(s[WideStringLength]);
char* tmp = new char[WideStringLength * 4 + 1];
memset(tmp, 0, WideStringLength * 4 + 1);
unsigned int pos = 0;
for(unsigned int i = 0; i < WideStringLength; i++)
{
uint32_t wc = s[i];
if(wc < 0x80)
{
tmp[pos++] = wc;
}
else if(wc < 0x800)
{
tmp[pos++] = (wc >> 6) | 0xC0 /* 0b11000000 */;
tmp[pos++] = (wc & 0x3F /* 0b00111111 */) | 0x80 /* 0b10000000 */;
}
else if(wc < 0x10000)
{
tmp[pos++] = (wc >> 12) | 0xE0 /* 0b11100000 */;
tmp[pos++] = ((wc >> 6) & 0x3F /* 0b00111111 */) | 0x80 /* 0b10000000 */;
tmp[pos++] = (wc & 0x3F /* 0b00111111 */) | 0x80 /* 0b10000000 */;
}
else
{
tmp[pos++] = (wc >> 18) | 0xF0 /* 0b11110000 */;
tmp[pos++] = ((wc >> 12) & 0x3F /* 0b00111111 */) | 0x80 /* 0b10000000 */;
tmp[pos++] = ((wc >> 6) & 0x3F /* 0b00111111 */) | 0x80 /* 0b10000000 */;
tmp[pos++] = (wc & 0x3F /* 0b00111111 */) | 0x80 /* 0b10000000 */;
}
}
SetString(tmp);
delete[] tmp;
}
}
void UTF8::String::CalculateStringLength()
{
// We are not writing anything to memory so limits are not needed
if(Data)
{
unsigned int n = 0, count = 0;
do
{
// We do not need to check line end here, it is checked when string is changed
n += GetSequenceLength(Data + n);
count++;
}
while(Data[n]);
StringLength = count;
}
else
{
StringLength = 0;
}
}
void UTF8::String::CheckIfStringIsCorrect(const char* str) const
{
if(str)
{
// We are not writing anything to memory so limits are not needed
unsigned int n = 0, i;
unsigned int SequenceLength;
while(str[n])
{
SequenceLength = GetSequenceLength(str + n);
for(i = 1; i < SequenceLength; i++)
{
if((((unsigned char) str[n + i]) >> 6) != 0x2 /* 0b00000010 */)
{
std::string s(str);
throw Exception("[CheckIfStringIsCorrect] Incorrect byte in UTF8 sequence: \"" + s + "\"");
}
}
n += SequenceLength;
if(n >= 0xFFFFFFFF - 4)
{
std::string s(str);
throw Exception("[CheckIfStringIsCorrect] termination char was not found in string: \"" + s + "\"");
}
}
}
}
bool UTF8::String::operator>(const UTF8::String & s) const
{
if(*this == s)
{
return false;
}
if(*this < s)
{
return false;
}
return true;
}
bool UTF8::String::operator<(const UTF8::String & s) const
{
unsigned int MinLength = StringLength < s.StringLength ? StringLength : s.StringLength;
//std::cout << "MinLength=" << MinLength;
unsigned int MyPos = 0, RemotePos = 0;
unsigned int MySequenceLength, RemoteSequenceLength;
for(unsigned int i = 0; i < MinLength; i++)
{
MySequenceLength = GetSequenceLength(Data + MyPos);
RemoteSequenceLength = GetSequenceLength(s.Data + RemotePos);
if(MySequenceLength < RemoteSequenceLength)
{
return true;
}
if(MySequenceLength > RemoteSequenceLength)
{
return false;
}
for(unsigned int j = 0; j < MySequenceLength; j++)
{
if(Data[MyPos + j] < s.Data[RemotePos + j])
{
return true;
}
if(Data[MyPos + j] > s.Data[RemotePos + j])
{
return false;
}
}
MyPos += MySequenceLength;
RemotePos += RemoteSequenceLength;
}
// If this string is substring of s (from left side) then it is lower
return StringLength < s.StringLength;
}
UTF8::String UTF8::String::operator[](unsigned int const n) const
{
if(n >= StringLength)
{
return UTF8::String();
}
if((int)n < 0)
{
return UTF8::String();
}
unsigned int pos = 0;
for(unsigned int i = 0; i < n; i++)
{
pos += GetSequenceLength(Data + pos);
}
char t[5];
memset(t, 0, 5);
memcpy(t, Data + pos, GetSequenceLength(Data + pos));
return UTF8::String(t);
}
unsigned int UTF8::String::GetSequenceLength(const char* StartByte) const
{
if(StartByte && strlen(StartByte))
{
unsigned char Byte = StartByte[0];
if(Byte < 128)
{
return 1;
}
// Here we need back order due to mask operation
if((Byte >> 5) == 0x6 /* 0b00000110 */)
{
return 2;
}
if((Byte >> 4) == 0xE /* 0b00001110 */)
{
return 3;
}
if((Byte >> 3) == 0x1E /* 0b00011110 */)
{
return 4;
}
throw Exception(std::string("[GetSequenceLength] Invalid UTF8 start byte. My own string is: [") + Data + "] Argument is: [" + StartByte + "]");
}
else
{
if(StartByte == 0)
StartByte = "(null)";
throw Exception(std::string("[GetSequenceLength] Invalid UTF8 start byte (it is empty). My own string is: [") + Data + "] Argument is: [" + StartByte + "]");
}
}
UTF8::String & UTF8::String::operator=(const String & Original)
{
// Check if objects are not same
if((unsigned int long) &Original != (unsigned int long) this)
{
Empty();
SetString(Original.Data);
}
return *this;
}
UTF8::String & UTF8::String::operator=(const char* str)
{
Empty();
SetString(str);
return *this;
}
UTF8::String & UTF8::String::operator=(const uint32_t* str)
{
Empty();
ConvertFromUTF32(str);
return *this;
}
UTF8::String::operator std::string() const
{
return this->ToString();
}
void UTF8::String::Empty()
{
if(DataArrayLength)
{
delete Data;
InitString();
}
}
std::string UTF8::String::ToString() const
{
if(DataArrayLength)
{
return std::string(Data);
}
else
{
return std::string();
}
}
UTF8::String UTF8::String::operator+(const char* s) const
{
UTF8::String res(*this);
res.AppendString(s);
return res;
}
bool UTF8::String::operator==(const UTF8::String & s) const
{
if(DataArrayLength != s.DataArrayLength)
{
return false;
}
else
{
for(unsigned int i = 0; i < DataArrayLength; i++)
{
if(Data[i] != s.Data[i])
{
return false;
}
}
return true;
}
}
bool UTF8::String::operator!=(const UTF8::String & s) const
{
return !(*this == s);
}
bool UTF8::String::operator==(const char* str) const
{
if(str && strlen(str))
{
if(DataArrayLength != strlen(str))
{
return false;
}
else
{
for(unsigned int i = 0; i < DataArrayLength; i++)
{
if(Data[i] != str[i])
{
return false;
}
}
return true;
}
}
else
{
return StringLength == 0;
}
}
bool UTF8::String::operator!=(const char* str) const
{
return !(*this == str);
}
const char* UTF8::String::ToConstCharPtr() const
{
return Data;
}
const char* UTF8::String::c_str() const
{
return Data;
}
unsigned int UTF8::String::Length() const
{
return StringLength;
}
unsigned int UTF8::String::DataLength() const
{
return DataArrayLength;
}
UTF8::String::~String()
{
Empty();
}
UTF8::String::String(const String & orig)
{
InitString();
SetString(orig.Data);
}

View File

@ -1,309 +0,0 @@
/**
* UTF8 string library.
*
* Allows to use native UTF8 sequences as a string class. Has many overloaded
* operators that provides such features as concatenation, types converting and
* much more.
*
* Distributed under GPL v3
*
* Author:
* Grigory Gorelov (gorelov@grigory.info)
* See more information on grigory.info
*/
#ifndef _UTF8_String_H
#define _UTF8_String_H
#include "Exception.h"
#include <string.h>
#include <stdint.h>
#include <fstream>
#include <vector>
namespace UTF8
{
/**
* The only string class containing everything to work with UTF8 strings
*/
class String
{
public:
static const int SearchDirectionFromLeftToRight = 1;
static const int SearchDirectionFromRightToLeft = 2;
/**
* Search substring in string
* @param StartPosition Position to start search
* @param Direction Search forward or backward, uses SearchDirectionFromLeftToRight and SearchDirectionFromRightToLeft
* @return Returns position of substring if found. Otherwise returns -1
*/
long Search(const UTF8::String & SubString, unsigned int StartPosition = 0, int Direction = SearchDirectionFromLeftToRight) const;
/// Simple constructor only initiates buffers
String();
/**
* Create string object from UTF8 char * string
*/
String(const char* str);
/**
* Create string object from UTF-32 string
*/
String(const uint32_t*);
/**
* Create string object from UTF8 std::string
*/
String(const std::string &);
/**
* Copying constructor. Feel free to such things UTF8::String s2=s1;
*/
String(const String & orig);
/**
* Deconstructor.
*/
~String();
/**
* Converts UTF8::String to std::string
*/
std::string ToString() const;
/**
* Reads content from a file and returns as UTF8::String
*/
static String FromFile(const UTF8::String & Path);
/**
* Converts UTF8::String to const char *
*/
const char* ToConstCharPtr() const;
/**
* Converts UTF8::String to const char *
*/
const char* c_str() const;
/**
* Separates string using given separator and returns vector
*/
std::vector <String> Explode(const String & Separator) const;
/**
* Creating String from array of String adding separator between them.
*/
static String Implode(const std::vector <String> & Strings, const String & Separator);
/**
* Sum operator. Provides String1+String2 exression.
*/
String operator+(const String &) const;
/**
* Sum operator. Provides String1+"Str" exression.
*/
String operator+(const char*) const;
/**
* Unary sum operator. Provides String1+=String2 expression.
*/
String & operator+=(const String &);
/**
* Assign operator. Provides String1=String2 expression.
*/
String & operator=(const String &);
/**
* Assign operator. Provides String1="New value" expression.
*/
String & operator=(const char*);
/**
* Assign operator. Provides String1=(uint32_t*) UTF32_StringPointer expression.
* Automatically converts UNICODE to UTF-8 ans stores in itself
*/
String & operator=(const uint32_t*);
/**
* Provides std::string test=String expression.
*/
operator std::string() const;
/**
* Returns substring of current string.
* @param Start Start position of substring
* @param Count Number of sybmols after start position. If number==0 string from Start till end is returned.
*/
String Substring(unsigned int Start, unsigned int Count = 0) const;
/**
* Replaces one text peace by another and returns result
* @param Search Search string
* @param Replace Replace string
* @return Returns result of replacement
*/
String Replace(const String & Search, const String & Replace) const;
/**
* Returns trimmed string. Removes whitespaces from left and right
*/
String Trim() const;
/**
* Returns string with nice quotes like this « ».
*/
String Quote() const;
/**
* Replaces region of string by text peace and returns result.
* @param Search Search string
* @param Replace Replace string
* @return Returns result of replacement
*/
String SubstringReplace(unsigned int Start, unsigned int Count, const String & Replace) const;
/**
* Returns position of substring in current string.
* @param Start Position to start search. Default is 0.
* @return If substring not found returns -1.
*/
long GetSubstringPosition(const UTF8::String & SubString, unsigned int Start = 0) const;
/**
* Get one char operator. Provides UTF8::String c=String1[1];
*/
String operator[](unsigned int const) const;
/**
* Test operator. Provides String1==String2 expression.
*/
bool operator==(const UTF8::String &) const;
/**
* Test operator. Provides String1!=String2 expression.
*/
bool operator!=(const UTF8::String &) const;
/**
* Test operator. Provides String1=="Test" expression.
*/
bool operator==(const char*) const;
/**
* Test operator. Provides String1!="Test" expression.
*/
bool operator!=(const char*) const;
/** Test operator. Provides String1<String2 expression.
* Operator compares left characters of two strings.
* If String1[0] value is less then the String2[0] returns true.
* If they are equal then goes to second character and so on.
* Can be used to sort strings alphabetical.
*/
bool operator<(const UTF8::String &) const;
/** Test operator. Provides String1>String2 expression.
* Operator compares left characters of two strings.
* If String1[0] value is greater then the String2[0] returns true.
* If they are equal then goes to second character and so on.
* Can be used to sort strings alphabetical.
*/
bool operator>(const UTF8::String &) const;
/**
* Returns current string length. Also see DataLength to get buffer
* size
*/
unsigned int Length() const;
/**
* Returns current char data array length, containig UTF8 string.
* As one character in UTF8 can be stored by more then one byte use
* this function to know how much memory allocated for the string.
*/
unsigned int DataLength() const;
/**
* Clears current string as if it is just created
*/
void Empty();
/**
* If string is a one character check if it is one of given
*/
bool CharacterIsOneOfThese(const UTF8::String & Characters) const;
/**
* Checks if this string contains given another string
*/
bool HasThisString(const UTF8::String & Str) const;
/**
* Special function to convert from very big integers
* Normally it is ok to assing UTF8::String to number. Or construct from it.
* This function exists only for very very big integers conversion.
*/
void ConvertFromInt64(int64_t n);
private:
char* Data;
unsigned int DataArrayLength;
unsigned int StringLength;
unsigned int GetSequenceLength(const char* StartByte) const;
void CheckIfStringIsCorrect(const char* str) const;
void CalculateStringLength();
void InitString();
void AppendString(const char* str);
void SetString(const char* str);
int GetSymbolIndexInDataArray(unsigned int Position) const;
void ConvertFromUTF32(const uint32_t*);
};
}
/**
* Not in class overloaded operator +. Provides "Sample"+String1 expression.
*/
UTF8::String operator+(const char*, const UTF8::String &);
/**
* Not in class overloaded operator +. Provides std::string("123")+String1 expression.
*/
UTF8::String operator+(const std::string &, const UTF8::String &);
/**
* Not in class overloaded operator ==. Provides "Test"==String1 expression.
*/
bool operator==(const char*, const UTF8::String &);
/**
* Not in class overloaded operator ==. Provides std::string==String1 expression.
*/
bool operator==(const std::string &, const UTF8::String &);
/**
* Not in class overloaded operator !=. Provides "Test"!=String1 expression.
*/
bool operator!=(const char*, const UTF8::String &);
/**
* Not in class overloaded operator !=. Provides std::string!=String1 expression.
*/
bool operator!=(const std::string &, const UTF8::String &);
/**
* Overloading for cout. Provides std::cout << (UTF8::String) operation;
*/
std::ostream & operator<<(std::ostream & os, const UTF8::String & s);
#endif /* _UTF8STRING_H */

View File

@ -1,29 +0,0 @@
#include "UString.h"
#include <windows.h>
//Functions taken from: http://www.nubaria.com/en/blog/?p=289
UString ConvertUtf16ToUtf8(const std::wstring & wstr)
{
std::string convertedString;
int requiredSize = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, 0, 0, 0, 0);
if(requiredSize > 0)
{
std::vector<char> buffer(requiredSize);
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &buffer[0], requiredSize, 0, 0);
convertedString.assign(buffer.begin(), buffer.end() - 1);
}
return convertedString;
}
std::wstring ConvertUtf8ToUtf16(const UString & str)
{
std::wstring convertedString;
int requiredSize = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, 0, 0);
if(requiredSize > 0)
{
std::vector<wchar_t> buffer(requiredSize);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &buffer[0], requiredSize);
convertedString.assign(buffer.begin(), buffer.end() - 1);
}
return convertedString;
}

View File

@ -1,12 +0,0 @@
#ifndef _USTRING_H
#define _USTRING_H
#include "String.h"
#include <string>
typedef UTF8::String UString;
UString ConvertUtf16ToUtf8(const std::wstring & wstr);
std::wstring ConvertUtf8ToUtf16(const UString & str);
#endif // _USTRING_H

View File

@ -100,7 +100,7 @@ static int _modpathfromaddr(duint addr, char* path, int size)
*path = '\0';
return 0;
}
strcpy_s(path, size, ConvertUtf16ToUtf8(wszModPath).c_str());
strcpy_s(path, size, StringUtils::Utf16ToUtf8(wszModPath()).c_str());
return (int)strlen(path);
}

View File

@ -173,8 +173,8 @@ extern "C" DLL_EXPORT bool _dbg_addrinfoget(duint addr, SEGMENTREG segment, ADDR
else if(!bOnlyCipAutoComments || addr == GetContextDataEx(hActiveThread, UE_CIP)) //no line number
{
DISASM_INSTR instr;
std::string temp_string;
std::string comment;
String temp_string;
String comment;
ADDRINFO newinfo;
char ascii[256 * 2] = "";
char unicode[256 * 2] = "";

View File

@ -141,13 +141,13 @@ void formatdec(char* string)
bool FileExists(const char* file)
{
DWORD attrib = GetFileAttributesW(ConvertUtf8ToUtf16(file).c_str());
DWORD attrib = GetFileAttributesW(StringUtils::Utf8ToUtf16(file).c_str());
return (attrib != INVALID_FILE_ATTRIBUTES && !(attrib & FILE_ATTRIBUTE_DIRECTORY));
}
bool DirExists(const char* dir)
{
DWORD attrib = GetFileAttributesW(ConvertUtf8ToUtf16(dir).c_str());
DWORD attrib = GetFileAttributesW(StringUtils::Utf8ToUtf16(dir).c_str());
return (attrib == FILE_ATTRIBUTE_DIRECTORY);
}
@ -156,7 +156,7 @@ bool GetFileNameFromHandle(HANDLE hFile, char* szFileName)
wchar_t wszFileName[MAX_PATH] = L"";
if(!PathFromFileHandleW(hFile, wszFileName, sizeof(wszFileName)))
return false;
strcpy_s(szFileName, MAX_PATH, ConvertUtf16ToUtf8(wszFileName).c_str());
strcpy_s(szFileName, MAX_PATH, StringUtils::Utf16ToUtf8(wszFileName).c_str());
return true;
}
@ -173,7 +173,7 @@ bool settingboolget(const char* section, const char* name)
arch GetFileArchitecture(const char* szFileName)
{
arch retval = notfound;
HANDLE hFile = CreateFileW(ConvertUtf8ToUtf16(szFileName).c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
HANDLE hFile = CreateFileW(StringUtils::Utf8ToUtf16(szFileName).c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if(hFile != INVALID_HANDLE_VALUE)
{
unsigned char data[0x1000];

View File

@ -21,7 +21,7 @@
#include "jansson\jansson.h"
#include "DeviceNameResolver\DeviceNameResolver.h"
#include "handle.h"
#include "UString\UString.h"
#include "stringutils.h"
#ifdef __GNUC__
#include "dbghelp\dbghelp.h"

View File

@ -28,7 +28,7 @@ void dbsave()
functioncachesave(root);
loopcachesave(root);
bpcachesave(root);
std::wstring wdbpath = ConvertUtf8ToUtf16(dbpath);
WString wdbpath = StringUtils::Utf8ToUtf16(dbpath);
if(json_object_size(root))
{
FILE* jsonFile = 0;
@ -59,7 +59,7 @@ void dbload()
return;
dprintf("loading database...");
DWORD ticks = GetTickCount();
std::wstring wdbpath = ConvertUtf8ToUtf16(dbpath);
WString wdbpath = StringUtils::Utf8ToUtf16(dbpath);
LZ4_STATUS status = LZ4_decompress_fileW(wdbpath.c_str(), wdbpath.c_str());
if(status != LZ4_SUCCESS && status != LZ4_INVALID_ARCHIVE)
{
@ -139,7 +139,7 @@ bool modload(uint base, uint size, const char* fullpath)
DWORD LoadedSize;
HANDLE FileMap;
ULONG_PTR FileMapVA;
std::wstring wszFullPath = ConvertUtf8ToUtf16(fullpath);
WString wszFullPath = StringUtils::Utf8ToUtf16(fullpath);
if(StaticFileLoadW(wszFullPath.c_str(), UE_ACCESS_READ, false, &FileHandle, &LoadedSize, &FileMap, &FileMapVA))
{
info.entry = GetPE32DataFromMappedFile(FileMapVA, 0, UE_OEP) + info.base; //get entry point

View File

@ -1,6 +1,5 @@
#include "argument.h"
#include "console.h"
#include "UString/UString.h"
/*
formatarg:

View File

@ -656,7 +656,7 @@ static void cbCreateProcess(CREATE_PROCESS_DEBUG_INFO* CreateProcessInfo)
if(!DevicePathFromFileHandleW(CreateProcessInfo->hFile, wszFileName, sizeof(wszFileName)))
strcpy(DebugFileName, "??? (GetFileNameFromHandle failed!)");
else
strcpy_s(DebugFileName, MAX_PATH, ConvertUtf16ToUtf8(wszFileName).c_str());
strcpy_s(DebugFileName, MAX_PATH, StringUtils::Utf16ToUtf8(wszFileName).c_str());
}
dprintf("Process Started: "fhex" %s\n", base, DebugFileName);
@ -701,12 +701,12 @@ static void cbCreateProcess(CREATE_PROCESS_DEBUG_INFO* CreateProcessInfo)
if(settingboolget("Events", "TlsCallbacks"))
{
DWORD NumberOfCallBacks = 0;
TLSGrabCallBackDataW(ConvertUtf8ToUtf16(DebugFileName).c_str(), 0, &NumberOfCallBacks);
TLSGrabCallBackDataW(StringUtils::Utf8ToUtf16(DebugFileName).c_str(), 0, &NumberOfCallBacks);
if(NumberOfCallBacks)
{
dprintf("TLS Callbacks: %d\n", NumberOfCallBacks);
Memory<uint*> TLSCallBacks(NumberOfCallBacks * sizeof(uint), "cbCreateProcess:TLSCallBacks");
if(!TLSGrabCallBackDataW(ConvertUtf8ToUtf16(DebugFileName).c_str(), TLSCallBacks, &NumberOfCallBacks))
if(!TLSGrabCallBackDataW(StringUtils::Utf8ToUtf16(DebugFileName).c_str(), TLSCallBacks, &NumberOfCallBacks))
dputs("failed to get TLS callback addresses!");
else
{
@ -858,7 +858,7 @@ static void cbLoadDll(LOAD_DLL_DEBUG_INFO* LoadDll)
if(!DevicePathFromFileHandleW(LoadDll->hFile, wszFileName, sizeof(wszFileName)))
strcpy(DLLDebugFileName, "??? (GetFileNameFromHandle failed!)");
else
strcpy_s(DLLDebugFileName, MAX_PATH, ConvertUtf16ToUtf8(wszFileName).c_str());
strcpy_s(DLLDebugFileName, MAX_PATH, StringUtils::Utf16ToUtf8(wszFileName).c_str());
}
SymLoadModuleEx(fdProcessInfo->hProcess, LoadDll->hFile, DLLDebugFileName, 0, (DWORD64)base, 0, 0, 0);
IMAGEHLP_MODULE64 modInfo;
@ -1172,13 +1172,13 @@ DWORD WINAPI threadDebugLoop(void* lpParameter)
bSkipExceptions = false;
bBreakOnNextDll = false;
INIT_STRUCT* init = (INIT_STRUCT*)lpParameter;
bFileIsDll = IsFileDLLW(ConvertUtf8ToUtf16(init->exe).c_str(), 0);
pDebuggedEntry = GetPE32DataW(ConvertUtf8ToUtf16(init->exe).c_str(), 0, UE_OEP);
bFileIsDll = IsFileDLLW(StringUtils::Utf8ToUtf16(init->exe).c_str(), 0);
pDebuggedEntry = GetPE32DataW(StringUtils::Utf8ToUtf16(init->exe).c_str(), 0, UE_OEP);
strcpy_s(szFileName, init->exe);
if(bFileIsDll)
fdProcessInfo = (PROCESS_INFORMATION*)InitDLLDebugW(ConvertUtf8ToUtf16(init->exe).c_str(), false, ConvertUtf8ToUtf16(init->commandline).c_str(), ConvertUtf8ToUtf16(init->currentfolder).c_str(), 0);
fdProcessInfo = (PROCESS_INFORMATION*)InitDLLDebugW(StringUtils::Utf8ToUtf16(init->exe).c_str(), false, StringUtils::Utf8ToUtf16(init->commandline).c_str(), StringUtils::Utf8ToUtf16(init->currentfolder).c_str(), 0);
else
fdProcessInfo = (PROCESS_INFORMATION*)InitDebugW(ConvertUtf8ToUtf16(init->exe).c_str(), ConvertUtf8ToUtf16(init->commandline).c_str(), ConvertUtf8ToUtf16(init->currentfolder).c_str());
fdProcessInfo = (PROCESS_INFORMATION*)InitDebugW(StringUtils::Utf8ToUtf16(init->exe).c_str(), StringUtils::Utf8ToUtf16(init->commandline).c_str(), StringUtils::Utf8ToUtf16(init->currentfolder).c_str());
if(!fdProcessInfo)
{
fdProcessInfo = &g_pi;
@ -1560,7 +1560,7 @@ static bool readwritejitkey(wchar_t* jit_key_value, DWORD* jit_key_vale_size, ch
if(lRv != ERROR_SUCCESS)
return false;
lRv = RegSetValueExW(hKey, ConvertUtf8ToUtf16(key).c_str(), 0, REG_SZ, (BYTE*)jit_key_value, (DWORD)(*jit_key_vale_size) + 1);
lRv = RegSetValueExW(hKey, StringUtils::Utf8ToUtf16(key).c_str(), 0, REG_SZ, (BYTE*)jit_key_value, (DWORD)(*jit_key_vale_size) + 1);
}
else
{
@ -1572,7 +1572,7 @@ static bool readwritejitkey(wchar_t* jit_key_value, DWORD* jit_key_vale_size, ch
return false;
}
lRv = RegQueryValueExW(hKey, ConvertUtf8ToUtf16(key).c_str(), 0, NULL, (LPBYTE)jit_key_value, jit_key_vale_size);
lRv = RegQueryValueExW(hKey, StringUtils::Utf8ToUtf16(key).c_str(), 0, NULL, (LPBYTE)jit_key_value, jit_key_vale_size);
if(lRv != ERROR_SUCCESS)
{
if(error != NULL)
@ -1755,7 +1755,7 @@ bool dbggetjit(char jit_entry[JIT_ENTRY_MAX_SIZE], arch arch_in, arch* arch_out,
*rw_error_out = rw_error;
return false;
}
strcpy_s(jit_entry, JIT_ENTRY_MAX_SIZE, ConvertUtf16ToUtf8(wszJitEntry).c_str());
strcpy_s(jit_entry, JIT_ENTRY_MAX_SIZE, StringUtils::Utf16ToUtf8(wszJitEntry).c_str());
return true;
}
@ -1765,7 +1765,7 @@ bool dbggetdefjit(char* jit_entry)
path[0] = '"';
wchar_t wszPath[MAX_PATH] = L"";
GetModuleFileNameW(GetModuleHandleW(NULL), wszPath, MAX_PATH);
strcpy(&path[1], ConvertUtf16ToUtf8(wszPath).c_str());
strcpy(&path[1], StringUtils::Utf16ToUtf8(wszPath).c_str());
strcat(path, ATTACH_CMD_LINE);
strcpy(jit_entry, path);
return true;
@ -1775,7 +1775,7 @@ bool dbgsetjit(char* jit_cmd, arch arch_in, arch* arch_out, readwritejitkey_erro
{
DWORD jit_cmd_size = (DWORD)strlen(jit_cmd) * sizeof(wchar_t);
readwritejitkey_error_t rw_error;
if(!readwritejitkey((wchar_t*)ConvertUtf8ToUtf16(jit_cmd).c_str(), & jit_cmd_size, "Debugger", arch_in, arch_out, & rw_error, true))
if(!readwritejitkey((wchar_t*)StringUtils::Utf8ToUtf16(jit_cmd).c_str(), & jit_cmd_size, "Debugger", arch_in, arch_out, & rw_error, true))
{
if(rw_error_out != NULL)
*rw_error_out = rw_error;
@ -1812,7 +1812,7 @@ bool dbglistprocesses(std::vector<PROCESSENTRY32>* list)
continue;
wchar_t szExePath[MAX_PATH] = L"";
if(GetModuleFileNameExW(hProcess, 0, szExePath, MAX_PATH))
strcpy_s(pe32.szExeFile, ConvertUtf16ToUtf8(szExePath).c_str());
strcpy_s(pe32.szExeFile, StringUtils::Utf16ToUtf8(szExePath).c_str());
list->push_back(pe32);
}
while(Process32Next(hProcessSnap, &pe32));

View File

@ -25,7 +25,7 @@ CMDRESULT cbDebugInit(int argc, char* argv[])
dputs("file does not exist!");
return STATUS_ERROR;
}
HANDLE hFile = CreateFileW(ConvertUtf8ToUtf16(arg1).c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
HANDLE hFile = CreateFileW(StringUtils::Utf8ToUtf16(arg1).c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if(hFile == INVALID_HANDLE_VALUE)
{
dputs("could not open file!");
@ -892,7 +892,7 @@ CMDRESULT cbDebugAttach(int argc, char* argv[])
dprintf("could not get module filename %X!\n", pid);
return STATUS_ERROR;
}
strcpy_s(szFileName, ConvertUtf16ToUtf8(wszFileName).c_str());
strcpy_s(szFileName, StringUtils::Utf16ToUtf8(wszFileName).c_str());
CloseHandle(CreateThread(0, 0, threadAttachLoop, (void*)pid, 0, 0));
return STATUS_CONTINUE;
}
@ -1358,7 +1358,7 @@ CMDRESULT cbDebugDownloadSymbol(int argc, char* argv[])
return STATUS_ERROR;
}
char szModulePath[MAX_PATH] = "";
strcpy_s(szModulePath, ConvertUtf16ToUtf8(wszModulePath).c_str());
strcpy_s(szModulePath, StringUtils::Utf16ToUtf8(wszModulePath).c_str());
char szOldSearchPath[MAX_PATH] = "";
if(!SymGetSearchPath(fdProcessInfo->hProcess, szOldSearchPath, MAX_PATH)) //backup current search path
{

View File

@ -223,7 +223,7 @@ CMDRESULT cbInstrChd(int argc, char* argv[])
dputs("directory doesn't exist");
return STATUS_ERROR;
}
SetCurrentDirectoryW(ConvertUtf8ToUtf16(argv[1]).c_str());
SetCurrentDirectoryW(StringUtils::Utf8ToUtf16(argv[1]).c_str());
dputs("current directory changed!");
return STATUS_CONTINUE;
}

View File

@ -383,8 +383,8 @@ bool mathfromstring(const char* string, uint* value, bool silent, bool baseonly,
memset(strright, 0, len + 1);
strncpy(strleft, string - negative, highestop_pos + negative);
strcpy(strright, string + highestop_pos + 1);
strcpy(strleft, UString(strleft).Trim().c_str());
strcpy(strright, UString(strright).Trim().c_str());
strcpy(strleft, StringUtils::Trim(strleft).c_str());
strcpy(strright, StringUtils::Trim(strright).c_str());
//dprintf("left: %s, right: %s, op: %c\n", strleft(), strright(), string[highestop_pos]);
if(!*strright)
return false;

View File

@ -160,7 +160,7 @@ int patchfile(const PATCHINFO* patchlist, int count, const char* szFileName, cha
sprintf(error, "failed to get module path of module %s", modname);
return -1;
}
if(!CopyFileW(szOriginalName, ConvertUtf8ToUtf16(szFileName).c_str(), false))
if(!CopyFileW(szOriginalName, StringUtils::Utf8ToUtf16(szFileName).c_str(), false))
{
if(error)
strcpy(error, "failed to make a copy of the original file (patch target is in use?)");
@ -170,7 +170,7 @@ int patchfile(const PATCHINFO* patchlist, int count, const char* szFileName, cha
DWORD LoadedSize;
HANDLE FileMap;
ULONG_PTR FileMapVA;
if(StaticFileLoadW(ConvertUtf8ToUtf16(szFileName).c_str(), UE_ACCESS_ALL, false, &FileHandle, &LoadedSize, &FileMap, &FileMapVA))
if(StaticFileLoadW(StringUtils::Utf8ToUtf16(szFileName).c_str(), UE_ACCESS_ALL, false, &FileHandle, &LoadedSize, &FileMap, &FileMapVA))
{
int patched = 0;
for(int i = 0; i < count; i++)
@ -182,7 +182,7 @@ int patchfile(const PATCHINFO* patchlist, int count, const char* szFileName, cha
*ptr = patchlist[i].newbyte;
patched++;
}
if(!StaticFileUnloadW(ConvertUtf8ToUtf16(szFileName).c_str(), true, FileHandle, LoadedSize, FileMap, FileMapVA))
if(!StaticFileUnloadW(StringUtils::Utf8ToUtf16(szFileName).c_str(), true, FileHandle, LoadedSize, FileMap, FileMapVA))
{
if(error)
strcpy(error, "StaticFileUnload failed");

View File

@ -16,7 +16,7 @@ void pluginload(const char* pluginDir)
//load new plugins
wchar_t currentDir[deflen] = L"";
GetCurrentDirectoryW(deflen, currentDir);
SetCurrentDirectoryW(ConvertUtf8ToUtf16(pluginDir).c_str());
SetCurrentDirectoryW(StringUtils::Utf8ToUtf16(pluginDir).c_str());
char searchName[deflen] = "";
#ifdef _WIN64
sprintf(searchName, "%s\\*.dp64", pluginDir);
@ -24,7 +24,7 @@ void pluginload(const char* pluginDir)
sprintf(searchName, "%s\\*.dp32", pluginDir);
#endif // _WIN64
WIN32_FIND_DATAW foundData;
HANDLE hSearch = FindFirstFileW(ConvertUtf8ToUtf16(searchName).c_str(), &foundData);
HANDLE hSearch = FindFirstFileW(StringUtils::Utf8ToUtf16(searchName).c_str(), &foundData);
if(hSearch == INVALID_HANDLE_VALUE)
{
SetCurrentDirectoryW(currentDir);
@ -36,8 +36,8 @@ void pluginload(const char* pluginDir)
//set plugin data
pluginData.initStruct.pluginHandle = curPluginHandle;
char szPluginPath[MAX_PATH] = "";
sprintf_s(szPluginPath, "%s\\%s", pluginDir, ConvertUtf16ToUtf8(foundData.cFileName).c_str());
pluginData.hPlugin = LoadLibraryW(ConvertUtf8ToUtf16(szPluginPath).c_str()); //load the plugin library
sprintf_s(szPluginPath, "%s\\%s", pluginDir, StringUtils::Utf16ToUtf8(foundData.cFileName).c_str());
pluginData.hPlugin = LoadLibraryW(StringUtils::Utf8ToUtf16(szPluginPath).c_str()); //load the plugin library
if(!pluginData.hPlugin)
{
dprintf("[PLUGIN] Failed to load plugin: %s\n", foundData.cFileName);

View File

@ -62,7 +62,7 @@ static int scriptinternalstep(int fromIp) //internal step routine
static bool scriptcreatelinemap(const char* filename)
{
HANDLE hFile = CreateFileW(ConvertUtf8ToUtf16(filename).c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
HANDLE hFile = CreateFileW(StringUtils::Utf8ToUtf16(filename).c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if(hFile == INVALID_HANDLE_VALUE)
{
GuiScriptError(0, "CreateFile failed...");

View File

@ -0,0 +1,81 @@
#include "stringutils.h"
#include <windows.h>
#include <iostream>
#include <sstream>
std::vector<String> StringUtils::Split(const String & s, char delim, std::vector<String> & elems)
{
std::stringstream ss(s);
String item;
while(std::getline(ss, item, delim))
{
if(!item.length())
continue;
elems.push_back(item);
}
return elems;
}
std::vector<String> StringUtils::Split(const String & s, char delim)
{
std::vector<String> elems;
Split(s, delim, elems);
return elems;
}
//Trim functions taken from: http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring/16743707#16743707
const String StringUtils::WHITESPACE = " \n\r\t";
String StringUtils::Trim(const String & s)
{
return TrimRight(TrimLeft(s));
}
String StringUtils::TrimLeft(const String & s)
{
size_t startpos = s.find_first_not_of(StringUtils::WHITESPACE);
return (startpos == String::npos) ? "" : s.substr(startpos);
}
String StringUtils::TrimRight(const String & s)
{
size_t endpos = s.find_last_not_of(StringUtils::WHITESPACE);
return (endpos == String::npos) ? "" : s.substr(0, endpos + 1);
}
//Conversion functions taken from: http://www.nubaria.com/en/blog/?p=289
String StringUtils::Utf16ToUtf8(const WString & wstr)
{
String convertedString;
int requiredSize = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, 0, 0, 0, 0);
if(requiredSize > 0)
{
std::vector<char> buffer(requiredSize);
WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, &buffer[0], requiredSize, 0, 0);
convertedString.assign(buffer.begin(), buffer.end() - 1);
}
return convertedString;
}
String StringUtils::Utf16ToUtf8(const wchar_t* wstr)
{
return Utf16ToUtf8(wstr ? WString(wstr) : WString());
}
WString StringUtils::Utf8ToUtf16(const String & str)
{
WString convertedString;
int requiredSize = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, 0, 0);
if(requiredSize > 0)
{
std::vector<wchar_t> buffer(requiredSize);
MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &buffer[0], requiredSize);
convertedString.assign(buffer.begin(), buffer.end() - 1);
}
return convertedString;
}
WString StringUtils::Utf8ToUtf16(const char* str)
{
return Utf8ToUtf16(str ? String(str) : String());
}

27
x64_dbg_dbg/stringutils.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef _STRINGUTILS_H
#define _STRINGUTILS_H
#include <string>
#include <vector>
typedef std::string String;
typedef std::wstring WString;
class StringUtils
{
public:
static std::vector<String> Split(const String & s, char delim, std::vector<String> & elems);
static std::vector<String> Split(const String & s, char delim);
static String Trim(const String & s);
static String TrimLeft(const String & s);
static String TrimRight(const String & s);
static String Utf16ToUtf8(const WString & wstr);
static String Utf16ToUtf8(const wchar_t* wstr);
static WString Utf8ToUtf16(const String & str);
static WString Utf8ToUtf16(const char* str);
private:
static const String WHITESPACE;
};
#endif //_STRINGUTILS_H

View File

@ -112,7 +112,7 @@ void symdownloadallsymbols(const char* szSymbolStore)
dprintf("SymUnloadModule64("fhex") failed!\n", modbase);
continue;
}
if(!SymLoadModuleEx(fdProcessInfo->hProcess, 0, ConvertUtf16ToUtf8(szModulePath).c_str(), 0, (DWORD64)modbase, 0, 0, 0))
if(!SymLoadModuleEx(fdProcessInfo->hProcess, 0, StringUtils::Utf16ToUtf8(szModulePath).c_str(), 0, (DWORD64)modbase, 0, 0, 0))
{
dprintf("SymLoadModuleEx("fhex") failed!\n", modbase);
continue;

View File

@ -19,7 +19,7 @@ static void varsetvalue(VAR* var, VAR_VALUE* value)
static bool varset(const char* name, VAR_VALUE* value, bool setreadonly)
{
std::string name_;
String name_;
if(*name != '$')
name_ = "$";
name_ += name;
@ -64,38 +64,15 @@ VAR* vargetptr()
return 0;
}
#include <iostream>
#include <sstream>
std::vector<std::string> & split(const std::string & s, char delim, std::vector<std::string> & elems)
{
std::stringstream ss(s);
std::string item;
while(std::getline(ss, item, delim))
{
if(!item.length())
continue;
elems.push_back(item);
}
return elems;
}
std::vector<std::string> split(const std::string & s, char delim)
{
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
bool varnew(const char* name, uint value, VAR_TYPE type)
{
if(!name)
return false;
std::vector<std::string> names = split(name, '\1');
std::string firstName;
std::vector<String> names = StringUtils::Split(name, '\1');
String firstName;
for(int i = 0; i < (int)names.size(); i++)
{
std::string name_;
String name_;
name = names.at(i).c_str();
if(*name != '$')
name_ = "$";
@ -119,7 +96,7 @@ bool varnew(const char* name, uint value, VAR_TYPE type)
static bool varget(const char* name, VAR_VALUE* value, int* size, VAR_TYPE* type)
{
std::string name_;
String name_;
if(*name != '$')
name_ = "$";
name_ += name;
@ -202,7 +179,7 @@ bool varset(const char* name, const char* string, bool setreadonly)
bool vardel(const char* name, bool delsystem)
{
std::string name_;
String name_;
if(*name != '$')
name_ = "$";
name_ += name;
@ -218,7 +195,7 @@ bool vardel(const char* name, bool delsystem)
{
VariableMap::iterator del = found;
found++;
if(found->second.name == std::string(name))
if(found->second.name == String(name))
variables.erase(del);
}
return true;
@ -226,7 +203,7 @@ bool vardel(const char* name, bool delsystem)
bool vargettype(const char* name, VAR_TYPE* type, VAR_VALUE_TYPE* valtype)
{
std::string name_;
String name_;
if(*name != '$')
name_ = "$";
name_ += name;

View File

@ -32,21 +32,21 @@ struct VAR_VALUE
struct VAR
{
std::string name;
std::string alias;
String name;
String alias;
VAR_TYPE type;
VAR_VALUE value;
};
struct CaseInsensitiveCompare
{
bool operator()(const std::string & str1, const std::string & str2) const
bool operator()(const String & str1, const String & str2) const
{
return _stricmp(str1.c_str(), str2.c_str()) < 0;
}
};
typedef std::map<std::string, VAR, CaseInsensitiveCompare> VariableMap;
typedef std::map<String, VAR, CaseInsensitiveCompare> VariableMap;
//functions
void varinit();

View File

@ -235,21 +235,21 @@ extern "C" DLL_EXPORT const char* _dbg_dbginit()
if(!GetModuleFileNameW(hInst, wszDir, deflen))
return "GetModuleFileNameW failed!";
char dir[deflen] = "";
strcpy_s(dir, ConvertUtf16ToUtf8(wszDir).c_str());
strcpy_s(dir, StringUtils::Utf16ToUtf8(wszDir).c_str());
int len = (int)strlen(dir);
while(dir[len] != '\\')
len--;
dir[len] = 0;
strcpy(alloctrace, dir);
PathAppendA(alloctrace, "\\alloctrace.txt");
DeleteFileW(ConvertUtf8ToUtf16(alloctrace).c_str());
DeleteFileW(StringUtils::Utf8ToUtf16(alloctrace).c_str());
setalloctrace(alloctrace);
strcpy(dbbasepath, dir); //debug directory
PathAppendA(dbbasepath, "db");
CreateDirectoryW(ConvertUtf8ToUtf16(dbbasepath).c_str(), 0); //create database directory
CreateDirectoryW(StringUtils::Utf8ToUtf16(dbbasepath).c_str(), 0); //create database directory
strcpy(szSymbolCachePath, dir);
PathAppendA(szSymbolCachePath, "symbols");
SetCurrentDirectoryW(ConvertUtf8ToUtf16(dir).c_str());;
SetCurrentDirectoryW(StringUtils::Utf8ToUtf16(dir).c_str());;
gMsgStack = msgallocstack();
if(!gMsgStack)
return "Could not allocate message stack!";
@ -259,15 +259,15 @@ extern "C" DLL_EXPORT const char* _dbg_dbginit()
char plugindir[deflen] = "";
strcpy(plugindir, dir);
PathAppendA(plugindir, "plugins");
CreateDirectoryW(ConvertUtf8ToUtf16(plugindir).c_str(), 0);
CreateDirectoryW(StringUtils::Utf8ToUtf16(plugindir).c_str(), 0);
pluginload(plugindir);
//handle command line
int argc = 0;
wchar_t** argv = CommandLineToArgvW(GetCommandLineW(), &argc);
if(argc == 2) //we have an argument
{
UString str = "init \"";
str += ConvertUtf16ToUtf8(argv[1]);
String str = "init \"";
str += StringUtils::Utf16ToUtf8(argv[1]);
str += "\"";
DbgCmdExec(str.c_str());
}
@ -275,10 +275,10 @@ extern "C" DLL_EXPORT const char* _dbg_dbginit()
{
if(_wcsicmp(argv[1], L"-a") == 0 && !_wcsicmp(argv[3], L"-e"))
{
UString str = "attach .";
str += ConvertUtf16ToUtf8(argv[2]);
String str = "attach .";
str += StringUtils::Utf16ToUtf8(argv[2]);
str += ", .";
str += ConvertUtf16ToUtf8(argv[4]);
str += StringUtils::Utf16ToUtf8(argv[4]);
DbgCmdExec(str.c_str());
}
}

View File

@ -33,12 +33,10 @@
<ClCompile Include="reference.cpp" />
<ClCompile Include="simplescript.cpp" />
<ClCompile Include="stackinfo.cpp" />
<ClCompile Include="stringutils.cpp" />
<ClCompile Include="symbolinfo.cpp" />
<ClCompile Include="thread.cpp" />
<ClCompile Include="threading.cpp" />
<ClCompile Include="UString\Exception.cpp" />
<ClCompile Include="UString\String.cpp" />
<ClCompile Include="UString\UString.cpp" />
<ClCompile Include="value.cpp" />
<ClCompile Include="variable.cpp" />
<ClCompile Include="x64_dbg.cpp" />
@ -82,14 +80,12 @@
<ClInclude Include="reference.h" />
<ClInclude Include="simplescript.h" />
<ClInclude Include="stackinfo.h" />
<ClInclude Include="stringutils.h" />
<ClInclude Include="symbolinfo.h" />
<ClInclude Include="thread.h" />
<ClInclude Include="threading.h" />
<ClInclude Include="TitanEngine\TitanEngine.h" />
<ClInclude Include="undocumented.h" />
<ClInclude Include="UString\Exception.h" />
<ClInclude Include="UString\String.h" />
<ClInclude Include="UString\UString.h" />
<ClInclude Include="value.h" />
<ClInclude Include="variable.h" />
<ClInclude Include="x64_dbg.h" />

View File

@ -13,9 +13,6 @@
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav</Extensions>
</Filter>
<Filter Include="Source Files\UString">
<UniqueIdentifier>{ee24febc-948e-4226-ba0e-68a9b449fb23}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\Interfaces/Exports">
<UniqueIdentifier>{44fd9eb7-2017-49b8-8d9a-dec680632343}</UniqueIdentifier>
</Filter>
@ -40,9 +37,6 @@
<Filter Include="Header Files\Third Party\TitanEngine">
<UniqueIdentifier>{23226861-3b20-42db-8dd6-c5d276ba7a83}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\Third Party\UString">
<UniqueIdentifier>{adf51b13-6f3b-4b04-9ba9-21fb7a38150d}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\Third Party\XEDParse">
<UniqueIdentifier>{6b85ff77-8866-4618-9d46-006d8c349f8f}</UniqueIdentifier>
</Filter>
@ -81,15 +75,6 @@
<ClCompile Include="x64_dbg.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="UString\Exception.cpp">
<Filter>Source Files\UString</Filter>
</ClCompile>
<ClCompile Include="UString\String.cpp">
<Filter>Source Files\UString</Filter>
</ClCompile>
<ClCompile Include="UString\UString.cpp">
<Filter>Source Files\UString</Filter>
</ClCompile>
<ClCompile Include="_dbgfunctions.cpp">
<Filter>Source Files\Interfaces/Exports</Filter>
</ClCompile>
@ -180,6 +165,9 @@
<ClCompile Include="log.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="stringutils.cpp">
<Filter>Source Files\Utilities</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="x64_dbg.h">
@ -224,15 +212,6 @@
<ClInclude Include="lz4\lz4hc.h">
<Filter>Header Files\Third Party\lz4</Filter>
</ClInclude>
<ClInclude Include="UString\Exception.h">
<Filter>Header Files\Third Party\UString</Filter>
</ClInclude>
<ClInclude Include="UString\String.h">
<Filter>Header Files\Third Party\UString</Filter>
</ClInclude>
<ClInclude Include="UString\UString.h">
<Filter>Header Files\Third Party\UString</Filter>
</ClInclude>
<ClInclude Include="_global.h">
<Filter>Header Files\Core</Filter>
</ClInclude>
@ -335,5 +314,8 @@
<ClInclude Include="undocumented.h">
<Filter>Header Files\Debugger Core</Filter>
</ClInclude>
<ClInclude Include="stringutils.h">
<Filter>Header Files\Utilities</Filter>
</ClInclude>
</ItemGroup>
</Project>