annotate demo/demo_util.c @ 9:477c36226481 tip

added old scripts for historical interest.
author Robert McIntyre <rlm@mit.edu>
date Fri, 21 Oct 2011 07:46:18 -0700
parents e38dacceb958
children
rev   line source
rlm@0 1 #include "demo_util.h"
rlm@0 2
rlm@0 3 #include <assert.h>
rlm@0 4 #include <string.h>
rlm@0 5 #include <stdlib.h>
rlm@0 6 #include <stdio.h>
rlm@0 7
rlm@0 8 /* Copyright (C) 2007 Shay Green. This module is free software; you
rlm@0 9 can redistribute it and/or modify it under the terms of the GNU Lesser
rlm@0 10 General Public License as published by the Free Software Foundation; either
rlm@0 11 version 2.1 of the License, or (at your option) any later version. This
rlm@0 12 module is distributed in the hope that it will be useful, but WITHOUT ANY
rlm@0 13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
rlm@0 14 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
rlm@0 15 details. You should have received a copy of the GNU Lesser General Public
rlm@0 16 License along with this module; if not, write to the Free Software Foundation,
rlm@0 17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
rlm@0 18
rlm@0 19 unsigned char* load_file( const char* path, long* size_out )
rlm@0 20 {
rlm@0 21 size_t size;
rlm@0 22 unsigned char* data;
rlm@0 23
rlm@0 24 FILE* in = fopen( path, "rb" );
rlm@0 25 if ( !in ) error( "Couldn't open file" );
rlm@0 26
rlm@0 27 fseek( in, 0, SEEK_END );
rlm@0 28 size = ftell( in );
rlm@0 29 if ( size_out )
rlm@0 30 *size_out = size;
rlm@0 31 rewind( in );
rlm@0 32
rlm@0 33 data = (unsigned char*) malloc( size );
rlm@0 34 if ( !data ) error( "Out of memory" );
rlm@0 35
rlm@0 36 if ( fread( data, 1, size, in ) < size ) error( "Couldn't read file" );
rlm@0 37 fclose( in );
rlm@0 38
rlm@0 39 return data;
rlm@0 40 }
rlm@0 41
rlm@0 42 void write_file( const char* path, void const* in, long size )
rlm@0 43 {
rlm@0 44 FILE* out = fopen( path, "wb" );
rlm@0 45 if ( !out ) error( "Couldn't create file" );
rlm@0 46 if ( (long) fwrite( in, 1, size, out ) < size ) error( "Couldn't write file" );
rlm@0 47 fclose( out );
rlm@0 48 }
rlm@0 49
rlm@0 50 void error( const char* str )
rlm@0 51 {
rlm@0 52 if ( str )
rlm@0 53 {
rlm@0 54 fprintf( stderr, "Error: %s\n", str );
rlm@0 55 exit( EXIT_FAILURE );
rlm@0 56 }
rlm@0 57 }