[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[leafnode-list] snprintf



I think I managed to hack together a version of snprintf() which could
be used if no snprintf was present. It certainly does not have all features
of the full snprintf but seems to be sufficient to use it in leafnode.

This version of snprintf understands %s, %c, %d, %l, %u and %lu. It
assumes (rightfully, I think) that a long or int number cannot have
more than 29 digits.

Here is the code (with a small main() for testing at the end):

%snip
/*
 * poor man's snprintf() - for systems which don't have their own
 *
 * Copyright (c) Cornelius Krasel 2000.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#ifndef HAVE_SNPRINTF

#include <stdio.h>
#include <stdarg.h>
#include <string.h>

int my_snprintf( char *str, size_t n, const char *format, ... ) {
    int rval;
    va_list ap;
    const char *p;
    char *q;
    int flag  = 0;
    int uflag = 0;
    int i     = 1;	/* because the terminating \0 also counts */
    size_t len;
    char   buf[30];	/* buffer for converting longs and ints */

    int  d;
    long l;
    char *s;
    char c;

    va_start( ap, format );
    p = format;
    q = str;
    while ( p && *p && ( i < n ) ) {
    	if ( ( *p == '%' )  && !flag ) {
	    uflag = 0;
	    flag  = 1;
	    *p++;
	}
	else if ( flag ) {
	    switch (*p)  {
		case 's': {
		    s = va_arg( ap, char * );
		    len = strlen( s );
		    if ( len > (n-i) )
			len = n - i;
		    *q = '\0';
		    strncat( q, s, len );
		    p++;
		    q += len;
		    i += len;
		    flag = 0;
		    break;
		}
		case 'u': {
		    /* next argument will be unsigned ! */
		    uflag = 1;
		    p++;
		    break;
		}
		case 'd':
		case 'l': {
		    if ( uflag ) {
			if ( *p == 'd' ) {
			    d = va_arg( ap, unsigned int );
			    sprintf( buf, "%u", d );
			}
			else {
			    l = va_arg( ap, unsigned long );
			    sprintf( buf, "%lu", l );
			}
		    }
		    else {
			if ( *p == 'd' ) {
			    d = va_arg( ap, int );
			    sprintf( buf, "%d", d );
			}
			else {
			    l = va_arg( ap, long );
			    sprintf( buf, "%l", l );
			}
		    }
		    *p++;
		    len = strlen(buf);
		    if ( len > (n-i) )
			len = n-i;
		    *q = '\0';
		    strncat( q, buf, len );
		    q += len;
		    i += len;
		    uflag = 0;
		    flag  = 0;
		    break;
		}
		case 'c': {
		    if ( uflag )
			c = va_arg( ap, unsigned char );
		    else
			c = va_arg( ap, char );
		    uflag = 0;
		    flag  = 0;
		    i++;
		    *q++ = c;
		    p++;
		    break;
		}
		case '%': {
		    flag  = 0;
		    i++;
		    *q++ = *p++;
		    break;
		}
	    }
	}
	else {
	    uflag = 0;
	    i++;
	    *q++ = *p++;
	}
    }
    va_end( ap );
    *q = '\0';
    return i;
}

#endif

int main( void ) {
    char test[7] = "1234567";
    char * test2;

    test2 = (char *)malloc(80);
    my_snprintf( test2, 7, "%c %s", 't', test );
    printf( "%s\n", test2 );
    my_snprintf( test2, 70, "%s %ul %ul %ul %s", "alt.religion.scientology",
	20, 5000, 80000, "just testing" );
    printf( "%s\n", test2 );
    exit(0);
}
%snip

One should always remember that this code will actually be used only by
a very small (and hopefully shrinking) proportion of users. Therefore,
I think it is not worth to put too much effort in it. (Of course, it
should not contain any bugs :-)

--Cornelius.

PS: Apologies to all readers of this list who are not interested in
    technical details: I hope this will soon be over :-)

-- 
/* Cornelius Krasel, U Wuerzburg, Dept. of Pharmacology, Versbacher Str. 9 */
/* D-97078 Wuerzburg, Germany   email: phak004@xxxxxxxxxxxxxxxxxxxxxx  SP4 */
/* "Science is the game we play with God to find out what His rules are."  */

-- 
leafnode-list@xxxxxxxxxxxxxxxxxxxxxxxxxxxx -- mailing list for leafnode
To unsubscribe, send mail with "unsubscribe" in the subject to the list