Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.16.0
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 
33 namespace open3d {
34 namespace utility {
35 namespace filesystem {
36 
53 std::string GetHomeDirectory();
54 
55 std::string GetFileExtensionInLowerCase(const std::string &filename);
56 
57 std::string GetFileNameWithoutExtension(const std::string &filename);
58 
59 std::string GetFileNameWithoutDirectory(const std::string &filename);
60 
61 std::string GetFileParentDirectory(const std::string &filename);
62 
63 std::string GetRegularizedDirectoryName(const std::string &directory);
64 
65 std::string GetWorkingDirectory();
66 
67 std::vector<std::string> GetPathComponents(const std::string &path);
68 
69 std::string GetTempDirectoryPath();
70 
71 bool ChangeWorkingDirectory(const std::string &directory);
72 
73 bool 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.
78 bool DirectoryIsEmpty(const std::string &directory);
79 
80 bool MakeDirectory(const std::string &directory);
81 
82 bool MakeDirectoryHierarchy(const std::string &directory);
83 
84 bool DeleteDirectory(const std::string &directory);
85 
86 bool FileExists(const std::string &filename);
87 
88 bool Copy(const std::string &src_path, const std::string &dst_path);
89 
90 bool RemoveFile(const std::string &filename);
91 
92 bool ListDirectory(const std::string &directory,
93  std::vector<std::string> &subdirs,
94  std::vector<std::string> &filenames);
95 
96 bool ListFilesInDirectory(const std::string &directory,
97  std::vector<std::string> &filenames);
98 
99 bool ListFilesInDirectoryWithExtension(const std::string &directory,
100  const std::string &extname,
101  std::vector<std::string> &filenames);
102 
103 std::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
108 FILE *FOpen(const std::string &filename, const std::string &mode);
109 std::string GetIOErrorString(const int errnoVal);
110 bool FReadToBuffer(const std::string &path,
111  std::vector<char> &bytes,
112  std::string *errorStr);
113 
114 std::string JoinPath(const std::vector<std::string> &path_components);
115 
116 std::string JoinPath(const std::string &path_component1,
117  const std::string &path_component2);
118 
119 std::string AddIfExist(const std::string &path,
120  const std::vector<std::string> &folder_names);
121 
132 class CFile {
133 public:
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 
179 private:
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
bool DeleteDirectory(const std::string &directory)
Definition: FileSystem.cpp:278
std::vector< std::string > FindFilesRecursively(const std::string &directory, std::function< bool(const std::string &)> is_match)
Definition: FileSystem.cpp:366
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:690
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 GetIOErrorString(const int errnoVal)
Definition: FileSystem.cpp:407
std::string GetFileExtensionInLowerCase(const std::string &filename)
Definition: FileSystem.cpp:106
bool Copy(const std::string &src_path, const std::string &dst_path)
Definition: FileSystem.cpp:295
std::string GetFileNameWithoutDirectory(const std::string &filename)
Definition: FileSystem.cpp:126
bool FReadToBuffer(const std::string &path, std::vector< char > &bytes, std::string *errorStr)
Definition: FileSystem.cpp:458
bool Open(const std::string &filename, const std::string &mode)
Open a file.
Definition: FileSystem.cpp:533
~CFile()
The destructor closes the file automatically.
Definition: FileSystem.cpp:531
const char * ReadLine()
Definition: FileSystem.cpp:614
size_t ReadData(T *data, size_t num_elems)
Definition: FileSystem.h:166
bool ListFilesInDirectoryWithExtension(const std::string &directory, const std::string &extname, std::vector< std::string > &filenames)
Definition: FileSystem.cpp:347
bool DirectoryExists(const std::string &directory)
Definition: FileSystem.cpp:244
bool ChangeWorkingDirectory(const std::string &directory)
Definition: FileSystem.cpp:240
bool ListFilesInDirectory(const std::string &directory, std::vector< std::string > &filenames)
Definition: FileSystem.cpp:341
void Close()
Close the file.
Definition: FileSystem.cpp:544
std::string GetTempDirectoryPath()
Definition: FileSystem.cpp:236
Definition: FileSystem.h:132
bool DirectoryIsEmpty(const std::string &directory)
Definition: FileSystem.cpp:248
void * bytes
Definition: TriangleMeshBuffers.cpp:177
std::string GetError()
Returns the last encountered error for this file.
Definition: FileSystem.cpp:542
FILE * GetFILE()
Returns the underlying C FILE pointer.
Definition: FileSystem.h:177
std::string GetFileParentDirectory(const std::string &filename)
Definition: FileSystem.cpp:135
int64_t CurPos()
Returns current position in the file (ftell).
Definition: FileSystem.cpp:554
std::string GetWorkingDirectory()
Definition: FileSystem.cpp:154
bool MakeDirectoryHierarchy(const std::string &directory)
Definition: FileSystem.cpp:263
Definition: PinholeCameraIntrinsic.cpp:35
bool RemoveFile(const std::string &filename)
Definition: FileSystem.cpp:308
std::string AddIfExist(const std::string &path, const std::vector< std::string > &folder_names)
Definition: FileSystem.cpp:520
std::string GetFileNameWithoutExtension(const std::string &filename)
Definition: FileSystem.cpp:120
std::vector< std::string > GetPathComponents(const std::string &path)
Definition: FileSystem.cpp:161
int64_t GetNumLines()
Returns the number of lines in the file.
Definition: FileSystem.cpp:587
std::string GetRegularizedDirectoryName(const std::string &directory)
Definition: FileSystem.cpp:144
bool FileExists(const std::string &filename)
Definition: FileSystem.cpp:288
int64_t GetFileSize()
Returns the file size in bytes.
Definition: FileSystem.cpp:566
bool ListDirectory(const std::string &directory, std::vector< std::string > &subdirs, std::vector< std::string > &filenames)
Definition: FileSystem.cpp:312
FILE * FOpen(const std::string &filename, const std::string &mode)
Definition: FileSystem.cpp:389
bool MakeDirectory(const std::string &directory)
Definition: FileSystem.cpp:255
const char const char value recording_handle imu_sample recording_handle uint8_t data
Definition: K4aPlugin.cpp:284