精减版printf
来源:互联网 发布:辐射3胜利狙击枪数据 编辑:程序博客网 时间:2024/05/22 07:53
转自:http://blog.csdn.net/xgbing/article/details/7642664
sstdio.c
- /* ----------------------------------------------------------------------------
- * ATMEL Microcontroller Software Support
- * ----------------------------------------------------------------------------
- * Copyright (c) 2008, Atmel Corporation
- *
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * - Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the disclaimer below.
- *
- * Atmel's name may not be used to endorse or promote products derived from
- * this software without specific prior written permission.
- *
- * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
- * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
- * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- * ----------------------------------------------------------------------------
- */
- //------------------------------------------------------------------------------
- /// \unit
- ///
- /// !Purpose
- ///
- /// Implementation of several stdio.h methods, such as printf(), sprintf() and
- /// so on. This reduces the memory footprint of the binary when using those
- /// methods, compared to the libc implementation.
- ///
- /// !Usage
- ///
- /// Adds stdio.c to the list of file to compile for the project. This will
- /// automatically replace libc methods by the custom ones.
- //------------------------------------------------------------------------------
- //------------------------------------------------------------------------------
- // Headers
- //------------------------------------------------------------------------------
- #include <stdarg.h>
- #include "sstdio.h"
- #include "debug.h"
- //------------------------------------------------------------------------------
- // Local Definitions
- //------------------------------------------------------------------------------
- // Maximum string size allowed (in bytes).
- #define MAX_STRING_SIZE 100
- //------------------------------------------------------------------------------
- // Global Variables
- //------------------------------------------------------------------------------
- //------------------------------------------------------------------------------
- // Local Functions
- //------------------------------------------------------------------------------
- //------------------------------------------------------------------------------
- // Writes a character inside the given string. Returns 1.
- // \param pStr Storage string.
- // \param c Character to write.
- //------------------------------------------------------------------------------
- static signed int PutChar(char *pStr, char c)
- {
- *pStr = c;
- return 1;
- }
- //------------------------------------------------------------------------------
- // Writes a string inside the given string.
- // Returns the size of the written
- // string.
- // \param pStr Storage string.
- // \param pSource Source string.
- //------------------------------------------------------------------------------
- static signed int PutString(char *pStr, const char *pSource)
- {
- signed int num = 0;
- while (*pSource != 0) {
- *pStr++ = *pSource++;
- num++;
- }
- return num;
- }
- //------------------------------------------------------------------------------
- // Writes an unsigned int inside the given string, using the provided fill &
- // width parameters.
- // Returns the size in characters of the written integer.
- // \param pStr Storage string.
- // \param fill Fill character.
- // \param width Minimum integer width.
- // \param value Integer value.
- //------------------------------------------------------------------------------
- #if 0
- static signed int PutUnsignedInt(
- char *pStr,
- char fill,
- signed int width,
- unsigned int value)
- {
- signed int num = 0;
- // Take current digit into account when calculating width
- width--;
- // Recursively write upper digits
- if ((value / 10) > 0) {
- num = PutUnsignedInt(pStr, fill, width, value / 10);
- pStr += num;
- }
- // Write filler characters
- else {
- while (width > 0) {
- PutChar(pStr, fill);
- pStr++;
- num++;
- width--;
- }
- }
- // Write lower digit
- num += PutChar(pStr, (value % 10) + '0');
- return num;
- }
- //------------------------------------------------------------------------------
- // Writes a signed int inside the given string, using the provided fill & width
- // parameters.
- // Returns the size of the written integer.
- // \param pStr Storage string.
- // \param fill Fill character.
- // \param width Minimum integer width.
- // \param value Signed integer value.
- //------------------------------------------------------------------------------
- static signed int PutSignedInt(
- char *pStr,
- char fill,
- signed int width,
- signed int value)
- {
- signed int num = 0;
- unsigned int absolute;
- // Compute absolute value
- if (value < 0) {
- absolute = -value;
- }
- else {
- absolute = value;
- }
- // Take current digit into account when calculating width
- width--;
- // Recursively write upper digits
- if ((absolute / 10) > 0) {
- if (value < 0) {
- num = PutSignedInt(pStr, fill, width, -(absolute / 10));
- }
- else {
- num = PutSignedInt(pStr, fill, width, absolute / 10);
- }
- pStr += num;
- }
- else {
- // Reserve space for sign
- if (value < 0) {
- width--;
- }
- // Write filler characters
- while (width > 0) {
- PutChar(pStr, fill);
- pStr++;
- num++;
- width--;
- }
- // Write sign
- if (value < 0) {
- num += PutChar(pStr, '-');
- pStr++;
- }
- }
- // Write lower digit
- num += PutChar(pStr, (absolute % 10) + '0');
- return num;
- }
- #endif
- //------------------------------------------------------------------------------
- // Writes an hexadecimal value into a string, using the given fill, width &
- // capital parameters.
- // Returns the number of char written.
- // \param pStr Storage string.
- // \param fill Fill character.
- // \param width Minimum integer width.
- // \param maj Indicates if the letters must be printed in lower- or upper-case.
- // \param value Hexadecimal value.
- //------------------------------------------------------------------------------
- static signed int PutHexa(
- char *pStr,
- char fill,
- signed int width,
- unsigned char maj,
- unsigned int value)
- {
- signed int num = 0;
- // Decrement width
- width--;
- // Recursively output upper digits
- if ((value >> 4) > 0) {
- num += PutHexa(pStr, fill, width, maj, value >> 4);
- pStr += num;
- }
- // Write filler chars
- else {
- while (width > 0) {
- PutChar(pStr, fill);
- pStr++;
- num++;
- width--;
- }
- }
- // Write current digit
- if ((value & 0xF) < 10) {
- PutChar(pStr, (value & 0xF) + '0');
- }
- else if (maj) {
- PutChar(pStr, (value & 0xF) - 10 + 'A');
- }
- else {
- PutChar(pStr, (value & 0xF) - 10 + 'a');
- }
- num++;
- return num;
- }
- //------------------------------------------------------------------------------
- // Global Functions
- //------------------------------------------------------------------------------
- //------------------------------------------------------------------------------
- /// Stores the result of a formatted string into another string. Format
- /// arguments are given in a va_list instance.
- /// Return the number of characters written.
- /// \param pStr Destination string.
- /// \param length Length of Destination string.
- /// \param pFormat Format string.
- /// \param ap Argument list.
- //------------------------------------------------------------------------------
- static signed int t_vsnprintf(char *pStr, unsigned int length, const char *pFormat, va_list ap)
- {
- char fill;
- unsigned char width;
- signed int num = 0;
- signed int size = 0;
- // Clear the string
- if (pStr) {
- *pStr = 0;
- }
- // Phase string
- while (*pFormat != 0 && size < length) {
- // Normal character
- if (*pFormat != '%') {
- *pStr++ = *pFormat++;
- size++;
- }
- // Escaped '%'
- else if (*(pFormat+1) == '%') {
- *pStr++ = '%';
- pFormat += 2;
- size++;
- }
- // Token delimiter
- else {
- fill = ' ';
- width = 0;
- pFormat++;
- // Parse filler
- if (*pFormat == '0') {
- fill = '0';
- pFormat++;
- }
- // Parse width
- while ((*pFormat >= '0') && (*pFormat <= '9')) {
- width = (width*10) + *pFormat-'0';
- pFormat++;
- }
- // Check if there is enough space
- if (size + width > length) {
- width = length - size;
- }
- // Parse type
- switch (*pFormat) {
- //case 'd':
- //case 'i': num = PutSignedInt(pStr, fill, width, va_arg(ap, signed int)); break;
- //case 'u': num = PutUnsignedInt(pStr, fill, width, va_arg(ap, unsigned int)); break;
- case 'x': num = PutHexa(pStr, fill, width, 0, va_arg(ap, unsigned int)); break;
- case 'X': num = PutHexa(pStr, fill, width, 1, va_arg(ap, unsigned int)); break;
- case 's': num = PutString(pStr, va_arg(ap, char *)); break;
- case 'c': num = PutChar(pStr, va_arg(ap, unsigned int)); break;
- default:
- return -1; //EOF
- }
- pFormat++;
- pStr += num;
- size += num;
- }
- }
- // NULL-terminated (final \0 is not counted)
- if (size < length) {
- *pStr = 0;
- }
- else {
- *(--pStr) = 0;
- size--;
- }
- return size;
- }
- //------------------------------------------------------------------------------
- /// Stores the result of a formatted string into another string. Format
- /// arguments are given in a va_list instance.
- /// Return the number of characters written.
- /// \param pString Destination string.
- /// \param pFormat Format string.
- /// \param ap Argument list.
- //------------------------------------------------------------------------------
- static signed int t_vsprintf(char *pString, const char *pFormat, va_list ap)
- {
- return t_vsnprintf(pString, MAX_STRING_SIZE, pFormat, ap);
- }
- //------------------------------------------------------------------------------
- /// Outputs a string on stdout.
- /// \param pStr String to output.
- //------------------------------------------------------------------------------
- static signed int t_fputs(const char *pStr)
- {
- dbg_print(pStr);
- return(0);
- }
- //------------------------------------------------------------------------------
- /// Outputs a formatted string on the given stream. Format arguments are given
- /// in a va_list instance.
- /// \param pStream Output stream.
- /// \param pFormat Format string
- /// \param ap Argument list.
- //------------------------------------------------------------------------------
- static signed int t_vfprintf(const char *pFormat, va_list ap)
- {
- char pStr[MAX_STRING_SIZE];
- char pError[] = "stdio.c: MAX_STRING_SIZE\n\r";
- // Write formatted string in buffer
- if (t_vsprintf(pStr, pFormat, ap) >= MAX_STRING_SIZE) {
- t_fputs(pError);
- while (1); // Increase MAX_STRING_SIZE
- }
- // Display string
- return t_fputs(pStr);
- }
- //------------------------------------------------------------------------------
- /// Outputs a formatted string on the DBGU stream. Format arguments are given
- /// in a va_list instance.
- /// \param pFormat Format string
- /// \param ap Argument list.
- //------------------------------------------------------------------------------
- static signed int t_vprintf(const char *pFormat, va_list ap)
- {
- return t_vfprintf(pFormat, ap);
- }
- //------------------------------------------------------------------------------
- /// Outputs a formatted string on the DBGU stream, using a variable number of
- /// arguments.
- /// \param pFormat Format string.
- //------------------------------------------------------------------------------
- signed int trace(const char *pFormat, ...)
- {
- va_list ap;
- signed int result;
- // Forward call to vprintf
- va_start(ap, pFormat);
- result = t_vprintf(pFormat, ap);
- va_end(ap);
- return result;
- }
sstdio.h
- #ifndef __SSTDIO_H__
- #define __SSTDIO_H__
- signed int trace(const char *pFormat, ...);
- #endif
略加修改,就可以用到嵌入式系统中。
0 0
- 精减版printf
- 精减版printf
- CAM350 10.5完美精减版
- archlinux 20140105版ISO精减安装为桌面工作环境
- 各种版本的称呼--例如Lite指精减版
- 精减struts
- 高精减
- go 文件差量对比,并用天做区分文件夹(几个月的精减版)
- Windows 下精减安装 QT
- printf
- printf
- printf
- printf()
- printf
- printf
- printf
- printf
- printf
- jsp page指令详解
- 告别码农,成为真正的程序员
- Android之SurfaceView学习(一)
- 黑马程序员---动态代理
- crm创建启用停用用户
- 精减版printf
- c++标准库
- SQL Server 日期相关
- ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- grails Domian对象转JSON去class以及自定义字段的最佳方式
- Android fill_parent、wrap_content和match_parent的区别
- IT相关书籍
- JSON-lib框架,转换JSON、XML不再困难
- Copying Books +uva+二分+贪心