file_system.hpp

    1: #ifndef FILE_SYSTEM_HPP
    2: #define FILE_SYSTEM_HPP
    3: /*------------------------------------------------------------------------------
    4: 
    5:   Author:    Andy Rushton
    6:   Copyright: (c) Southampton University 1999-2004
    7:              (c) Andy Rushton           2004-2008
    8:   License:   BSD License, see ../docs/license.html
    9: 
   10:   Simplified access to the File system
   11: 
   12:   All file system access and filename manipulation should be done
   13:   with this package. Then it is only necessary to port this package
   14:   to port all file handling.
   15: 
   16: ------------------------------------------------------------------------------*/
   17: #include "os_fixes.hpp"
   18: #include <string>
   19: #include <vector>
   20: #include <time.h>
   21: 
   22: ////////////////////////////////////////////////////////////////////////////////
   23: // implement string comparison of paths - Unix is case-sensitive, Windows is case-insensitive
   24: 
   25: bool path_compare(const std::string& l, const std::string& r);
   26: 
   27: ////////////////////////////////////////////////////////////////////////////////
   28: // classifying functions
   29: 
   30: bool is_present(const std::string& thing);
   31: bool is_folder(const std::string& thing);
   32: bool is_file(const std::string& thing);
   33: 
   34: ////////////////////////////////////////////////////////////////////////////////
   35: // file functions
   36: 
   37: bool file_exists(const std::string& filespec);
   38: bool file_readable(const std::string& filespec);
   39: bool file_writable(const std::string& filespec);
   40: size_t file_size(const std::string& filespec);
   41: bool file_delete(const std::string& filespec);
   42: bool file_rename (const std::string& old_filespec, const std::string& new_filespec);
   43: bool file_move (const std::string& old_filespec, const std::string& new_filespec);
   44: bool file_copy (const std::string& old_filespec, const std::string& new_filespec);
   45: 
   46: // Read-only versus read-write control. This is equivalent to chmod on Unix,
   47: // but I've insulated the user from the low-level routine because of
   48: // differences in the OSs' interpretation of the mode parameter. I've also
   49: // defined a new set of constants to control this, again because of
   50: // inconsistencies. The idea is to combine the constants as bit-masks so as to
   51: // build up a set of permissions. The modes are ORed together to build up a
   52: // set of permissions and then ANDed with a mask to control which people have
   53: // that permission. Permissions can be ORed together too. So, for example, to
   54: // give the owner read-write access and all others only read access, you would
   55: // use the expression:
   56: //   ((read_mode | write_mode) & owner_mask) | (read_mode & (group_mask | other_mask))
   57: // This can be simplified by using combined modes and combined masks to:
   58: //   (read_write_mode & owner_mask) | (read_mode & non_owner_mask)
   59: 
   60: // basic modes
   61: extern const int read_mode;
   62: extern const int write_mode;
   63: extern const int execute_mode;
   64: // combined modes
   65: extern const int none_mode;
   66: extern const int read_write_mode;
   67: extern const int all_mode;
   68: // basic users
   69: extern const int owner_mask;
   70: extern const int group_mask;
   71: extern const int other_mask;
   72: // combined users
   73: extern const int non_owner_mask;
   74: extern const int all_mask;
   75: // common settings
   76: extern const int read_mode_all;
   77: extern const int read_write_mode_owner_read_mode_all;
   78: extern const int read_mode_owner_only;
   79: extern const int read_write_mode_owner_only;
   80: // the function itself
   81: bool file_set_mode(const std::string& filespec, int mode);
   82: 
   83: // get the file's time stamps as a time_t - see the stlplus time.hpp package
   84: time_t file_created(const std::string& filespec);
   85: time_t file_modified(const std::string& filespec);
   86: time_t file_accessed(const std::string& filespec);
   87: 
   88: std::string create_filespec(const std::string& folder, const std::string& filename);
   89: std::string create_filespec(const std::string& folder, const std::string& basename, const std::string& extension);
   90: std::string create_filename(const std::string& basename, const std::string& extension);
   91: 
   92: ////////////////////////////////////////////////////////////////////////////////
   93: // folder functions
   94: 
   95: bool folder_create(const std::string& folder);
   96: bool folder_exists(const std::string& folder);
   97: bool folder_readable(const std::string& folder);
   98: bool folder_writable(const std::string& folder);
   99: bool folder_delete(const std::string& folder, bool recurse = false);
  100: bool folder_rename (const std::string& old_directory, const std::string& new_directory);
  101: bool folder_empty(const std::string& folder);
  102: 
  103: bool folder_set_current(const std::string& folder);
  104: std::string folder_current(void);
  105: std::string folder_current_full(void);
  106: std::string folder_home(void);
  107: std::string folder_down(const std::string& folder, const std::string& subfolder);
  108: std::string folder_up(const std::string& folder, unsigned levels = 1);
  109: 
  110: std::vector<std::string> folder_subdirectories(const std::string& folder);
  111: std::vector<std::string> folder_files(const std::string& folder);
  112: std::vector<std::string> folder_all(const std::string& folder);
  113: std::vector<std::string> folder_wildcard(const std::string& folder, const std::string& wildcard, bool subdirs = true, bool files = true);
  114: 
  115: ////////////////////////////////////////////////////////////////////////////////
  116: // path functions
  117: 
  118: bool is_full_path(const std::string& path);
  119: bool is_relative_path(const std::string& path);
  120: 
  121: // convert to a full path relative to the root path
  122: std::string folder_to_path(const std::string& root, const std::string& folder);
  123: std::string filespec_to_path(const std::string& root, const std::string& filespec);
  124: 
  125: // convert to a full path relative to the current working directory
  126: std::string folder_to_path(const std::string& folder);
  127: std::string filespec_to_path(const std::string& filespec);
  128: 
  129: // convert to a relative path relative to the root path
  130: std::string folder_to_relative_path(const std::string& root, const std::string& folder);
  131: std::string filespec_to_relative_path(const std::string& root, const std::string& filespec);
  132: 
  133: // convert to a relative path relative to the current working directory
  134: std::string folder_to_relative_path(const std::string& folder);
  135: std::string filespec_to_relative_path(const std::string& filespec);
  136: 
  137: // append a folder separator to the path to make it absolutely clear that it is a folder
  138: std::string folder_append_separator(const std::string& folder);
  139: 
  140: ////////////////////////////////////////////////////////////////////////////////
  141: // access functions split a filespec into its elements
  142: 
  143: std::string basename_part(const std::string& filespec);
  144: std::string filename_part(const std::string& filespec);
  145: std::string extension_part(const std::string& filespec);
  146: std::string folder_part(const std::string& filespec);
  147: 
  148: // split a path into a vector of elements - i.e. split at the folder separator
  149: std::vector<std::string> folder_elements(const std::string& folder);
  150: std::vector<std::string> filespec_elements(const std::string& filespec);
  151: 
  152: ////////////////////////////////////////////////////////////////////////////////
  153: // Path lookup functions
  154: 
  155: #ifdef MSWINDOWS
  156: #define PATH_SPLITTER ";"
  157: #else
  158: #define PATH_SPLITTER ":"
  159: #endif
  160: 
  161: // The lookup normally carried out by the shell to find a command in a
  162: // directory in the PATH. Give this function the name of a command and it
  163: // will return the full path. It returns an empty string on failure.
  164: std::string path_lookup (const std::string& command);
  165: 
  166: // Generalised form of the above, takes a second argument
  167: // - the list to search. This can be used to do other path lookups,
  168: // such as LD_LIBRARY_PATH. The third argument specifies the splitter -
  169: // the default value of PATH_SPLITTER is appropriate for environment variables.
  170: // It returns an empty string on failure.
  171: std::string lookup (const std::string& file, const std::string& path, const std::string& splitter = PATH_SPLITTER);
  172: 
  173: // utility function for finding the folder that contains the current executable
  174: // the argument is the argv[0] parameter passed to main
  175: std::string install_path(const std::string& argv0);
  176: 
  177: ////////////////////////////////////////////////////////////////////////////////
  178: 
  179: #endif