libcamera  v0.0.0
Supporting cameras in Linux since 2019
utils.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3  * Copyright (C) 2018, Google Inc.
4  *
5  * utils.h - Miscellaneous utility functions
6  */
7 #ifndef __LIBCAMERA_INTERNAL_UTILS_H__
8 #define __LIBCAMERA_INTERNAL_UTILS_H__
9 
10 #include <algorithm>
11 #include <chrono>
12 #include <memory>
13 #include <ostream>
14 #include <sstream>
15 #include <string>
16 #include <string.h>
17 #include <sys/time.h>
18 
19 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
20 
21 #ifndef __DOXYGEN__
22 
23 /* uClibc and uClibc-ng don't provide O_TMPFILE */
24 #ifndef O_TMPFILE
25 #define O_TMPFILE (020000000 | O_DIRECTORY)
26 #endif
27 
28 #endif
29 
30 namespace libcamera {
31 
32 namespace utils {
33 
34 const char *basename(const char *path);
35 
36 char *secure_getenv(const char *name);
37 std::string dirname(const std::string &path);
38 
39 template<class InputIt1, class InputIt2>
40 unsigned int set_overlap(InputIt1 first1, InputIt1 last1,
41  InputIt2 first2, InputIt2 last2)
42 {
43  unsigned int count = 0;
44 
45  while (first1 != last1 && first2 != last2) {
46  if (*first1 < *first2) {
47  ++first1;
48  } else {
49  if (!(*first2 < *first1))
50  count++;
51  ++first2;
52  }
53  }
54 
55  return count;
56 }
57 
58 /* C++11 doesn't provide std::clamp */
59 template <typename T>
60 const T& clamp(const T& v, const T& lo, const T& hi)
61 {
62  return std::max(lo, std::min(v, hi));
63 }
64 
65 using clock = std::chrono::steady_clock;
66 using duration = std::chrono::steady_clock::duration;
67 using time_point = std::chrono::steady_clock::time_point;
68 
69 struct timespec duration_to_timespec(const duration &value);
70 std::string time_point_to_string(const time_point &time);
71 
72 #ifndef __DOXYGEN__
73 struct _hex {
74  uint64_t v;
75  unsigned int w;
76 };
77 
78 std::basic_ostream<char, std::char_traits<char>> &
79 operator<<(std::basic_ostream<char, std::char_traits<char>> &stream, const _hex &h);
80 #endif
81 
82 template<typename T>
83 _hex hex(T value, unsigned int width = 0);
84 
85 #ifndef __DOXYGEN__
86 template<>
87 inline _hex hex<int32_t>(int32_t value, unsigned int width)
88 {
89  return { static_cast<uint64_t>(value), width ? width : 8 };
90 }
91 
92 template<>
93 inline _hex hex<uint32_t>(uint32_t value, unsigned int width)
94 {
95  return { static_cast<uint64_t>(value), width ? width : 8 };
96 }
97 
98 template<>
99 inline _hex hex<int64_t>(int64_t value, unsigned int width)
100 {
101  return { static_cast<uint64_t>(value), width ? width : 16 };
102 }
103 
104 template<>
105 inline _hex hex<uint64_t>(uint64_t value, unsigned int width)
106 {
107  return { static_cast<uint64_t>(value), width ? width : 16 };
108 }
109 #endif
110 
111 size_t strlcpy(char *dst, const char *src, size_t size);
112 
113 #ifndef __DOXYGEN__
114 template<typename Container, typename UnaryOp>
115 std::string join(const Container &items, const std::string &sep, UnaryOp op)
116 {
117  std::ostringstream ss;
118  bool first = true;
119 
120  for (typename Container::const_iterator it = std::begin(items);
121  it != std::end(items); ++it) {
122  if (!first)
123  ss << sep;
124  else
125  first = false;
126 
127  ss << op(*it);
128  }
129 
130  return ss.str();
131 }
132 
133 template<typename Container>
134 std::string join(const Container &items, const std::string &sep)
135 {
136  std::ostringstream ss;
137  bool first = true;
138 
139  for (typename Container::const_iterator it = std::begin(items);
140  it != std::end(items); ++it) {
141  if (!first)
142  ss << sep;
143  else
144  first = false;
145 
146  ss << *it;
147  }
148 
149  return ss.str();
150 }
151 #else
152 template<typename Container, typename UnaryOp>
153 std::string join(const Container &items, const std::string &sep, UnaryOp op = nullptr);
154 #endif
155 
156 namespace details {
157 
158 class StringSplitter
159 {
160 public:
161  StringSplitter(const std::string &str, const std::string &delim);
162 
163  class iterator
164  {
165  public:
166  iterator(const StringSplitter *ss, std::string::size_type pos);
167 
168  iterator &operator++();
169  std::string operator*() const;
170  bool operator!=(const iterator &other) const;
171 
172  private:
173  const StringSplitter *ss_;
174  std::string::size_type pos_;
175  std::string::size_type next_;
176  };
177 
178  iterator begin() const;
179  iterator end() const;
180 
181 private:
182  std::string str_;
183  std::string delim_;
184 };
185 
186 } /* namespace details */
187 
188 details::StringSplitter split(const std::string &str, const std::string &delim);
189 
190 std::string libcameraBuildPath();
191 std::string libcameraSourcePath();
192 
193 } /* namespace utils */
194 
195 } /* namespace libcamera */
196 
197 #endif /* __LIBCAMERA_INTERNAL_UTILS_H__ */
const char * basename(const char *path)
Strip the directory prefix from the path.
Definition: utils.cpp:50
details::StringSplitter split(const std::string &str, const std::string &delim)
Split a string based on a delimiter.
Definition: utils.cpp:332
std::string time_point_to_string(const time_point &time)
Convert a time point to a string representation.
Definition: utils.cpp:184
const T & clamp(const T &v, const T &lo, const T &hi)
Definition: utils.h:60
std::string libcameraSourcePath()
Retrieve the path to the source directory.
Definition: utils.cpp:417
_hex hex(T value, unsigned int width=0)
Write an hexadecimal value to an output string.
size_t strlcpy(char *dst, const char *src, size_t size)
Copy a string with a size limit.
Definition: utils.cpp:249
char * secure_getenv(const char *name)
Get an environment variable.
Definition: utils.cpp:70
std::chrono::steady_clock clock
The libcamera clock (monotonic)
Definition: utils.h:65
std::chrono::steady_clock::time_point time_point
The libcamera time point related to libcamera::utils::clock.
Definition: utils.h:67
unsigned int set_overlap(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2)
Count the number of elements in the intersection of two ranges.
Definition: utils.h:40
std::string libcameraBuildPath()
Retrieve the path to the build directory.
Definition: utils.cpp:377
struct timespec duration_to_timespec(const duration &value)
Convert a duration to a timespec.
Definition: utils.cpp:170
std::string join(const Container &items, const std::string &sep, UnaryOp op=nullptr)
Join elements of a container in a string with a separator.
std::chrono::steady_clock::duration duration
The libcamera duration related to libcamera::utils::clock.
Definition: utils.h:66
std::string dirname(const std::string &path)
Identify the dirname portion of a path.
Definition: utils.cpp:91