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