📄 stringconv.c
字号:
/*""FILE COMMENT""************************************************************** System Name : RENESAS T-Engine / micro T-Engine* File Name : stringconv.c* Version : 1.01.00* Contents : setup string for display* Model : RENESAS T-Engine / micro T-Engine* CPU : RENESAS T-Engine Series CPU* Compiler : GNU* OS : T-Kernel** note : The Software is being delivered to you "AS IS" * : and Renesas,whether explicitly or implicitly makes * : no warranty as to its Use or performance. * : RENESAS AND ITS SUPPLIER DO NOT AND CANNOT WARRANT * : THE PERFORMANCE OR RESULTS YOU MAY OBTAIN BY USING * : THE SOFTWARE. AS TO ANY MATTER INCLUDING WITHOUT * : LIMITATION NONINFRINGEMENT OF THIRD PARTY RIGHTS,* : MERCHANTABILITY, INTEGRATION, SATISFACTORY QUALITY, * : OR FITNESS FOR ANY PARTICULAR PURPOSE.** Copyright (c) 2004-2006 RENESAS TECHNOLOGY CORP. All Rights Reserved.* AND RENESAS SOLUTIONS CORP. All Rights Reserved.** history : 2004.10.01 ver1.00.00* : 2006.02.01 ver1.01.00*""FILE COMMENT END""*********************************************************/#include <tk/tkernel.h>#include <stdarg.h>#ifdef NOUSE_STDIOLIB/*""FUNC COMMENT""******************************************************* * ID : X.Y.Z * Outline : change value to string *----------------------------------------------------------------------- * Include : *----------------------------------------------------------------------- * Definition : static int valtostr(unsigned char *,unsigned long, * unsigned int ,onst char *,int,int,int) *----------------------------------------------------------------------- * Function : change value to string *----------------------------------------------------------------------- * Argument : unsigned char *string; converted string * : unsigned long val; value * : unsigned int radix; radix * : const char *radchar; base string for convert * : int width; * : int minus; * : int padding; *----------------------------------------------------------------------- * Return : converted counts *----------------------------------------------------------------------- * Input : None * Output : None *----------------------------------------------------------------------- * Used Func : None *----------------------------------------------------------------------- * Notice : None *""FUNC COMMENT END""***************************************************/static int valtostr(unsigned char *string,unsigned long val, unsigned int radix, const char *radchar,int width, int minus, int padding){ char buf[12]; int i, j,count; i = 0; do { buf[i++] = radchar[val % radix]; val /= radix; } while (val != 0); if(width) count = width; else count = i; width -= minus; if (minus > 0 && padding > 0) { *string++ = '-'; } for (j = i; j < width; j++) { *string++ = (char)(padding > 0 ? '0' : ' '); } if (minus > 0 && padding <= 0) { *string++ = '-'; } while (i > 0) *string++ = buf[--i]; return(count);}static char const raddec[] = "0123456789";static char const radhex[] = "0123456789abcdef";static char const radHEX[] = "0123456789ABCDEF";/*""FUNC COMMENT""******************************************************* * ID : X.Y.Z * Outline : convert string for display *----------------------------------------------------------------------- * Include : *----------------------------------------------------------------------- * Definition : void convertstring(unsigned char *,unsigned char *, * : VP *args) *----------------------------------------------------------------------- * Function : convert string for display *----------------------------------------------------------------------- * Argument : unsigned char *dst; convert string * : unsigned char *src; base string * : VP *args; arguments *----------------------------------------------------------------------- * Return : None *----------------------------------------------------------------------- * Input : None * Output : None *----------------------------------------------------------------------- * Used Func : valtostr() *----------------------------------------------------------------------- * Notice : None *""FUNC COMMENT END""***************************************************/void convertstring(unsigned char *dst,unsigned char *src,VP *args){ unsigned char chr; int width,cnt,order; int padding; long value; char *string; while ((chr = *src++) != '\0') { if (chr != '%') { /* string case */ *dst++ = chr; continue; } else { /* %* format -> convert value to string */ width = padding = order = 0; chr = *src++; if (chr == '-') { order = 1; chr = *src++; } else { if(chr == '0') { padding = 1; chr = *src++; } } while ('0' <= chr && chr <= '9') { width = width*10 + chr - '0'; chr = *src++; } switch (chr) { case 'd': value = (long)(*args++); if (value >= 0) { cnt = valtostr(dst,(unsigned long)value, 10, raddec, width, 0, padding); dst += cnt; } else { cnt = valtostr(dst,(unsigned long)(-value), 10, raddec, width, 1, padding); dst += cnt; } break; case 'x': value = (long)(*args++); cnt = valtostr(dst,(unsigned long) value, 16, radhex, width, 0, padding); dst += cnt; break; case 'X': value = (long)(*args++); cnt = valtostr(dst,(unsigned long) value, 16, radHEX, width, 0, padding); dst += cnt; break; case 'c': /* %*s */ chr = (char)(*args++); *dst++ = chr; break; case 's': /* %*s */ string = (char *)(*args++); while ((chr = *string++) != '\0') { *dst++ = chr; } break; case '%': /* %% */ *dst++ = '%'; break; default: break; } } } *dst++ = '\0'; }// typedef char *va_list;char strstrstr[128];int printf( const char *fmt, ... ){ va_list adx; va_start(adx,fmt); convertstring(strstrstr,fmt,adx); va_end( adx ); tm_putstring(strstrstr); return 0;}#endif // NOUSE_STDIOLIB
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -