Search
lxdream.org :: lxdream/src/util.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/util.c
changeset 425:17f019f4ed8d
prev422:61a0598e07ff
next437:2c259474b474
author nkeynes
date Sun Oct 07 06:03:22 2007 +0000 (12 years ago)
permissions -rw-r--r--
last change Fix compilation warnings
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@425
     2
 * $Id: util.c,v 1.8 2007-10-07 06:03:22 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@17
    20
#include "dream.h"
nkeynes@17
    21
nkeynes@422
    22
void fwrite_string( const char *s, FILE *f )
nkeynes@17
    23
{
nkeynes@17
    24
    uint32_t len = 0;
nkeynes@17
    25
    if( s == NULL ) {
nkeynes@17
    26
	fwrite( &len, sizeof(len), 1, f );
nkeynes@17
    27
    } else {
nkeynes@17
    28
	len = strlen(s)+1;
nkeynes@17
    29
	fwrite( &len, sizeof(len), 1, f );
nkeynes@17
    30
	fwrite( s, len, 1, f );
nkeynes@17
    31
    }
nkeynes@17
    32
}
nkeynes@17
    33
nkeynes@17
    34
int fread_string( char *s, int maxlen, FILE *f ) 
nkeynes@17
    35
{
nkeynes@17
    36
    uint32_t len;
nkeynes@17
    37
    fread( &len, sizeof(len), 1, f );
nkeynes@17
    38
    if( len != 0 ) {
nkeynes@17
    39
	fread( s, len > maxlen ? maxlen : len, 1, f );
nkeynes@17
    40
    }
nkeynes@17
    41
    return len;
nkeynes@17
    42
}
nkeynes@117
    43
nkeynes@117
    44
void fwrite_dump( unsigned char *data, unsigned int length, FILE *f ) 
nkeynes@117
    45
{
nkeynes@117
    46
    unsigned int i, j;
nkeynes@117
    47
    for( i =0; i<length; i+=16 ) {
nkeynes@117
    48
	fprintf( f, "%08X:", i);
nkeynes@117
    49
	for( j=i; j<i+16; j++ ) {
nkeynes@117
    50
	    if( (j % 4) == 0 )
nkeynes@117
    51
		fprintf( f, " " );
nkeynes@117
    52
	    if( j < length )
nkeynes@117
    53
		fprintf( f, " %02X", (unsigned int)(data[j]) );
nkeynes@117
    54
	    else
nkeynes@117
    55
		fprintf( f, "   " );
nkeynes@117
    56
	}
nkeynes@117
    57
	fprintf( f, "  " );
nkeynes@117
    58
	for( j=i; j<i+16 && j<length; j++ ) {
nkeynes@117
    59
	    fprintf( f, "%c", isprint(data[j]) ? data[j] : '.' );
nkeynes@117
    60
	}
nkeynes@117
    61
	fprintf( f, "\n" );
nkeynes@117
    62
    }
nkeynes@117
    63
}
nkeynes@187
    64
nkeynes@187
    65
void fwrite_dump32( unsigned int *data, unsigned int length, FILE *f ) 
nkeynes@187
    66
{
nkeynes@220
    67
    fwrite_dump32v( data, length, 8, f );
nkeynes@220
    68
}
nkeynes@220
    69
nkeynes@220
    70
void fwrite_dump32v( unsigned int *data, unsigned int length, int wordsPerLine, FILE *f ) 
nkeynes@220
    71
{
nkeynes@187
    72
    unsigned int i, j;
nkeynes@220
    73
    for( i =0; i<length>>2; i+=wordsPerLine ) {
nkeynes@187
    74
	fprintf( f, "%08X:", i);
nkeynes@220
    75
	for( j=i; j<i+wordsPerLine; j++ ) {
nkeynes@187
    76
	    if( j < length )
nkeynes@187
    77
		fprintf( f, " %08X", (unsigned int)(data[j]) );
nkeynes@187
    78
	    else
nkeynes@187
    79
		fprintf( f, "         " );
nkeynes@187
    80
	}
nkeynes@187
    81
	fprintf( f, "\n" );
nkeynes@187
    82
    }
nkeynes@187
    83
}
.