///////////////////////////////////////////////////////////////////////////// // Name: pokemod/String.cpp // Purpose: A custom string class derived from the C++ standard string // to accomodate simple format functionality // Author: Ben Boeckel // Modified by: Ben Boeckel // Created: Mon Feb 19 22:03:45 2007 // Copyright: ©2007 Nerdy Productions // Licence: // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License along // with this program; if not, write to the Free Software Foundation, Inc., // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ///////////////////////////////////////////////////////////////////////////// #include "String.h" PokeGen::PokeMod::String::String(const std::string &str) { assign(str); } PokeGen::PokeMod::String::String(const char *fmt, ...) { va_list args; va_start(args, fmt); printf(fmt, args); va_end(args); } void PokeGen::PokeMod::String::Printf(const char *fmt, ...) { va_list args; va_start(args, fmt); printf(fmt, args); va_end(args); } void PokeGen::PokeMod::String::Printf(const char *fmt, va_list &args) { Format unit; std::stringstream ss; const char *p = fmt; while (*p) { if (*p == '%') { if (*++p) { switch (*p) { // C-strings case 's': unit.s = va_arg(args, const char *); append(unit.s); break; // Integers case 'i': case 'd': unit.i = va_arg(args, int); ss.str(""); ss << unit.i; append(ss.str()); break; // Unsigned integers case 'u': unit.u = va_arg(args, unsigned); ss.str(""); ss << unit.u; append(ss.str()); break; // Longs case 'I': case 'D': unit.l = va_arg(args, long); ss.str(""); ss << unit.l; append(ss.str()); break; // Unsigned longs case 'U': unit.ul = va_arg(args, unsigned long); ss.str(""); ss << unit.ul; append(ss.str()); break; // Characters case 'c': unit.c = (char)va_arg(args, int); append(1, unit.c); break; // Unsigned characters case 'x': unit.uc = (unsigned char)va_arg(args, int); append(1, unit.uc); break; // Floats case 'f': unit.f = va_arg(args, double); ss.str(""); ss << unit.f; append(ss.str()); break; // Percent character case '%': append(1, '%'); break; // Unknown sequence default: append(1, '%'); append(1, *p); } } else { append(1, '%'); break; } } else 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 { return c_str(); }