libcamera  v0.0.0+100-debian/0_git20200629+e7aa92a-8-9-g77f5237c-dirty (2021-05-05T16:20:29+01:00)
Supporting cameras in Linux since 2019
file.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3  * Copyright (C) 2020, Google Inc.
4  *
5  * file.h - File I/O operations
6  */
7 #ifndef __LIBCAMERA_INTERNAL_FILE_H__
8 #define __LIBCAMERA_INTERNAL_FILE_H__
9 
10 #include <map>
11 #include <string>
12 #include <sys/types.h>
13 
14 #include <libcamera/class.h>
15 #include <libcamera/span.h>
16 
17 namespace libcamera {
18 
19 class File
20 {
21 public:
22  enum MapFlag {
24  MapPrivate = (1 << 0),
25  };
26 
27  enum OpenMode {
28  NotOpen = 0,
29  ReadOnly = (1 << 0),
30  WriteOnly = (1 << 1),
32  };
33 
34  File(const std::string &name);
35  File();
36  ~File();
37 
38  const std::string &fileName() const { return name_; }
39  void setFileName(const std::string &name);
40  bool exists() const;
41 
42  bool open(OpenMode mode);
43  bool isOpen() const { return fd_ != -1; }
44  OpenMode openMode() const { return mode_; }
45  void close();
46 
47  int error() const { return error_; }
48  ssize_t size() const;
49 
50  off_t pos() const;
51  off_t seek(off_t pos);
52 
53  ssize_t read(const Span<uint8_t> &data);
54  ssize_t write(const Span<const uint8_t> &data);
55 
56  Span<uint8_t> map(off_t offset = 0, ssize_t size = -1,
57  MapFlag flags = MapNoOption);
58  bool unmap(uint8_t *addr);
59 
60  static bool exists(const std::string &name);
61 
62 private:
64 
65  void unmapAll();
66 
67  std::string name_;
68  int fd_;
69  OpenMode mode_;
70 
71  int error_;
72  std::map<void *, size_t> maps_;
73 };
74 
75 } /* namespace libcamera */
76 
77 #endif /* __LIBCAMERA_INTERNAL_FILE_H__ */
Utilities to help constructing class interfaces.
OpenMode
Mode in which a file is opened.
Definition: file.h:27
The file is not open.
Definition: file.h:28
MapFlag
Flags for the File::map() function.
Definition: file.h:22
off_t seek(off_t pos)
Set the read or write position.
Definition: file.cpp:264
The file is open for writing.
Definition: file.h:30
The file is open for reading.
Definition: file.h:29
bool unmap(uint8_t *addr)
Unmap a region mapped with map()
Definition: file.cpp:421
Top-level libcamera namespace.
Definition: bound_method.h:15
File()
Construct a File without an associated name.
Definition: file.cpp:86
void close()
Close the file.
Definition: file.cpp:200
#define LIBCAMERA_DISABLE_COPY(klass)
Disable copy construction and assignment of the klass.
Interface for I/O operations on files.
Definition: file.h:19
const std::string & fileName() const
Retrieve the file name.
Definition: file.h:38
OpenMode openMode() const
Retrieve the file open mode.
Definition: file.h:44
No option (used as default value)
Definition: file.h:23
The file is open for reading and writing.
Definition: file.h:31
bool isOpen() const
Check if the file is open.
Definition: file.h:43
Span< uint8_t > map(off_t offset=0, ssize_t size=-1, MapFlag flags=MapNoOption)
Map a region of the file in the process memory.
Definition: file.cpp:374
void setFileName(const std::string &name)
Set the name of the file.
Definition: file.cpp:118
~File()
Destroy a File instance.
Definition: file.cpp:97
ssize_t read(const Span< uint8_t > &data)
Read data from the file.
Definition: file.cpp:291
ssize_t size() const
Retrieve the file size.
Definition: file.cpp:230
ssize_t write(const Span< const uint8_t > &data)
Write data to the file.
Definition: file.cpp:329
bool open(OpenMode mode)
Open the file in the given mode.
Definition: file.cpp:159
The memory region is mapped as private, changes are not reflected in the file constents.
Definition: file.h:24
int error() const
Retrieve the file error status.
Definition: file.h:47
off_t pos() const
Return current read or write position.
Definition: file.cpp:250
bool exists() const
Check if the file specified by fileName() exists.
Definition: file.cpp:141