/*	
 *	clipprnt.c
 *
 *	CLiP - Common Library for P/ECE
 *	Copyright (C) 2001-2015 Naoyuki Sawa
 *
 *	* Tue May 20 06:00:00 JST 2003 Naoyuki Sawa
 *	- http://minix.technoir.org/src/lib/stdio/doprnt.c傪婎偵夵憿偟傑偟偨丅
 *	* Thu Jul 13 06:12:00 JST 2006 Naoyuki Sawa
 *	- snprintf(),vsnprintf()傪捛壛偟傑偟偨丅
 *	- snprintf(),vsnprintf()懳墳偺偨傔偵丄婛懚偺doprnt()傪夵憿偟傑偟偨丅
 *	  doprnt()偼sprintf(),vsprintf()偑棙梡偟偰偄偨僒僽儖乕僠儞偱丄夵憿屻傕堷偒懕偒棙梡偟傑偡丅
 *	  sprintf(),vsprintf()偵塭嬁偑弌側偄傛偆婥傪偮偗偨偮傕傝偱偡偑丄偟偽傜偔偺娫偼梫拲堄偱偡丅
 *	* Wed Nov 16 02:21:10 JST 2011 Naoyuki Sawa
 *	- 宆僼傿乕儖僪暥帤'g'(媦傃'G')偵娙堈懳墳偟傑偟偨丅
 *	* Tue Jan 31 23:45:07 JST 2012 Naoyuki Sawa
 *	- f_print()偺丄愨懳抣偑2147483648.0(=INT_MAX+1)埲忋偺幚悢傪昞帵偡傞偲暥帤壔偗偡傞僶僌傪廋惓偟傑偟偨丅
 *	  愨懳抣偑4294967296.0(=UINT_MAX+1)埲忋偺幚悢傪昞帵偡傞偲暥帤壔偗偡傞僶僌(惂栺丠)偼丄傑偩枹廋惓偱偡丅
 *	* Thu Oct 02 01:02:39 JST 2014 Naoyuki Sawa
 *	- snprintf(),vsnprintf()偺栠傝抣偺嫇摦傪VC++6.0弨嫆佀C99弨嫆偵曄峏偟傑偟偨丅
 *	  scprintf(),vscprintf(),asprintf(),vasprintf()傪捛壛偟傑偟偨丅
 *	* Sun Oct 12 15:49:27 JST 2014 Naoyuki Sawa
 *	- doprnt()娭悢偵丄埲壓偺曄峏傪峴偄傑偟偨丅
 *	- 摉儌僕儏乕儖傪VC++6.0偱價儖僪偡傞応崌丄OUT偺掕媊偑Windows僿僢僟僼傽僀儖乽rpcdce.h乿偲徴撍偡傞偨傔丄OUT傪梊傔掕媊夝彍偟傑偟偨丅
 *	  摉儌僕儏乕儖傪VC++6.0偱價儖僪偡傞応崌偺懳嶔偑昁梫偵側偭偨棟桼偼丄埲壓偺偲偍傝偱偡丅
 *	- 偙傟傑偱偼丄VC++6.0偵娷傑傟偰偄傞printf摍偱廩暘偩偭偨偺偱丄摉儌僕儏乕儖傪VC++6.0偱價儖僪偡傞偙偲偼峫椂偟偰偄側偐偭偨偺偱偡偑丄
 *	  snprintf(),vsnprintf()偺栠傝抣偺嫇摦曄峏傗丄scprintf(),vscprintf(),asprintf(),vasprintf()偺捛壛偵敽偄 (2014/10/02僐儊儞僩嶲徠)
 *	  VC++6.0梡偺僾儘僕僃僋僩偵傕摉儌僕儏乕儖傪娷傔偰價儖僪偟丄P/ECE梡僾儘僌儔儉偲摦嶌傪嫟捠壔偟偨偄偲巚偄傑偟偨丅
 *	  偦偺偨傔偵丄摉儌僕儏乕儖傪VC++6.0偱價儖僪偡傞応崌傕寈崘偑弌側偄傛偆丄懳嶔偟傑偟偨丅
 *	* Thu Jan 22 21:10:35 JST 2015 Naoyuki Sawa
 *	- 挿偝廋忺巕'll'傪壖懳墳偟傑偟偨丅
 *	  挿偝廋忺巕'll'偑巜掕偝傟偨応崌丄壜曄堷悢偐傜偺抣偺庢摼偲丄曄姺巜掕巕'n'傊偺抣偺奿擺傪丄64價僢僩偱峴偆傛偆偵偟偨偩偗偱偡丅
 *	  撪晹張棟偼32價僢僩偺傑傑偱偁傝丄32價僢僩偺斖埻傪挻偊傞64價僢僩抣傪巜掕偡傞偲丄壓埵32價僢僩晹暘偺傒傪夝庍偟偰昞帵偟傑偡丅
 *	- 撪晹張棟傪32價僢僩偺傑傑偵偟偨棟桼偼丄婬側働乕僗偵懳墳偡傞偨傔偵惈擻偑掅壓偡傞偙偲傪旔偗傞偨傔偱偡丅
 *	  long long宆傪巊梡偡傞僾儘僌儔儉偱傕丄傎偲傫偳偺応崌丄抣偼32價僢僩偺斖埻偵廂傑傞偲巚偄傑偡丅
 *	  傕偟廂傑傜側偔偰傕丄昞帵偑偍偐偟偔側傞偩偗偱丄摦嶌忋偼(偍偦傜偔)栤戣偁傝傑偣傫丅
 */
#include "clip.h"

/****************************************************************************
 *
 ****************************************************************************/

					// 亖堄枴亖		亖椺亖
#define FL_LJUST	(1 <<  0)	// 嵍媗傔		%-5d
#define FL_ZEROFILL	(1 <<  1)	// 0媗傔		%05d
#define FL_SIGN		(1 <<  2)	// +/-昞帵		%+5d
#define FL_SPACE	(1 <<  3)	// 嬻敀/-昞帵		% 5d
#define FL_ALT		(1 <<  4)	// 0/0x/0X昞帵		%#x
#define FL_SHORT	(1 <<  5)	// 僴乕僼儚乕僪堷悢	%hd
#define FL_LONG_LONG	(1 <<  6)	// 64價僢僩堷悢		%lld				//{{2015/01/22曄峏:挿偝廋忺巕'll'傪壖懳墳偟傑偟偨丅}}
#define FL_PRECSPEC	(1 <<  7)	// 惛搙巜掕偁傝		%9.5f
#define FL_SIGNEDCONV	(1 <<  8)	// 晞崋偁傝		%d
#define FL_NOMORE	(1 << 31)	// 乮撪晹巊梡乯

/* width傗precision傪庢摼偟傑偡丅 */
static const char*
gnum(const char* fmt, int* ip, va_list* app)
{
	int i, c;

	if(*fmt == '*') {
		*ip = va_arg(*app, int);
		fmt++;
	} else {
		i = 0;
		for(;;) {
			c = *fmt;
			if(c < '0' || '9' < c) break;
			i = i * 10 + (c - '0');
			fmt++;
		}
		*ip = i;
	}

	return fmt;
}

