Flutter Engine
file.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FILESYSTEM_FILE_H_
6 #define FILESYSTEM_FILE_H_
7 
8 #include <string>
9 #include <vector>
10 
13 
14 namespace filesystem {
15 
16 class Descriptor {
17  public:
18  using Handle = int;
19 
20  Descriptor(Handle handle) : handle_(handle) {}
21 
23  if (is_valid()) {
24  IGNORE_EINTR(::close(handle_));
25  }
26  }
27 
28  bool is_valid() { return handle_ >= 0; }
29 
30  Handle get() { return handle_; }
31 
32  private:
33  Handle handle_ = -1;
34 
35  Descriptor(Descriptor&) = delete;
36 
37  void operator=(const Descriptor&) = delete;
38 };
39 
40 // Reads the contents of the file at the given path or file descriptor and
41 // stores the data in result. Returns true if the file was read successfully,
42 // otherwise returns false. If this function returns false, |result| will be
43 // the empty string.
44 bool ReadFileToString(const std::string& path, std::string* result);
45 bool ReadFileDescriptorToString(int fd, std::string* result);
46 
47 // Reads the contents of the file at the given path and if successful, returns
48 // pair of read allocated bytes with data and size of the data if succesful.
49 // pair of <nullptr, -1> if read failed.
50 std::pair<uint8_t*, intptr_t> ReadFileToBytes(const std::string& path);
51 std::pair<uint8_t*, intptr_t> ReadFileDescriptorToBytes(int fd);
52 
53 } // namespace filesystem
54 
55 #endif // FILESYSTEM_FILE_H_
#define IGNORE_EINTR(x)
Definition: eintr_wrapper.h:46
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
std::pair< uint8_t *, intptr_t > ReadFileToBytes(const std::string &path)
Definition: file.cc:94
bool ReadFileDescriptorToString(int fd, std::string *result)
Definition: file.cc:90
Descriptor(Handle handle)
Definition: file.h:20
std::pair< uint8_t *, intptr_t > ReadFileDescriptorToBytes(int fd)
Definition: file.cc:63
bool ReadFileToString(const std::string &path, std::string *result)
Definition: file.cc:85