Changeset 711
- Timestamp:
- Dec 21, 2006, 9:10:30 AM (17 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/nni_test.cc
r710 r711 36 36 void check_file_access(std::string& str) 37 37 { 38 utility::FileUtil file(str); 39 if (file.access_rights(str,"r")) { 38 if (utility::FileUtil(str).permissions("r")) { 40 39 std::cerr << "test_nni: Cannot access file " << str << std::endl; 41 40 exit(-1); -
trunk/test/vector_test.cc
r710 r711 34 34 void check_file_access(std::string& str) 35 35 { 36 utility::FileUtil file(str);37 if ( file.access_rights(str,"r")) {36 37 if (utility::FileUtil(str).permissions("r")) { 38 38 std::cerr << "test_nni: Cannot access file " << str << std::endl; 39 39 exit(-1); -
trunk/yat/utility/FileUtil.cc
r710 r711 40 40 41 41 FileUtil::FileUtil(const std::string& path) 42 : no_file_(false),path_(path)42 : path_(path) 43 43 { 44 if ( stat(path_.c_str(),&stat_) && !(no_file_=(errno==ENOENT)) )45 throw IO_error(std::string("stat(2) call failed with errno: "+errno));46 44 } 47 45 48 46 49 int FileUtil::access_rights(const std::string& path, 50 const std::string& bits) const 47 int FileUtil::permissions(const std::string& bits) const 51 48 { 52 49 std::string tryme=path_; 53 if ( no_file_) {50 if (!exists()) { 54 51 std::string::size_type pos=path_.find_last_of('/'); 55 52 tryme = ( (pos!=std::string::npos) ? path_.substr(0,pos) : "." ); … … 74 71 75 72 76 bool FileUtil:: file_exists(const std::string& file) const73 bool FileUtil::exists(void) const 77 74 { 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 79 79 } 80 80 -
trunk/yat/utility/FileUtil.h
r710 r711 38 38 39 39 /// 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). 41 43 /// 42 44 class FileUtil { … … 44 46 45 47 /// 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 50 49 /// 51 50 explicit FileUtil(const std::string& path); 52 51 53 52 /// 54 /// @brief Check file/directory permissions53 /// \brief Check file/directory permissions 55 54 /// 56 55 /// 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. 58 58 /// 59 /// @note Checking permissions on a non-existent file will return60 /// the permissions for the parent directory, but only the parent61 /// directory. If the parent directory does not exist then fail is62 /// 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. 63 63 /// 64 /// @return On success (all requested permissions granted), zero64 /// \return On success (all requested permissions granted), zero 65 65 /// is returned. On error (at least one bit in mode asked for a 66 66 /// permission that is denied, or some other error occurred), -1 67 67 /// is returned, and errno is set appropriately. 68 68 /// 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; 70 72 71 73 /// 72 /// @brief Check whether \a file exists.74 /// \brief Check whether \a file exists. 73 75 /// 74 /// @return True if \a file exists, false otherwise.76 /// The check for the file is done when calling this function. 75 77 /// 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; 77 86 78 87 /// 79 /// @brief Get path associated with the object.88 /// \brief Get path associated with the object. 80 89 /// 81 90 const std::string& path(void) const; … … 87 96 FileUtil(const FileUtil&); 88 97 89 bool no_file_;90 98 std::string path_; 91 struct stat stat_;92 99 }; 93 100
Note: See TracChangeset
for help on using the changeset viewer.