Added Gary's snprintf() and changed all places to use it.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@379 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Björn Stenberg 2002-05-02 11:44:15 +00:00
parent a98b20e22a
commit 824a003052
6 changed files with 140 additions and 15 deletions

View file

@ -33,7 +33,7 @@ OBJS := $(SRC:%.c=%.o)
all : archos.mod # archos.asm
archos.elf : $(OBJS) app.lds
$(CC) -nostdlib -o archos.elf $(OBJS) -lgcc -Tapp.lds -Wl,-Map,archos.map
$(CC) -nostdlib -o archos.elf $(OBJS) -lgcc -lc -Tapp.lds -Wl,-Map,archos.map
archos.bin : archos.elf
$(OC) -O binary archos.elf archos.bin

127
firmware/common/sprintf.c Normal file
View file

@ -0,0 +1,127 @@
/***************************************************************************
* __________ __ ___.
* 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>
static const char hexdigit[] = "0123456789ABCDEF";
int vsnprintf (char *buf, int size, const char *fmt, va_list ap)
{
char *bp = buf;
char *end = buf + size - 1;
char *str;
char tmpbuf[12], pad;
int ch, width, val, sign;
tmpbuf[sizeof tmpbuf - 1] = '\0';
while ((ch = *fmt++) != '\0' && bp < end)
{
if (ch == '%')
{
ch = *fmt++;
pad = ' ';
if (ch == '0')
pad = '0';
width = 0;
while (ch >= '0' && ch <= '9')
{
width = 10*width + ch - '0';
ch = *fmt++;
}
str = tmpbuf + sizeof tmpbuf - 1;
switch (ch)
{
case 'c':
*--str = va_arg (ap, int);
break;
case 's':
str = va_arg (ap, char*);
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':
val = va_arg (ap, int);
do
{
*--str = hexdigit[val & 0xf];
val >>= 4;
}
while (val > 0);
break;
default:
*--str = ch;
break;
}
if (width > 0)
{
width -= strlen (str);
while (width-- > 0 && buf < end)
*bp++ = pad;
}
while (*str != '\0' && buf < end)
*bp++ = *str++;
}
else
*bp++ = ch;
}
*bp++ = '\0';
return bp - buf - 1;
}
int snprintf (char *buf, int size, const char *fmt, ...)
{
int n;
va_list ap;
va_start (ap, fmt);
n = vsnprintf (buf, size, fmt, ap);
va_end (ap);
return n;
}

View file

@ -29,7 +29,7 @@ char debugbuf[200];
static int debug_tx_ready(void)
{
return (SSR1 & SCI_TDRE);
return (SSR1 & SCI_TDRE);
}
static void debug_tx_char(char ch)
@ -180,7 +180,7 @@ void debugf(char *fmt, ...)
va_list ap;
va_start(ap, fmt);
vsprintf(debugmembuf, fmt, ap);
vsnprintf(debugmembuf, sizeof(debugmembuf), fmt, ap);
va_end(ap);
debug(debugmembuf);
}
@ -197,7 +197,7 @@ void debugf(char *fmt, ...)
va_list ap;
va_start( ap, fmt );
vsprintf( debugmembuf, fmt, ap );
vsnprintf( debugmembuf, sizeof(debugmembuf), fmt, ap );
va_end( ap );
printf( debugmembuf );
}

View file

@ -27,10 +27,7 @@
#include <unistd.h>
#include <string.h>
#include <errno.h>
#ifdef SIMULATOR
#include <fcntl.h>
#endif
struct mp3entry {
char *path;
@ -530,7 +527,7 @@ char *secs2str(int ms)
static char buffer[32];
int secs = ms/1000;
ms %= 1000;
sprintf(buffer, "%d:%02d.%d", secs/60, secs%60, ms/100);
snprintf(buffer, sizeof(buffer), "%d:%02d.%d", secs/60, secs%60, ms/100);
return buffer;
}

View file

@ -45,7 +45,7 @@ void panicf( char *fmt, ...)
va_list ap;
va_start( ap, fmt );
vsprintf( panic_buf, fmt, ap );
vsnprintf( panic_buf, sizeof(panic_buf), fmt, ap );
va_end( ap );
panic( panic_buf );

View file

@ -37,7 +37,7 @@ int reload_playlist_info( playlist_info_t *playlist )
/* return a dummy playlist entry */
sprintf( playlist->filename, "\\playlists\\1.m3u" );
strncpy( playlist->filename, "\\playlists\\1.m3u", sizeof(playlist->filename) );
playlist->indices_count = 4;
@ -62,7 +62,8 @@ void load_playlist( playlist_info_t *playlist, const char *filename ) {
char *m3u_buf = NULL;
char debug_message[128];
sprintf( debug_message, "load_playlist( %s )\n", filename );
snprintf( debug_message, sizeof(debug_message),
"load_playlist( %s )\n", filename );
debug( debug_message );
/* read file */
@ -71,7 +72,7 @@ void load_playlist( playlist_info_t *playlist, const char *filename ) {
/* store playlist filename */
sprintf( playlist->filename, filename );
strncpy( playlist->filename, filename, sizeof(playlist->filename) );
/* add track indices to playlist data structure */
@ -159,7 +160,7 @@ void extend_indices( playlist_info_t *playlist, int new_index )
track_t next_playlist_track( playlist_info_t *playlist ) {
track_t track;
sprintf( track.filename, "boogie" );
strncpy( track.filename, "boogie", sizeof(track.filename) );
return track;
}
@ -320,13 +321,13 @@ void get_indices_as_string( char *string, playlist_info_t *playlist )
{
/* first iteration - no comma */
sprintf( tmp, "%d", p[count] );
snprintf( tmp, sizeof(tmp), "%d", p[count] );
}
else
{
/* normal iteration - insert comma */
sprintf( tmp, ",%d", p[count] );
snprintf( tmp, sizeof(tmp), ",%d", p[count] );
}
strcat( string, tmp );