tvm
Loading...
Searching...
No Matches
file_utils.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
24#ifndef TVM_SUPPORT_FILE_UTILS_H_
25#define TVM_SUPPORT_FILE_UTILS_H_
26
27#include <tvm/ffi/error.h>
28#include <tvm/ffi/string.h>
29
30#include <fstream>
31#include <string>
32#include <utility>
33
34namespace tvm {
35namespace support {
36
38inline ffi::String GetFileFormat(const ffi::String& path, const ffi::String& format) {
39 if (!format.empty()) return format;
40 std::string file_name = path;
41 size_t pos = file_name.find_last_of('.');
42 return pos == std::string::npos ? ffi::String() : ffi::String(file_name.substr(pos + 1));
43}
44
46inline ffi::Bytes ReadBytesFromFile(const ffi::String& path) {
47 std::ifstream fs(path.c_str(), std::ios::in | std::ios::binary);
48 TVM_FFI_ICHECK(!fs.fail()) << "Cannot open " << path;
49 fs.seekg(0, std::ios::end);
50 size_t size = static_cast<size_t>(fs.tellg());
51 fs.seekg(0, std::ios::beg);
52 std::string data(size, '\0');
53 fs.read(data.data(), size);
54 return ffi::Bytes(std::move(data));
55}
56
58inline void WriteBytesToFile(const ffi::String& path, const ffi::Bytes& data) {
59 std::ofstream fs(path.c_str(), std::ios::out | std::ios::binary);
60 TVM_FFI_ICHECK(!fs.fail()) << "Cannot open " << path;
61 fs.write(data.data(), data.size());
62}
63
64} // namespace support
65} // namespace tvm
66#endif // TVM_SUPPORT_FILE_UTILS_H_
ffi::String GetFileFormat(const ffi::String &path, const ffi::String &format)
Get the explicit format, or the extension of the file path.
Definition file_utils.h:38
void WriteBytesToFile(const ffi::String &path, const ffi::Bytes &data)
Write binary bytes to a file.
Definition file_utils.h:58
ffi::Bytes ReadBytesFromFile(const ffi::String &path)
Read a file as binary bytes.
Definition file_utils.h:46
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23