io.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #ifndef TOOLS_IO_H_
  15. #define TOOLS_IO_H_
  16. #include <cstdint>
  17. #include <cstdio>
  18. #include <cstring>
  19. #include <vector>
  20. #if defined(SPIRV_WINDOWS)
  21. #include <fcntl.h>
  22. #include <io.h>
  23. #define SET_STDIN_TO_BINARY_MODE() _setmode(_fileno(stdin), O_BINARY);
  24. #define SET_STDIN_TO_TEXT_MODE() _setmode(_fileno(stdin), O_TEXT);
  25. #define SET_STDOUT_TO_BINARY_MODE() _setmode(_fileno(stdout), O_BINARY);
  26. #define SET_STDOUT_TO_TEXT_MODE() _setmode(_fileno(stdout), O_TEXT);
  27. #define SET_STDOUT_MODE(mode) _setmode(_fileno(stdout), mode);
  28. #else
  29. #define SET_STDIN_TO_BINARY_MODE()
  30. #define SET_STDIN_TO_TEXT_MODE()
  31. #define SET_STDOUT_TO_BINARY_MODE() 0
  32. #define SET_STDOUT_TO_TEXT_MODE() 0
  33. #define SET_STDOUT_MODE(mode)
  34. #endif
  35. // Appends the contents of the |file| to |data|, assuming each element in the
  36. // file is of type |T|.
  37. template <typename T>
  38. void ReadFile(FILE* file, std::vector<T>* data) {
  39. if (file == nullptr) return;
  40. const int buf_size = 1024;
  41. T buf[buf_size];
  42. while (size_t len = fread(buf, sizeof(T), buf_size, file)) {
  43. data->insert(data->end(), buf, buf + len);
  44. }
  45. }
  46. // Returns true if |file| has encountered an error opening the file or reading
  47. // the file as a series of element of type |T|. If there was an error, writes an
  48. // error message to standard error.
  49. template <class T>
  50. bool WasFileCorrectlyRead(FILE* file, const char* filename) {
  51. if (file == nullptr) {
  52. fprintf(stderr, "error: file does not exist '%s'\n", filename);
  53. return false;
  54. }
  55. if (ftell(file) == -1L) {
  56. if (ferror(file)) {
  57. fprintf(stderr, "error: error reading file '%s'\n", filename);
  58. return false;
  59. }
  60. } else {
  61. if (sizeof(T) != 1 && (ftell(file) % sizeof(T))) {
  62. fprintf(
  63. stderr,
  64. "error: file size should be a multiple of %zd; file '%s' corrupt\n",
  65. sizeof(T), filename);
  66. return false;
  67. }
  68. }
  69. return true;
  70. }
  71. // Appends the contents of the file named |filename| to |data|, assuming
  72. // each element in the file is of type |T|. The file is opened as a binary file
  73. // If |filename| is nullptr or "-", reads from the standard input, but
  74. // reopened as a binary file. If any error occurs, writes error messages to
  75. // standard error and returns false.
  76. template <typename T>
  77. bool ReadBinaryFile(const char* filename, std::vector<T>* data) {
  78. const bool use_file = filename && strcmp("-", filename);
  79. FILE* fp = nullptr;
  80. if (use_file) {
  81. fp = fopen(filename, "rb");
  82. } else {
  83. SET_STDIN_TO_BINARY_MODE();
  84. fp = stdin;
  85. }
  86. ReadFile(fp, data);
  87. bool succeeded = WasFileCorrectlyRead<T>(fp, filename);
  88. if (use_file && fp) fclose(fp);
  89. return succeeded;
  90. }
  91. // Appends the contents of the file named |filename| to |data|, assuming
  92. // each element in the file is of type |T|. The file is opened as a text file
  93. // If |filename| is nullptr or "-", reads from the standard input, but
  94. // reopened as a text file. If any error occurs, writes error messages to
  95. // standard error and returns false.
  96. template <typename T>
  97. bool ReadTextFile(const char* filename, std::vector<T>* data) {
  98. const bool use_file = filename && strcmp("-", filename);
  99. FILE* fp = nullptr;
  100. if (use_file) {
  101. fp = fopen(filename, "r");
  102. } else {
  103. SET_STDIN_TO_TEXT_MODE();
  104. fp = stdin;
  105. }
  106. ReadFile(fp, data);
  107. bool succeeded = WasFileCorrectlyRead<T>(fp, filename);
  108. if (use_file && fp) fclose(fp);
  109. return succeeded;
  110. }
  111. namespace {
  112. // A class to create and manage a file for outputting data.
  113. class OutputFile {
  114. public:
  115. // Opens |filename| in the given mode. If |filename| is nullptr, the empty
  116. // string or "-", stdout will be set to the given mode.
  117. OutputFile(const char* filename, const char* mode) {
  118. const bool use_stdout =
  119. !filename || (filename[0] == '-' && filename[1] == '\0');
  120. if (use_stdout) {
  121. if (strchr(mode, 'b')) {
  122. old_mode_ = SET_STDOUT_TO_BINARY_MODE();
  123. } else {
  124. old_mode_ = SET_STDOUT_TO_TEXT_MODE();
  125. }
  126. fp_ = stdout;
  127. } else {
  128. fp_ = fopen(filename, mode);
  129. }
  130. }
  131. ~OutputFile() {
  132. if (fp_ == stdout) {
  133. SET_STDOUT_MODE(old_mode_);
  134. } else if (fp_ != nullptr) {
  135. fclose(fp_);
  136. }
  137. }
  138. // Returns a file handle to the file.
  139. FILE* GetFileHandle() const { return fp_; }
  140. private:
  141. FILE* fp_;
  142. int old_mode_;
  143. };
  144. } // namespace
  145. // Writes the given |data| into the file named as |filename| using the given
  146. // |mode|, assuming |data| is an array of |count| elements of type |T|. If
  147. // |filename| is nullptr or "-", writes to standard output. If any error occurs,
  148. // returns false and outputs error message to standard error.
  149. template <typename T>
  150. bool WriteFile(const char* filename, const char* mode, const T* data,
  151. size_t count) {
  152. OutputFile file(filename, mode);
  153. FILE* fp = file.GetFileHandle();
  154. if (fp == nullptr) {
  155. fprintf(stderr, "error: could not open file '%s'\n", filename);
  156. return false;
  157. }
  158. size_t written = fwrite(data, sizeof(T), count, fp);
  159. if (count != written) {
  160. fprintf(stderr, "error: could not write to file '%s'\n", filename);
  161. return false;
  162. }
  163. return true;
  164. }
  165. #endif // TOOLS_IO_H_