system.hh 5.86 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
 * @file:   system.hh
 * @author: Jordan P. Lefebvre
 *
 * Created on October 23, 2017, 16:34 PM
 */

#ifndef RADIX_RADIXCORE_SYSTEM_HH_
#define RADIX_RADIXCORE_SYSTEM_HH_

#include <fstream>
#include <string>
#include <vector>

15
#include "radixcore/visibility.hh"
16

17
18
namespace radix
{
19
20
21
22
23
24
25
26
27
28
29
30
/**
 * @brief Provides platform independent popen call
 * function creates a pipe and asynchronously executes a spawned
 * copy of the command processor with the specified string command
 * Default open mode of "r".
 * Open mode options:<br/>
 * <ul><li> "r" - read standard output</li>
 * <li> "w" - write standard input</li>
 * <li> "b" - open in binary mode </li>
 * <li> "t" - open in text mode </li>
 * @return <b>FILE*</b> nullptr on error
 */
31
32
RADIX_PUBLIC FILE *pipe_open(const std::string &command,
                             const std::string &mode = "r");
33
34
35
36
37
38
39
40

/**
 * @brief Provides platform independent pclose call
 * Returns the exit status of the terminating command processor,
 * or –1 if an error occurs.
 * @param pipe <b>FILE*</b> stream to close
 * @return process return code
 */
41
RADIX_PUBLIC int pipe_close(FILE *pipe);
42
43
44
45
46
47

/**
 * @brief Sets the environment variable {@code var} to the value
 * @param var <b>std::string</b>
 * @param value <b> std::string</b>
 */
48
RADIX_PUBLIC void set_env(const std::string &var, const std::string &value);
49
50
51
52
53
54

/**
 * @brief Retrieve the environment variable {@code var}
 * @param var <b>std::string</b>
 * @return  <b>std::string</b>
 */
55
RADIX_PUBLIC std::string env(const std::string &var);
56
57
58
59

/**
 * @brief Get hostname of this computer
 */
60
RADIX_PUBLIC std::string computer_name();
61
62
63
64
65
66

/**
 * @brief Get the separator character
 * Useful for abstracting platform differences
 * @return <b>char</b>
 */
67
RADIX_PUBLIC char separator_char(char forceSep = ' ');
68
69
70
71
72
73
74
75
76
77
78
79
/**
 * @brief Get the alternate separator character
 * Useful for abstracting platform differences
 * @return <b>char</b>
 */
RADIX_PUBLIC char alt_separator_char();

/**
 * @brief Update provided path to be native
 * Useful for abstracting platform differences
 * @return <b>std::string</b>
 */
80
RADIX_PUBLIC std::string to_native_path(const std::string &path);
81
82
83
84
85

/**
 * @brief Retrieves the current working directory
 * @return <b>std::string</b>
 */
86
RADIX_PUBLIC std::string current_dir();
87
88
89
90
91
92
93
94
95
96
97

/**
 * @brief Cross-platform, non-QT path listing with globs
 * Perform a file listing using using "dir /B "+spec on windows
 * and "ls -1 "+spec otherwise.  The result is returned in a vector
 * of strings with a return value of false if paths are empty.
 * @param spec <b>std::string</b> path specification (including globs)
 * @param path <b>std::vector<std::string></b> resultant paths
 * @param append <b>bool</b> whether to append to existing paths (default=false)
 * @return <b>bool</b> - false if no matching paths
 */
98
99
100
RADIX_PUBLIC bool get_paths(const std::string &spec,
                            std::vector<std::string> &paths,
                            bool append = false);
101
102
103
104
105
106

/**
 * @brief repeatedly call getPaths on each element
 * @param path <b>std::vector<std::string></b> resultant paths
 * @return <b>bool</b> - false if no matching paths
 */
107
RADIX_PUBLIC bool expand_paths(std::vector<std::string> &paths);
108
109

/**
110
 * @brief expand the input path via expand_paths
111
112
113
 * @param path <b>std::string</b> input/output path
 * @return <b>bool</b> - false if no matching path
 */
114
RADIX_PUBLIC bool expand_path(std::string &path);
115
116
117
118
119

/**
 * @brief Retrieves the current working directory's name
 * @return <b>std::string</b>
 */
120
RADIX_PUBLIC std::string current_dirname();
121
122
123
124
125
126
127

/**
 * @brief designed to behave identical to unix util dirname
 * @param path <b>std::string</b> file system path
 * @return <b>std::string</b> file system path to the directory containing
 * the last file/directory listed
 */
128
RADIX_PUBLIC std::string dirname(const std::string &path, char forceSep = ' ');
129
130
131
132
133
134
135

/**
 * @brief designed to behave identical to unix util basename
 * @param path <b>std::string</b> file system path
 * @return <b>std::string</b> file system name of the directory containing
 * the last file/directory listed
 */
136
137
138
RADIX_PUBLIC std::string basename(const std::string &path,
                                  bool removeExtension = false,
                                  char forceSep        = ' ');
139
140
141
142
143
144
145
146

/**
 * @brief get the file extension
 * The file extension is extracted as the last sequence of non-"." characters
 * after the last ".". If there is no "." then the empty string "" is returned.
 * @param path <b>std::string</b> file system path
 * @return <b>std::string</b> extension or empty
 */
147
RADIX_PUBLIC std::string file_extension(const std::string &path);
148
149
150
151
152
153
154
155
156
157
158
159

/**
 * @brief Changes the current working directory to the specified directory
 * On Windows the string must not exceed MAX_PATH characters, including
 * the terminating null character. The final character before the null
 * character must be a backslash ("\"). If you do not specify the
 * backslash, it will be added for you; therefore, specify MAX_PATH-2
 * characters for the path unless you include the trailing backslash,
 * in which case, specify MAX_PATH-1 characters for the path
 * @param directory <b>std::string</b>
 * @return bool
 */
160
RADIX_PUBLIC bool set_current_dir(const std::string &directory);
161
162
163
164

/**
 * @brief Does this path exist?
 */
165
RADIX_PUBLIC bool path_exists(const std::string &path);
166
167
168
169

/**
 * @brief Does this file exist?
 */
170
RADIX_PUBLIC bool file_exists(const std::string &file);
171
172
173
174

/**
 * @brief Does this directory exist?
 */
175
RADIX_PUBLIC bool dir_exists(const std::string &directory);
176
177
178
179
180

/**
 * @brief Create a directory
 * @return true if success
 */
181
RADIX_PUBLIC bool make_dir(const std::string &path);
182
183
184
185
186

/**
 * @brief Remove an empty directory
 * @return true if success
 */
187
RADIX_PUBLIC bool remove_dir(const std::string &path);
188
189
190
191
192

/**
 * @brief Remove a single file
 * @return true if success
 */
193
RADIX_PUBLIC bool remove_file(const std::string &path);
194
195
196
197

}  // namespace radix

#endif /** RADIX_RADIXCORE_SYSTEM_HH_ */