⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printf_large.c

📁 这项工程将让您把自己的MP3播放器平台
💻 C
📖 第 1 页 / 共 2 页
字号:
/*-------------------------------------------------------------------------
  printf_large.c - formatted output conversion

             Written By - Martijn van Balen aed@iae.nl (1999)
             Added %f By - johan.knol@iduna.nl (2000)
             Refactored by - Maarten Brock (2004)

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA

   In other words, you are welcome to use, share and improve this program.
   You are forbidden to forbid anyone else to use, share and improve
   what you give them.   Help stamp out software-hoarding!
-------------------------------------------------------------------------*/

#if defined(__ds390)
#define USE_FLOATS 1
#endif

#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdbool.h>
#include <sdcc-lib.h>

#define PTR value.ptr

#ifdef SDCC_ds390
#define NULL_STRING "<NULL>"
#define NULL_STRING_LENGTH 6
#endif

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


//++
//  This file comes from the SDCC distribution.  I've made a few minor changes
// to it, particularly to support a precision specification for string fields,
// and it's necessary to use this version with the MP3 player.  These changes
// are all compatible with standard C and at some point they may become part
// of the SDCC distribution.  This file was originally distributed under the
// terms of the LGPL and its use here is consistent with that license.  In the
// interest of saving space I haven't included the entire SDCC source code,
// however if you want it you can obtain it from the Source Forge archive
// at http://sdcc.sourceforge.net
//
//					Bob Armstrong [19-Oct-05]
//					Spare Time Gizmos
//--


//typedef char * ptr_t;
#define ptr_t char *

#ifdef toupper
#undef toupper
#endif

//#define toupper(c) ((c)&=~0x20)
#define toupper(c) ((c)&=0xDF)

typedef union
{
  unsigned char  byte[5];
  long           l;
  unsigned long  ul;
  float          f;
  char           *ptr;
} value_t;

static const char memory_id[] = "IXCP-";

#ifndef SDCC_STACK_AUTO
  static BOOL lower_case;
  static pfn_outputchar output_char;
  static void* p;
  static value_t value;
#endif

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

#ifdef SDCC_STACK_AUTO
  static void output_digit( unsigned char n, BOOL lower_case, pfn_outputchar output_char, void* p )
#else
  static void output_digit( unsigned char n )
#endif
  {
    output_char( n <= 9 ? '0'+n :
               (lower_case ? n+(char)('a'-10) : n+(char)('A'-10)), p );
  }

/*--------------------------------------------------------------------------*/

#ifdef SDCC_STACK_AUTO
  #define OUTPUT_2DIGITS( B )	output_2digits( B, lower_case, output_char, p )
  static void output_2digits( unsigned char b, BOOL lower_case, pfn_outputchar output_char, void* p )
  {
    output_digit( b>>4,   lower_case, output_char, p );
    output_digit( b&0x0F, lower_case, output_char, p );
  }
#else
  #define OUTPUT_2DIGITS( B )	output_2digits( B )
  static void output_2digits( unsigned char b )
  {
    output_digit( b>>4   );
    output_digit( b&0x0F );
  }
#endif

/*--------------------------------------------------------------------------*/

#if defined SDCC_STACK_AUTO
static void calculate_digit( value_t* value, unsigned char radix )
{
  unsigned char i;

  for( i = 32; i != 0; i-- )
  {
    value->byte[4] = (value->byte[4] << 1) | ((value->ul >> 31) & 0x01);
    value->ul <<= 1;

    if (radix <= value->byte[4] )
    {
      value->byte[4] -= radix;
      value->ul |= 1;
    }
  }
}
#else
static void calculate_digit( unsigned char radix )
{
  unsigned char i;

  for( i = 32; i != 0; i-- )
  {
    value.byte[4] = (value.byte[4] << 1) | ((value.ul >> 31) & 0x01);
    value.ul <<= 1;

    if (radix <= value.byte[4] )
    {
      value.byte[4] -= radix;
      value.ul |= 1;
    }
  }
}
#endif

#if USE_FLOATS

/* This is a very inefficient but direct approach, since we have no math
   library yet (e.g. log()).
   It does most of the modifiers, but has some restrictions. E.g. the
   abs(float) shouldn't be bigger than an unsigned long (that's
   about 4294967295), but still makes it usefull for most real-life
   applications.
*/

#define DEFAULT_FLOAT_PRECISION 6

#ifdef SDCC_STACK_AUTO
#define OUTPUT_FLOAT(F, W, D, L, Z, S, P)	output_float(F, W, D, L, Z, S, P, output_char, p)
static int output_float (float f, unsigned char reqWidth,
                         signed char reqDecimals,
                         BOOL left, BOOL zero, BOOL sign, BOOL space,
                         pfn_outputchar output_char, void* p)
