Changeset 711


Ignore:
Timestamp:
Dec 21, 2006, 9:10:30 AM (15 years ago)
Author:
Jari Häkkinen
Message:

Fixes #1 and #80. Interface changed.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/nni_test.cc

    r710 r711  
    3636void check_file_access(std::string& str)
    3737{
    38   utility::FileUtil file(str);
    39   if (file.access_rights(str,"r")) {
     38  if (utility::FileUtil(str).permissions("r")) {
    4039    std::cerr << "test_nni: Cannot access file " << str << std::endl;
    4140    exit(-1);
  • trunk/test/vector_test.cc

    r710 r711  
    3434void check_file_access(std::string& str)
    3535{
    36   utility::FileUtil file(str);
    37   if (file.access_rights(str,"r")) {
     36 
     37  if (utility::FileUtil(str).permissions("r")) {
    3838    std::cerr << "test_nni: Cannot access file " << str << std::endl;
    3939    exit(-1);
  • trunk/yat/utility/FileUtil.cc

    r710 r711  
    4040
    4141  FileUtil::FileUtil(const std::string& path)
    42     : no_file_(false), path_(path)
     42    : path_(path)
    4343  {
    44     if ( stat(path_.c_str(),&stat_) && !(no_file_=(errno==ENOENT)) )
    45         throw IO_error(std::string("stat(2) call failed with errno: "+errno));
    4644  }
    4745
    4846
    49   int FileUtil::access_rights(const std::string& path,
    50                               const std::string& bits) const
     47  int FileUtil::permissions(const std::string& bits) const
    5148  {
    5249    std::string tryme=path_;
    53     if (no_file_) {
     50    if (!exists()) {
    5451      std::string::size_type pos=path_.find_last_of('/');
    5552      tryme = ( (pos!=std::string::npos) ? path_.substr(0,pos) : "." );
     
    7471
    7572
    76   bool FileUtil::file_exists(const std::string& file) const
     73  bool FileUtil::exists(void) const
    7774  {
    78     return !no_file_;
     75    struct stat statt;
     76    if ( stat(path_.c_str(),&statt) && (errno!=ENOENT) )
     77      throw IO_error(std::string("stat(2) call failed with errno: "+errno));
     78    return !errno;  // 0 if file exists, non-zero if file does not exist
    7979  }
    8080
  • trunk/yat/utility/FileUtil.h

    r710 r711  
    3838
    3939  ///
    40   /// FileUtil is useful for many common task on files.
     40  /// FileUtil is a utility class for checking file/directory
     41  /// existence and access permissions. FileUtil is a wrapper to
     42  /// access(2) and stat(2).
    4143  ///
    4244  class FileUtil {
     
    4446
    4547    ///
    46     /// \brief Constructor
    47     ///
    48     /// \exception IO_error if underlying stat(2) call fails in other
    49     /// ways than file does not exist.
     48    /// \brief Constructor, copies \a path only
    5049    ///
    5150    explicit FileUtil(const std::string& path);
    5251
    5352    ///
    54     /// @brief Check file/directory permissions
     53    /// \brief Check file/directory permissions
    5554    ///
    5655    /// Check if access permissions match \a mode. \a mode must be
    57     /// given as r, w, x, or combinations of these letters.
     56    /// given as r, w, x, or combinations of these letters. The check
     57    /// is performed att call time.
    5858    ///
    59     /// @note Checking permissions on a non-existent file will return
    60     /// the permissions for the parent directory, but only the parent
    61     /// directory. If the parent directory does not exist then fail is
    62     /// returned.
     59    /// \note Checking permissions on a non-existent file will match
     60    /// the permissions against the parent directory, but only the
     61    /// parent directory. This means that when the parent directory
     62    /// does not exist then fail is returned.
    6363    ///
    64     /// @return On success (all requested permissions granted), zero
     64    /// \return On success (all requested permissions granted), zero
    6565    /// is returned. On error (at least one bit in mode asked for a
    6666    /// permission that is denied, or some other error occurred), -1
    6767    /// is returned, and errno is set appropriately.
    6868    ///
    69     int access_rights(const std::string& path,const std::string& bits) const;
     69    /// \exception Throws whatever exists() throws.
     70    ///
     71    int permissions(const std::string& bits) const;
    7072
    7173    ///
    72     /// @brief Check whether \a file exists.
     74    /// \brief Check whether \a file exists.
    7375    ///
    74     /// @return True if \a file exists, false otherwise.
     76    /// The check for the file is done when calling this function.
    7577    ///
    76     bool file_exists(const std::string& file) const;
     78    /// \return True if \a file exists, false otherwise.
     79    ///
     80    /// \exception IO_error if underlying stat(2) call fails in other
     81    /// ways than that file does not exist.
     82    ///
     83    /// \see stat(2)
     84    ///
     85    bool exists(void) const;
    7786
    7887    ///
    79     /// @brief Get path associated with the object.
     88    /// \brief Get path associated with the object.
    8089    ///
    8190    const std::string& path(void) const;
     
    8796    FileUtil(const FileUtil&);
    8897
    89     bool no_file_;
    9098    std::string path_;
    91     struct stat stat_;
    9299  };
    93100
Note: See TracChangeset for help on using the changeset viewer.