2002-05-02 11:44:15 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002 by Gary Czvitkovicz
|
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Minimal printf and snprintf formatting functions
|
|
|
|
*
|
|
|
|
* These support %c %s %d and %x
|
|
|
|
* Field width and zero-padding flag only
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
2003-03-16 19:43:28 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#include "file.h" /* for write(), used in fprintf() */
|
2003-04-25 12:36:52 +00:00
|
|
|
#include "sprintf.h" /* to allow the simulator magic */
|
2002-05-02 11:44:15 +00:00
|
|
|
|
|
|
|
static const char hexdigit[] = "0123456789ABCDEF";
|
|
|
|
|
2003-03-16 19:43:28 +00:00
|
|
|
static int format(
|
|
|
|
/* call 'push()' for each output letter */
|
|
|
|
int (*push)(void *userp, unsigned char data),
|
|
|
|
void *userp,
|
|
|
|
const char *fmt,
|
|
|
|
va_list ap)
|
2002-05-02 11:44:15 +00:00
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
char tmpbuf[12], pad;
|
2007-03-19 10:57:23 +00:00
|
|
|
int ch, width, val, sign, precision;
|
2005-02-25 18:50:16 +00:00
|
|
|
long lval, lsign;
|
2003-05-24 00:19:27 +00:00
|
|
|
unsigned int uval;
|
2005-01-23 23:08:07 +00:00
|
|
|
unsigned long ulval;
|
2003-03-16 19:43:28 +00:00
|
|
|
bool ok = true;
|
2002-05-02 11:44:15 +00:00
|
|
|
|
|
|
|
tmpbuf[sizeof tmpbuf - 1] = '\0';
|
|
|
|
|
2003-03-16 19:43:28 +00:00
|
|
|
while ((ch = *fmt++) != '\0' && ok)
|
2002-05-02 11:44:15 +00:00
|
|
|
{
|
2005-01-24 00:56:39 +00:00
|
|
|
if (ch == '%')
|
|
|
|
{
|
|
|
|
ch = *fmt++;
|
|
|
|
pad = ' ';
|
|
|
|
if (ch == '0')
|
|
|
|
pad = '0';
|
|
|
|
|
|
|
|
width = 0;
|
|
|
|
while (ch >= '0' && ch <= '9')
|
|
|
|
{
|
|
|
|
width = 10*width + ch - '0';
|
|
|
|
ch = *fmt++;
|
|
|
|
}
|
2007-03-19 10:57:23 +00:00
|
|
|
|
|
|
|
precision = 0;
|
|
|
|
if(ch == '.')
|
|
|
|
{
|
|
|
|
ch = *fmt++;
|
|
|
|
while (ch >= '0' && ch <= '9')
|
|
|
|
{
|
|
|
|
precision = 10*precision + ch - '0';
|
|
|
|
ch = *fmt++;
|
|
|
|
}
|
|
|
|
}
|
2005-01-24 00:56:39 +00:00
|
|
|
|
|
|
|
str = tmpbuf + sizeof tmpbuf - 1;
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case 'c':
|
|
|
|
*--str = va_arg (ap, int);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
str = va_arg (ap, char*);
|
2007-03-19 10:57:23 +00:00
|
|
|
if(precision > 0)
|
|
|
|
str[precision] = '\0';
|
2005-01-24 00:56:39 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
val = sign = va_arg (ap, int);
|
|
|
|
if (val < 0)
|
|
|
|
val = -val;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
*--str = (val % 10) + '0';
|
|
|
|
val /= 10;
|
|
|
|
}
|
|
|
|
while (val > 0);
|
|
|
|
if (sign < 0)
|
|
|
|
*--str = '-';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
uval = va_arg (ap, int);
|
|
|
|
do
|
|
|
|
{
|
|
|
|
*--str = hexdigit[uval & 0xf];
|
|
|
|
uval >>= 4;
|
|
|
|
}
|
|
|
|
while (uval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
ch = *fmt++;
|
|
|
|
switch(ch) {
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
ulval = va_arg (ap, long);
|
|
|
|
do
|
|
|
|
{
|
|
|
|
*--str = hexdigit[ulval & 0xf];
|
|
|
|
ulval >>= 4;
|
|
|
|
}
|
|
|
|
while (ulval);
|
|
|
|
break;
|
|
|
|
case 'd':
|
2005-02-25 18:50:16 +00:00
|
|
|
lval = lsign = va_arg (ap, long);
|
2005-01-24 00:56:39 +00:00
|
|
|
if (lval < 0)
|
|
|
|
lval = -lval;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
*--str = (lval % 10) + '0';
|
|
|
|
lval /= 10;
|
2005-01-23 23:08:07 +00:00
|
|
|
}
|
2005-01-24 00:56:39 +00:00
|
|
|
while (lval > 0);
|
2005-02-25 18:50:16 +00:00
|
|
|
if (lsign < 0)
|
2005-01-24 00:56:39 +00:00
|
|
|
*--str = '-';
|
2005-01-23 23:08:07 +00:00
|
|
|
break;
|
2005-01-24 00:56:39 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
*--str = 'l';
|
|
|
|
*--str = ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*--str = ch;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (width > 0)
|
|
|
|
{
|
|
|
|
width -= strlen (str);
|
|
|
|
while (width-- > 0 && ok)
|
|
|
|
ok=push(userp, pad);
|
|
|
|
}
|
|
|
|
while (*str != '\0' && ok)
|
2003-03-16 19:43:28 +00:00
|
|
|
ok=push(userp, *str++);
|
2005-01-24 00:56:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ok=push(userp, ch);
|
2003-03-16 19:43:28 +00:00
|
|
|
}
|
|
|
|
return ok; /* true means good */
|
|
|
|
}
|
|
|
|
|
2006-10-16 09:06:19 +00:00
|
|
|
#if !defined(SIMULATOR) || !defined(linux)
|
2006-10-12 20:09:18 +00:00
|
|
|
/* ALSA library requires a more advanced snprintf, so let's not
|
2006-10-16 09:06:19 +00:00
|
|
|
override it in simulator for Linux. Note that Cygwin requires
|
|
|
|
our snprintf or it produces garbled output after a while. */
|
2006-10-12 20:09:18 +00:00
|
|
|
|
2003-03-16 19:43:28 +00:00
|
|
|
struct for_snprintf {
|
|
|
|
unsigned char *ptr; /* where to store it */
|
|
|
|
int bytes; /* amount already stored */
|
|
|
|
int max; /* max amount to store */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int sprfunc(void *ptr, unsigned char letter)
|
|
|
|
{
|
|
|
|
struct for_snprintf *pr = (struct for_snprintf *)ptr;
|
|
|
|
if(pr->bytes < pr->max) {
|
|
|
|
*pr->ptr = letter;
|
|
|
|
pr->ptr++;
|
|
|
|
pr->bytes++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false; /* filled buffer */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-25 12:43:44 +00:00
|
|
|
int snprintf(char *buf, size_t size, const char *fmt, ...)
|
2003-03-16 19:43:28 +00:00
|
|
|
{
|
|
|
|
bool ok;
|
|
|
|
va_list ap;
|
|
|
|
struct for_snprintf pr;
|
|
|
|
|
2005-11-25 00:10:12 +00:00
|
|
|
pr.ptr = (unsigned char *)buf;
|
2003-03-16 19:43:28 +00:00
|
|
|
pr.bytes = 0;
|
|
|
|
pr.max = size;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
ok = format(sprfunc, &pr, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
/* make sure it ends with a trailing zero */
|
2004-04-22 21:19:33 +00:00
|
|
|
pr.ptr[(pr.bytes < pr.max) ? 0 : -1] = '\0';
|
2003-03-16 19:43:28 +00:00
|
|
|
|
|
|
|
return pr.bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
int vsnprintf(char *buf, int size, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
bool ok;
|
|
|
|
struct for_snprintf pr;
|
|
|
|
|
2005-11-25 00:10:12 +00:00
|
|
|
pr.ptr = (unsigned char *)buf;
|
2003-03-16 19:43:28 +00:00
|
|
|
pr.bytes = 0;
|
|
|
|
pr.max = size;
|
|
|
|
|
|
|
|
ok = format(sprfunc, &pr, fmt, ap);
|
|
|
|
|
|
|
|
/* make sure it ends with a trailing zero */
|
2004-04-22 21:19:33 +00:00
|
|
|
pr.ptr[(pr.bytes < pr.max) ? 0 : -1] = '\0';
|
2003-03-16 19:43:28 +00:00
|
|
|
|
|
|
|
return pr.bytes;
|
|
|
|
}
|
|
|
|
|
2006-10-16 09:06:19 +00:00
|
|
|
#endif /* Linux SIMULATOR */
|
2006-10-12 20:09:18 +00:00
|
|
|
|
2003-03-16 19:43:28 +00:00
|
|
|
struct for_fprintf {
|
|
|
|
int fd; /* where to store it */
|
|
|
|
int bytes; /* amount stored */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int fprfunc(void *pr, unsigned char letter)
|
|
|
|
{
|
|
|
|
struct for_fprintf *fpr = (struct for_fprintf *)pr;
|
|
|
|
int rc = write(fpr->fd, &letter, 1);
|
|
|
|
|
|
|
|
if(rc > 0) {
|
|
|
|
fpr->bytes++; /* count them */
|
|
|
|
return true; /* we are ok */
|
2002-05-02 11:44:15 +00:00
|
|
|
}
|
|
|
|
|
2003-03-16 19:43:28 +00:00
|
|
|
return false; /* failure */
|
2002-05-02 11:44:15 +00:00
|
|
|
}
|
|
|
|
|
2003-03-16 19:43:28 +00:00
|
|
|
|
2005-02-22 12:19:12 +00:00
|
|
|
int fdprintf(int fd, const char *fmt, ...)
|
2002-05-02 11:44:15 +00:00
|
|
|
{
|
2003-03-16 19:43:28 +00:00
|
|
|
bool ok;
|
2002-05-02 11:44:15 +00:00
|
|
|
va_list ap;
|
2003-03-16 19:43:28 +00:00
|
|
|
struct for_fprintf fpr;
|
|
|
|
|
|
|
|
fpr.fd=fd;
|
|
|
|
fpr.bytes=0;
|
2002-05-02 11:44:15 +00:00
|
|
|
|
2003-03-16 19:43:28 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
ok = format(fprfunc, &fpr, fmt, ap);
|
|
|
|
va_end(ap);
|
2002-05-02 11:44:15 +00:00
|
|
|
|
2003-03-16 19:43:28 +00:00
|
|
|
return fpr.bytes; /* return 0 on error */
|
2002-05-02 11:44:15 +00:00
|
|
|
}
|
2003-04-25 13:35:31 +00:00
|
|
|
|