Open3D (C++ API)  0.15.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
69bool ChangeWorkingDirectory(const std::string &directory);
70
71bool DirectoryExists(const std::string &directory);
72
73bool MakeDirectory(const std::string &directory);
74
75bool MakeDirectoryHierarchy(const std::string &directory);
76
77bool DeleteDirectory(const std::string &directory);
78
79bool FileExists(const std::string &filename);
80
81bool Copy(const std::string &src_path, const std::string &dst_path);
82
83bool RemoveFile(const std::string &filename);
84
85bool ListDirectory(const std::string &directory,
86 std::vector<std::string> &subdirs,
87 std::vector<std::string> &filenames);
88
89bool ListFilesInDirectory(const std::string &directory,
90 std::vector<std::string> &filenames);
91
92bool ListFilesInDirectoryWithExtension(const std::string &directory,
93 const std::string &extname,
94 std::vector<std::string> &filenames);
95
96std::vector<std::string> FindFilesRecursively(
97 const std::string &directory,
98 std::function<bool(const std::string &)> is_match);
99
100// wrapper for fopen that enables unicode paths on Windows
101FILE *FOpen(const std::string &filename, const std::string &mode);
102std::string GetIOErrorString(const int errnoVal);
103bool FReadToBuffer(const std::string &path,
104 std::vector<char> &bytes,
105 std::string *errorStr);
106
117class CFile {
118public:
120 ~CFile();
121
123 bool Open(const std::string &filename, const std::string &mode);
124
126 std::string GetError();
127
129 void Close();
130
132 int64_t CurPos();
133
135 int64_t GetFileSize();
136
138 int64_t GetNumLines();
139
144 const char *ReadLine();
145
150 template <class T>
151 size_t ReadData(T *data, size_t num_elems) {
152 return ReadData(data, sizeof(T), num_elems);
153 }
154
159 size_t ReadData(void *data, size_t elem_size, size_t num_elems);
160
162 FILE *GetFILE() { return file_; }
163
164private:
165 FILE *file_ = nullptr;
166 int error_code_ = 0;
167 std::vector<char> line_buffer_;
168};
169
170} // namespace filesystem
171} // namespace utility
172} // namespace open3d
void * bytes
Definition: TriangleMeshBuffers.cpp:177
Definition: FileSystem.h:117
~CFile()
The destructor closes the file automatically.
Definition: FileSystem.cpp:495
int64_t CurPos()
Returns current position in the file (ftell).
Definition: FileSystem.cpp:518
bool Open(const std::string &filename, const std::string &mode)
Open a file.
Definition: FileSystem.cpp:497
size_t ReadData(T *data, size_t num_elems)
Definition: FileSystem.h:151
FILE * GetFILE()
Returns the underlying C FILE pointer.
Definition: FileSystem.h:162
void Close()
Close the file.
Definition: FileSystem.cpp:508
std::string GetError()
Returns the last encountered error for this file.
Definition: FileSystem.cpp:506
int64_t GetNumLines()
Returns the number of lines in the file.
Definition: FileSystem.cpp:551
int64_t GetFileSize()
Returns the file size in bytes.
Definition: FileSystem.cpp:530
const char * ReadLine()
Definition: FileSystem.cpp:578
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:236
std::string GetFileNameWithoutDirectory(const std::string &filename)
Definition: FileSystem.cpp:126
std::string GetFileExtensionInLowerCase(const std::string &filename)
Definition: FileSystem.cpp:106
bool RemoveFile(const std::string &filename)
Definition: FileSystem.cpp:297
bool MakeDirectory(const std::string &directory)
Definition: FileSystem.cpp:244
bool ListDirectory(const std::string &directory, std::vector< std::string > &subdirs, std::vector< std::string > &filenames)
Definition: FileSystem.cpp:301
std::string GetHomeDirectory()
Get the HOME directory for the user.
Definition: FileSystem.cpp:74
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:378
bool DeleteDirectory(const std::string &directory)
Definition: FileSystem.cpp:267
bool ListFilesInDirectoryWithExtension(const std::string &directory, const std::string &extname, std::vector< std::string > &filenames)
Definition: FileSystem.cpp:336
bool DirectoryExists(const std::string &directory)
Definition: FileSystem.cpp:240
bool FileExists(const std::string &filename)
Definition: FileSystem.cpp:277
std::string GetFileParentDirectory(const std::string &filename)
Definition: FileSystem.cpp:135
bool Copy(const std::string &src_path, const std::string &dst_path)
Definition: FileSystem.cpp:284
bool MakeDirectoryHierarchy(const std::string &directory)
Definition: FileSystem.cpp:252
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:447
std::vector< std::string > FindFilesRecursively(const std::string &directory, std::function< bool(const std::string &)> is_match)
Definition: FileSystem.cpp:355
bool ListFilesInDirectory(const std::string &directory, std::vector< std::string > &filenames)
Definition: FileSystem.cpp:330
std::string GetIOErrorString(const int errnoVal)
Definition: FileSystem.cpp:396
Definition: PinholeCameraIntrinsic.cpp:35