• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <vector>
21 
22 // Appends the content from the file named as |filename| to |data|, assuming
23 // each element in the file is of type |T|. The file is opened with the given
24 // |mode|. If |filename| is nullptr or "-", reads from the standard input, but
25 // reopened with the given mode. If any error occurs, writes error messages to
26 // standard error and returns false.
27 template <typename T>
ReadFile(const char * filename,const char * mode,std::vector<T> * data)28 bool ReadFile(const char* filename, const char* mode, std::vector<T>* data) {
29   const int buf_size = 1024;
30   const bool use_file = filename && strcmp("-", filename);
31   if (FILE* fp =
32           (use_file ? fopen(filename, mode) : freopen(nullptr, mode, stdin))) {
33     T buf[buf_size];
34     while (size_t len = fread(buf, sizeof(T), buf_size, fp)) {
35       data->insert(data->end(), buf, buf + len);
36     }
37     if (ftell(fp) == -1L) {
38       if (ferror(fp)) {
39         fprintf(stderr, "error: error reading file '%s'\n", filename);
40         if (use_file) fclose(fp);
41         return false;
42       }
43     } else {
44       if (sizeof(T) != 1 && (ftell(fp) % sizeof(T))) {
45         fprintf(
46             stderr,
47             "error: file size should be a multiple of %zd; file '%s' corrupt\n",
48             sizeof(T), filename);
49         if (use_file) fclose(fp);
50         return false;
51       }
52     }
53     if (use_file) fclose(fp);
54   } else {
55     fprintf(stderr, "error: file does not exist '%s'\n", filename);
56     return false;
57   }
58   return true;
59 }
60 
61 // Writes the given |data| into the file named as |filename| using the given
62 // |mode|, assuming |data| is an array of |count| elements of type |T|. If
63 // |filename| is nullptr or "-", writes to standard output. If any error occurs,
64 // returns false and outputs error message to standard error.
65 template <typename T>
WriteFile(const char * filename,const char * mode,const T * data,size_t count)66 bool WriteFile(const char* filename, const char* mode, const T* data,
67                size_t count) {
68   const bool use_stdout =
69       !filename || (filename[0] == '-' && filename[1] == '\0');
70   if (FILE* fp = (use_stdout ? stdout : fopen(filename, mode))) {
71     size_t written = fwrite(data, sizeof(T), count, fp);
72     if (count != written) {
73       fprintf(stderr, "error: could not write to file '%s'\n", filename);
74       if (!use_stdout) fclose(fp);
75       return false;
76     }
77     if (!use_stdout) fclose(fp);
78   } else {
79     fprintf(stderr, "error: could not open file '%s'\n", filename);
80     return false;
81   }
82   return true;
83 }
84 
85 #endif  // TOOLS_IO_H_
86