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