Open3D (C++ API)  0.16.1
FileSystem.h
Go to the documentation of this file.
1// ----------------------------------------------------------------------------
2// - Open3D: www.open3d.org -
3// ----------------------------------------------------------------------------
4// The MIT License (MIT)
5//
6// Copyright (c) 2018-2021 www.open3d.org
7//
8// Permission is hereby granted, free of charge, to any person obtaining a copy
9// of this software and associated documentation files (the "Software"), to deal
10// in the Software without restriction, including without limitation the rights
11// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12// copies of the Software, and to permit persons to whom the Software is
13// furnished to do so, subject to the following conditions:
14//
15// The above copyright notice and this permission notice shall be included in
16// all copies or substantial portions of the Software.
17//
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24// IN THE SOFTWARE.
25// ----------------------------------------------------------------------------
26
27#pragma once
28
29#include <functional>
30#include <string>
31#include <vector>
32
33namespace open3d {
34namespace utility {
35namespace filesystem {
36
53std::string GetHomeDirectory();
54
55std::string GetFileExtensionInLowerCase(const std::string &filename);
56
57std::string GetFileNameWithoutExtension(const std::string &filename);
58
59std::string GetFileNameWithoutDirectory(const std::string &filename);
60
61std::string GetFileParentDirectory(const std::string &filename);
62
63std::string GetRegularizedDirectoryName(const std::string &directory);
64
65std::string GetWorkingDirectory();
66
67std::vector<std::string> GetPathComponents(const std::string &path);
68
69std::string GetTempDirectoryPath();
70
71bool ChangeWorkingDirectory(const std::string &directory);
72
73bool DirectoryExists(const std::string &directory);
74
75// Return true if the directory is present and empty. Return false if the
76// directory is present but not empty. Throw an exception if the directory is
77// not present.
78bool DirectoryIsEmpty(const std::string &directory);
79
80bool MakeDirectory(const std::string &directory);
81
82bool MakeDirectoryHierarchy(const std::string &directory);
83
84bool DeleteDirectory(const std::string &directory);
85
86bool FileExists(const std::string &filename);
87
88bool Copy(const std::string &src_path, const std::string &dst_path);
89
90bool RemoveFile(const std::string &filename);
91
92bool ListDirectory(const std::string &directory,
93 std::vector<std::string> &subdirs,
94 std::vector<std::string> &filenames);
95
96bool ListFilesInDirectory(const std::string &directory,
97 std::vector<std::string> &filenames);
98
99bool ListFilesInDirectoryWithExtension(const std::string &directory,
100 const std::string &extname,
101 std::vector<std::string> &filenames);
102
103std::vector<std::string> FindFilesRecursively(
104 const std::string &directory,
105 std::function<bool(const std::string &)> is_match);
106
107// wrapper for fopen that enables unicode paths on Windows
108FILE *FOpen(const std::string &filename, const std::string &mode);
109std::string GetIOErrorString(const int errnoVal);
110bool FReadToBuffer(const std::string &path,
111 std::vector<char> &bytes,
112 std::string *errorStr);
113
114std::string JoinPath(const std::vector<std::string> &path_components);
115
116std::string JoinPath(const std::string &path_component1,
117 const std::string &path_component2);
118
119std::string AddIfExist(const std::string &path,
120 const std::vector<std::string> &folder_names);
121
132class CFile {
133public:
135 ~CFile();
136
138 bool Open(const std::string &filename, const std::string &mode);
139
141 std::string GetError();
142
144 void Close();
145
147 int64_t CurPos();
148
150 int64_t GetFileSize();
151
153 int64_t GetNumLines();
154
159 const char *ReadLine();
160
165 template <class T>
166 size_t ReadData(T *data, size_t num_elems) {
167 return ReadData(data, sizeof(T), num_elems);
168 }
169
174 size_t ReadData(void *data, size_t elem_size, size_t num_elems);
175
177 FILE *GetFILE() { return file_; }
178
179private:
180 FILE *file_ = nullptr;
181 int error_code_ = 0;
182 std::vector<char> line_buffer_;
183};
184
185} // namespace filesystem
186} // namespace utility
187} // namespace open3d
void * bytes
Definition: TriangleMeshBuffers.cpp:177
Definition: FileSystem.h:132
~CFile()
The destructor closes the file automatically.
Definition: FileSystem.cpp:531
int64_t CurPos()
Returns current position in the file (ftell).
Definition: FileSystem.cpp:554
bool Open(const std::string &filename, const std::string &mode)
Open a file.
Definition: FileSystem.cpp:533
size_t ReadData(T *data, size_t num_elems)
Definition: FileSystem.h:166
FILE * GetFILE()
Returns the underlying C FILE pointer.
Definition: FileSystem.h:177
void Close()
Close the file.
Definition: FileSystem.cpp:544
std::string GetError()
Returns the last encountered error for this file.
Definition: FileSystem.cpp:542
int64_t GetNumLines()
Returns the number of lines in the file.
Definition: FileSystem.cpp:587
int64_t GetFileSize()
Returns the file size in bytes.
Definition: FileSystem.cpp:566
const char * ReadLine()
Definition: FileSystem.cpp:614
const char const char value recording_handle imu_sample recording_handle uint8_t data
Definition: K4aPlugin.cpp:288
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:697
bool ChangeWorkingDirectory(const std::string &directory)
Definition: FileSystem.cpp:240
std::string GetFileNameWithoutDirectory(const std::string &filename)
Definition: FileSystem.cpp:126
std::string GetTempDirectoryPath()
Definition: FileSystem.cpp:236
std::string GetFileExtensionInLowerCase(const std::string &filename)
Definition: FileSystem.cpp:106
bool RemoveFile(const std::string &filename)
Definition: FileSystem.cpp:308
bool MakeDirectory(const std::string &directory)
Definition: FileSystem.cpp:255
bool ListDirectory(const std::string &directory, std::vector< std::string > &subdirs, std::vector< std::string > &filenames)
Definition: FileSystem.cpp:312
std::string GetHomeDirectory()
Get the HOME directory for the user.
Definition: FileSystem.cpp:74
std::string JoinPath(const std::string &path_component1, const std::string &path_component2)
Definition: FileSystem.cpp:506
std::string GetFileNameWithoutExtension(const std::string &filename)
Definition: FileSystem.cpp:120
std::vector< std::string > GetPathComponents(const std::string &path)
Definition: FileSystem.cpp:161
FILE * FOpen(const std::string &filename, const std::string &mode)
Definition: FileSystem.cpp:389
bool DeleteDirectory(const std::string &directory)
Definition: FileSystem.cpp:278
bool ListFilesInDirectoryWithExtension(const std::string &directory, const std::string &extname, std::vector< std::string > &filenames)
Definition: FileSystem.cpp:347
std::string AddIfExist(const std::string &path, const std::vector< std::string > &folder_names)
Definition: FileSystem.cpp:520
bool DirectoryExists(const std::string &directory)
Definition: FileSystem.cpp:244
bool FileExists(const std::string &filename)
Definition: FileSystem.cpp:288
std::string GetFileParentDirectory(const std::string &filename)
Definition: FileSystem.cpp:135
bool DirectoryIsEmpty(const std::string &directory)
Definition: FileSystem.cpp:248
bool Copy(const std::string &src_path, const std::string &dst_path)
Definition: FileSystem.cpp:295
bool MakeDirectoryHierarchy(const std::string &directory)
Definition: FileSystem.cpp:263
std::string GetWorkingDirectory()
Definition: FileSystem.cpp:154
std::string GetRegularizedDirectoryName(const std::string &directory)
Definition: FileSystem.cpp:144
bool FReadToBuffer(const std::string &path, std::vector< char > &bytes, std::string *errorStr)
Definition: FileSystem.cpp:458
std::vector< std::string > FindFilesRecursively(const std::string &directory, std::function< bool(const std::string &)> is_match)
Definition: FileSystem.cpp:366
bool ListFilesInDirectory(const std::string &directory, std::vector< std::string > &filenames)
Definition: FileSystem.cpp:341
std::string GetIOErrorString(const int errnoVal)
Definition: FileSystem.cpp:407
Definition: PinholeCameraIntrinsic.cpp:35