///////////////////////////////////////////////////////////////////////////// // 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; } } PokeGen::PokeMod::String::operator const char *() const { return c_str(); }