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 | |
Urvang Joshi | e9a15a3 | 2012-11-07 14:41:15 -0800 | [diff] [blame^] | 57 | int ExUtilWriteFile(const char* const file_name, |
| 58 | const uint8_t* data, size_t data_size) { |
| 59 | int ok; |
| 60 | FILE* out; |
| 61 | |
| 62 | if (file_name == NULL || data == NULL) { |
| 63 | return 0; |
| 64 | } |
| 65 | out = fopen(file_name, "wb"); |
| 66 | if (out == NULL) { |
| 67 | fprintf(stderr, "Error! Cannot open output file '%s'\n", file_name); |
| 68 | return 0; |
| 69 | } |
| 70 | ok = (fwrite(data, data_size, 1, out) == 1); |
| 71 | fclose(out); |
| 72 | return ok; |
| 73 | } |
| 74 | |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 75 | #if defined(__cplusplus) || defined(c_plusplus) |
| 76 | } // extern "C" |
| 77 | #endif |