/* 惍悢抣傪昞帵偟傑偡丅 */
static char*
i_compute(unsigned val, int base, char* s, int precision)
{
	int c;

	c = val % base;
	val /= base;
	if(val || precision > 1) s = i_compute(val, base, s, precision - 1);
	*s++ = c < 10 ? '0' + c : 'a' + (c - 10);

	return s;
}

/* 惍悢堷悢傪昞帵偟傑偡丅 */
static char*
o_print(char* s, int flags, int nrdigits, int c, va_list* app)
{
	unsigned val;
	int base;
	char *old_s = s;

	/* 抣傪庢摼丅 */
	       if(flags & FL_LONG_LONG) {
#ifdef  _MSC_VER
		                            val = va_arg(*app, unsigned __int64  );		//{{2015/01/22曄峏:挿偝廋忺巕'll'傪壖懳墳偟傑偟偨丅}}
#else //_MSC_VER
		                            val = va_arg(*app, unsigned long long);		//{{2015/01/22曄峏:挿偝廋忺巕'll'傪壖懳墳偟傑偟偨丅}}
#endif//_MSC_VER
	} else if(flags & FL_SHORT    ) {
		if(flags & FL_SIGNEDCONV) { val = va_arg(*app,          short    ); }
		else                      { val = va_arg(*app, unsigned short    ); }
	} else {
		                            val = va_arg(*app, unsigned          );
	}

	/* 晞崋昞帵丅 */
	if(flags & FL_SIGNEDCONV) {
		if((int)val < 0) {
			*s++ = '-';
			val = -(int)val;
		} else if(flags & FL_SIGN) {
			*s++ = '+';
		} else if(flags & FL_SPACE) {
			*s++ = ' ';
		}
	}

	/* 婎悢昞帵丅 */
	if(flags & FL_ALT) {
		switch(c) {
		case 'o': *s++ = '0';             break;
		case 'p':
		case 'x':
		case 'X': *s++ = '0'; *s++ = 'x'; break;
		}
	}

	/* 抣傪昞帵丅 */
	switch(c) {
	case 'b': base =  2; break;
	case 'o': base =  8; break;
	case 'd':
	case 'i':
	case 'u': base = 10; break;
	case 'p':
	case 'x':
	case 'X': base = 16; break;
	default : base = -1; DIE();
	}
	s = i_compute(val, base, s, nrdigits);
	if(c == 'X') {
		while(old_s != s) {
			*old_s = toupper(*old_s);
			old_s++;
		}
	}

	return s;
}

/* 晜摦彫悢堷悢傪昞帵偟傑偡丅 */
static char*
f_print(char* s, int flags, int precision, va_list* app)
{
	double val;
	int c;

	/* 抣傪庢摼丅 */
	val = va_arg(*app, double);

	/* 晞崋昞帵丅 */
	if(val < 0) {
		*s++ = '-';
		val = -val;
	} else if(flags & FL_SIGN) {
		*s++ = '+';
	} else if(flags & FL_SPACE) {
		*s++ = ' ';
	}

	/* 巐幪屲擖梡偺+0.5or0.05or0.005... */
	val += pow(10, -precision) / 2;

	/* 惍悢晹昞帵丅 */
	c = (int)val;
//{{2012/01/31:愨懳抣偑2147483648.0(=INT_MAX+1)埲忋偺幚悢傪昞帵偡傞偲暥帤壔偗偡傞僶僌傪廋惓
//	val -= c;
//伀2012/01/31:愨懳抣偑2147483648.0(=INT_MAX+1)埲忋偺幚悢傪昞帵偡傞偲暥帤壔偗偡傞僶僌傪廋惓
	val -= (unsigned)c;
//}}2012/01/31:愨懳抣偑2147483648.0(=INT_MAX+1)埲忋偺幚悢傪昞帵偡傞偲暥帤壔偗偡傞僶僌傪廋惓
	s = i_compute(c, 10, s, 1);

	/* 彫悢晹昞帵丅 */
	if(precision) {
		*s++ = '.';
		do {
			val *= 10;
			c = (int)val;
			val -= c;
			*s++ = '0' + c;
		} while(--precision);
	}

	return s;
}

