James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 1 | // Copyright 2012 Google Inc. All Rights Reserved. |
| 2 | // |
James Zern | d640614 | 2013-06-06 23:05:58 -0700 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license |
| 4 | // that can be found in the COPYING file in the root of the source |
| 5 | // tree. An additional intellectual property rights grant can be found |
| 6 | // in the file PATENTS. All contributing project authors may |
| 7 | // be found in the AUTHORS file in the root of the source tree. |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 8 | // ----------------------------------------------------------------------------- |
| 9 | // |
| 10 | // Utility functions used by the example programs. |
| 11 | // |
| 12 | |
| 13 | #include "./example_util.h" |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame^] | 16 | #include <string.h> |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 17 | |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 18 | // ----------------------------------------------------------------------------- |
| 19 | // File I/O |
| 20 | |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame^] | 21 | static const size_t kBlockSize = 16384; // default initial size |
| 22 | |
| 23 | int ExUtilReadFromStdin(const uint8_t** data, size_t* data_size) { |
| 24 | size_t max_size = 0; |
| 25 | size_t size = 0; |
| 26 | uint8_t* input = NULL; |
| 27 | |
| 28 | if (data == NULL || data_size == NULL) return 0; |
| 29 | *data = NULL; |
| 30 | *data_size = 0; |
| 31 | |
| 32 | while (!feof(stdin)) { |
| 33 | // We double the buffer size each time and read as much as possible. |
| 34 | const size_t extra_size = (max_size == 0) ? kBlockSize : max_size; |
| 35 | void* const new_data = realloc(input, max_size + extra_size); |
| 36 | if (new_data == NULL) goto Error; |
| 37 | input = (uint8_t*)new_data; |
| 38 | max_size += extra_size; |
| 39 | size += fread(input + size, 1, extra_size, stdin); |
| 40 | if (size < max_size) break; |
| 41 | } |
| 42 | if (ferror(stdin)) goto Error; |
| 43 | *data = input; |
| 44 | *data_size = size; |
| 45 | return 1; |
| 46 | |
| 47 | Error: |
| 48 | free(input); |
| 49 | fprintf(stderr, "Could not read from stdin\n"); |
| 50 | return 0; |
| 51 | } |
| 52 | |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 53 | int ExUtilReadFile(const char* const file_name, |
| 54 | const uint8_t** data, size_t* data_size) { |
| 55 | int ok; |
| 56 | void* file_data; |
| 57 | size_t file_size; |
| 58 | FILE* in; |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame^] | 59 | const int from_stdin = (file_name == NULL) || !strcmp(file_name, "-"); |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 60 | |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame^] | 61 | if (from_stdin) return ExUtilReadFromStdin(data, data_size); |
| 62 | |
| 63 | if (data == NULL || data_size == NULL) return 0; |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 64 | *data = NULL; |
| 65 | *data_size = 0; |
| 66 | |
| 67 | in = fopen(file_name, "rb"); |
| 68 | if (in == NULL) { |
| 69 | fprintf(stderr, "cannot open input file '%s'\n", file_name); |
| 70 | return 0; |
| 71 | } |
| 72 | fseek(in, 0, SEEK_END); |
| 73 | file_size = ftell(in); |
| 74 | fseek(in, 0, SEEK_SET); |
| 75 | file_data = malloc(file_size); |
| 76 | if (file_data == NULL) return 0; |
| 77 | ok = (fread(file_data, file_size, 1, in) == 1); |
| 78 | fclose(in); |
| 79 | |
| 80 | if (!ok) { |
James Zern | 14d42af | 2013-03-20 16:59:35 -0700 | [diff] [blame] | 81 | fprintf(stderr, "Could not read %d bytes of data from file %s\n", |
| 82 | (int)file_size, file_name); |
James Zern | 061263a | 2012-05-11 16:00:57 -0700 | [diff] [blame] | 83 | free(file_data); |
| 84 | return 0; |
| 85 | } |
| 86 | *data = (uint8_t*)file_data; |
| 87 | *data_size = file_size; |
| 88 | return 1; |
| 89 | } |
| 90 | |
Urvang Joshi | e9a15a3 | 2012-11-07 14:41:15 -0800 | [diff] [blame] | 91 | int ExUtilWriteFile(const char* const file_name, |
| 92 | const uint8_t* data, size_t data_size) { |
| 93 | int ok; |
| 94 | FILE* out; |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame^] | 95 | const int to_stdout = (file_name == NULL) || !strcmp(file_name, "-"); |
Urvang Joshi | e9a15a3 | 2012-11-07 14:41:15 -0800 | [diff] [blame] | 96 | |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame^] | 97 | if (data == NULL) { |
Urvang Joshi | e9a15a3 | 2012-11-07 14:41:15 -0800 | [diff] [blame] | 98 | return 0; |
| 99 | } |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame^] | 100 | out = to_stdout ? stdout : fopen(file_name, "wb"); |
Urvang Joshi | e9a15a3 | 2012-11-07 14:41:15 -0800 | [diff] [blame] | 101 | if (out == NULL) { |
| 102 | fprintf(stderr, "Error! Cannot open output file '%s'\n", file_name); |
| 103 | return 0; |
| 104 | } |
| 105 | ok = (fwrite(data, data_size, 1, out) == 1); |
skal | 2bcad89 | 2014-03-12 19:32:16 +0100 | [diff] [blame^] | 106 | if (out != stdout) fclose(out); |
Urvang Joshi | e9a15a3 | 2012-11-07 14:41:15 -0800 | [diff] [blame] | 107 | return ok; |
| 108 | } |
| 109 | |