James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 1 | // Copyright 2012 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // This code is licensed under the same terms as WebM: |
| 4 | // Software License Agreement: http://www.webmproject.org/license/software/ |
| 5 | // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ |
| 6 | // ----------------------------------------------------------------------------- |
| 7 | // |
| 8 | // Utility functions used by the example programs. |
| 9 | // |
| 10 | |
| 11 | #include "./example_util.h" |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | |
| 15 | #if defined(__cplusplus) || defined(c_plusplus) |
| 16 | extern "C" { |
| 17 | #endif |
| 18 | |
| 19 | // ----------------------------------------------------------------------------- |
| 20 | // File I/O |
| 21 | |
| 22 | int ExUtilReadFile(const char* const file_name, |
| 23 | const uint8_t** data, size_t* data_size) { |
| 24 | int ok; |
| 25 | void* file_data; |
| 26 | size_t file_size; |
| 27 | FILE* in; |
| 28 | |
| 29 | if (file_name == NULL || data == NULL || data_size == NULL) return 0; |
| 30 | *data = NULL; |
| 31 | *data_size = 0; |
| 32 | |
| 33 | in = fopen(file_name, "rb"); |
| 34 | if (in == NULL) { |
| 35 | fprintf(stderr, "cannot open input file '%s'\n", file_name); |
| 36 | return 0; |
| 37 | } |
| 38 | fseek(in, 0, SEEK_END); |
| 39 | file_size = ftell(in); |
| 40 | fseek(in, 0, SEEK_SET); |
| 41 | file_data = malloc(file_size); |
| 42 | if (file_data == NULL) return 0; |
| 43 | ok = (fread(file_data, file_size, 1, in) == 1); |
| 44 | fclose(in); |
| 45 | |
| 46 | if (!ok) { |
| 47 | fprintf(stderr, "Could not read %zu bytes of data from file %s\n", |
| 48 | file_size, file_name); |
| 49 | free(file_data); |
| 50 | return 0; |
| 51 | } |
| 52 | *data = (uint8_t*)file_data; |
| 53 | *data_size = file_size; |
| 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | #if defined(__cplusplus) || defined(c_plusplus) |
| 58 | } // extern "C" |
| 59 | #endif |