static int
//doprnt(char* out, const char* fmt, va_list ap)
//亊2006/07/13 snprintf(),vsnprintf()懳墳
//亊
//亊- snprintf(),vsnprintf()懳墳偵偍偄偰丄doprnt()偺堷悢偵僶僢僼傽僒僀僘(cnt)傪捛壛偡傞偺偱側偔丄僶僢僼傽枛旜偺億僀儞僞(end)傪捛壛偡傞傛偆愝寁偟偨棟桼偼丄
//亊  偙偆偡傞曽偑丄vsprintf()偐傜偺棙梡帪偵丄柍尷僶僢僼傽傪昞尰偡傞偺偑梕堈偩偭偨偐傜偱偡丅(柍尷僶僢僼傽偺枛旜偺億僀儞僞偼丄((char*)-1)偲昞偣傑偡丅)
//亊  傑偨丄doprnt()偺幚憰偵偍偄偰傕丄巆傝僶僢僼傽僒僀僘傪娗棟偡傞傛傝傕丄師偺弌椡埵抲(out)偑僶僢僼傽枛旜(end)傪挻偊偰偄側偄偐傪専嵏偡傞曽偑娙扨偱偡丅
//亊
//亊- snprintf()傪埲壓偺傛偆偵屇傃弌偟偨応崌丄
//亊
//亊	char buf[10];
//亊	snprintf(buf, 10, fmt, ...);
//亊
//亊  doprnt()傊偺堷悢out,end偼丄壓恾偺傛偆側埵抲傪巜偟偰偄傑偡丅
//亊
//亊	buf[0][1][2][3][4][5][6][7][8][9]
//亊	    |                          |
//亊	   out                        end
//亊
//亊  end偼(僶僢僼傽偺枛旜)偱偁傝丄(僶僢僼傽偺枛旜+1)偱偼側偄偙偲偵拲堄偟偰偔偩偝偄丅
//亊  廔抂偺nul傪娷傑側偄丄桳岠側暥帤傪奿擺偱偒傞偺偼丄(out乣end-1)偺斖埻偲側傝傑偡丅
//亊
//亊- 廔抂偺nul傪娷傑側偄桳岠側暥帤偑(out乣end-1)偺斖埻偵廂傑偭偨応崌丄捈屻偵nul傪奿擺偟丄nul傪娷傑側偄暥帤悢傪曉偟傑偡丅
//亊  廔抂偺nul傪娷傑側偄桳岠側暥帤偑(end-1)傪挻偊傞応崌丄(end-1)埲崀傪愗傝幪偰偰(end)偵nul傪奿擺偟丄寢壥(-1)傪曉偟傑偡丅
//亊
//亊- 傕偟丄snprintf()傪僶僢僼傽僒僀僘0偱屇傃弌偟偨応崌丄
//亊
//亊	snprintf(buf, 0, fmt, ...);
//亊
//亊  doprnt()傊偺堷悢out,end偼丄end偑out傛傝傕慜傪巜偟偰偄傑偡丅
//亊
//亊	buf[-1][0]
//亊	    |   |
//亊	   end out
//亊
//亊  偙偺応崌丄廔抂偺nul傕奿擺偣偢偵丄寢壥(-1)傪曉偟傑偡丅
//亊
//伀* Thu Oct 02 01:02:39 JST 2014 Naoyuki Sawa
//伀- snprintf(),vsnprintf()偺栠傝抣偺嫇摦傪VC++6.0弨嫆佀C99弨嫆偵曄峏偟傑偟偨丅
//伀
//伀  偦傟偵敽偄丄doprnt()偺堷悢end偺巇條傪丄(僶僢僼傽偺枛旜)佀(僶僢僼傽偺枛旜+1)偵曄峏偟傑偟偨丅
//伀  乽snprintf(NULL,0,fmt,...)乿偐傜屇傃弌偝傟偨応崌偵丄(out=0x00000000,end=0xFFFFFFFF)偵側傞栤戣傪旔偗傞偨傔偱偡丅
//伀
//伀  OUT(C)儅僋儘偑丄僶僢僼傽廔抂偺nul傪奿擺偡傞埵抲傑偱丄暥帤傪奿擺偟偰偟傑偆偙偲偵側傝傑偡偑丄嵟屻偵nul偱忋彂偒偡傞偺偱栤戣偁傝傑偣傫丅
//伀  乽if(out<end)乿偺忦審傪乽if(out<(end-1))乿偵曄峏偟偰傕椙偐偭偨偺偱偡偑丄僐乕僪僒僀僘偑憹偊傞偺偱丄峴偄傑偣傫偱偟偨丅
//伀  慜弎偺偲偍傝丄乽if(out<end)乿偺傑傑偱傕丄寢壥偼摨偠偵側傞偺偱栤戣偁傝傑偣傫丅
//伀
//伀  崱屻偼丄out偲end偺巜偡埵抲偼丄埲壓偺傛偆偵側傝傑偡丅
//伀
//伀- snprintf()傪埲壓偺傛偆偵屇傃弌偟偨応崌丄
//伀
//伀	char buf[10];
//伀	snprintf(buf, 10, fmt, ...);
//伀
//伀  doprnt()傊偺堷悢out,end偼丄壓恾偺傛偆側埵抲傪巜偟偰偄傑偡丅
//伀
//伀	buf[0][1][2][3][4][5][6][7][8][9](10)
//伀	    |                             |
//伀	   out                           end
//伀
//伀- 傕偟丄snprintf()傪僶僢僼傽僒僀僘0偱屇傃弌偟偨応崌丄
//伀
//伀	snprintf(buf, 0, fmt, ...);
//伀
//伀  doprnt()傊偺堷悢out,end偼丄偳偪傜傕摨偠埵抲傪巜偟偰偄傑偡丅
//伀
//伀	buf[0]
//伀	    |
//伀	 out,end 
//伀
doprnt(char* out, char* end, const char* fmt, va_list ap)
{
//{{2014/10/12捛壛:摉儌僕儏乕儖傪VC++6.0偱價儖僪偡傞応崌丄OUT偺掕媊偑Windows僿僢僟僼傽僀儖乽rpcdce.h乿偲徴撍偡傞偨傔丄OUT傪梊傔掕媊夝彍偟傑偟偨丅
#ifdef  WIN32
#undef  OUT
#endif//WIN32
//}}2014/10/12捛壛:摉儌僕儏乕儖傪VC++6.0偱價儖僪偡傞応崌丄OUT偺掕媊偑Windows僿僢僟僼傽僀儖乽rpcdce.h乿偲徴撍偡傞偨傔丄OUT傪梊傔掕媊夝彍偟傑偟偨丅
/*{{2006/07/13 snprintf(),vsnprintf()懳墳*/
#define OUT(C)				\
	do {				\
		char __c__ = (C);	\
		if(out < end) {		\
			*out = __c__;	\
		}			\
		out++;			\
	} while(0)
/*}}2006/07/13 snprintf(),vsnprintf()懳墳*/

	int c, pad, len, flags, width, precision, zfill;
	char buf[256];	/* 悢抣偺弌椡暆傗惛搙偑偙偺僒僀僘傪挻偊傞偲堎忢摦嶌偟傑偡丅 */
	char *s, *old_s;
	char *old_out = out;

	/* 彂幃暥帤楍傪憱嵏偟偰乧 */
	while((c = *fmt++) != '\0') {
		/* 亀%[flags][width][.precision][h|l]type亁偱側偗傟偽偦偺傑傑昞帵丅 */
		if(c != '%') {
			//*out++ = c;
			//伀2006/07/13 snprintf(),vsnprintf()懳墳
			OUT(c);
			continue;
		}

		/* [flags]庢摼丅 */
		flags = 0;
		do {
			switch(*fmt) {
			case '-': flags |= FL_LJUST;    break;	/* 嵍媗傔 */
			case '0': flags |= FL_ZEROFILL; break;	/* 0媗傔 */
			case '+': flags |= FL_SIGN;     break;	/* +/-昞帵 */
			case ' ': flags |= FL_SPACE;    break;	/* 嬻敀/-昞帵 */
			case '#': flags |= FL_ALT;      break;	/* 0/0x/0X昞帵 */
			default : flags |= FL_NOMORE;   continue;
			}
			fmt++;
		} while(!(flags & FL_NOMORE));
		if((flags & FL_LJUST) && (flags & FL_ZEROFILL)) DIE();
		if((flags & FL_SIGN ) && (flags & FL_SPACE   )) DIE();

		/* [width]庢摼丅 */
		fmt = gnum(fmt, &width, &ap);
		if(*fmt == '.') {
			fmt++;
			fmt = gnum(fmt, &precision, &ap);
			flags |= FL_PRECSPEC;
		}

		/* [h|l]庢摼丅 */
		switch(*fmt) {
		case 'h': flags |= FL_SHORT; fmt++; break;
		case 'l': fmt++; if(*fmt == 'l') { flags |= FL_LONG_LONG; fmt++; } break;	//{{2015/01/22曄峏:挿偝廋忺巕'll'傪壖懳墳偟傑偟偨丅}}
		case 'L': DIE(); /* long double 偼枹懳墳 */
		}

		s = old_s = buf;
		switch(c = *fmt++) {
		default:
			//*out++ = c;
			//伀2006/07/13 snprintf(),vsnprintf()懳墳
			OUT(c);
			continue;
		case 'n':
			       if(flags & FL_LONG_LONG) {
#ifdef  _MSC_VER
				*va_arg(ap, __int64  *) = (__int64  )(out - old_out);		//{{2015/01/22曄峏:挿偝廋忺巕'll'傪壖懳墳偟傑偟偨丅}}
#else //_MSC_VER
				*va_arg(ap, long long*) = (long long)(out - old_out);		//{{2015/01/22曄峏:挿偝廋忺巕'll'傪壖懳墳偟傑偟偨丅}}
#endif//_MSC_VER
			} else if(flags & FL_SHORT    ) {
				*va_arg(ap, short    *) = (short    )(out - old_out);
			} else {
				*va_arg(ap, int      *) = (int      )(out - old_out);
			}
			continue;
		case 'c':
			*s++ = va_arg(ap, int);
			break;
		case 's':
			old_s = va_arg(ap, char*);
			if(old_s == NULL) old_s = "(null)";
			s = old_s;
			while(*s != '\0') {
				if(flags & FL_PRECSPEC) {
					if(!precision) break;
					precision--;
				}
				s++;
			}
			break;
		case 'b':
		case 'o':
		case 'u':
		case 'p':
		case 'x':
		case 'X':
			if(!(flags & FL_PRECSPEC)) precision = 1;
			s = o_print(s, flags, precision, c, &ap);
			break;
		case 'd':
		case 'i':
			flags |= FL_SIGNEDCONV;
			if(!(flags & FL_PRECSPEC)) precision = 1;
			s = o_print(s, flags, precision, c, &ap);
			break;
		case 'f':
		case 'e': /* 1.2e3宍幃偼枹懳墳丅'f'偲摨偠埖偄偲偟傑偡丅 */
		case 'E': /* 1.2e3宍幃偼枹懳墳丅'f'偲摨偠埖偄偲偟傑偡丅 */
		case 'g': /* 1.2e3宍幃偼枹懳墳丅'f'偲摨偠埖偄偲偟傑偡丅 */
		case 'G': /* 1.2e3宍幃偼枹懳墳丅'f'偲摨偠埖偄偲偟傑偡丅 */
			flags |= FL_SIGNEDCONV;
			if(!(flags & FL_PRECSPEC)) precision = 6;
			s = f_print(s, flags, precision, &ap);
/*{{2011/11/16捛壛 'g'(媦傃'G')娙堈懳墳*/
			/* * Wed Nov 16 02:21:10 JST 2011 Naoyuki Sawa
			 * - 宆僼傿乕儖僪暥帤'g'(媦傃'G')偵娙堈懳墳偟傑偟偨丅
			 * - 杮棃偺丄'g'(媦傃'G')偺摿挜偼丄埲壓偺偲偍傝偱偡丅
			 *   嘆 悢抣偵傛偭偰丄'f'枖偼'e'(枖偼'E')偺彂幃傪丄帺摦慖戰偡傞丅
			 *   嘇 惛搙偼丄彫悢晹偺寘悢偱偼側偔丄惍悢晹+彫悢晹偺寘悢偱偁傞丅
			 *   嘊 彫悢晹偺枛旜偵偁傞'0'(慡偰'0'側傜偽'.'傕)傪丄昞帵偟側偄丅
			 * - 'g'(媦傃'G')傪梡偄傞棟桼偼丄嘊偺栚揑偑庡偱偁傞傛偆偱偡丅
			 *   偦偙偱崱夞偼丄嘆嘇偼懳墳偣偢丄嘊偺傒傪娙堈懳墳偟傑偟偨丅
			 * - 'g'(媦傃'G')偺巊梡椺偲偟偰偼丄LUA尵岅偑'g'傪巊梡偟偰偄傑偡丅
			 *   LUA尵岅偼慡偰偺悢抣偑幚悢偱偡偑丄'g'傪巊梡偟偰悢抣傪昞帵偟偰偄傞偨傔丄彫悢晹偺柍偄悢抣偼丄偁偨偐傕惍悢偱偁傞偐偺傛偆偵昞帵偝傟傞偺偱偡丅
			 */
			if((c == 'g') || (c == 'G')) {
				/* 惛搙偑0偱側偗傟偽丄f_print()偺張棟偱丄'.'偲彮側偔偲傕堦寘偺彫悢晹偑丄s偵奿擺偝傟偰偄傞偼偢丅 */
				if(precision) {
					/* s偵奿擺偝傟偨暥帤楍偐傜丄彫悢晹枛旜偺'0'偲丄慡偰'0'側傜偽'.'傕庢傝彍偔丅 */
					for(;;) {
						/* 堦暥帤慜偑'.'側傜偽丄'.'傪庢傝彍偄偰丄敳偗傞丅 */
						       if(*(s - 1) == '.') {
							s--;
							break;
						/* 堦暥帤慜偑'0'側傜偽丄'0'傪庢傝彍偄偰丄宲懕偡傞丅 */
						} else if(*(s - 1) == '0') {
							s--;
						/* 堦暥帤慜偑'1'乣'9'側傜偽丄庢傝彍偐偢偵丄敳偗傞丅 */
						} else {
							break;
						}
					}
				}
			}
/*}}2011/11/16捛壛 'g'(媦傃'G')娙堈懳墳*/
			break;
		}

		zfill = (flags & FL_ZEROFILL) ? '0' : ' ';
		len = s - old_s;
		pad = width - len;
		if(pad > 0) {
			if(!(flags & FL_LJUST)) {
				/* 晞崋(+/-/嬻敀)偲婎悢(0/0x/0X)偼0媗傔偵愭峴偟傑偡丅 */
				if(flags & FL_ZEROFILL) {
					if(flags & FL_SIGNEDCONV) {
						switch(*old_s) {
						case '+':
						case '-':
						case ' ':
							//*out++ = *old_s++;
							//伀2006/07/13 snprintf(),vsnprintf()懳墳
							OUT(*old_s++);
							len--;
							break;
						}
					}
					if(flags & FL_ALT) {
						//switch(c) {
						//case 'o':
						//	*out++ = *old_s++;
						//	len--;
						//	break;
						//case 'p':
						//case 'x':
						//case 'X':
						//	*out++ = *old_s++;
						//	*out++ = *old_s++;
						//	len -= 2;
						//	break;
						//}
						//伀2006/07/13 snprintf(),vsnprintf()懳墳
						switch(c) {
						case 'p':
						case 'x':
						case 'X':
							OUT(*old_s++);
							len--;
							/* FALLTHRU */
						case 'o':
							OUT(*old_s++);
							len--;
							break;
						}
					}
				}
				/* 嬻敀傑偨偼0媗傔丅 */
				while(pad > 0) {
					//*out++ = zfill;
					//伀2006/07/13 snprintf(),vsnprintf()懳墳
					OUT(zfill);
					pad--;
				}
			}
		}
		while(len > 0) {
			//*out++ = *old_s++;
			//伀2006/07/13 snprintf(),vsnprintf()懳墳
			OUT(*old_s++);
			len--;
		}
		while(pad > 0) {
			//*out++ = zfill;
			//伀2006/07/13 snprintf(),vsnprintf()懳墳
			OUT(zfill);
			pad--;
		}
	}

//{{2014/10/02曄峏:snprintf(),vsnprintf()偺栠傝抣偺嫇摦傪VC++6.0弨嫆佀C99弨嫆偵曄峏偟傑偟偨丅
//	//*out = '\0'; /* 廔抂偺NUL朰傟偢偵! */
//	//return out - old_out;
//	//伀2006/07/13 snprintf(),vsnprintf()懳墳
//	if(out <= end) {
//		*out = '\0';
//		return out - old_out;
//	} else {
//		if(end >= old_out) {
//			*end = '\0';
//		} else {
//			/* 僶僢僼傽僒僀僘偑0埲壓偺応崌偼丄nul傕奿擺偟側偄丅 */
//		}
//		return -1;
//	}
//伀2014/10/02曄峏:snprintf(),vsnprintf()偺栠傝抣偺嫇摦傪VC++6.0弨嫆佀C99弨嫆偵曄峏偟傑偟偨丅
	if(out < end) {
		*out = '\0';
	} else {
		if(end > old_out) {
			*(end - 1) = '\0';
		} else {
			/* 僶僢僼傽僒僀僘偑0埲壓偺応崌偼丄nul傕奿擺偟側偄丅 */
		}
	}
	return out - old_out;	//僶僢僼傽晄懌帪偼(-1)偱側偔乽傕偟廫暘側僗儁乕僗偑偁傟偽彂偒崬傑傟偨偱偁傠偆暥帤偺屄悢乿傪曉偟傑偡丅
//}}2014/10/02曄峏:snprintf(),vsnprintf()偺栠傝抣偺嫇摦傪VC++6.0弨嫆佀C99弨嫆偵曄峏偟傑偟偨丅

/*{{2006/07/13 snprintf(),vsnprintf()懳墳*/
#undef OUT
/*}}2006/07/13 snprintf(),vsnprintf()懳墳*/
}

