7 #ifndef __LIBCAMERA_INTERNAL_UTILS_H__
8 #define __LIBCAMERA_INTERNAL_UTILS_H__
19 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
25 #define O_TMPFILE (020000000 | O_DIRECTORY)
34 const char *
basename(
const char *path);
37 std::string
dirname(
const std::string &path);
39 template<
class InputIt1,
class InputIt2>
41 InputIt2 first2, InputIt2 last2)
43 unsigned int count = 0;
45 while (first1 != last1 && first2 != last2) {
46 if (*first1 < *first2) {
49 if (!(*first2 < *first1))
60 const T&
clamp(
const T& v,
const T& lo,
const T& hi)
62 return std::max(lo, std::min(v, hi));
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;
78 std::basic_ostream<char, std::char_traits<char>> &
79 operator<<(std::basic_ostream<
char, std::char_traits<char>> &stream,
const _hex &h);
83 _hex
hex(T value,
unsigned int width = 0);
87 inline _hex hex<int32_t>(int32_t value,
unsigned int width)
89 return {
static_cast<uint64_t
>(value), width ? width : 8 };
93 inline _hex hex<uint32_t>(uint32_t value,
unsigned int width)
95 return {
static_cast<uint64_t
>(value), width ? width : 8 };
99 inline _hex hex<int64_t>(int64_t value,
unsigned int width)
101 return {
static_cast<uint64_t
>(value), width ? width : 16 };
105 inline _hex hex<uint64_t>(uint64_t value,
unsigned int width)
107 return {
static_cast<uint64_t
>(value), width ? width : 16 };
111 size_t strlcpy(
char *dst,
const char *src,
size_t size);
114 template<
typename Container,
typename UnaryOp>
115 std::string
join(
const Container &items,
const std::string &sep, UnaryOp op)
117 std::ostringstream ss;
120 for (
typename Container::const_iterator it = std::begin(items);
121 it != std::end(items); ++it) {
133 template<
typename Container>
134 std::string
join(
const Container &items,
const std::string &sep)
136 std::ostringstream ss;
139 for (
typename Container::const_iterator it = std::begin(items);
140 it != std::end(items); ++it) {
152 template<
typename Container,
typename UnaryOp>
153 std::string
join(
const Container &items,
const std::string &sep, UnaryOp op =
nullptr);
161 StringSplitter(
const std::string &str,
const std::string &delim);
166 iterator(
const StringSplitter *ss, std::string::size_type pos);
168 iterator &operator++();
169 std::string operator*()
const;
170 bool operator!=(
const iterator &other)
const;
173 const StringSplitter *ss_;
174 std::string::size_type pos_;
175 std::string::size_type next_;
178 iterator begin()
const;
179 iterator end()
const;
188 details::StringSplitter
split(
const std::string &str,
const std::string &delim);
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