libcamera  v0.0.0+3-3b6e2d50-dirty (2022-01-27T16:18:04+08: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_BASE_FILE_H__
8 #define __LIBCAMERA_BASE_FILE_H__
9 
10 #include <sys/types.h>
11 
12 #include <map>
13 #include <string>
14 
15 #include <libcamera/base/private.h>
16 
17 #include <libcamera/base/class.h>
18 #include <libcamera/base/flags.h>
19 #include <libcamera/base/span.h>
20 
21 namespace libcamera {
22 
23 class File
24 {
25 public:
26  enum class MapFlag {
27  NoOption = 0,
28  Private = (1 << 0),
29  };
30 
32 
33  enum class OpenModeFlag {
34  NotOpen = 0,
35  ReadOnly = (1 << 0),
36  WriteOnly = (1 << 1),
37  ReadWrite = ReadOnly | WriteOnly,
38  };
39 
41 
42  File(const std::string &name);
43  File();
44  ~File();
45 
46  const std::string &fileName() const { return name_; }
47  void setFileName(const std::string &name);
48  bool exists() const;
49 
50  bool open(OpenMode mode);
51  bool isOpen() const { return fd_ != -1; }
52  OpenMode openMode() const { return mode_; }
53  void close();
54 
55  int error() const { return error_; }
56  ssize_t size() const;
57 
58  off_t pos() const;
59  off_t seek(off_t pos);
60 
61  ssize_t read(const Span<uint8_t> &data);
62  ssize_t write(const Span<const uint8_t> &data);
63 
64  Span<uint8_t> map(off_t offset = 0, ssize_t size = -1,
65  MapFlags flags = MapFlag::NoOption);
66  bool unmap(uint8_t *addr);
67 
68  static bool exists(const std::string &name);
69 
70 private:
72 
73  void unmapAll();
74 
75  std::string name_;
76  int fd_;
77  OpenMode mode_;
78 
79  int error_;
80  std::map<void *, size_t> maps_;
81 };
82 
85 
86 } /* namespace libcamera */
87 
88 #endif /* __LIBCAMERA_BASE_FILE_H__ */
Utilities to help constructing class interfaces.
#define LIBCAMERA_DISABLE_COPY(klass)
Disable copy construction and assignment of the klass.
Interface for I/O operations on files.
Definition: file.h:24
MapFlag
Flags for the File::map() function.
Definition: file.h:26
int error() const
Retrieve the file error status.
Definition: file.h:55
const std::string & fileName() const
Retrieve the file name.
Definition: file.h:46
bool exists() const
Check if the file specified by fileName() exists.
Definition: file.cpp:151
bool open(OpenMode mode)
Open the file in the given mode.
Definition: file.cpp:169
ssize_t write(const Span< const uint8_t > &data)
Write data to the file.
Definition: file.cpp:339
void close()
Close the file.
Definition: file.cpp:210
OpenModeFlag
Mode in which a file is opened.
Definition: file.h:33
bool isOpen() const
Check if the file is open.
Definition: file.h:51
~File()
Destroy a File instance.
Definition: file.cpp:107
File()
Construct a File without an associated name.
Definition: file.cpp:96
Flags< OpenModeFlag > OpenMode
A bitwise combination of File::OpenModeFlag values.
Definition: file.h:40
OpenMode openMode() const
Retrieve the file open mode.
Definition: file.h:52
Flags< MapFlag > MapFlags
A bitwise combination of File::MapFlag values.
Definition: file.h:31
ssize_t read(const Span< uint8_t > &data)
Read data from the file.
Definition: file.cpp:301
off_t seek(off_t pos)
Set the read or write position.
Definition: file.cpp:274
Span< uint8_t > map(off_t offset=0, ssize_t size=-1, MapFlags flags=MapFlag::NoOption)
Map a region of the file in the process memory.
Definition: file.cpp:384
bool unmap(uint8_t *addr)
Unmap a region mapped with map()
Definition: file.cpp:431
ssize_t size() const
Retrieve the file size.
Definition: file.cpp:240
off_t pos() const
Return current read or write position.
Definition: file.cpp:260
void setFileName(const std::string &name)
Set the name of the file.
Definition: file.cpp:128
Type-safe container for enum-based bitfields.
Definition: flags.h:16
Enum-based bit fields.
#define LIBCAMERA_FLAGS_ENABLE_OPERATORS(_enum)
Enable bitwise operations on the enum enumeration.
Top-level libcamera namespace.
Definition: bound_method.h:15