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

📄 stdiop.c

📁 ATMEL单片机可用的文件系统源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/***********************************************************************/
/*                                                                     */
/*   Module:  stdiop.c                                                 */
/*   Release: 2004.5                                                   */
/*   Version: 2003.5                                                   */
/*   Purpose: stdio.h print formatting for non-floating point builds   */
/*                                                                     */
/*---------------------------------------------------------------------*/
/*                                                                     */
/*               Copyright 2004, Blunk Microsystems                    */
/*                      ALL RIGHTS RESERVED                            */
/*                                                                     */
/*   Licensees have the non-exclusive right to use, modify, or extract */
/*   this computer program for software development at a single site.  */
/*   This program may be resold or disseminated in executable format   */
/*   only. The source code may not be redistributed or resold.         */
/*                                                                     */
/***********************************************************************/
#include "stdiop.h"

/***********************************************************************/
/* Symbol Definitions                                                  */
/***********************************************************************/
#define BUF_BYTES   200           /* size of buf used in ProcessPrintf */

/***********************************************************************/
/* Local Function Definitions                                          */
/***********************************************************************/

/***********************************************************************/
/*    send_buf: Write multiple chars from buffer to stream             */
/*                                                                     */
/*      Inputs: stream = output stream                                 */
/*              buf = buffer from which charaters are sent             */
/*              num_bytes = number of bytes to send                    */
/*                                                                     */
/*     Returns: 0 on success, -1 on error                              */
/*                                                                     */
/***********************************************************************/
static int send_buf(FILE *stream, char *buf, int num_bytes)
{
  if (stream->write(stream, (const ui8*)buf, (ui32)num_bytes) !=
      num_bytes)
  {
    stream->errcode = get_errno();
    return -1;
  }
  return 0;
}

/***********************************************************************/
/*  send_chars: Write a char multiple times to stream                  */
/*                                                                     */
/*      Inputs: stream = output stream                                 */
/*              c = character to send multiple time                    */
/*              num_bytes = number of bytes to send                    */
/*                                                                     */
/*     Returns: 0 on success, -1 on error                              */
/*                                                                     */
/***********************************************************************/
static int send_chars(FILE *stream, char c, int num_bytes)
{
  int i;

  for (i = 0; i < num_bytes; ++i)
    if (stream->write(stream, (const ui8*)&c, 1) != 1)
    {
      stream->errcode = get_errno();
      return -1;
    }

  return 0;
}

/***********************************************************************/
/*   send_char: Write character to stream                              */
/*                                                                     */
/*      Inputs: stream = output stream                                 */
/*              c = char to be displayed                               */
/*                                                                     */
/*     Returns: 0 on success, -1 on failure                            */
/*                                                                     */
/***********************************************************************/
static int send_char(FILE *stream, char c)
{
  /*-------------------------------------------------------------------*/
  /* If write to stream failed, return error.                          */
  /*-------------------------------------------------------------------*/
  if (stream->write(stream, (const ui8 *)&c, 1) != 1)
  {
    stream->errcode = get_errno();
    return -1;
  }

  return 0;
}

/***********************************************************************/
/* trnsfrm_val: Put the equivalent of val into buf                     */
/*                                                                     */
/*      Inputs: buf = destination string                               */
/*              val = value to be converted                            */
/*              base = base of conversion                              */
/*              x = for base 16, whether it's 'x' or 'X'               */
/*              p = precision                                          */
/*                                                                     */
/*     Returns: start position in buf for valid bytes                  */
/*                                                                     */
/***********************************************************************/
static int trnsfrm_val(char buf[], ui32 val, int base, int x, int p)
{
  ui32 t_val;
  int  i;

  /*-------------------------------------------------------------------*/
  /* Fill buf with string equivalent of val, starting from the end.    */
  /*-------------------------------------------------------------------*/
  for (i = BUF_BYTES - 1;; --i, val /= base)
  {
    /*-----------------------------------------------------------------*/
    /* Get the next digit.                                             */
    /*-----------------------------------------------------------------*/
    t_val = val % base;

    /*-----------------------------------------------------------------*/
    /* If base is 16 and t_val > 9, convert to A-F, else get 0-9.      */
    /*-----------------------------------------------------------------*/
    if ((base == 16) && (t_val > 9))
      buf[i] = x ? (char)('a' + t_val - 10) : (char)('A' + t_val - 10);
    else
      buf[i] = (char)('0' + t_val);

    /*-----------------------------------------------------------------*/
    /* Break whenever val gets below base so we can't divide anymore.  */
    /*-----------------------------------------------------------------*/
    if (val < base)
      break;
  }

  /*-------------------------------------------------------------------*/
  /* If padding is needed, fill with 0's.                              */
  /*-------------------------------------------------------------------*/
  while ((i > 0) && (p > BUF_BYTES - i))
    buf[--i] = '0';

  return i;
}