/****************************************************************************
 *
 ****************************************************************************/

/* 乽#include <piece.h>乿偺慜偵乽#define NOPCESPRINTF乿傪朰傟偢偵両 */

/* P/ECE僇乕僱儖傑偨偼EPSON儔僀僽儔儕撪偺娭悢傪抲偒姺偊傑偡丅 */
int
vsprintf(char* out, const char* fmt, va_list ap)
{
	//return doprnt(out, fmt, ap);
	//伀2006/07/13 snprintf(),vsnprintf()懳墳
	return doprnt(out, (char*)-1/*柍尷僶僢僼傽偺枛旜*/, fmt, ap);	//佀{{2014/10/02旛峫:尩枾偵偼嵍婰偺巜掕偱偼柍尷僶僢僼傽偵[0xFFFFFFFF]偑娷傑傟傑偣傫丅杮摉偵[0xFFFFFFFF]傪娷傓帠偼峫偊傜傟側偄偺偱幚幙栤戣偁傝傑偣傫丅}}
}

/* P/ECE僇乕僱儖傑偨偼EPSON儔僀僽儔儕撪偺娭悢傪抲偒姺偊傑偡丅 */
int
sprintf(char* out, const char* fmt, ...)
{
	int n;
	va_list ap;
	va_start(ap, fmt);
	n = vsprintf(out, fmt, ap);
	va_end(ap);
	return n;
}

