// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // formatter.hsp // Last Modified: 2006/04/01-15:33. // // strfを使って強引に printf と sprintf を使用するモジュール // いずれも引数合計9つまでしか対応してない // fprintf とか知りません。 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // 多重インクルード阻止 #ifndef __FORMATTER_HSP__ #define __FORMATTER_HSP__ #module "formatter" // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // モジュール開始 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #define DUMMY_ARG "DUMMY_ARG" // - - - - - - - -_sprintf- - - - - - - - - - - - - - - - - - - - - - - - - - - - - // strfを強引に複数の引数に対応させ、Perlのsprintfと同じようなものを提供 // (C/C++ のとはちょっと違う) // 下の sprintfを介して実行されるのに注意 // param _format 書式 // param p1 ~ p8 処理したい引数 // return 書式に従って処理された文字列 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #defcfunc _sprintf str _format, str p1, str p2, str p3, str p4, str p5, str p6, str p7, str p8 args = p1, p2, p3, p4, p5, p6, p7, p8 format = _format sdim tmp, strlen( format ) sdim ret, strlen( format ) index = 0 repeat 9 getstr tmp, format, index, '%' if( strsize == 0 ) { // もう無い break } index += strsize if( cnt == 0 ) { // 最初に見つかったやつは %を含むことは無いからそのまんま ret += tmp continue } else { if( args.(cnt-1) == DUMMY_ARG ) { // ダミーに反応したら ret += "%" + tmp // とりあえずくっつけて break // 終了 } // 仮初期化 maxTypesIndex = 0 thisType = 0 types = "cdouxXeEfgGs" // % に属する "type" を解決する // 結果はthisTypeに収納 repeat strlen( types ) typesIndex = instr( tmp, 0, strmid( types, cnt, 1 ) ) if( typesIndex == -1 ) { continue } if( maxTypesIndex <= typesIndex ) { maxTypesIndex = typesIndex thisType = strmid( types, cnt, 1 ) } loop // "type" を初期化 integerTypes = "cdouxX" floatTypes = "eEfgG" stringTypes = "s" // thisTypes にしたがってretへ格納 if( instr( integerTypes, 0, thistype ) != -1 ) { // on type int ret += strf( "%" + tmp, int( args.(cnt-1) ) ) } else { if( instr( floatTypes, 0, thistype ) != -1 ) { // on type float ret += strf( "%" + tmp, double( args.(cnt-1) ) ) } else { if( instr( stringTypes, 0, thistype ) != -1 ) { // on type string ret += strf( "%" + tmp, args.(cnt-1) ) } } } } loop return ret // _sprintf を複数の引数に対応させるためのラッパ #define global ctype sprintf(%1,%2=DUMMY_ARG@formatter,%3=DUMMY_ARG@formatter,%4=DUMMY_ARG@formatter,%5=DUMMY_ARG@formatter,%6=DUMMY_ARG@formatter,%7=DUMMY_ARG@formatter,%8=DUMMY_ARG@formatter,%9=DUMMY_ARG@formatter) _sprintf( (%1), ""+(%2), ""+(%3), ""+(%4), ""+(%5), ""+(%6), ""+(%7), ""+(%8), ""+(%9) ) // - - - - - - - - - printf- - - - - - - - - - - - - - - - - - - - - - - - - - - - // 一般に言うprintfを強引に使う // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #define global printf(%1,%2=DUMMY_ARG@formatter,%3=DUMMY_ARG@formatter,%4=DUMMY_ARG@formatter,%5=DUMMY_ARG@formatter,%6=DUMMY_ARG@formatter,%7=DUMMY_ARG@formatter,%8=DUMMY_ARG@formatter,%9=DUMMY_ARG@formatter) mes sprintf(%1,%2,%3,%4,%5,%6,%7,%8,%9) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // モジュール終了 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #global #endif // of __FORMATTER_HSP__