Behold my masterpiece. safeprint () is a special case when there's just a string. safeprintf () works the same as printf ().
Code:
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "Error.h"
int safeprint ( const char* file , const unsigned int line , const char* pstring )
{
int ilength = strlen ( pstring ) ;
int icharsprinted = printf ( "%s" , pstring ) ;
if ( ilength != icharsprinted )
{
ErrorMessage ( "Couldn't print to screen." , file , line ) ;
return -1 ;
}
return 0 ;
}
static const unsigned int buffersize = 200 ;
int safeprintf ( const char* file , const unsigned int line , char* formatstring , ... )
{
va_list va ;
va_start ( va , formatstring ) ;
int icharsprinted = vprintf ( formatstring , va ) ;
if ( icharsprinted < 0 )
{
ErrorMessage ( "Couldn't print to screen." , file , line ) ;
return -1 ;
}
va_start ( va , formatstring ) ;
char buffer [ buffersize ] ;
int ilength = vsnprintf ( buffer , buffersize , formatstring , va ) ;
if ( ilength < 0 )
{
ErrorMessage ( "Couldn't print to screen." , file , line ) ;
return -1 ;
}
va_end ( va ) ;
if ( ilength != icharsprinted )
{
ErrorMessage ( "Couldn't print to screen." , file , line ) ;
return -1 ;
}
return 0 ;
}