rlm@0: #include "demo_util.h" rlm@0: rlm@0: #include rlm@0: #include rlm@0: #include rlm@0: #include rlm@0: rlm@0: /* Copyright (C) 2007 Shay Green. This module is free software; you rlm@0: can redistribute it and/or modify it under the terms of the GNU Lesser rlm@0: General Public License as published by the Free Software Foundation; either rlm@0: version 2.1 of the License, or (at your option) any later version. This rlm@0: module is distributed in the hope that it will be useful, but WITHOUT ANY rlm@0: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS rlm@0: FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more rlm@0: details. You should have received a copy of the GNU Lesser General Public rlm@0: License along with this module; if not, write to the Free Software Foundation, rlm@0: Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ rlm@0: rlm@0: unsigned char* load_file( const char* path, long* size_out ) rlm@0: { rlm@0: size_t size; rlm@0: unsigned char* data; rlm@0: rlm@0: FILE* in = fopen( path, "rb" ); rlm@0: if ( !in ) error( "Couldn't open file" ); rlm@0: rlm@0: fseek( in, 0, SEEK_END ); rlm@0: size = ftell( in ); rlm@0: if ( size_out ) rlm@0: *size_out = size; rlm@0: rewind( in ); rlm@0: rlm@0: data = (unsigned char*) malloc( size ); rlm@0: if ( !data ) error( "Out of memory" ); rlm@0: rlm@0: if ( fread( data, 1, size, in ) < size ) error( "Couldn't read file" ); rlm@0: fclose( in ); rlm@0: rlm@0: return data; rlm@0: } rlm@0: rlm@0: void write_file( const char* path, void const* in, long size ) rlm@0: { rlm@0: FILE* out = fopen( path, "wb" ); rlm@0: if ( !out ) error( "Couldn't create file" ); rlm@0: if ( (long) fwrite( in, 1, size, out ) < size ) error( "Couldn't write file" ); rlm@0: fclose( out ); rlm@0: } rlm@0: rlm@0: void error( const char* str ) rlm@0: { rlm@0: if ( str ) rlm@0: { rlm@0: fprintf( stderr, "Error: %s\n", str ); rlm@0: exit( EXIT_FAILURE ); rlm@0: } rlm@0: }