#else
#define OUTPUT_FLOAT(F, W, D, L, Z, S, P)	output_float(F, W, D, L, Z, S, P)
static int output_float (float f, unsigned char reqWidth,
                         signed char reqDecimals,
                         BOOL left, BOOL zero, BOOL sign, BOOL space)
#endif
{
  char negative=0;
  unsigned long integerPart;
  float decimalPart;
  char fpBuffer[128];
  char fpBI=0, fpBD;
  unsigned char minWidth, i;
  int charsOutputted=0;

  // save the sign
  if (f<0) {
    negative=1;
    f=-f;
  }

  if (f>0x00ffffff) {
    // this part is from Frank van der Hulst
    signed char exp;

    for (exp = 0; f >= 10.0; exp++) f /=10.0;
    for (       ; f < 1.0;   exp--) f *=10.0;

    if (negative) {
      output_char ('-', p);
      charsOutputted++;
    } else {
      if (sign) {
        output_char ('+', p);
        charsOutputted++;
      }
    }
    charsOutputted += OUTPUT_FLOAT(f, 0, reqDecimals, 0, 0, 0, 0);
    output_char ('e', p);
    charsOutputted++;
    if (exp<0) {
      output_char ('-', p);
      charsOutputted++;
      exp = -exp;
    }
    output_char ('0'+exp/10, p);
    output_char ('0'+exp%10, p);
    charsOutputted += 2;
    return charsOutputted;
  }

  // split the float
  integerPart=f;
  decimalPart=f-integerPart;

  // fill the buffer with the integerPart (in reversed order!)
  while (integerPart) {
    fpBuffer[fpBI++]='0' + integerPart%10;
    integerPart /= 10;
  }
  if (!fpBI) {
    // we need at least a 0
    fpBuffer[fpBI++]='0';
  }

  // display some decimals as default
  if (reqDecimals==-1)
    reqDecimals=DEFAULT_FLOAT_PRECISION;

  // fill buffer with the decimalPart (in normal order)
  fpBD=fpBI;
  if (i=reqDecimals /* that's an assignment */) {
    do {
      decimalPart *= 10.0;
      // truncate the float
      integerPart=decimalPart;
      fpBuffer[fpBD++]='0' + integerPart;
      decimalPart-=integerPart;
    } while (--i);
  }

  minWidth=fpBI; // we need at least these
  minWidth+=reqDecimals?reqDecimals+1:0; // maybe these
  if (negative || sign || space)
    minWidth++; // and maybe even this :)

  if (!left && reqWidth>i) {
    if (zero) {
      if (negative)
      {
        output_char('-', p);
        charsOutputted++;
      }
      else if (sign)
      {
        output_char('+', p);
        charsOutputted++;
      }
      else if (space)
      {
        output_char(' ', p);
        charsOutputted++;
      }
      while (reqWidth-->minWidth)
      {
        output_char('0', p);
        charsOutputted++;
      }
    } else {
      while (reqWidth-->minWidth)
      {
        output_char(' ', p);
        charsOutputted++;
      }
      if (negative)
      {
        output_char('-', p);
        charsOutputted++;
      }
      else if (sign)
      {
        output_char('+', p);
        charsOutputted++;
      }
      else if (space)
      {
        output_char(' ', p);
        charsOutputted++;
      }
    }
  } else {
    if (negative)
    {
      output_char('-', p);
      charsOutputted++;
    }
    else if (sign)
    {
      output_char('+', p);
      charsOutputted++;
    }
    else if (space)
    {
      output_char(' ', p);
      charsOutputted++;
    }
  }

  // output the integer part
  i=fpBI-1;
  do {
    output_char (fpBuffer[i], p);
    charsOutputted++;
  } while (i--);

  // ouput the decimal part
  if (reqDecimals) {
    output_char ('.', p);
    charsOutputted++;
    i=fpBI;
    while (reqDecimals--)
    {
      output_char (fpBuffer[i++], p);
      charsOutputted++;
    }
  }

  if (left && reqWidth>minWidth) {
    while (reqWidth-->minWidth)
    {
      output_char(' ', p);
      charsOutputted++;
    }
  }
  return charsOutputted;
}
#endif

int _print_format (pfn_outputchar pfn, void* pvoid, const char *format, va_list ap)
{
  BOOL   left_justify;
  BOOL   zero_padding;
  BOOL   prefix_sign;
  BOOL   prefix_space;
  BOOL   signed_argument;
  BOOL   char_argument;
  BOOL   long_argument;
  BOOL   float_argument;
#ifdef SDCC_STACK_AUTO
  BOOL   lower_case;
  value_t value;
#endif
  BOOL   lsd;

  unsigned char radix;
  int charsOutputted;
  unsigned char  width;
  signed char decimals;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -