Files.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <spdlog/spdlog.h>
4 
5 #include <fstream>
6 #include <tl/optional.hpp>
7 
8 #include "Platform.hpp"
9 
10 namespace dai {
11 
12 tl::optional<std::string> saveFileToTemporaryDirectory(std::vector<uint8_t> data, std::string filename, std::string fpath = "") {
13  if(fpath.empty()) {
14  fpath = platform::getTempPath();
15  }
16  std::string path = std::string(fpath);
17  if(path.back() != '/' && path.back() != '\\') {
18  path += '/';
19  }
20  path += filename;
21 
22  std::ofstream file(path, std::ios::binary);
23  if(!file.is_open()) {
24  spdlog::error("Couldn't open file {} for writing", path);
25  return tl::nullopt;
26  }
27 
28  file.write(reinterpret_cast<char*>(data.data()), data.size());
29  file.close();
30  if(!file.good()) {
31  spdlog::error("Couldn't write to file {}", path);
32  return tl::nullopt;
33  }
34  spdlog::debug("Saved file {} to {}", filename, path);
35  return std::string(path);
36 }
37 
38 } // namespace dai
optional.hpp
tl::nullopt
static constexpr nullopt_t nullopt
Represents an empty optional.
Definition: 3rdparty/tl/optional.hpp:663
dai::logger::debug
void debug(const FormatString &fmt, Args &&...args)
Definition: Logging.hpp:72
DAI_SPAN_NAMESPACE_NAME::detail::data
constexpr auto data(C &c) -> decltype(c.data())
Definition: span.hpp:177
dai::logger::error
void error(const FormatString &fmt, Args &&...args)
Definition: Logging.hpp:90
dai::platform::getTempPath
std::string getTempPath()
Definition: Platform.cpp:59
Platform.hpp
tl::optional< std::string >
dai
Definition: CameraExposureOffset.hpp:6
dai::saveFileToTemporaryDirectory
tl::optional< std::string > saveFileToTemporaryDirectory(std::vector< uint8_t > data, std::string filename, std::string fpath="")
Definition: Files.hpp:12


depthai
Author(s): Martin Peterlin
autogenerated on Sat Mar 22 2025 02:58:19