/***********************************************************************/
/*   print_buf: Send chars from buf to stream                          */
/*                                                                     */
/*      Inputs: stream = pointer to file control block                 */
/*              buf = string to be printed                             */
/*              buf_size = size of buf in bytes                        */
/*              start = start of valid bytes in buf                    */
/*              flags, specialValue = special printf controls          */
/*              specifier = output specifier from format string        */
/*              width = size of the output                             */
/*              precision = precision from format string               */
/*                                                                     */
/*     Returns: Number of bytes printed, -1 on error                   */
/*                                                                     */
/***********************************************************************/
static int print_buf(FILE *stream, char buf[], int buf_size, int start,
                     int flags, int specifier, int width, int precision)
{
  int add = 0, curr = start, printed_bytes = 0;

  /*-------------------------------------------------------------------*/
  /* If size is 0 then there is nothing to be printed.                 */
  /*-------------------------------------------------------------------*/
  if (start == buf_size)
  {
    /*-----------------------------------------------------------------*/
    /* Output a ' ' if space flag is enabled.                          */
    /*-----------------------------------------------------------------*/
    if (flags & SPACE_FLAG)
    {
      if (send_char(stream, ' '))
        return -1;
      return 1;
    }
    return 0;
  }

  /*-------------------------------------------------------------------*/
  /* If specifier is either %d or %i check for the plus or space flags */
  /* to be enabled.                                                    */
  /*-------------------------------------------------------------------*/
  if (specifier == DI_SPEC)
  {
    /*-----------------------------------------------------------------*/
    /* If the plus flag is enabled and the number is not negative,     */
    /* then put a '+' in front.                                        */
    /*-----------------------------------------------------------------*/
    if ((flags & PLUS_FLAG) && (buf[curr] != '-'))
    {
      if (width > 0) --width;
      add = PLUS_SIGN;
    }

    /*-----------------------------------------------------------------*/
    /* Else if the space flag is on and the number is not negative,    */
    /* then put a ' ' in front.                                        */
    /*-----------------------------------------------------------------*/
    else if ((flags & SPACE_FLAG) && (buf[curr] != '-'))
    {
      if (width > 0) --width;
      add = SPACE_SIGN;
    }
  }

  /*-------------------------------------------------------------------*/
  /* If width is more than bytes in buf, pad with spaces to the right. */
  /*-------------------------------------------------------------------*/
  if ((flags & MINUS_FLAG) && (width > buf_size - curr))
  {
    /*-----------------------------------------------------------------*/
    /* Send either one '+' or ' ' if necessary.                        */
    /*-----------------------------------------------------------------*/
    if (add == PLUS_SIGN)
    {
      if (send_char(stream, '+'))
        return -1;
      ++printed_bytes;
    }
    else if (add == SPACE_SIGN)
    {
      if (send_char(stream, ' '))
        return -1;
      ++printed_bytes;
    }

    /*-----------------------------------------------------------------*/
    /* First output the contents of the buffer, buf.                   */
    /*-----------------------------------------------------------------*/
    if (send_buf(stream, &buf[curr], buf_size - curr))
      return -1;
    printed_bytes += (buf_size - curr);

    /*-----------------------------------------------------------------*/
    /* Update width and add spaces for padding.                        */
    /*-----------------------------------------------------------------*/
    width -= (buf_size - curr);
    if (send_chars(stream, ' ', width))
      return -1;
    if (width > 0)
      printed_bytes += width;
  }

  /*-------------------------------------------------------------------*/
  /* Else if width is more than bytes in buf, pad with ' ' or '+'.     */
  /*-------------------------------------------------------------------*/
  else if ((flags & ZERO_FLAG) && (width > buf_size - curr))
  {
    /*-----------------------------------------------------------------*/
    /* Send either one '+' or ' ' if necessary.                        */
    /*-----------------------------------------------------------------*/
    if (add == PLUS_SIGN)
    {
      if (send_char(stream, '+'))
        return -1;
      ++printed_bytes;
    }
    else if (add == SPACE_SIGN)
    {
      if (send_char(stream, ' '))
        return -1;
      ++printed_bytes;
    }

    /*-----------------------------------------------------------------*/
    /* Place '0X', '0x' if necessary.                                  */
    /*-----------------------------------------------------------------*/
    if ((flags & NUM_FLAG) && (specifier == X_SPEC))
    {
      if (send_buf(stream, &buf[curr], 2))
        return -1;
      width -= 2;
      curr += 2;
      printed_bytes += 2;
    }

    /*-----------------------------------------------------------------*/
    /* Pad with zeros to the left until width.                         */
    /*-----------------------------------------------------------------*/
    if (send_chars(stream, '0', width - (buf_size - curr)))
      return -1;
    if (width - (buf_size - curr) > 0)
      printed_bytes += (width - (buf_size - curr));

    /*-----------------------------------------------------------------*/
    /* Now send the actual buf.                                        */
    /*-----------------------------------------------------------------*/
    if (send_buf(stream, &buf[curr], buf_size - curr))
      return -1;
    printed_bytes += (buf_size - curr);
  }

  /*-------------------------------------------------------------------*/
  /* Else simply fill out buffer.                                      */
  /*-------------------------------------------------------------------*/
  else
  {
    /*-----------------------------------------------------------------*/
    /* Figure out how many spaces of padding are needed.               */
    /*-----------------------------------------------------------------*/
    width -= (buf_size - curr);

    /*-----------------------------------------------------------------*/
    /* Pad with spaces to the left until width.                        */
    /*-----------------------------------------------------------------*/
    if (send_chars(stream, ' ', width))
      return -1;
    if (width > 0)
      printed_bytes += width;

    /*-----------------------------------------------------------------*/
    /* Send either one '+' or ' ' if necessary.                        */
    /*-----------------------------------------------------------------*/
    if (add == PLUS_SIGN)
    {
      if (send_char(stream, '+'))
        return -1;
      ++printed_bytes;
    }
    else if (add == SPACE_SIGN)
    {
      if (send_char(stream, ' '))
        return -1;
      ++printed_bytes;
    }

    /*-----------------------------------------------------------------*/
    /* Now, output the contents of the buffer, buf.                    */
    /*-----------------------------------------------------------------*/
    if (send_buf(stream, &buf[curr], buf_size - curr))

⌨️ 快捷键说明

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