From 9102febc37475af113681eaaee02ecc2ea04b4da Mon Sep 17 00:00:00 2001 From: Ben Boeckel Date: Thu, 28 Jun 2007 15:25:39 +0000 Subject: Minor various fixes, Dialog validation started, GUI fixes, String methods added git-svn-id: https://pokegen.svn.sourceforge.net/svnroot/pokegen/trunk@21 6ecfd1a5-f3ed-3746-8530-beee90d26b22 --- pokemod/String.cpp | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 2 deletions(-) (limited to 'pokemod/String.cpp') diff --git a/pokemod/String.cpp b/pokemod/String.cpp index 0d8b8367..61bc7e37 100644 --- a/pokemod/String.cpp +++ b/pokemod/String.cpp @@ -37,7 +37,7 @@ PokeGen::PokeMod::String::String(const char *fmt, ...) va_end(args); } -void PokeGen::PokeMod::String::printf(const char *fmt, ...) +void PokeGen::PokeMod::String::Printf(const char *fmt, ...) { va_list args; va_start(args, fmt); @@ -45,7 +45,7 @@ void PokeGen::PokeMod::String::printf(const char *fmt, ...) va_end(args); } -void PokeGen::PokeMod::String::printf(const char *fmt, va_list &args) +void PokeGen::PokeMod::String::Printf(const char *fmt, va_list &args) { Format unit; std::stringstream ss; @@ -130,7 +130,116 @@ void PokeGen::PokeMod::String::printf(const char *fmt, va_list &args) append(1, *p); ++p; } +} + +unsigned PokeGen::PokeMod::String::Count(const char c) const +{ + unsigned count = 0; + for (unsigned i = 0; i < length(); ++i) + count += (*this[i] == c); + return count; +} + +unsigned PokeGen::PokeMod::String::Count(const String &s, bool canOverlap) const +{ + unsigned count = 0; + for (unsigned i = 0; i + s.length() < length(); ++i) + { + if (this->substr(i, i + s.length() - 1) == s) + { + ++count; + if (!canOverlap) + i += s.length() - 1; + } + } + return count; } + +bool PokeGen::PokeMod::String::Convert(bool &val) const +{ + if (*this == "true") + val = true; + else if (*this == "false") + val = false; + else + return false; + return true; +} + +bool PokeGen::PokeMod::String::Convert(char &val) const +{ + long temp; + char *end; + temp = std::strtol(c_str(), &end, 10); + if ((CHAR_MIN <= temp) && (temp <= CHAR_MAX) && !*end) + val = temp; + else + return false; + return true; +} + +bool PokeGen::PokeMod::String::Convert(unsigned char &val) const +{ + unsigned long temp; + char *end; + temp = std::strtoul(c_str(), &end, 10); + if ((temp <= UCHAR_MAX) && !*end) + val = temp; + else + return false; + return true; +} + +bool PokeGen::PokeMod::String::Convert(int &val) const +{ + long temp; + char *end; + temp = std::strtol(c_str(), &end, 10); + if ((INT_MIN <= temp) && (temp <= INT_MAX) && !*end) + val = temp; + else + return false; + return true; +} + +bool PokeGen::PokeMod::String::Convert(unsigned &val) const +{ + unsigned long temp; + char *end; + temp = std::strtoul(c_str(), &end, 10); + if ((temp <= UINT_MAX) && !*end) + val = temp; + else + return false; + return true; +} + +bool PokeGen::PokeMod::String::Convert(long &val) const +{ + long temp; + char *end; + temp = std::strtol(c_str(), &end, 10); + if (!*end) + val = temp; + return !*end; +} + +bool PokeGen::PokeMod::String::Convert(unsigned long &val) const +{ + unsigned long temp; + char *end; + temp = std::strtoul(c_str(), &end, 10); + if (!*end) + val = temp; + return !*end; +} + +bool PokeGen::PokeMod::String::Convert(double &val) const +{ + char *end; + val = std::strtod(c_str(), &end); + return !*end; +} PokeGen::PokeMod::String::operator const char *() const { -- cgit