/*{{2006/07/13 snprintf(),vsnprintf()懳墳*/

/* * Thu Jul 13 06:12:00 JST 2006 Naoyuki Sawa
 * - vsnprintf()傪捛壛偟傑偟偨丅
 *   P/ECE奐敪娐嫬偺儔僀僽儔儕偵偼丄vsnprintf()偑娷傑傟偰偄傑偣傫丅
 * - vsnprintf()偼丄C99偵偰惓幃偵捛壛偝傟偨娭悢偱偡丅
 *   C99偵弨嫆偟偰偄側偄僐儞僷僀儔偱偼丄vsnprintf()傪僒億乕僩偟偰偄側偐偭偨傝丄嫇摦偑堘偭偰偄傞応崌偑偁傝傑偡丅
 *   偨偲偊偽VC++6.0偺_vsnprintf()偼丄弌椡暥帤悢偑(n-1)傪挻偊傞応崌偺嫇摦偑堘偭偰偄傑偡丅
 *   弌椡暥帤悢偑(n-1)傪挻偊傞応崌丄C99偺巇條偱偼丄(n-1)暥帤傑偱暥帤傪弌椡偟丄(n)暥帤栚偵nul傪奿擺偟傑偡丅
 *   偦傟偵懳偟偰VC++6.0偺巇條偱偼丄(n)暥帤傑偱暥帤傪弌椡偟丄nul傪奿擺偟傑偣傫丅(strncpy()偵帡偨嫇摦偱偡丅)
 *   C99偺巇條偺曽偑丄弌椡暥帤楍偑妋幚偵nul廔抂偝傟傞偺偱丄巊偄堈偄偲巚偄傑偡丅
 *   崱夞偺幚憰偼丄C99偺巇條偵廬偄傑偟偨丅
 * - C99偲VC++6.0偺嫇摦偺堘偄傪帵偡僒儞僾儖僐乕僪傪丄埲壓偵帵偟傑偡丅
 *   埲壓偺僒儞僾儖僐乕僪偼snprintf()偺椺偱偡偑丄vsnprintf()傕摨條偱偡丅
 *
 *	main() {
 *	    char buf[] = "0123456789";
 *	    snprintf(buf, 5, "ABCDEFG");     // C99偺嫇摦乧
 *	    printf("<%s>\n", buf);           //   寢壥乽ABCD乿
 *	    _snprintf(buf, 5, "ABCDEFG");    // VC++6.0偺嫇摦乧
 *	    printf("<%s>\n", buf);           //   寢壥乽ABCDE56789乿
 *	}
 *
 * - 嶲峫帒椏乽僾儘僌儔儈儞僌尵岅 C 偺怴婡擻 (http://seclan.dll.jp/c99d/c99d08.htm#dt19991115)乿傛傝
 *	劇劅劅劅劍劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劉
 *	劆柤慜	劆int vsnprintf(char * restrict s, size_t n, const char * restrict format, va_list arg);		劆
 *	劌劅劅劅劑劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劎
 *	劆僿僢僟劆stdio.h												劆
 *	劌劅劅劅劑劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劎
 *	劆堷悢	劆s      : 弌椡愭僶僢僼傽										劆
 *	劆	劆n      : 弌椡愭僶僢僼傽偺暥帤悢									劆
 *	劆	劆format : 彂幃												劆
 *	劆	劆arg    : va_start 偱弶婜壔偟偨抣									劆
 *	劌劅劅劅劑劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劎
 *	劆栠傝抣劆弌椡偟偨暥帤悢丅廔抂偺僰儖暥帤偼娷傑側偄丅僄儔乕帪偼晧偺抣丅						劆	佀{{2014/10/02曄峏:snprintf(),vsnprintf()偺栠傝抣偺嫇摦傪VC++6.0弨嫆佀C99弨嫆偵曄峏偟傑偟偨丅僶僢僼傽晄懌帪偼(-1)偱側偔乽傕偟廫暘側僗儁乕僗偑偁傟偽彂偒崬傑傟偨偱偁傠偆暥帤偺屄悢乿傪曉偟傑偡丅僄儔乕偑敪惗偡傞偙偲偼柍偄偺偱幚幙(-1)偑曉傞偙偲偼桳傝傑偣傫丅徻嵶偼乽Thu Oct 02 01:02:39 JST 2014乿偺僐儊儞僩傪嶲徠偟偰偔偩偝偄丅}}
 *	劌劅劅劅劑劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劎
 *	劆夝愢	劆vsprintf 偺 弌椡暥帤悢懳墳斉丅format 偵廬偄暥帤傪彂幃壔偟偰 s 偵嵟戝 n - 1 暥帤弌椡偡傞丅		劆
 *	劆	劆n - 1 暥帤埲忋弌椡偝傟偨応崌偼攋婞偝傟傞丅s 偺廔抂偵偼僰儖暥帤偑晅壛偝傟傞丅				劆
 *	劆	劆嶲峫 : wchar_t 傪庴偗晅偗傞娭悢 vswprintf 偑偁傝丄偙傟偼僶僢僼傽暥帤悢傪巜掕偱偒傞巇條偵側偭偰偄傞丅	劆
 *	劋劅劅劅劏劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劊
 */
int
vsnprintf(char* out, int cnt, const char* fmt, va_list ap)
{
//{{2014/10/02曄峏:snprintf(),vsnprintf()偺栠傝抣偺嫇摦傪VC++6.0弨嫆佀C99弨嫆偵曄峏偟傑偟偨丅
//	return doprnt(out, out + cnt - 1/*僶僢僼傽偺枛旜*/, fmt, ap);
//伀2014/10/02曄峏:snprintf(),vsnprintf()偺栠傝抣偺嫇摦傪VC++6.0弨嫆佀C99弨嫆偵曄峏偟傑偟偨丅
	return doprnt(out, (out + cnt)/*僶僢僼傽偺枛旜+1*/, fmt, ap);
//}}2014/10/02曄峏:snprintf(),vsnprintf()偺栠傝抣偺嫇摦傪VC++6.0弨嫆佀C99弨嫆偵曄峏偟傑偟偨丅
}

/* * Thu Jul 13 06:12:00 JST 2006 Naoyuki Sawa
 * - snprintf()傪捛壛偟傑偟偨丅
 *   P/ECE奐敪娐嫬偺儔僀僽儔儕偵偼丄snprintf()偑娷傑傟偰偄傑偣傫丅
 * - VC++6.0偺_snprintf()偲偺嫇摦偺堘偄偵偮偄偰偼丄vsnprintf()偲摨條偱偡丅
 *   vsnprintf()偺僐儊儞僩傪嶲徠偟偰偔偩偝偄丅
 *
 * - 嶲峫帒椏乽僾儘僌儔儈儞僌尵岅 C 偺怴婡擻 (http://seclan.dll.jp/c99d/c99d08.htm#dt19991115)乿傛傝
 *	劇劅劅劅劍劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劉
 *	劆柤慜	劆int snprintf(char * restrict s, size_t n, const char * restrict format, ...);				劆
 *	劌劅劅劅劑劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劎
 *	劆僿僢僟劆stdio.h												劆
 *	劌劅劅劅劑劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劎
 *	劆堷悢	劆s      : 弌椡愭僶僢僼傽										劆
 *	劆	劆n      : 弌椡愭僶僢僼傽偺暥帤悢									劆
 *	劆	劆format : 彂幃												劆
 *	劆	劆...    : 彂幃偱巜掕偟偨抣										劆
 *	劌劅劅劅劑劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劎
 *	劆栠傝抣劆弌椡偟偨暥帤悢丅廔抂偺僰儖暥帤偼娷傑側偄丅僄儔乕帪偼晧偺抣丅						劆	佀{{2014/10/02曄峏:snprintf(),vsnprintf()偺栠傝抣偺嫇摦傪VC++6.0弨嫆佀C99弨嫆偵曄峏偟傑偟偨丅僶僢僼傽晄懌帪偼(-1)偱側偔乽傕偟廫暘側僗儁乕僗偑偁傟偽彂偒崬傑傟偨偱偁傠偆暥帤偺屄悢乿傪曉偟傑偡丅僄儔乕偑敪惗偡傞偙偲偼柍偄偺偱幚幙(-1)偑曉傞偙偲偼桳傝傑偣傫丅徻嵶偼乽Thu Oct 02 01:02:39 JST 2014乿偺僐儊儞僩傪嶲徠偟偰偔偩偝偄丅}}
 *	劌劅劅劅劑劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劎
 *	劆夝愢	劆sprintf 偺弌椡暥帤悢巜掕斉丅format 偵廬偄暥帤傪彂幃壔偟偰 s 偵嵟戝 n - 1 暥帤弌椡偡傞丅		劆
 *	劆	劆n - 1 暥帤埲忋弌椡偝傟偨応崌偼攋婞偝傟傞丅s 偺廔抂偵偼僰儖暥帤偑晅壛偝傟傞丅				劆
 *	劆	劆嶲峫 : wchar_t 傪庴偗晅偗傞娭悢 swprintf 偑偁傝丄偙傟偼僶僢僼傽暥帤悢傪巜掕偱偒傞巇條偵側偭偰偄傞丅	劆
 *	劋劅劅劅劏劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劊
 */
int
snprintf(char* out, int cnt, const char* fmt, ...)
{
	int n;
	va_list ap;
	va_start(ap, fmt);
	n = vsnprintf(out, cnt, fmt, ap);
	va_end(ap);
	return n;
}
/*}}2006/07/13 snprintf(),vsnprintf()懳墳*/

/****************************************************************************
 *
 ****************************************************************************/

//{{2014/10/02捛壛:snprintf(),vsnprintf()偺栠傝抣偺嫇摦傪VC++6.0弨嫆佀C99弨嫆偵曄峏偟傑偟偨丅scprintf(),vscprintf(),asprintf(),vasprintf()傪捛壛偟傑偟偨丅

