Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.15.1
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 bool ChangeWorkingDirectory(const std::string &directory);
70 
71 bool DirectoryExists(const std::string &directory);
72 
73 bool MakeDirectory(const std::string &directory);
74 
75 bool MakeDirectoryHierarchy(const std::string &directory);
76 
77 bool DeleteDirectory(const std::string &directory);
78 
79 bool FileExists(const std::string &filename);
80 
81 bool Copy(const std::string &src_path, const std::string &dst_path);
82 
83 bool RemoveFile(const std::string &filename);
84 
85 bool ListDirectory(const std::string &directory,
86  std::vector<std::string> &subdirs,
87  std::vector<std::string> &filenames);
88 
89 bool ListFilesInDirectory(const std::string &directory,
90  std::vector<std::string> &filenames);
91 
92 bool ListFilesInDirectoryWithExtension(const std::string &directory,
93  const std::string &extname,
94  std::vector<std::string> &filenames);
95 
96 std::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
101 FILE *FOpen(const std::string &filename, const std::string &mode);
102 std::string GetIOErrorString(const int errnoVal);
103 bool FReadToBuffer(const std::string &path,
104  std::vector<char> &bytes,
105  std::string *errorStr);
106 
117 class CFile {
118 public:
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 
164 private:
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
bool DeleteDirectory(const std::string &directory)
Definition: FileSystem.cpp:267
std::vector< std::string > FindFilesRecursively(const std::string &directory, std::function< bool(const std::string &)> is_match)
Definition: FileSystem.cpp:355
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 GetIOErrorString(const int errnoVal)
Definition: FileSystem.cpp:396
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:284
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:447
bool Open(const std::string &filename, const std::string &mode)
Open a file.
Definition: FileSystem.cpp:497
~CFile()
The destructor closes the file automatically.
Definition: FileSystem.cpp:495
const char * ReadLine()
Definition: FileSystem.cpp:578
size_t ReadData(T *data, size_t num_elems)
Definition: FileSystem.h:151
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 ChangeWorkingDirectory(const std::string &directory)
Definition: FileSystem.cpp:236
bool ListFilesInDirectory(const std::string &directory, std::vector< std::string > &filenames)
Definition: FileSystem.cpp:330
void Close()
Close the file.
Definition: FileSystem.cpp:508
Definition: FileSystem.h:117
void * bytes
Definition: TriangleMeshBuffers.cpp:177
std::string GetError()
Returns the last encountered error for this file.
Definition: FileSystem.cpp:506
FILE * GetFILE()
Returns the underlying C FILE pointer.
Definition: FileSystem.h:162
std::string GetFileParentDirectory(const std::string &filename)
Definition: FileSystem.cpp:135
int64_t CurPos()
Returns current position in the file (ftell).
Definition: FileSystem.cpp:518
std::string GetWorkingDirectory()
Definition: FileSystem.cpp:154
bool MakeDirectoryHierarchy(const std::string &directory)
Definition: FileSystem.cpp:252
Definition: PinholeCameraIntrinsic.cpp:35
bool RemoveFile(const std::string &filename)
Definition: FileSystem.cpp:297
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:551
std::string GetRegularizedDirectoryName(const std::string &directory)
Definition: FileSystem.cpp:144
bool FileExists(const std::string &filename)
Definition: FileSystem.cpp:277
int64_t GetFileSize()
Returns the file size in bytes.
Definition: FileSystem.cpp:530
bool ListDirectory(const std::string &directory, std::vector< std::string > &subdirs, std::vector< std::string > &filenames)
Definition: FileSystem.cpp:301
FILE * FOpen(const std::string &filename, const std::string &mode)
Definition: FileSystem.cpp:378
bool MakeDirectory(const std::string &directory)
Definition: FileSystem.cpp:244
const char const char value recording_handle imu_sample recording_handle uint8_t data
Definition: K4aPlugin.cpp:284