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