//	* Thu Oct 02 01:02:39 JST 2014 Naoyuki Sawa
//	- snprintf(),vsnprintf()偺栠傝抣偺嫇摦傪VC++6.0弨嫆佀C99弨嫆偵曄峏偟傑偟偨丅
//	  scprintf(),vscprintf(),asprintf(),vasprintf()傪捛壛偟傑偟偨丅
//	  徻嵶偼丄埲壓偺崁栚傪嶲徠偟偰偔偩偝偄丅
//	- 偙傟傑偱丄廔抂偺nul偺嫇摦偵偮偄偰偼C99弨嫆偵偟偰偄偨偺偱偡偑丄snprintf偺僶僢僼傽晄懌帪偺栠傝抣偺嫇摦偑VC++6.0偺嫇摦偵側偭偰偟傑偭偰偄傑偟偨丅
//	  C99(媦傃GCC)偼乽傕偟廫暘側僗儁乕僗偑偁傟偽彂偒崬傑傟偨偱偁傠偆暥帤偺屄悢乿傪曉偟傑偡丅VC++6.0偼乽晧偺抣(幚嵺偵偼-1)乿傪曉偟傑偡丅乧嶲峫帒椏嘆
//	  snprintf偺僶僢僼傽晄懌帪偺栠傝抣偺嫇摦傕丄C99弨嫆偺曽偑曋棙偱偁傞偨傔丄C99弨嫆偵曄峏偡傞偙偲偵偟傑偟偨丅
//	- 偙偺曄峏偑丄婛懚偺僾儘僌儔儉偵塭嬁偡傞壜擻惈偑偁傞偨傔丄拲堄偟偰偔偩偝偄丅
//	  摉儔僀僽儔儕撪偱偼丄cliptime.c偺strftime()偵塭嬁偑桳偭偨偨傔丄strftime()傪廋惓偟傑偟偨丅
//	  崱屻偼丄snprintf偺栠傝抣偐傜僶僢僼傽晄懌偐傪敾抐偡傞偵偼丄乽retval<0乿偱偼側偔乽(unsigned)retval>=(unsigned)cnt乿偲偟偰偔偩偝偄丅
//	- VC++娐嫬偱傕丄_vscprintf傪巊梡偡傞偙偲偱丄C99弨嫆偺嫇摦傪僔儈儏儗乕僩偡傞偙偲偑偱偒傑偡丅乧嶲峫帒椏嘇
//	  偨偩偟丄VC++6.0偵偼_vscprintf偼桳傝傑偣傫丅(Visual Studio 2005埲崀偱捛壛偝傟偨傛偆偱偡丅)
//	  _vscprintf偼丄偦傟扨撈偱傕曋棙偦偆側偺偱丄P/ECE娐嫬傗VC++6.0娐嫬偱傕巊梡偱偒傞傛偆丄摉儔僀僽儔儕撪偱幚憰偡傞偙偲偵偟傑偟偨丅
//	- _vscprintf偲儊儌儕妋曐傪暪梡偡傞偲丄僼僅乕儅僢僩暥帤楍傪揥奐偡傞僶僢僼傽偺妋曐偑峴偊傑偡丅乧嶲峫帒椏嘊
//	  GCC偵偼丄忋婰傪傑偲傔偰峴偆娭悢偲偟偰丄asprintf偑桳傝傑偡丅乧嶲峫帒椏嘋
//	  曋棙側偺偱丄P/ECE娐嫬傗VC++6.0娐嫬偱傕巊梡偱偒傞傛偆丄摉儔僀僽儔儕撪偱幚憰偡傞偙偲偵偟傑偟偨丅
//	  側偍丄僄儔乕偑惗偠偨帪偺strp偺嫇摦偼丄FreeBSD偺幚憰偵屳姺偲偟丄僄儔乕偺嵺偵偼strp傪NULL偵僙僢僩偡傞偙偲偵偟傑偟偨丅
//	- asprintf傪巊偆偲丄擇偮偺暥帤楍傪楢寢偟偰丄怴偟偄儊儌儕僽儘僢僋傪嶌惉偡傞偙偲偑偱偒傑偡丅
//	  strdup()偲strcat()傪摨帪偵峴偆傛偆側姶偠偱偡丅乧嶲峫帒椏嘍
//	
//	仭嶲峫帒椏嘆乽Stack Overflow乿(http://stackoverflow.com/)偺丄亀snprintf and Visual Studio 2010亁(http://stackoverflow.com/questions/2915672/snprintf-and-visual-studio-2010)偺儁乕僕傛傝堷梡:
//	劔Microsoft says its customers aren't pressuring enough for C99 support. But with snprintf part of C++11, you're likely to see it implemented by 2014.
//	劔Until then, you can simulate (v)snprintf like this:
//	劔劆#ifdef  _MSC_VER
//	劔劆#define snprintf c99_snprintf
//	劔劆inline int c99_snprintf(char* str, size_t size, const char* format, ...) {
//	劔劆  int count;
//	劔劆  va_list ap;
//	劔劆  va_start(ap, format);
//	劔劆  count = c99_vsnprintf(str, size, format, ap);
//	劔劆  va_end(ap);
//	劔劆  return count;
//	劔劆}
//	劔劆inline int c99_vsnprintf(char* str, size_t size, const char* format, va_list ap) {
//	劔劆    int count = -1;
//	劔劆    if(size != 0)
//	劔劆      count = _vsnprintf_s(str, size, _TRUNCATE, format, ap);
//	劔劆    if(count == -1)
//	劔劆      count = _vscprintf(format, ap);
//	劔劆    return count;
//	劔劆}
//	劔劆#endif//_MSC_VER
//	劔As mentioned in the standard:
//	劔丒at most size bytes get written (including terminating '\0')
//	劔丒if output was truncated, the return value is the number of characters (excluding terminating '\0') that would have been written
//	劔劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅
//	劔This will not always terminate the string with a 0 which is required on a overflow.
//	劔The second if in c99_vsnprintf must be: if (count == -1) { if (size > 0) str[size - 1] = 0; count = _vscprintf(format, ap); }
//	
//	仭嶲峫帒椏嘇乽Microsoft Developer Network乿(http://msdn.microsoft.com/)偺丄亀_vscprintf丄_vscprintf_l丄_vscwprintf丄_vscwprintf_l亁(http://msdn.microsoft.com/ja-jp/library/w05tbk72.aspx)偺儁乕僕傛傝堷梡:
//	劔堷悢儕僗僩傊偺億僀儞僞乕傪巊梡偟偰彂幃巜掕偝傟偨暥帤楍偺暥帤悢傪曉偟傑偡丅
//	劔              int _vscprintf(const char* format, va_list argptr);
//	劔              int _vscprintf_l(const char* format, locale_t locale, va_list argptr);
//	劔              int _vscwprintf(const wchar_t* format, va_list argptr);
//	劔              int _vscwprintf_l(const wchar_t* format, locale_t locale, va_list argptr);
//	劔僷儔儊乕僞乕  format 彂幃巜掕暥帤楍丅
//	劔              argptr 堷悢儕僗僩傊偺億僀儞僞乕丅
//	劔              locale 巊梡偡傞儘働乕儖丅
//	劔栠傝抣        _vscprintf娭悢偼丄堷悢儕僗僩偑巜偡暥帤楍偑丄巜掕偺彂幃壔僐乕僪傪巊梡偟偰弌椡偝傟傞偐丄傑偨偼僼傽僀儖傗僶僢僼傽乕偵憲怣偝傟偨応崌偵惗惉偝傟傞暥帤悢傪曉偟傑偡丅
//	劔              曉偝傟傞抣偵偼丄廔抂偺NULL暥帤偼娷傑傟傑偣傫丅儚僀僪暥帤偺応崌偼丄_vscwprintf偑摨偠婡擻傪幚峴偟傑偡丅
//	劔              _l僒僼傿僢僋僗偑晅偄偰偄傞偙傟傜偺娭悢偺奺僶乕僕儑儞偼丄尰嵼偺僗儗僢僪儘働乕儖偺戙傢傝偵搉偝傟偨儘働乕儖僷儔儊乕僞乕傪巊梡偡傞揰傪彍偄偰摨偠偱偡丅
//	劔              format偑null億僀儞僞乕偺応崌偼丄乽僷儔儊乕僞乕偺専徹乿偵愢柧偝傟偰偄傞傛偆偵丄柍岠側僷儔儊乕僞乕僴儞僪儔乕偑屇傃弌偝傟傑偡丅
//	劔              幚峴偺宲懕偑嫋壜偝傟偨応崌丄娭悢偼-1傪曉偟丄errno傪EINVAL偵愝掕偟傑偡丅
//	劔夝愢          奺argument偼format偺懳墳偡傞彂幃巜掕偵廬偭偰(懚嵼偡傞応崌)偵曄姺偝傟傑偡丅format偼捠忢偺暥帤偱峔惉偟丄偦偺宍幃偲婡擻偼printf娭悢偺format偲摨偠偱偡丅
//			
//	仭嶲峫帒椏嘊乽jianji-production乿(http://jianji.noor.jp/)偝傫偺丄亀僼僅乕儅僢僩暥帤楍傪揥奐偡傞僶僢僼傽偺妋曐亁(http://jianji.noor.jp/wiki/index.cgi?page=%A5%D5%A5%A9%A1%BC%A5%DE%A5%C3%A5%C8%CA%B8%BB%FA%CE%F3%A4%F2%C5%B8%B3%AB%A4%B9%A4%EB%A5%D0%A5%C3%A5%D5%A5%A1%A4%CE%B3%CE%CA%DD)偺儁乕僕傛傝堷梡:
//	劔僐儞僷僀儖娐嫬偵傛偭偰暘偐傟傞傕傛偆丅
//	劔仩GNU奼挘偺応崌
//	劔vasprintf偱僶僢僼傽偺妋曐偲僐僺乕傪摨帪偵峴偆丅
//	劔劆void MyPrintf(const char* format, ...) {
//	劔劆  va_list argptr;
//	劔劆  va_start(argptr, format);
//	劔劆  char* buf_ = NULL;
//	劔劆  vasprintf(&buf_, format, argptr);
//	劔劆  MyPrintString(buf_);
//	劔劆  free(buf_);
//	劔劆  va_end(argptr);
//	劔劆}
//	劔仩VC奼挘偺応崌
//	劔_vscprintf偱揥奐屻偺暥帤楍挿傪庢摼偟丄偦傟偵+1偟偨挿偝偺僶僢僼傽傪梡堄偡傞丅
//	劔劆void MyPrintf(const char* format, ...) {
//	劔劆  va_list argptr;
//	劔劆  va_start(argptr, format);
//	劔劆  int length = _vscprintf(format, argptr);
//	劔劆  char* buf_ = new char [length + 1];
//	劔劆  vsprintf(buf_, format, argptr);
//	劔劆  MyPrintString(buf_);
//	劔劆  delete[] buf_;
//	劔劆  va_end(argptr);
//	劔劆}
//	劔VC奼挘偼儊儌儕妋曐傪奜晹壔偱偒傞偺偱丄傛傝廮擃側報徾丅
//	
//	仭嶲峫帒椏嘋乽JM Project乿(http://linuxjm.sourceforge.jp/)偺丄亀Man page of ASPRINTF亁(http://linuxjm.sourceforge.jp/html/LDP_man-pages/man3/asprintf.3.html)偺儁乕僕傛傝堷梡:
//	劔柤慜          asprintf, vasprintf - 暥帤楍傪妱傝摉偰偰偦傟偵弌椡偡傞
//	劔彂幃          #define _GNU_SOURCE /* feature_test_macros(7) 嶲徠 */
//	劔              #include <stdio.h>
//	劔              int asprintf(char** strp, const char* fmt, ...);
//	劔              int vasprintf(char** strp, const char* fmt, va_list ap);
//	劔愢柧          asprintf()娭悢偲vasprintf()娭悢偲偼丄偦傟偧傟sprintf(3)娭悢偲vsprintf(3)娭悢偲偵帡偰偄傞偑丄
//	劔              弌椡暥帤楍傪(廔抂偺僰儖僶僀僩('\0')傕娷傔偰)曐帩偡傞偺偵廫暘側戝偒偝偺儊儌儕傪妋曐偟丄嵟弶偺堷悢偵偦偺暥帤楍傊偺億僀儞僞傪曉偡丅
//	劔              偙偺億僀儞僞偼丄晄梫偵側偭偨傜free(3)偵搉偟丄妱傝摉偰傜傟偨婰壇椞堟傪夝曻偡傋偒偱偁傞丅
//	劔曉傝抣        惉岟偡傞偲丄偙傟傜偺娭悢偼弌椡偝傟偨僶僀僩悢傪(sprintf(3)偺傛偆偵)曉偡丅
//	劔              儊儌儕偺妱傝摉偰偑偱偒側偐偭偨傝丄偦偺懠僄儔乕偑惗偠傞偲丄偙傟傜偺娭悢偼-1傪曉偟丄strp偺撪梕偼枹掕媊偲側傞丅
//	劔弨嫆          偙傟傜偺娭悢偼GNU偺奼挘偱偁傝丄C傗POSIX偺傕偺偱偼側偄丅偙傟傜偼*BSD偱傕棙梡偱偒傞丅
//	劔              FreeBSD偺幚憰偱偼丄僄儔乕偺嵺偵偼strp傪NULL偵僙僢僩偡傞丅
//	
//	仭嶲峫帒椏嘍乽Stack Overflow乿(http://stackoverflow.com/)偺丄亀Is there a neat way to do strdup() followed by strcat()?亁(http://stackoverflow.com/questions/12591074/is-there-a-neat-way-to-do-strdup-followed-by-strcat)偺儁乕僕傛傝堷梡:
//	劔Say I wanted to duplicate a string then concatenate a value to it.
//	劔Using stl std::string, it's:
//	劔劆string s = "hello";
//	劔劆string s2 = s + " there"; //effectively dup/cat
//	劔in C:
//	劔劆char* s = "hello";
//	劔劆char* s2 = strdup(s);
//	劔劆strcat(s2, " there"); //s2 is too short for this operation
//	劔The only way I know to do this in C is:
//	劔劆char* s = "hello";
//	劔劆char* s2 = (char*)malloc(strlen(s) + strlen(" there") + 1); //allocate enough space
//	劔劆strcpy(s2, s);
//	劔劆strcat(s2, " there");
//	劔Is there a more elegant way to do this in C?
//	劔劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅
//	劔A GNU extension is asprintf() that allocates the required buffer:
//	劔劆char* s2;
//	劔劆if(-1 != asprintf(&s2, "%s%s", "hello", " there") {
//	劔劆  free(s2);
//	劔劆}

int scprintf(const char* fmt, ...) {
	int n;
	va_list ap;
	va_start(ap, fmt);
	n = vscprintf(fmt, ap);
	va_end(ap);
	return n;
}
int vscprintf(const char* fmt, va_list ap) {
	return doprnt(NULL, NULL, fmt, ap);
}

int asprintf(char** strp, const char* fmt, ...) {
	int n;
	va_list ap;
	va_start(ap, fmt);
	n = vasprintf(strp, fmt, ap);
	va_end(ap);
	return n;
}
int vasprintf(char** strp, const char* fmt, va_list ap) {
	if(!(*strp = malloc(vscprintf(fmt, ap) + 1/*nul*/))) { return -1; }
	return vsprintf(*strp, fmt, ap);
}

//}}2014/10/02捛壛:snprintf(),vsnprintf()偺栠傝抣偺嫇摦傪VC++6.0弨嫆佀C99弨嫆偵曄峏偟傑偟偨丅scprintf(),vscprintf(),asprintf(),vasprintf()傪捛壛偟傑偟偨丅
