1 // Copyright (c) 2016 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef TOOLS_IO_H_
16 #define TOOLS_IO_H_
17 
18 #include <cstdint>
19 #include <cstdio>
20 #include <cstring>
21 #include <vector>
22 
23 // Appends the content from the file named as |filename| to |data|, assuming
24 // each element in the file is of type |T|. The file is opened with the given
25 // |mode|. If |filename| is nullptr or "-", reads from the standard input, but
26 // reopened with the given mode. If any error occurs, writes error messages to
27 // standard error and returns false.
28 template <typename T>
ReadFile(const char * filename,const char * mode,std::vector<T> * data)29 bool ReadFile(const char* filename, const char* mode, std::vector<T>* data) {
30   const int buf_size = 1024;
31   const bool use_file = filename && strcmp("-", filename);
32   if (FILE* fp =
33           (use_file ? fopen(filename, mode) : freopen(nullptr, mode, stdin))) {
34     T buf[buf_size];
35     while (size_t len = fread(buf, sizeof(T), buf_size, fp)) {
36       data->insert(data->end(), buf, buf + len);
37     }
38     if (ftell(fp) == -1L) {
39       if (ferror(fp)) {
40         fprintf(stderr, "error: error reading file '%s'\n", filename);
41         if (use_file) fclose(fp);
42         return false;
43       }
44     } else {
45       if (sizeof(T) != 1 && (ftell(fp) % sizeof(T))) {
46         fprintf(
47             stderr,
48             "error: file size should be a multiple of %zd; file '%s' corrupt\n",
49             sizeof(T), filename);
50         if (use_file) fclose(fp);
51         return false;
52       }
53     }
54     if (use_file) fclose(fp);
55   } else {
56     fprintf(stderr, "error: file does not exist '%s'\n", filename);
57     return false;
58   }
59   return true;
60 }
61 
62 // Writes the given |data| into the file named as |filename| using the given
63 // |mode|, assuming |data| is an array of |count| elements of type |T|. If
64 // |filename| is nullptr or "-", writes to standard output. If any error occurs,
65 // returns false and outputs error message to standard error.
66 template <typename T>
WriteFile(const char * filename,const char * mode,const T * data,size_t count)67 bool WriteFile(const char* filename, const char* mode, const T* data,
68                size_t count) {
69   const bool use_stdout =
70       !filename || (filename[0] == '-' && filename[1] == '\0');
71   if (FILE* fp = (use_stdout ? stdout : fopen(filename, mode))) {
72     size_t written = fwrite(data, sizeof(T), count, fp);
73     if (count != written) {
74       fprintf(stderr, "error: could not write to file '%s'\n", filename);
75       if (!use_stdout) fclose(fp);
76       return false;
77     }
78     if (!use_stdout) fclose(fp);
79   } else {
80     fprintf(stderr, "error: could not open file '%s'\n", filename);
81     return false;
82   }
83   return true;
84 }
85 
86 #endif  // TOOLS_IO_H_
87