/* * @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 #include #include #include "radixcore/visibility.hh" namespace radix { /** * @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:
*
  • "r" - read standard output
  • *
  • "w" - write standard input
  • *
  • "b" - open in binary mode
  • *
  • "t" - open in text mode
  • * @return FILE* nullptr on error */ RADIX_PUBLIC FILE *pipe_open(const std::string &command, const std::string &mode = "r"); /** * @brief Provides platform independent pclose call * Returns the exit status of the terminating command processor, * or –1 if an error occurs. * @param pipe FILE* stream to close * @return process return code */ RADIX_PUBLIC int pipe_close(FILE *pipe); /** * @brief Sets the environment variable {@code var} to the value * @param var std::string * @param value std::string */ RADIX_PUBLIC void set_env(const std::string &var, const std::string &value); /** * @brief Retrieve the environment variable {@code var} * @param var std::string * @return std::string */ RADIX_PUBLIC std::string env(const std::string &var); /** * @brief Get hostname of this computer */ RADIX_PUBLIC std::string computer_name(); /** * @brief Get the separator character * Useful for abstracting platform differences * @return char */ RADIX_PUBLIC char separator_char(char forceSep = ' '); /** * @brief Get the alternate separator character * Useful for abstracting platform differences * @return char */ RADIX_PUBLIC char alt_separator_char(); /** * @brief Update provided path to be native * Useful for abstracting platform differences * @return std::string */ RADIX_PUBLIC std::string to_native_path(const std::string &path); /** * @brief Retrieves the current working directory * @return std::string */ RADIX_PUBLIC std::string current_dir(); /** * @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 std::string path specification (including globs) * @param path std::vector resultant paths * @param append bool whether to append to existing paths (default=false) * @return bool - false if no matching paths */ RADIX_PUBLIC bool get_paths(const std::string &spec, std::vector &paths, bool append = false); /** * @brief repeatedly call getPaths on each element * @param path std::vector resultant paths * @return bool - false if no matching paths */ RADIX_PUBLIC bool expand_paths(std::vector &paths); /** * @brief expand the input path via expand_paths * @param path std::string input/output path * @return bool - false if no matching path */ RADIX_PUBLIC bool expand_path(std::string &path); /** * @brief Retrieves the current working directory's name * @return std::string */ RADIX_PUBLIC std::string current_dirname(); /** * @brief designed to behave identical to unix util dirname * @param path std::string file system path * @return std::string file system path to the directory containing * the last file/directory listed */ RADIX_PUBLIC std::string dirname(const std::string &path, char forceSep = ' '); /** * @brief designed to behave identical to unix util basename * @param path std::string file system path * @return std::string file system name of the directory containing * the last file/directory listed */ RADIX_PUBLIC std::string basename(const std::string &path, bool removeExtension = false, char forceSep = ' '); /** * @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 std::string file system path * @return std::string extension or empty */ RADIX_PUBLIC std::string file_extension(const std::string &path); /** * @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 std::string * @return bool */ RADIX_PUBLIC bool set_current_dir(const std::string &directory); /** * @brief Does this path exist? */ RADIX_PUBLIC bool path_exists(const std::string &path); /** * @brief Does this file exist? */ RADIX_PUBLIC bool file_exists(const std::string &file); /** * @brief Does this directory exist? */ RADIX_PUBLIC bool dir_exists(const std::string &directory); /** * @brief Create a directory * @return true if success */ RADIX_PUBLIC bool make_dir(const std::string &path); /** * @brief Remove an empty directory * @return true if success */ RADIX_PUBLIC bool remove_dir(const std::string &path); /** * @brief Remove a single file * @return true if success */ RADIX_PUBLIC bool remove_file(const std::string &path); } // namespace radix #endif /** RADIX_RADIXCORE_SYSTEM_HH_ */