Search
lxdream.org :: lxdream/src/util.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/util.c
changeset 447:3e095bfcb476
prev437:2c259474b474
next477:9a373f2ff009
author nkeynes
date Tue Oct 16 12:36:29 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Add gui error reporting
Add initial controller settings dialog
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@447
     2
 * $Id: util.c,v 1.10 2007-10-16 12:36:29 nkeynes Exp $
nkeynes@31
     3
 *
nkeynes@31
     4
 * Miscellaneous utility functions.
nkeynes@31
     5
 *
nkeynes@31
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@31
     7
 *
nkeynes@31
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@31
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@31
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@31
    11
 * (at your option) any later version.
nkeynes@31
    12
 *
nkeynes@31
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@31
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@31
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@31
    16
 * GNU General Public License for more details.
nkeynes@31
    17
 */
nkeynes@31
    18
nkeynes@425
    19
#include <ctype.h>
nkeynes@437
    20
#include <stdarg.h>
nkeynes@437
    21
#include <stdio.h>
nkeynes@437
    22
#include <stdlib.h>
nkeynes@437
    23
#include <time.h>
nkeynes@17
    24
#include "dream.h"
nkeynes@437
    25
#include "sh4/sh4core.h"
nkeynes@437
    26
nkeynes@437
    27
char *msg_levels[] = { "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE" };
nkeynes@437
    28
int global_msg_level = EMIT_WARN;
nkeynes@17
    29
nkeynes@422
    30
void fwrite_string( const char *s, FILE *f )
nkeynes@17
    31
{
nkeynes@17
    32
    uint32_t len = 0;
nkeynes@17
    33
    if( s == NULL ) {
nkeynes@17
    34
	fwrite( &len, sizeof(len), 1, f );
nkeynes@17
    35
    } else {
nkeynes@17
    36
	len = strlen(s)+1;
nkeynes@17
    37
	fwrite( &len, sizeof(len), 1, f );
nkeynes@17
    38
	fwrite( s, len, 1, f );
nkeynes@17
    39
    }
nkeynes@17
    40
}
nkeynes@17
    41
nkeynes@17
    42
int fread_string( char *s, int maxlen, FILE *f ) 
nkeynes@17
    43
{
nkeynes@17
    44
    uint32_t len;
nkeynes@17
    45
    fread( &len, sizeof(len), 1, f );
nkeynes@17
    46
    if( len != 0 ) {
nkeynes@17
    47
	fread( s, len > maxlen ? maxlen : len, 1, f );
nkeynes@17
    48
    }
nkeynes@17
    49
    return len;
nkeynes@17
    50
}
nkeynes@117
    51
nkeynes@117
    52
void fwrite_dump( unsigned char *data, unsigned int length, FILE *f ) 
nkeynes@117
    53
{
nkeynes@117
    54
    unsigned int i, j;
nkeynes@117
    55
    for( i =0; i<length; i+=16 ) {
nkeynes@117
    56
	fprintf( f, "%08X:", i);
nkeynes@117
    57
	for( j=i; j<i+16; j++ ) {
nkeynes@117
    58
	    if( (j % 4) == 0 )
nkeynes@117
    59
		fprintf( f, " " );
nkeynes@117
    60
	    if( j < length )
nkeynes@117
    61
		fprintf( f, " %02X", (unsigned int)(data[j]) );
nkeynes@117
    62
	    else
nkeynes@117
    63
		fprintf( f, "   " );
nkeynes@117
    64
	}
nkeynes@117
    65
	fprintf( f, "  " );
nkeynes@117
    66
	for( j=i; j<i+16 && j<length; j++ ) {
nkeynes@117
    67
	    fprintf( f, "%c", isprint(data[j]) ? data[j] : '.' );
nkeynes@117
    68
	}
nkeynes@117
    69
	fprintf( f, "\n" );
nkeynes@117
    70
    }
nkeynes@117
    71
}
nkeynes@187
    72
nkeynes@187
    73
void fwrite_dump32( unsigned int *data, unsigned int length, FILE *f ) 
nkeynes@187
    74
{
nkeynes@220
    75
    fwrite_dump32v( data, length, 8, f );
nkeynes@220
    76
}
nkeynes@220
    77
nkeynes@220
    78
void fwrite_dump32v( unsigned int *data, unsigned int length, int wordsPerLine, FILE *f ) 
nkeynes@220
    79
{
nkeynes@187
    80
    unsigned int i, j;
nkeynes@220
    81
    for( i =0; i<length>>2; i+=wordsPerLine ) {
nkeynes@187
    82
	fprintf( f, "%08X:", i);
nkeynes@220
    83
	for( j=i; j<i+wordsPerLine; j++ ) {
nkeynes@187
    84
	    if( j < length )
nkeynes@187
    85
		fprintf( f, " %08X", (unsigned int)(data[j]) );
nkeynes@187
    86
	    else
nkeynes@187
    87
		fprintf( f, "         " );
nkeynes@187
    88
	}
nkeynes@187
    89
	fprintf( f, "\n" );
nkeynes@187
    90
    }
nkeynes@187
    91
}
nkeynes@437
    92
nkeynes@437
    93
nkeynes@437
    94
void log_message( void *ptr, int level, const gchar *source, const char *msg, ... )
nkeynes@437
    95
{
nkeynes@437
    96
    char buf[20], addr[10] = "", *p;
nkeynes@437
    97
    const gchar *arr[4] = {buf, source, addr};
nkeynes@437
    98
    int posn;
nkeynes@437
    99
    time_t tm = time(NULL);
nkeynes@437
   100
    va_list ap;
nkeynes@437
   101
nkeynes@437
   102
    if( level > global_msg_level ) {
nkeynes@437
   103
	return; // ignored
nkeynes@437
   104
    }
nkeynes@447
   105
nkeynes@437
   106
    va_start(ap, msg);
nkeynes@437
   107
nkeynes@447
   108
    if( level <= EMIT_ERR ) {
nkeynes@447
   109
	gchar *text = g_strdup_vprintf( msg, ap );
nkeynes@447
   110
	if( gui_error_dialog( text ) ) {
nkeynes@447
   111
	    g_free(text);
nkeynes@447
   112
	    va_end(ap);
nkeynes@447
   113
	    return;
nkeynes@447
   114
	}
nkeynes@447
   115
	g_free(text);
nkeynes@447
   116
    }
nkeynes@447
   117
nkeynes@447
   118
nkeynes@437
   119
    strftime( buf, sizeof(buf), "%H:%M:%S", localtime(&tm) );
nkeynes@437
   120
    fprintf( stderr, "%s %08X %-5s ", buf, sh4r.pc, msg_levels[level] );
nkeynes@437
   121
    vfprintf( stderr, msg, ap );
nkeynes@447
   122
    va_end(ap);
nkeynes@437
   123
    fprintf( stderr, "\n" );
nkeynes@437
   124
}
.