Debian Codegraph @

tools/io.h

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