io.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. // Sets the contents of the file named |filename| in |data|, assuming each
  21. // element in the file is of type |uint32_t|. The file is opened as a binary
  22. // file. If |filename| is nullptr or "-", reads from the standard input, but
  23. // reopened as a binary file. If any error occurs, writes error messages to
  24. // standard error and returns false.
  25. //
  26. // If the given input is detected to be in ascii hex, it is converted to binary
  27. // automatically. In that case, the shape of the input data is determined based
  28. // on the representation of the magic number:
  29. //
  30. // * "[0]x[0]7230203": Every following "0x..." represents a word.
  31. // * "[0]x[0]7[,] [0]x23...": Every following "0x..." represents a byte, stored
  32. // in big-endian order
  33. // * "[0]x[0]3[,] [0]x[0]2...": Every following "0x..." represents a byte,
  34. // stored in little-endian order
  35. // * "07[, ]23...": Every following "XY" represents a byte, stored in
  36. // big-endian order
  37. // * "03[, ]02...": Every following "XY" represents a byte, stored in
  38. // little-endian order
  39. bool ReadBinaryFile(const char* filename, std::vector<uint32_t>* data);
  40. // The hex->binary logic of |ReadBinaryFile| applied to a pre-loaded stream of
  41. // bytes. Used by tests to avoid having to call |ReadBinaryFile| with temp
  42. // files. Returns false in case of parse errors.
  43. bool ConvertHexToBinary(const std::vector<char>& stream,
  44. std::vector<uint32_t>* data);
  45. // Sets the contents of the file named |filename| in |data|, assuming each
  46. // element in the file is of type |char|. The file is opened as a text file. If
  47. // |filename| is nullptr or "-", reads from the standard input, but reopened as
  48. // a text file. If any error occurs, writes error messages to standard error and
  49. // returns false.
  50. bool ReadTextFile(const char* filename, std::vector<char>* data);
  51. // Writes the given |data| into the file named as |filename| using the given
  52. // |mode|, assuming |data| is an array of |count| elements of type |T|. If
  53. // |filename| is nullptr or "-", writes to standard output. If any error occurs,
  54. // returns false and outputs error message to standard error.
  55. template <typename T>
  56. bool WriteFile(const char* filename, const char* mode, const T* data,
  57. size_t count);
  58. #endif // TOOLS_IO_H_