// -*- c++ -*- /* * Jakelib2 - General purpose C++ library * Copyright (C) 2001 Florian Wolff (florian@donuz.de) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: printf-macro.inc,v 1.3 2006-01-07 11:09:41 florian Exp $ */ #define JAKELIB_PRINTF(fstring, buf) \ StringBuffer buf(fstring->length()); \ StringBuffer format; \ \ va_list arg; \ va_start(arg, fstring); \ \ jchar c; \ int len = fstring->length(); \ \ for (int x = 0; x < len; x++) { \ c = fstring->charAt(x); \ \ if (c != '%') { \ buf.append(c); \ } \ else { \ x++; \ \ /* Extract width specification: */ \ int width = 0; \ jchar fillChar = ' '; \ \ format.setLength(0); \ c = fstring->charAt(x); \ while (x < len && ((c >= '0' && c <= '9') || c == '-')) { \ format.append(c); \ x++; \ c = fstring->charAt(x); \ } \ if (format.length() > 0) { \ width = Integer::parseInt(format.toString()); \ if (format.charAt(0) == '0') { \ fillChar = '0'; \ } \ } \ \ switch(c) { \ case 'c': \ { \ jchar ch = va_arg(arg, int); \ buf.append(ch); \ } \ break; \ \ case 's': \ { \ jbyte* s = va_arg(arg, jbyte*); \ int l = strlen(s); \ if (width > 0) { \ for (int idx = l; idx < width; idx++) \ buf.append(' '); \ } \ buf.append(s); \ if (width < 0) { \ for (int idx = l; idx < -width; idx++) \ buf.append(' '); \ } \ } \ break; \ \ case 'S': \ { \ Object *obj = va_arg(arg, Object*); \ String *s = (obj != null ? obj->toString() : String::nullString); \ \ if (width > 0) { \ for (int idx = s->length(); idx < width; idx++) \ buf.append(' '); \ } \ buf.append(s); \ if (width < 0) { \ for (int idx = s->length(); idx < -width; idx++) \ buf.append(' '); \ } \ } \ break; \ \ case 'i': \ { \ jint i = va_arg(arg, jint); \ String* s = Integer::toString(i); \ \ if (width > 0) { \ if (fillChar == '0' && i < 0) { \ buf.append('-'); \ s = s->substring(1); \ width--; \ } \ for (int idx = s->length(); idx < width; idx++) \ buf.append(fillChar); \ } \ buf.append(s); \ if (width < 0) { \ for (int idx = s->length(); idx < -width; idx++) \ buf.append(' '); \ } \ } \ break; \ \ case 'o': \ { \ jint i = va_arg(arg, jint); \ String* s = Integer::toOctalString(i); \ \ if (width > 0) { \ if (fillChar == '0' && i < 0) { \ buf.append('-'); \ s = s->substring(1); \ width--; \ } \ for (int idx = s->length(); idx < width; idx++) \ buf.append(fillChar); \ } \ buf.append(s); \ if (width < 0) { \ for (int idx = s->length(); idx < -width; idx++) \ buf.append(' '); \ } \ } \ break; \ \ case 'l': \ { \ jlong l = va_arg(arg, jlong); \ String* s = Long::toString(l); \ \ if (width > 0) { \ if (fillChar == '0' && l < 0) { \ buf.append('-'); \ s = s->substring(1); \ width--; \ } \ for (int idx = s->length(); idx < width; idx++) \ buf.append(fillChar); \ } \ buf.append(s); \ if (width < 0) { \ for (int idx = s->length(); idx < -width; idx++) \ buf.append(' '); \ } \ } \ break; \ \ case 'f': \ { \ jfloat f = (jfloat) va_arg(arg, jdouble); \ buf.append(Float::toString(f)); \ } \ break; \ \ case 'd': \ { \ jdouble d = va_arg(arg, jdouble); \ buf.append(Double::toString(d)); \ } \ break; \ \ default: \ buf.append(c); \ break; \ } \ } \ } \ \ va_end(arg);