Open3D (C++ API)  0.17.0
FileSystem.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <functional>
11 #include <string>
12 #include <vector>
13 
14 namespace open3d {
15 namespace utility {
16 namespace filesystem {
17 
34 std::string GetHomeDirectory();
35 
36 std::string GetFileExtensionInLowerCase(const std::string &filename);
37 
38 std::string GetFileNameWithoutExtension(const std::string &filename);
39 
40 std::string GetFileNameWithoutDirectory(const std::string &filename);
41 
42 std::string GetFileParentDirectory(const std::string &filename);
43 
44 std::string GetRegularizedDirectoryName(const std::string &directory);
45 
46 std::string GetWorkingDirectory();
47 
48 std::vector<std::string> GetPathComponents(const std::string &path);
49 
50 std::string GetTempDirectoryPath();
51 
52 bool ChangeWorkingDirectory(const std::string &directory);
53 
54 bool DirectoryExists(const std::string &directory);
55 
56 // Return true if the directory is present and empty. Return false if the
57 // directory is present but not empty. Throw an exception if the directory is
58 // not present.
59 bool DirectoryIsEmpty(const std::string &directory);
60 
61 bool MakeDirectory(const std::string &directory);
62 
63 bool MakeDirectoryHierarchy(const std::string &directory);
64 
65 bool DeleteDirectory(const std::string &directory);
66 
67 bool FileExists(const std::string &filename);
68 
69 bool Copy(const std::string &src_path, const std::string &dst_path);
70 
71 bool RemoveFile(const std::string &filename);
72 
73 bool ListDirectory(const std::string &directory,
74  std::vector<std::string> &subdirs,
75  std::vector<std::string> &filenames);
76 
77 bool ListFilesInDirectory(const std::string &directory,
78  std::vector<std::string> &filenames);
79 
80 bool ListFilesInDirectoryWithExtension(const std::string &directory,
81  const std::string &extname,
82  std::vector<std::string> &filenames);
83 
84 std::vector<std::string> FindFilesRecursively(
85  const std::string &directory,
86  std::function<bool(const std::string &)> is_match);
87 
88 // wrapper for fopen that enables unicode paths on Windows
89 FILE *FOpen(const std::string &filename, const std::string &mode);
90 std::string GetIOErrorString(const int errnoVal);
91 bool FReadToBuffer(const std::string &path,
92  std::vector<char> &bytes,
93  std::string *errorStr);
94 
95 std::string JoinPath(const std::vector<std::string> &path_components);
96 
97 std::string JoinPath(const std::string &path_component1,
98  const std::string &path_component2);
99 
100 std::string AddIfExist(const std::string &path,
101  const std::vector<std::string> &folder_names);
102 
113 class CFile {
114 public:
116  ~CFile();
117 
119  bool Open(const std::string &filename, const std::string &mode);
120 
122  std::string GetError();
123 
125  void Close();
126 
128  int64_t CurPos();
129 
131  int64_t GetFileSize();
132 
134  int64_t GetNumLines();
135 
140  const char *ReadLine();
141 
146  template <class T>
147  size_t ReadData(T *data, size_t num_elems) {
148  return ReadData(data, sizeof(T), num_elems);
149  }
150 
155  size_t ReadData(void *data, size_t elem_size, size_t num_elems);
156 
158  FILE *GetFILE() { return file_; }
159 
160 private:
161  FILE *file_ = nullptr;
162  int error_code_ = 0;
163  std::vector<char> line_buffer_;
164 };
165 
166 } // namespace filesystem
167 } // namespace utility
168 } // namespace open3d
void * bytes
Definition: TriangleMeshBuffers.cpp:158
Definition: FileSystem.h:113
~CFile()
The destructor closes the file automatically.
Definition: FileSystem.cpp:512
int64_t CurPos()
Returns current position in the file (ftell).
Definition: FileSystem.cpp:535
bool Open(const std::string &filename, const std::string &mode)
Open a file.
Definition: FileSystem.cpp:514
size_t ReadData(T *data, size_t num_elems)
Definition: FileSystem.h:147
void Close()
Close the file.
Definition: FileSystem.cpp:525
std::string GetError()
Returns the last encountered error for this file.
Definition: FileSystem.cpp:523
int64_t GetNumLines()
Returns the number of lines in the file.
Definition: FileSystem.cpp:568
FILE * GetFILE()
Returns the underlying C FILE pointer.
Definition: FileSystem.h:158
int64_t GetFileSize()
Returns the file size in bytes.
Definition: FileSystem.cpp:547
const char * ReadLine()
Definition: FileSystem.cpp:595
const char const char value recording_handle imu_sample recording_handle uint8_t data
Definition: K4aPlugin.cpp:269
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c k4a_image_t image_handle uint8_t image_handle image_handle image_handle image_handle image_handle timestamp_usec white_balance image_handle k4a_device_configuration_t config device_handle char size_t serial_number_size bool int32_t int32_t int32_t int32_t k4a_color_control_mode_t default_mode mode
Definition: K4aPlugin.cpp:678
bool ChangeWorkingDirectory(const std::string &directory)
Definition: FileSystem.cpp:221
std::string GetFileNameWithoutDirectory(const std::string &filename)
Definition: FileSystem.cpp:107
std::string GetTempDirectoryPath()
Definition: FileSystem.cpp:217
std::string GetFileExtensionInLowerCase(const std::string &filename)
Definition: FileSystem.cpp:87
bool RemoveFile(const std::string &filename)
Definition: FileSystem.cpp:289
bool MakeDirectory(const std::string &directory)
Definition: FileSystem.cpp:236
bool ListDirectory(const std::string &directory, std::vector< std::string > &subdirs, std::vector< std::string > &filenames)
Definition: FileSystem.cpp:293
std::string GetHomeDirectory()
Get the HOME directory for the user.
Definition: FileSystem.cpp:55
std::string JoinPath(const std::string &path_component1, const std::string &path_component2)
Definition: FileSystem.cpp:487
std::string GetFileNameWithoutExtension(const std::string &filename)
Definition: FileSystem.cpp:101
std::vector< std::string > GetPathComponents(const std::string &path)
Definition: FileSystem.cpp:142
FILE * FOpen(const std::string &filename, const std::string &mode)
Definition: FileSystem.cpp:370
bool DeleteDirectory(const std::string &directory)
Definition: FileSystem.cpp:259
bool ListFilesInDirectoryWithExtension(const std::string &directory, const std::string &extname, std::vector< std::string > &filenames)
Definition: FileSystem.cpp:328
std::string AddIfExist(const std::string &path, const std::vector< std::string > &folder_names)
Definition: FileSystem.cpp:501
bool DirectoryExists(const std::string &directory)
Definition: FileSystem.cpp:225
bool FileExists(const std::string &filename)
Definition: FileSystem.cpp:269
std::string GetFileParentDirectory(const std::string &filename)
Definition: FileSystem.cpp:116
bool DirectoryIsEmpty(const std::string &directory)
Definition: FileSystem.cpp:229
bool Copy(const std::string &src_path, const std::string &dst_path)
Definition: FileSystem.cpp:276
bool MakeDirectoryHierarchy(const std::string &directory)
Definition: FileSystem.cpp:244
std::string GetWorkingDirectory()
Definition: FileSystem.cpp:135
std::string GetRegularizedDirectoryName(const std::string &directory)
Definition: FileSystem.cpp:125
bool FReadToBuffer(const std::string &path, std::vector< char > &bytes, std::string *errorStr)
Definition: FileSystem.cpp:439
std::vector< std::string > FindFilesRecursively(const std::string &directory, std::function< bool(const std::string &)> is_match)
Definition: FileSystem.cpp:347
bool ListFilesInDirectory(const std::string &directory, std::vector< std::string > &filenames)
Definition: FileSystem.cpp:322
std::string GetIOErrorString(const int errnoVal)
Definition: FileSystem.cpp:388
Definition: PinholeCameraIntrinsic.cpp:16