strf関数を可変長に&型が合わない場合は変換するようにするパッチ作成中

static char *cnvformat()
{
	//		フォーマット付き文字列を作成する
	//
	char *fstr, *p, *fp, *tmp;
	int i, len;
	
	p = ctx->stmp;
	// フォーマット文字列をコピーして使う
	fp = code_gets();
	fstr = sbAlloc( strlen( fp ) + 1 );
	sbCopy( &fstr, fp, strlen( fp ) + 1 );
	fp = fstr;
	tmp = sbAlloc(64);
	len = 0;
	
	while (1) {
		// '%' までをコピー
		for( i = 0; fp[i] != '\0'; i ++ ) {
			if ( fp[i] == '%' ) break;
			if ( ( fp[i] >= 0x81 && fp[i] <= 0x9F ) || ( fp[i] >= 0xE0 && fp[i] <= 0xFC ) ) {
				i ++;
			}
		}
		memcpy( &p[len], fp, i );
		len += i;
		fp += i;
		if ( *fp == '\0' ) break;
		
		i = strspn( fp + 1, " #*+-.0123456789Lhl" );
		sbCopy( &tmp, fp, i + 3 );
		tmp[i+2] = '\0';
		sbAdd( &tmp, "%n", 3, 1 );
		fp += 1 + i;
		
		if ( *fp == '%' ) continue;
		if ( code_get() <= PARAM_END ) {
			throw HSPERR_INVALID_FUNCPARAM;
		}
		switch (*fp) { // cdEefGgiopsuXx
		case 'd': case 'i': case 'c': case 'o': case 'x': case 'X': case 'u': case 'p':
			sprintf( &p[len], tmp, *(int *)HspVarCoreCnvPtr( mpval, HSPVAR_FLAG_INT ), &i );
			break;
		case 'f': case 'e': case 'E': case 'g': case 'G':
			sprintf( &p[len], tmp, *(double *)HspVarCoreCnvPtr( mpval, HSPVAR_FLAG_DOUBLE ), &i );
			break;
		case 's': 
			sprintf( &p[len], tmp, (char *)HspVarCoreCnvPtr( mpval, HSPVAR_FLAG_STR ), &i );
			break;
		default:
			throw HSPERR_INVALID_FUNCPARAM;
		}
		fp ++;
		len += i;
	}
	p[len] = '\0';
	sbFree( tmp );
	sbFree( fstr );
	
	return p;
}
	case 0x103:								// strf
		ptr = cnvformat();
		break;
OpenHSP
Copyright (C) 1997-2008, Onion Software/onitama, all rights reserved.
This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are disclaimed.
  • `*'にまだ対応していない。strf( "%*d", 5, 123 )とかstrf( "%.*s", 3, "hello" )とか。
  • バッファの拡張をしていない

この二つに対応するためには変換指定文字列をstrspnでなくまじめに解析しないといけないよなー、面倒だ。
とりあえずダラーは後回し。
(13:57追記)`*'に対応できた。次はバッファの自動拡張か。必要なサイズを概算(大目に見積もって)しないといけないんだよな。めんどー。