From 6baf1692832ed442f2e409fee5fd9d291a2f9cec Mon Sep 17 00:00:00 2001 From: Tim Warren Date: Mon, 13 Apr 2015 09:30:56 -0400 Subject: [PATCH] Disable controls that aren't relevent at program start --- include/UTF8Strings/Exception.cpp | 50 ++ include/UTF8Strings/Exception.h | 64 +++ include/UTF8Strings/String.cpp | 899 ++++++++++++++++++++++++++++++ include/UTF8Strings/String.h | 325 +++++++++++ nbproject/project.xml | 2 +- src/widgets/MainFrame.cpp | 28 +- src/widgets/MainFrame.h | 5 + src/widgets/TabContainer.cpp | 5 + 8 files changed, 1372 insertions(+), 6 deletions(-) create mode 100644 include/UTF8Strings/Exception.cpp create mode 100644 include/UTF8Strings/Exception.h create mode 100644 include/UTF8Strings/String.cpp create mode 100644 include/UTF8Strings/String.h diff --git a/include/UTF8Strings/Exception.cpp b/include/UTF8Strings/Exception.cpp new file mode 100644 index 0000000..90dad36 --- /dev/null +++ b/include/UTF8Strings/Exception.cpp @@ -0,0 +1,50 @@ +/** + * 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" + + +const int UTF8::Exception::UnspecifiedException; +const int UTF8::Exception::StringToIntConversionError; +const int UTF8::Exception::StringToDoubleConversionError; +const int UTF8::Exception::FileNotFound; +const int UTF8::Exception::StringIsNotACharacter; + +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; +} \ No newline at end of file diff --git a/include/UTF8Strings/Exception.h b/include/UTF8Strings/Exception.h new file mode 100644 index 0000000..a22fb74 --- /dev/null +++ b/include/UTF8Strings/Exception.h @@ -0,0 +1,64 @@ +/** + * 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 + +namespace UTF8 { + + /** + * Exception class. When something bad happens it is thowed by UTF8::String. + */ + class Exception { + public: + static const int UnspecifiedException = 1; + static const int StringToIntConversionError = 2; + static const int StringToDoubleConversionError = 3; + static const int FileNotFound = 4; + static const int 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 */ + diff --git a/include/UTF8Strings/String.cpp b/include/UTF8Strings/String.cpp new file mode 100644 index 0000000..c3bfb46 --- /dev/null +++ b/include/UTF8Strings/String.cpp @@ -0,0 +1,899 @@ +/** + * 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 +#include +#include +#include +#include +#include +#include +#include "Exception.h" + +void UTF8::String::ConvertFromDouble(const long double d, const UTF8::String &ThousandSeparator, const UTF8::String &FractionSeparator, const int IntegerPartLength, const int FractionPartLength) { + + std::ostringstream os; + os.precision(15); + os << d; + + + UTF8::String Number(os.str()); + UTF8::String Integer, Fraction; + + // Extracting integer and fraction + std::vector Extracted = Number.Explode("."); + + unsigned int IntegerLength; + if (IntegerPartLength) { + IntegerLength = IntegerPartLength; + } else { + IntegerLength = Extracted[0].Length(); + } + + unsigned int FractionLength; + if (FractionPartLength) { + FractionLength = FractionPartLength; + } else { + if (Extracted.size() > 1) { + FractionLength = Extracted[1].Length(); + } else { + FractionLength = 0; + } + } + + // Parsing integer + for (unsigned int i = 0; i < IntegerLength; i++) { + if ((i > 0) && (i % 3 == 0)) { + Integer = ThousandSeparator + Integer; + } + + if (Extracted[0].Length() < i + 1) { + Integer = "0" + Integer; + } else { + Integer = Extracted[0][Extracted[0].Length() - 1 - i] + Integer; + } + } + + + // Parsing fraction + if (FractionLength) { + Fraction = FractionSeparator; + for (unsigned int i = 0; i < FractionLength; i++) { + if ((Extracted.size() > 1) && (Extracted[1].Length() > i)) { + Fraction += Extracted[1][i]; + } else { + Fraction += "0"; + } + } + } + + + + * this = Integer + Fraction; + + +} + +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); + unsigned int Length = 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 (n < 0) { + if (Direction == SearchDirectionFromRightToLeft) { + return -1; + } else { + n = 0; + } + } + + while (((Direction == SearchDirectionFromLeftToRight) && (n < Length() - SubstringLength + 1)) || ((Direction == SearchDirectionFromRightToLeft) && (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; + long 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 &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::Explode(const String &Separator) const { + std::vector v; + + unsigned int prev = 0; + + 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 operator+(const long l, const UTF8::String &StringObj) { + UTF8::String s(l); + 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 shure all preparations are done there + + if (str && strlen(str)) { + if (DataArrayLength) { + CheckIfStringIsCorrect(str); + + unsigned int StrLength = 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 shure all preparations are done there + + if (str && strlen(str)) { + CheckIfStringIsCorrect(str); + + Empty(); + + DataArrayLength = 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 ((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(); +} + +UTF8::String::String(const long double d, const UTF8::String &ThousandSeparator, const UTF8::String &DecimalSeparator, const int IntegerPartCount, const int FractionPartCount) { + + InitString(); + ConvertFromDouble(d, ThousandSeparator, DecimalSeparator, IntegerPartCount, FractionPartCount); +} + +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. StringLength: ") + StringLength + " Position: " + Position + " String: [" + Data + "]").ToString()); + } + + 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 (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) | 0b11000000; + tmp[pos++] = (wc & 0b111111) | 0b10000000; + } else if (wc < 0x10000) { + tmp[pos++] = (wc >> 12) | 0b11100000; + tmp[pos++] = ((wc >> 6) & 0b111111) | 0b10000000; + tmp[pos++] = (wc & 0b111111) | 0b10000000; + } else { + + tmp[pos++] = (wc >> 18) | 0b11110000; + tmp[pos++] = ((wc >> 12) & 0b111111) | 0b10000000; + tmp[pos++] = ((wc >> 6) & 0b111111) | 0b10000000; + tmp[pos++] = (wc & 0b111111) | 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) != 0b10) { + 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= 0; i--) { + c = Data[i]; + if ((c >= '0') && (c <= '9')) { + int_part += (c - '0') * mul; + mul *= 10; + } else { + if (c == '.') { + prec_part = (double) int_part / (double) mul; + int_part = 0; + mul = 1; + } else { + if ((c == '-') && (i == 0)) { + int_part = -int_part; + prec_part = -prec_part; + } else { + + UTF8::String err = "Cannot convert \"" + * this+"\" to double."; + throw UTF8::Exception(err.ToConstCharPtr(), UTF8::Exception::StringToDoubleConversionError); + } + } + } + } + return int_part + prec_part; +} + +int64_t UTF8::String::ToLong() const { + int64_t mul = 1; + char c; + int64_t number = 0; + + for (int i = DataArrayLength - 1; i >= 0; i--) { + c = Data[i]; + if ((c >= '0') && (c <= '9')) { + number += (c - '0') * mul; + mul *= 10; + } else { + if (c == '.') { + number = 0; + mul = 1; + } else { + if ((c == '-') && (i == 0)) { + number = -number; + } else { + + UTF8::String err = "Cannot convert \"" + * this+"\" to number."; + throw UTF8::Exception(err.ToConstCharPtr(), UTF8::Exception::StringToIntConversionError); + } + } + } + } + + return number; + +} + +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 (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 (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; +} + +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); +} + diff --git a/include/UTF8Strings/String.h b/include/UTF8Strings/String.h new file mode 100644 index 0000000..4a71f27 --- /dev/null +++ b/include/UTF8Strings/String.h @@ -0,0 +1,325 @@ +/** + * 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 +#include +#include +#include + +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); + + /** + * Converting from long constructor. Automatically generates string from number. + */ + String(const long double d,const UTF8::String &ThousandSeparator = "", const UTF8::String &FractionSeparator = ".", const int IntegerPartLength = 0, const int FractionPartLength = 0); + + ~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 long + */ + int64_t ToLong() const; + + /** + * Converts UTF8::String to double + */ + double ToDouble() const; + + /** + * Converts UTF8::String to const char * + */ + const char * ToConstCharPtr() const; + + /** + * Separates string using given separator and returns vector + */ + std::vector Explode(const String &Separator) const; + + /** + * Creating String from array of String adding separator between them. + */ + static String Implode(const std::vector &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*); + + /** + * Assign operator. Provides String1=(long double) expression. + */ + String & operator=(long double); + + /** + * 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 String1String2 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 *); + void ConvertFromDouble(const long double d, const UTF8::String &ThousandSeparator = "", const UTF8::String &DecimalSeparator = ".", const int IntegerPartCount = 0, const int FractionPartCount = 0); + + }; + +} + +/** + * 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 123+String1 expression. + */ +UTF8::String operator+(long, 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 */ + + diff --git a/nbproject/project.xml b/nbproject/project.xml index 0e6722a..d0214a9 100644 --- a/nbproject/project.xml +++ b/nbproject/project.xml @@ -6,7 +6,7 @@ Tyro cpp - h,hpp + h,hpp,xpm UTF-8 diff --git a/src/widgets/MainFrame.cpp b/src/widgets/MainFrame.cpp index 0925c3e..03b1dc2 100644 --- a/src/widgets/MainFrame.cpp +++ b/src/widgets/MainFrame.cpp @@ -62,7 +62,7 @@ void MainFrame::SetupToolbar() CreateToolBar(wxNO_BORDER | wxTB_FLAT | wxTB_HORIZONTAL); - wxToolBar *toolBar = GetToolBar(); + toolBar = GetToolBar(); vector bitmaps; @@ -78,18 +78,21 @@ void MainFrame::SetupToolbar() //toolBar->AddTool(wxID_CLOSE, "Close", bitmaps[3], "Close file"); //toolBar->AddSeparator(); //toolBar->AddTool(wxID_ANY, "Settings", bitmaps[4], "Change Settings"); + + toolBar->EnableTool(wxID_SAVE, false); + toolBar->Realize(); } void MainFrame::SetupMenu() { // create a menu bar - wxMenuBar* mbar = new wxMenuBar(); + mbar = new wxMenuBar(); // Create Base menus - wxMenu* fileMenu = new wxMenu(_T("")); - wxMenu* editMenu = new wxMenu(_T("")); - wxMenu* helpMenu = new wxMenu(_T("")); + fileMenu = new wxMenu(_T("")); + editMenu = new wxMenu(_T("")); + helpMenu = new wxMenu(_T("")); // Add items to top-level menus fileMenu->Append(wxID_NEW, _T("&New\tCtrl+N"), _T("Create a new file")); @@ -100,6 +103,10 @@ void MainFrame::SetupMenu() fileMenu->AppendSeparator(); fileMenu->Append(wxID_CLOSE, _T("&Close\tCtrl+W"), _T("Close the current document")); fileMenu->Append(wxID_EXIT, _T("&Quit\tCtrl+Q"), _T("Quit the application")); + + fileMenu->Enable(wxID_SAVE, false); + fileMenu->Enable(wxID_SAVEAS, false); + fileMenu->Enable(wxID_CLOSE, false); editMenu->Append(wxID_UNDO, _T("&Undo\tCtrl+Z"), _T("Undo last action")); editMenu->Append(wxID_REDO, _T("&Redo\tCtrl+Y"), _T("Redo last action")); @@ -109,7 +116,18 @@ void MainFrame::SetupMenu() editMenu->Append(wxID_PASTE, _T("&Paste\tCtrl+V"), _T("Paste contents of clipboard")); editMenu->Append(wxID_CLEAR, _T("&Delete\tDel")); editMenu->AppendSeparator(); + editMenu->Append (wxID_FIND, _("&Find\tCtrl+F")); + editMenu->AppendSeparator(); editMenu->Append(wxID_SELECTALL, _T("Select All\tCtrl+A"), _T("Select all the text in the current document")); + + editMenu->Enable(wxID_UNDO, false); + editMenu->Enable(wxID_REDO, false); + editMenu->Enable(wxID_CUT, false); + editMenu->Enable(wxID_COPY, false); + editMenu->Enable(wxID_PASTE, false); + editMenu->Enable(wxID_CLEAR, false); + editMenu->Enable(wxID_FIND, false); + editMenu->Enable(wxID_SELECTALL, false); helpMenu->Append(wxID_ABOUT, _T("&About...\tF1"), _T("Show info about this application")); diff --git a/src/widgets/MainFrame.h b/src/widgets/MainFrame.h index 0483e02..7b19dfd 100644 --- a/src/widgets/MainFrame.h +++ b/src/widgets/MainFrame.h @@ -21,6 +21,11 @@ class MainFrame: public wxFrame ~MainFrame(); private: TabContainer *notebook; + wxToolBar *toolBar; + wxMenuBar *mbar; + wxMenu *fileMenu; + wxMenu *editMenu; + wxMenu *helpMenu; enum { idMenuQuit = 1000, diff --git a/src/widgets/TabContainer.cpp b/src/widgets/TabContainer.cpp index 0feffbb..ad6d0a8 100644 --- a/src/widgets/TabContainer.cpp +++ b/src/widgets/TabContainer.cpp @@ -5,6 +5,7 @@ #include "TabContainer.h" static unsigned long untitled_document_count = 0; +static unsigned long open_document_count = 0; TabContainer::TabContainer( wxWindow* parent, @@ -21,6 +22,8 @@ TabContainer::~TabContainer() {} void TabContainer::AddTab() { untitled_document_count++; + open_document_count++; + wxString caption; caption.Printf("Untitled %lu", untitled_document_count); @@ -32,6 +35,8 @@ void TabContainer::AddTab() void TabContainer::AddTab(wxString filePath) { + open_document_count++; + wxFileName fileName(filePath); wxString caption= fileName.GetFullName();