23#define UPRINTF_S16_FMT "%d"
24#define UPRINTF_U16_FMT "%u"
26#define UPRINTF_S16_DIGITS 5
28#ifndef UPRINTF_S32_FMT
29# if __LONG_WIDTH__ == 64 || __EMSCRIPTEN__
30# define UPRINTF_S32_FMT "%d"
31# elif __LONG_WIDTH__ == 32 || defined( __WATCOMC__ ) || defined( __BORLANDC__ ) || _MSC_VER <= 1200
32# define UPRINTF_S32_FMT "%ld"
36#ifndef UPRINTF_U32_FMT
37# if __LONG_WIDTH__ == 64 || __EMSCRIPTEN__
38# define UPRINTF_U32_FMT "%u"
39# elif __LONG_WIDTH__ == 32 || defined( __WATCOMC__ ) || defined( __BORLANDC__ ) || _MSC_VER <= 1200
40# define UPRINTF_U32_FMT "%lu"
44#ifndef UPRINTF_X32_FMT
45# if __LONG_WIDTH__ == 64 || __EMSCRIPTEN__
46# define UPRINTF_X32_FMT "%x"
47# elif __LONG_WIDTH__ == 32 || defined( __WATCOMC__ ) || defined( __BORLANDC__ ) || _MSC_VER <= 1200
48# define UPRINTF_X32_FMT "%lx"
54#define NEWLINE_STR "\n"
59#ifndef UPRINTF_BUFFER_SZ_MAX
60# define UPRINTF_BUFFER_SZ_MAX 1024
64# define platform_file FILE*
67#ifndef platform_fprintf
68# define platform_fprintf fprintf
71#ifndef platform_vfprintf
72# define platform_vfprintf vfprintf
76# define platform_fopen fopen
79#ifndef platform_fflush
80# define platform_fflush fflush
83#ifndef platform_fclose
84# define platform_fclose fclose
91# define LOG_ERR_TARGET g_log_file
92# define LOG_STD_TARGET g_log_file
94# define LOG_ERR_TARGET stderr
95# define LOG_STD_TARGET stdout
100# define SIZE_T_FMT "%lu"
101# define SSIZE_T_FMT "%ld"
102# define OFF_T_FMT "%lld"
103# elif defined( _WIN64 )
104# define SIZE_T_FMT "%I64u"
105# define SSIZE_T_FMT "%I64d"
106# define OFF_T_FMT "%I32d"
108# define SIZE_T_FMT "%lu"
109# define SSIZE_T_FMT "%ld"
110# define OFF_T_FMT "%lu"
113# define SIZE_T_FMT "%u"
114# define SSIZE_T_FMT "%d"
115# define OFF_T_FMT "%lu"
118#ifdef MAUG_OS_DOS_REAL
119# define UPRINTF_MS_FMT UPRINTF_U16_FMT
121# define UPRINTF_MS_FMT UPRINTF_U32_FMT
124#if !defined( DEBUG_THRESHOLD )
125# define DEBUG_THRESHOLD 1
137#if defined( MAUG_OS_NDS )
143static void _internal_debug_printf(
144 const char* src_file,
int line,
int level,
const char* fmt, ...
147 char buffer[UPRINTF_BUFFER_SZ_MAX + 1];
148 char line_buffer[11] = { 0 };
150 if( level >= DEBUG_THRESHOLD ) {
151 va_start( argp, fmt );
152 vsnprintf( buffer, UPRINTF_BUFFER_SZ_MAX, fmt, argp );
154 snprintf( line_buffer, 10,
"%d", line );
155 nocashMessage( src_file );
156 nocashMessage(
" (" );
157 nocashMessage( line_buffer );
158 nocashMessage(
"): " );
159 nocashMessage( buffer );
160 nocashMessage(
"\n" );
164#define debug_printf( lvl, ... ) \
165 _internal_debug_printf( __FILE__, __LINE__, lvl, __VA_ARGS__ )
167static void error_printf(
const char* fmt, ... ) {
169 char buffer[UPRINTF_BUFFER_SZ_MAX + 1];
171 va_start( argp, fmt );
172 vsnprintf( buffer, UPRINTF_BUFFER_SZ_MAX, fmt, argp );
175 nocashMessage( buffer );
176 nocashMessage(
"\n" );
179# define size_printf( lvl, name, sz ) debug_printf( lvl, name " size is " SIZE_T_FMT " bytes", (sz) );
182#elif defined( UPRINTF_ANCIENT_C )
187#ifdef RETROFLAT_API_WINCE
189static void debug_printf(
int level,
const char* fmt, ... ) {
193static void error_printf(
const char* fmt, ... ) {
199static void debug_printf(
int level,
const char* fmt, ... ) {
202 if( level >= DEBUG_THRESHOLD ) {
203 va_start( argp, fmt );
204 vprintf( fmt, argp );
210static void error_printf(
const char* fmt, ... ) {
213 va_start( argp, fmt );
214 vprintf( fmt, argp );
221# define size_printf( lvl, name, sz ) debug_printf( lvl, name " size is " SIZE_T_FMT " bytes", (sz) );
223# define size_multi_printf( lvl, name, sz, max ) debug_printf( lvl, "single " name " size is " SIZE_T_FMT " bytes, " name " array size is " SIZE_T_FMT " bytes", (sz), ((sz) * (max)) );
226#elif defined( UPRINTF_LOG )
229# ifdef NO_DEBUG_LINE_NUMBER
230# define LINE_NUMBER() 0
232# define LINE_NUMBER() __LINE__
235# define internal_debug_printf( lvl, ... ) if( NULL != LOG_ERR_TARGET && lvl >= DEBUG_THRESHOLD ) { platform_fprintf( LOG_STD_TARGET, "(%d) " __FILE__ ": %d: ", lvl, LINE_NUMBER() ); platform_fprintf( LOG_STD_TARGET, __VA_ARGS__ ); platform_fprintf( LOG_STD_TARGET, NEWLINE_STR ); platform_fflush( LOG_STD_TARGET ); }
237# define internal_error_printf( ... ) if( NULL != LOG_ERR_TARGET ) { platform_fprintf( LOG_ERR_TARGET, "(E) " __FILE__ ": %d: ", LINE_NUMBER() ); platform_fprintf( LOG_ERR_TARGET, __VA_ARGS__ ); platform_fprintf( LOG_ERR_TARGET, NEWLINE_STR ); platform_fflush( LOG_ERR_TARGET ); }
239# define debug_printf( lvl, ... ) internal_debug_printf( lvl, __VA_ARGS__ )
241# define error_printf( ... ) internal_error_printf( __VA_ARGS__ )
243# define size_printf( lvl, name, sz ) internal_debug_printf( lvl, name " size is " SIZE_T_FMT " bytes", (sz) );
245# define size_multi_printf( lvl, name, sz, max ) internal_debug_printf( lvl, "single " name " size is " SIZE_T_FMT " bytes, " name " array size is " SIZE_T_FMT " bytes", (sz), ((sz) * (max)) );
253# define debug_printf( ... )
254# define error_printf( ... )
255# define size_printf( ... )
256# define size_multi_printf( ... )
264# define logging_init() g_log_file = platform_fopen( LOG_FILE_NAME, "w" );
265# define logging_shutdown() platform_fclose( g_log_file );
267# if defined( UPRINTF_C )
268platform_file g_log_file = NULL;
270extern platform_file g_log_file;
275# define logging_init()
276# define logging_shutdown()
280#define maug_bufcat( c, buf, buf_idx, buf_sz, cleanup ) \
281 buf[buf_idx++] = c; \
282 if( buf_idx >= buf_sz ) { goto cleanup; }
284#define maug_bufpad( pad_c, pad_sz, i, buf, buf_idx, buf_sz, cleanup ) \
286 while( i < pad_sz ) { \
287 maug_bufcat( pad_c, buffer, buffer_idx, buffer_sz, cleanup ); \
291int maug_digits(
long int num,
int base );
293int maug_zdigits(
size_t num,
int base );
295#define maug_xtoa( num, base, rem, dest, dest_idx, digits, digits_done, pad ) \
296 dest_idx += digits; \
297 while( 0 != num ) { \
300 dest[--dest_idx] = (9 < rem) ? \
309 while( digits_done < digits ) { \
310 dest[--dest_idx] = '0'; \
317#define maug_hctoi( c ) \
318 ('9' > (c) ? (c) - '0' : 'a' > (c) ? 10 + (c) - 'A' : 10 + (c) - 'a')
320int maug_is_num(
const char* str,
size_t str_sz, uint8_t base, uint8_t sign );
322int maug_is_float(
const char* str,
size_t str_sz );
324int maug_itoa(
long int num,
char* dest,
int dest_sz,
int base );
326int maug_utoa( uint32_t num,
char* dest,
int dest_sz,
int base );
328int maug_ztoa(
size_t num,
char* dest,
int dest_sz,
int base );
330uint32_t maug_atou32(
const char* buffer,
size_t buffer_sz, uint8_t base );
332int32_t maug_atos32(
const char* buffer,
size_t buffer_sz );
334float maug_atof(
const char* buffer,
size_t buffer_sz );
336void maug_str_upper(
char* line,
size_t line_sz );
338void maug_str_lower(
char* line,
size_t line_sz );
341 size_t idx,
const char* line,
size_t line_sz,
char* out,
size_t out_sz );
344 size_t idx,
const char* line,
size_t line_sz, int16_t* out );
347 char* buffer,
int buffer_sz,
const char* fmt, va_list vargs );
349void maug_snprintf(
char* buffer,
int buffer_sz,
const char* fmt, ... );
351void maug_printf(
const char* fmt, ... );
357uint32_t g_maug_printf_line = 0;
358int g_maug_uprintf_threshold = DEBUG_THRESHOLD;
360int maug_is_num(
const char* str,
size_t str_sz, uint8_t base, uint8_t sign ) {
364 str_sz = maug_strlen( str );
367 for( i = 0 ; str_sz > i ; i++ ) {
369 if( sign && 0 == i &&
'-' == str[i] ) {
371 }
else if(
'0' > str[i] ||
'9' < str[i] ) {
379int maug_is_float(
const char* str,
size_t str_sz ) {
383 str_sz = maug_strlen( str );
386 for( i = 0 ; str_sz > i ; i++ ) {
387 if( (
'0' > str[i] ||
'9' < str[i]) &&
'.' != str[i] ) {
395int maug_digits(
long int num,
int base ) {
422int maug_zdigits(
size_t num,
int base ) {
444int maug_itoa(
long int num,
char* dest,
int dest_sz,
int base ) {
451 digits = maug_digits( num, base );
452 assert( (0 == num && 1 == digits) || (0 != num && 0 < digits) );
453 assert( digits < dest_sz );
459 }
else if( 0 > num ) {
464 maug_xtoa( num, base, rem, dest, dest_idx, digits, digits_done, pad );
467 dest[dest_idx] =
'-';
477int maug_utoa( uint32_t num,
char* dest,
int dest_sz,
int base ) {
483 digits = maug_digits( num, base );
484 assert( (0 == num && 1 == digits) || (0 != num && 0 < digits) );
485 assert( digits < dest_sz );
493 maug_xtoa( num, base, rem, dest, dest_idx, digits, digits_done, pad );
501int maug_ztoa(
size_t num,
char* dest,
int dest_sz,
int base ) {
507 digits = maug_zdigits( num, base );
508 assert( (0 == num && 1 == digits) || (0 != num && 0 < digits) );
509 assert( digits < dest_sz );
517 maug_xtoa( num, base, rem, dest, dest_idx, digits, digits_done, pad );
525uint32_t maug_atou32(
const char* buffer,
size_t buffer_sz, uint8_t base ) {
527 uint32_t u32_out = 0;
529 if( 0 == buffer_sz ) {
530 buffer_sz = maug_strlen( buffer );
533 for( i = 0 ; buffer_sz > i ; i++ ) {
534 if(
'a' <= buffer[i] ) {
536 u32_out += (buffer[i] -
'a') + 10;
537 }
else if(
'A' <= buffer[i] ) {
539 u32_out += (buffer[i] -
'A') + 10;
540 }
else if(
'0' <= buffer[i] ) {
542 u32_out += (buffer[i] -
'0');
553int32_t maug_atos32(
const char* buffer,
size_t buffer_sz ) {
558 if( 0 == buffer_sz ) {
559 buffer_sz = maug_strlen( buffer );
562 for( i = 0 ; buffer_sz > i ; i++ ) {
563 if(
'-' == buffer[i] && 0 == i ) {
565 }
else if(
'0' <= buffer[i] &&
'9' >= buffer[i] ) {
567 s32_out += (buffer[i] -
'0');
582float maug_atof(
const char* buffer,
size_t buffer_sz ) {
587 if( 0 == buffer_sz ) {
588 buffer_sz = maug_strlen( buffer );
591 for( i = 0 ; buffer_sz > i ; i++ ) {
592 if(
'0' <= buffer[i] &&
'9' >= buffer[i] ) {
596 float_out += (buffer[i] -
'0');
599 float_out += ((buffer[i] -
'0') * dec_sz);
602 }
else if(
'.' == buffer[i] ) {
615void maug_str_upper(
char* line,
size_t line_sz ) {
618 for( i = 0 ; line_sz > i ; i++ ) {
619 if( 0x61 <= line[i] && 0x7a >= line[i] ) {
627void maug_str_lower(
char* line,
size_t line_sz ) {
630 for( i = 0 ; line_sz > i ; i++ ) {
631 if( 0x41 <= line[i] && 0x5a >= line[i] ) {
640 size_t idx,
const char* line,
size_t line_sz,
char* out,
size_t out_sz
648 line_sz = maug_strlen( line );
652 for( i_in = 0 ; line_sz >= i_in ; i_in++ ) {
653 if(
'\n' == line[i_in] ||
' ' == line[i_in] ||
'\0' == line[i_in] ) {
656 if( idx_iter > idx ) {
660 assert( i_out < out_sz );
664 }
else if( idx_iter == idx ) {
665 if( i_out + 1 >= out_sz ) {
667 "token " SIZE_T_FMT
" would exceed buffer sz: " SIZE_T_FMT,
672 out[i_out++] = line[i_in];
676 error_printf(
"token " SIZE_T_FMT
" not available in line!", idx );
686 size_t idx,
const char* line,
size_t line_sz, int16_t* out
689 char out_str[UPRINTF_S16_DIGITS + 1];
691 maug_mzero( out_str, UPRINTF_S16_DIGITS + 1 );
693 retval = maug_tok_str(
694 idx, line, line_sz, out_str, UPRINTF_S16_DIGITS + 1 );
695 maug_cleanup_if_not_ok();
697 *out = maug_atos32( out_str, UPRINTF_S16_DIGITS );
708 char* buffer,
int buffer_sz,
const char* fmt, va_list vargs
717 int spec_is_long = 0;
719 for( i = 0 ;
'\0' != fmt[i] ; i++ ) {
731 spec.s = va_arg( vargs,
char* );
735 while(
'\0' != spec.s[j] ) {
736 maug_bufcat( spec.s[j++], buffer, buffer_idx,
737 buffer_sz, cleanup );
743 spec.u = va_arg( vargs, uint32_t );
745 spec.u = va_arg( vargs,
unsigned int );
749 pad_len -= maug_digits( spec.d, 10 );
750 maug_bufpad( pad_char, pad_len, j,
751 buffer, buffer_idx, buffer_sz, cleanup );
754 buffer_idx += maug_utoa(
755 spec.d, &(buffer[buffer_idx]), buffer_sz - buffer_idx, 10 );
760 spec.d = va_arg( vargs,
long int );
762 spec.d = va_arg( vargs,
int );
766 pad_len -= maug_digits( spec.d, 10 );
767 maug_bufpad( pad_char, pad_len, j,
768 buffer, buffer_idx, buffer_sz, cleanup );
771 buffer_idx += maug_itoa(
772 spec.d, &(buffer[buffer_idx]), buffer_sz - buffer_idx, 10 );
776 spec.z = va_arg( vargs,
size_t );
779 pad_len -= maug_zdigits( spec.z, 10 );
780 maug_bufpad( pad_char, pad_len, j,
781 buffer, buffer_idx, buffer_sz, cleanup );
784 buffer_idx += maug_ztoa(
785 spec.z, &(buffer[buffer_idx]), buffer_sz - buffer_idx, 10 );
789 spec.d = va_arg( vargs,
int );
792 pad_len -= maug_digits( spec.d, 16 );
793 maug_bufpad( pad_char, pad_len, j,
794 buffer, buffer_idx, buffer_sz, cleanup );
797 buffer_idx += maug_utoa(
798 spec.d, &(buffer[buffer_idx]), buffer_sz - buffer_idx, 16 );
802 spec.c = va_arg( vargs,
int );
806 maug_bufpad( pad_char, pad_len, j,
807 buffer, buffer_idx, buffer_sz, cleanup );
810 maug_bufcat( spec.c, buffer, buffer_idx,
811 buffer_sz, cleanup );
817 maug_bufcat(
'%', buffer, buffer_idx, buffer_sz, cleanup );
843 pad_len += (c -
'0');
847 }
else if(
'%' != c ) {
853 maug_bufcat( c, buffer, buffer_idx,
854 buffer_sz, cleanup );
861 maug_bufcat(
'\0', buffer, buffer_idx,
862 buffer_sz, cleanup );
871void maug_snprintf(
char* buffer,
int buffer_sz,
const char* fmt, ... ) {
874 va_start( vargs, fmt );
875 maug_vsnprintf( buffer, buffer_sz, fmt, vargs );
881void maug_printf(
const char* fmt, ... ) {
882 char buffer[UPRINTF_BUFFER_SZ_MAX];
885 va_start( vargs, fmt );
886 maug_vsnprintf( buffer, UPRINTF_BUFFER_SZ_MAX, fmt, vargs );
894#ifndef RETROFLAT_API_WINCE
896void maug_debug_printf(
897 FILE* out, uint8_t flags,
const char* src_name,
size_t line, int16_t lvl,
910 if( lvl >= g_maug_uprintf_threshold ) {
911 platform_fprintf( out,
"(%d) %s : " SIZE_T_FMT
": ",
912 lvl, src_name, line );
914 va_start( vargs, fmt );
915 platform_vfprintf( out, fmt, vargs );
918 platform_fprintf( out, NEWLINE_STR );
919 platform_fflush( out );
927extern uint32_t g_maug_printf_line;
int MERROR_RETVAL
Return type indicating function returns a value from this list.
Definition merror.h:19
#define maug_mzero(ptr, sz)
Zero the block of memory pointed to by ptr.
Definition mmem.h:62