MVE - Multi-View Environment mve-devel
Loading...
Searching...
No Matches
file_system.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015, Simon Fuhrmann, Andre Schulz
3 * TU Darmstadt - Graphics, Capture and Massively Parallel Computing
4 * All rights reserved.
5 *
6 * This software may be modified and distributed under the terms
7 * of the BSD 3-Clause license. See the LICENSE.txt file for details.
8 */
9
10#ifndef UTIL_FS_HEADER
11#define UTIL_FS_HEADER
12
13#include <string>
14#include <vector>
15
16#include "util/defines.h"
17
20
21/*
22 * -------------------------- Path operations ------------------------
23 */
24
26bool exists (char const* pathname);
27
29bool dir_exists (char const* pathname);
30
32bool file_exists (char const* pathname);
33
35char const* get_app_data_dir (void);
36
38char const* get_home_dir (void);
39
41char* get_cwd (char* buf, std::size_t size);
42
48bool set_cwd (char const* pathname);
49
50/*
51 * -------------------------- File operations ------------------------
52 */
53
54// TODO: define modes for mkdir() for all platforms?
56bool mkdir (char const* pathname/*, mode_t mode*/);
57
59bool rmdir (char const* pathname);
60
62bool unlink (char const* pathname);
63
65bool rename (char const* from, char const* to);
66
68void copy_file (char const* src, char const* dst);
69
70/*
71 * ----------------------------- File IO ----------------------------
72 */
73
75void
76read_file_to_string (std::string const& filename, std::string* data);
77
79void
80write_string_to_file (std::string const& data, std::string const& filename);
81
83void
84write_string_to_file (char const* data, std::size_t len,
85 std::string const& filename);
86
87/*
88 * ------------------------- String processing -----------------------
89 */
90
92std::string get_cwd_string (void);
93
95std::string get_binary_path (void);
96
98bool is_absolute (std::string const& path);
99
101std::string sanitize_path (std::string const& path);
102
107std::string join_path (std::string const& path1, std::string const& path2);
108
110std::string abspath (std::string const& path);
111
113std::string dirname (std::string const& path);
114
116std::string basename (std::string const& path);
117
122std::string replace_extension (std::string const& fn, std::string const& ext);
123
124/*
125 * ------------------------- File abstraction ------------------------
126 */
127
128struct File
129{
130 std::string path;
131 std::string name;
132 bool is_dir;
133
134 File (void);
135 File (std::string const& path, std::string const& name, bool isdir = false);
136 std::string get_absolute_name (void) const;
137 bool operator< (File const& rhs) const;
138};
139
140/*
141 * ------------------------- Directory reading -----------------------
142 */
143
145class Directory : public std::vector<File>
146{
147public:
148 Directory (void);
149 Directory (std::string const& path);
150 void scan (std::string const& path);
151};
152
153/*
154 * --------------------- File locking mechanism ----------------------
155 */
156
166{
167public:
169 {
177 LOCK_CREATE_ERROR
178 };
179
180public:
181 FileLock (void);
182
188 FileLock (std::string const& filename);
189
191 ~FileLock (void);
192
196 Status acquire (std::string const& filename);
197
203 Status acquire_retry (std::string const& filename,
204 int retries = 50, int sleep = 100);
205
209 bool is_locked (std::string const& filename);
210
216 bool wait_lock (std::string const& filename,
217 int retries = 50, int sleep = 100);
218
223 bool release (void);
224
228 std::string const& get_reason (void) const;
229
230private:
231 std::string lockfile;
232 std::string reason;
233};
234
235/*
236 * -------------------------- Implementation -------------------------
237 */
238
239inline
240File::File (void)
241 : is_dir(false)
242{
243}
244
245inline
246File::File (std::string const& path, std::string const& name, bool isdir)
247 : path(path), name(name), is_dir(isdir)
248{
249}
250
251inline
253{
254}
255
256inline
257Directory::Directory (std::string const& path)
258{
259 this->scan(path);
260}
261
262inline
264{
265}
266
267inline
269{
270 this->release();
271}
272
273inline std::string const&
275{
276 return this->reason;
277}
278
281
282#endif /* UTIL_FS_HEADER */
Directory abstraction to scan directory contents.
void scan(std::string const &path)
A simple file-based file lock implementation.
bool release(void)
Removes the lock if it exists.
std::string const & get_reason(void) const
If locking failes, this returns the reason for failure.
@ LOCK_CREATED
The lock has been created successfully.
@ LOCK_EXISTS
The lock has NOT been created because a lock already exists.
@ LOCK_PERSISTENT
The lock has NOT been created because an existing lock persisted.
~FileLock(void)
Removes the lock if it exists.
std::string replace_extension(std::string const &fn, std::string const &ext)
Replaces extension of the given file with 'ext'.
bool unlink(char const *pathname)
Unlinks (deletes) the given file.
std::string sanitize_path(std::string const &path)
Canonicalize slashes in the given path.
std::string abspath(std::string const &path)
Returns the absolute representation of the given path.
bool is_absolute(std::string const &path)
Checks whether the given path is absolute.
char * get_cwd(char *buf, size_t size)
bool mkdir(char const *pathname)
Creates a new directory.
void write_string_to_file(std::string const &data, std::string const &filename)
Writes the given data into a file.
bool rmdir(char const *pathname)
Removes an empty directory.
void copy_file(char const *src, char const *dst)
Copies a file from 'src' to 'dst', throws FileException on error.
bool exists(char const *pathname)
Determines if the given path is a directory.
std::string get_binary_path(void)
Returns the path of the binary currently executing.
std::string dirname(std::string const &path)
Returns the directory name component of the given path.
bool dir_exists(char const *pathname)
Determines if the given path is a directory.
char const * get_app_data_dir(void)
Determines the current user's path for application data.
bool rename(char const *from, char const *to)
Renames the given file 'from' to new name 'to'.
bool set_cwd(char const *pathname)
Changes the current working directory to 'pathname' and returns true on success.
std::string join_path(std::string const &path1, std::string const &path2)
Concatenate and canonicalize two paths.
bool file_exists(char const *pathname)
Determines if the given path is a file.
std::string basename(std::string const &path)
Returns the file name component of the given path.
void read_file_to_string(std::string const &filename, std::string *data)
Reads the whole file into a string.
char const * get_home_dir(void)
Determines the home path for the current user.
std::string get_cwd_string(void)
Determines the CWD and returns a convenient string.
std::string path
std::string name
#define UTIL_NAMESPACE_BEGIN
Definition defines.h:13
#define UTIL_NAMESPACE_END
Definition defines.h:14
#define UTIL_FS_NAMESPACE_BEGIN
Definition defines.h:16
#define UTIL_FS_NAMESPACE_END
Definition defines.h:17