Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 66 lines (57 sloc) 1.786 kb
720b0e7 @uliwitness Added licensing information to the source files.
authored
1 //
2 // GetFileContents.h
3 // Carlson
4 //
5 // Created by Uli Kusterer on 16.04.06.
6 // Copyright 2006 Uli Kusterer.
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
26 //
dc5be1c Initial check-in.
uli authored
27
28 #include "GetFileContents.h"
29 #include <stdio.h>
30 #include <stdlib.h>
31
32
33 char* GetFileContents( const char* fname )
34 {
35 // Open script to run:
36 FILE* theFile = fopen( fname, "r" );
37 if( !theFile )
38 {
39 printf("ERROR: Can't open file \"%s\".\n", fname);
40 return NULL;
41 }
42
43 // Find out file length:
44 fseek( theFile, 0, SEEK_END );
45 int len = ftell( theFile ),
46 readbytes;
47 char* codeStr = (char*) malloc( len +1 );
48
49 // Rewind and read in whole file:
50 fseek( theFile, 0, SEEK_SET );
51 readbytes = fread( codeStr, 1, len, theFile );
52 if( readbytes != len )
53 {
54 free( codeStr );
55 fclose( theFile );
56 printf("ERROR: Couldn't read from file \"%s\" (%d bytes read).\n",fname,readbytes);
57 return NULL;
58 }
59 codeStr[len] = 0; // Terminate string.
60 fclose( theFile );
61
62 return codeStr;
63 }
64
65
Something went wrong with that request. Please try again.