| 1 | /* tol_bdir.h: BDir(dir)class and functions header |
|---|
| 2 | GNU/TOL Language. |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2003 - Bayes Decision, SL (Spain [EU]) |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2, or(at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | This program is distributed in the hope that it will be useful, |
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | GNU General Public License for more details. |
|---|
| 15 | |
|---|
| 16 | You should have received a copy of the GNU General Public License |
|---|
| 17 | along with this program; if not, write to the Free Software |
|---|
| 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
|---|
| 19 | USA. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | /*! \class BDir.h |
|---|
| 23 | * \brief Defining BDir basic class, its attributes and methods prototypes |
|---|
| 24 | * \brief functionality has been implemented in dir.cpp, dirtext.cpp and |
|---|
| 25 | * \brief dircomma.cpp |
|---|
| 26 | */ |
|---|
| 27 | |
|---|
| 28 | #ifndef TOL_BDIR_H |
|---|
| 29 | #define TOL_BDIR_H 1 |
|---|
| 30 | |
|---|
| 31 | #include <tol/tol_bobject.h> |
|---|
| 32 | #include <tol/tol_barray.h> |
|---|
| 33 | |
|---|
| 34 | #if HAVE_DIRENT_H |
|---|
| 35 | # include <dirent.h> |
|---|
| 36 | # define NAMLEN(dirent)strlen((dirent)->d_name) |
|---|
| 37 | #else |
|---|
| 38 | # define dirent direct |
|---|
| 39 | # define NAMLEN(dirent)(dirent)->d_namlen |
|---|
| 40 | # if HAVE_SYS_NDIR_H |
|---|
| 41 | # include <sys/ndir.h> |
|---|
| 42 | # endif |
|---|
| 43 | # if HAVE_SYS_DIR_H |
|---|
| 44 | # include <sys/dir.h> |
|---|
| 45 | # endif |
|---|
| 46 | # if HAVE_NDIR_H |
|---|
| 47 | # include <ndir.h> |
|---|
| 48 | # endif |
|---|
| 49 | #endif |
|---|
| 50 | |
|---|
| 51 | #ifdef __MINGW32__ |
|---|
| 52 | # define SLASH '/' |
|---|
| 53 | # define TSLASH "/" |
|---|
| 54 | # define DIRMAXNAMLEN MAX_PATH |
|---|
| 55 | #elif UNIX |
|---|
| 56 | # define SLASH '/' |
|---|
| 57 | # define TSLASH "/" |
|---|
| 58 | # define DIRMAXNAMLEN MAXNAMLEN |
|---|
| 59 | #else |
|---|
| 60 | # define SLASH '\\' |
|---|
| 61 | # define TSLASH "\\" |
|---|
| 62 | # ifdef _MSC_VER |
|---|
| 63 | # include <stdlib.h> |
|---|
| 64 | # define DIRMAXNAMLEN _MAX_FNAME |
|---|
| 65 | # else |
|---|
| 66 | # define DIRMAXNAMLEN MAXPATH |
|---|
| 67 | # endif |
|---|
| 68 | #endif |
|---|
| 69 | |
|---|
| 70 | #ifndef _MSC_VER |
|---|
| 71 | # define direct dirent |
|---|
| 72 | #else |
|---|
| 73 | # include <direct.h> |
|---|
| 74 | #endif |
|---|
| 75 | |
|---|
| 76 | #if _MSC_VER |
|---|
| 77 | # include <io.h> |
|---|
| 78 | #endif // _MSC_VER |
|---|
| 79 | #include <sys/stat.h> |
|---|
| 80 | |
|---|
| 81 | //-------------------------------------------------------------------- |
|---|
| 82 | class BDir: public BObject |
|---|
| 83 | |
|---|
| 84 | /*! Operating system directories. This object gives the following |
|---|
| 85 | * information: |
|---|
| 86 | * - Files: Only files. |
|---|
| 87 | * - Dirs : Siubdirectories inside the directory. |
|---|
| 88 | * The own directory . and its parent .. is allways omitted. |
|---|
| 89 | */ |
|---|
| 90 | //-------------------------------------------------------------------- |
|---|
| 91 | { |
|---|
| 92 | private: |
|---|
| 93 | BBool exist_; //!< BTRUE if the directory(or file)exists |
|---|
| 94 | BBool isFile_; //!< BTRUE if is a file, not a directory |
|---|
| 95 | BInt nBytes_; //!< Size if is a file, 0 id is a directory |
|---|
| 96 | time_t mTime_; //!< Last modification time measured in seconds |
|---|
| 97 | BBool empty_; //!< BFALSE if the directory only has . and .. |
|---|
| 98 | BArray<BObject> dirName_; //!< All subdirectories excluding . and .. |
|---|
| 99 | BArray<BObject> fileName_; //!< All files |
|---|
| 100 | BArray<BInt> fileBytes_; //!< Size of all files |
|---|
| 101 | BArray<time_t> fileTime_; //!< Modification time of all files |
|---|
| 102 | BInt numFiles_; //!< Number of files |
|---|
| 103 | BInt numDirs_; //!< Number of subdirectories excluding . and .. |
|---|
| 104 | |
|---|
| 105 | public: |
|---|
| 106 | // Constructors and destructors: |
|---|
| 107 | //! Create a new BDir |
|---|
| 108 | BDir(const BText& dirName); |
|---|
| 109 | //! Destroy a BDir |
|---|
| 110 | ~BDir(){ } |
|---|
| 111 | |
|---|
| 112 | // Access and manipulation: inline |
|---|
| 113 | //! \a fileName_ returns elements n-th of the attribute |
|---|
| 114 | BText FileName(BInt n) const { return(fileName_[n].Name()); } |
|---|
| 115 | //! \a fileTime_ returns elements n-th of the attribute |
|---|
| 116 | BInt FileBytes(BInt n) const { return(fileBytes_[n]); } |
|---|
| 117 | //! \a fileTime_ returns elements n-th of the attribute |
|---|
| 118 | time_t FileTime(BInt n) const { return(fileTime_[n]); } |
|---|
| 119 | //! \a dirName_ returns elements n-th of the attribute |
|---|
| 120 | BText DirName(BInt n) const { return(dirName_[n].Name()); } |
|---|
| 121 | //! return \a numFiles_ attribute |
|---|
| 122 | BInt NumFiles() const { return(numFiles_); } |
|---|
| 123 | //! return \a numDirs_ attribute |
|---|
| 124 | BInt NumDirs() const { return(numDirs_); } |
|---|
| 125 | //! return \a nBytes_ attribute |
|---|
| 126 | BInt Bytes() const { return(nBytes_); } |
|---|
| 127 | //! return \a mTime_ attribute |
|---|
| 128 | time_t Time() const { return(mTime_); } |
|---|
| 129 | //! return \a exist_ attribute |
|---|
| 130 | BBool Exist() const { return(exist_); } |
|---|
| 131 | //! return \a empty_ attribute |
|---|
| 132 | BBool Empty() const { return(empty_); } |
|---|
| 133 | //! indicates if it exist and he is a directory |
|---|
| 134 | BBool IsDir() const { return(exist_ && !isFile_); } |
|---|
| 135 | //! indicates if it exist and he is a file |
|---|
| 136 | BBool IsFile() const { return(exist_ && isFile_); } |
|---|
| 137 | |
|---|
| 138 | // Functions: |
|---|
| 139 | //! finds for the name of a file |
|---|
| 140 | BInt FindFile(const BText& fileName) const; |
|---|
| 141 | //! finds for the name of a directory |
|---|
| 142 | BInt FindDir(const BText& dirName ) const; |
|---|
| 143 | //! Transform in text a directory object |
|---|
| 144 | BText Text(BBool sizes=BFALSE, BInt columns=3, BBool recursive=BFALSE); |
|---|
| 145 | //! Execute an instruction to all the archives of the directory |
|---|
| 146 | BInt Command(BBool trace, BBool recursive, |
|---|
| 147 | const BText& command, const BText& pattern); |
|---|
| 148 | |
|---|
| 149 | // General: |
|---|
| 150 | //! Get current directory |
|---|
| 151 | static BText GetCurrent(); |
|---|
| 152 | //! Change the current directory |
|---|
| 153 | static void ChangeCurrent(const BText& d); |
|---|
| 154 | |
|---|
| 155 | static BBool CheckIsDir(const BText& dirName); |
|---|
| 156 | DeclareClassNewDelete(BDir); |
|---|
| 157 | }; |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | //-------------------------------------------------------------------- |
|---|
| 161 | // external functions |
|---|
| 162 | //-------------------------------------------------------------------- |
|---|
| 163 | //! Verifies if he is a directory |
|---|
| 164 | //BBool CheckIsDir(const BText& dirName); |
|---|
| 165 | //! Verifies if he is a file |
|---|
| 166 | TOL_API BBool CheckIsFile(const BText& fileName); |
|---|
| 167 | //! Gets the path of file |
|---|
| 168 | TOL_API BText GetFilePath(const BText& nameFile); |
|---|
| 169 | //! Gets the name of file path |
|---|
| 170 | TOL_API BText GetFileName(const BText& path); |
|---|
| 171 | //! Gets the extension of file |
|---|
| 172 | TOL_API BText GetFileExtension(const BText& path); |
|---|
| 173 | //! Gets the name of file without extension |
|---|
| 174 | TOL_API BText GetFilePrefix(const BText& path); |
|---|
| 175 | //! Gets an absolute path of file |
|---|
| 176 | TOL_API BText GetAbsolutePath(const BText& path); |
|---|
| 177 | //! Gets the path of file in an standard way |
|---|
| 178 | TOL_API BText GetStandardAbsolutePath(const BText& path_); |
|---|
| 179 | //! Indicates if path is absolute or relative |
|---|
| 180 | TOL_API BBool IsAbsolutePath(const BText& path); |
|---|
| 181 | //! Gets the size of a file |
|---|
| 182 | TOL_API BInt GetFileSize(const BText& fileName); |
|---|
| 183 | //! Gets the last modification time of a file |
|---|
| 184 | TOL_API time_t GetFileTime(const BText& fileName); |
|---|
| 185 | //! Gets the last modification and access time of a file |
|---|
| 186 | TOL_API void SetFileTime(const BText& fileName, time_t time); |
|---|
| 187 | //! Pust the last modification time of first file in second file |
|---|
| 188 | TOL_API void TouchAsFile(const BText& fromFile, |
|---|
| 189 | const BText& toFile); |
|---|
| 190 | //! Gets the path in the operative system style |
|---|
| 191 | TOL_API BText SysPath (const BText& path); |
|---|
| 192 | //! Returns true for both / and \ slashes |
|---|
| 193 | TOL_API bool IsSlash (char ch); |
|---|
| 194 | //! Replace all slashes by standard one for operative system |
|---|
| 195 | TOL_API BText ReplaceSlash (const BText& path); |
|---|
| 196 | //! Removes last slash and puts true endSlash if paths end at an slash |
|---|
| 197 | TOL_API BText RemoveLastSlash (const BText& path, bool& endSlash); |
|---|
| 198 | //! Removes last slash if paths end at an slash |
|---|
| 199 | TOL_API BText RemoveLastSlash (const BText& path); |
|---|
| 200 | |
|---|
| 201 | #endif // TOL_BDIR_H |
|---|