further FileInfo.[Ch] cleaning

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@4649 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2002-07-16 13:00:12 +00:00
parent 7eafa404b8
commit e76b396311
3 changed files with 66 additions and 71 deletions

View File

@ -13,7 +13,6 @@
#include <cstdlib> #include <cstdlib>
#include <pwd.h> #include <pwd.h>
#include <grp.h> #include <grp.h>
//#include <cstring>
#include <map> #include <map>
#include <algorithm> #include <algorithm>
@ -188,7 +187,7 @@ void FileDialog::Private::Reread()
DIR * dir = ::opendir(directory_.c_str()); DIR * dir = ::opendir(directory_.c_str());
if (!dir) { if (!dir) {
Alert::err_alert(_("Warning! Couldn't open directory."), Alert::err_alert(_("Warning! Couldn't open directory."),
directory_); directory_);
directory_ = lyx::getcwd(); directory_ = lyx::getcwd();
dir = ::opendir(directory_.c_str()); dir = ::opendir(directory_.c_str());
} }
@ -204,13 +203,13 @@ void FileDialog::Private::Reread()
// Splits complete directory name into directories and compute depth // Splits complete directory name into directories and compute depth
depth_ = 0; depth_ = 0;
string line, Temp; string line, Temp;
char szMode[15]; string mode;
string File = directory_; string File = directory_;
if (File != "/") { if (File != "/") {
File = split(File, Temp, '/'); File = split(File, Temp, '/');
} }
while (!File.empty() || !Temp.empty()) { while (!File.empty() || !Temp.empty()) {
string dline = "@b"+line + Temp + '/'; string dline = "@b" + line + Temp + '/';
fl_add_browser_line(file_dlg_form_->List, dline.c_str()); fl_add_browser_line(file_dlg_form_->List, dline.c_str());
File = split(File, Temp, '/'); File = split(File, Temp, '/');
line += ' '; line += ' ';
@ -220,16 +219,16 @@ void FileDialog::Private::Reread()
// Parses all entries of the given subdirectory // Parses all entries of the given subdirectory
time_t curTime = time(0); time_t curTime = time(0);
rewinddir(dir); rewinddir(dir);
while (dirent * pDirEntry = readdir(dir)) { while (dirent * entry = readdir(dir)) {
bool isLink = false, isDir = false; bool isLink = false, isDir = false;
// If the pattern doesn't start with a dot, skip hidden files // If the pattern doesn't start with a dot, skip hidden files
if (!mask_.empty() && mask_[0] != '.' && if (!mask_.empty() && mask_[0] != '.' &&
pDirEntry->d_name[0] == '.') entry->d_name[0] == '.')
continue; continue;
// Gets filename // Gets filename
string fname = pDirEntry->d_name; string fname = entry->d_name;
// Under all circumstances, "." and ".." are not wanted // Under all circumstances, "." and ".." are not wanted
if (fname == "." || fname == "..") if (fname == "." || fname == "..")
@ -244,10 +243,10 @@ void FileDialog::Private::Reread()
if (!fileInfo.isOK()) if (!fileInfo.isOK())
continue; continue;
fileInfo.modeString(szMode); mode = fileInfo.modeString();
unsigned int nlink = fileInfo.getNumberOfLinks(); unsigned int const nlink = fileInfo.getNumberOfLinks();
string user = lyxUserCache.find(fileInfo.getUid()); string const user = lyxUserCache.find(fileInfo.getUid());
string group = lyxGroupCache.find(fileInfo.getGid()); string const group = lyxGroupCache.find(fileInfo.getGid());
time_t modtime = fileInfo.getModificationTime(); time_t modtime = fileInfo.getModificationTime();
string Time = ctime(&modtime); string Time = ctime(&modtime);
@ -265,21 +264,22 @@ void FileDialog::Private::Reread()
Time.erase(16, string::npos); Time.erase(16, string::npos);
} }
string Buffer = string(szMode) + ' ' + string buffer = mode + ' ' +
tostr(nlink) + ' ' + tostr(nlink) + ' ' +
user + ' ' + user + ' ' +
group + ' ' + group + ' ' +
Time.substr(4, string::npos) + ' '; Time.substr(4, string::npos) + ' ';
Buffer += pDirEntry->d_name; buffer += entry->d_name;
Buffer += fileInfo.typeIndicator(); buffer += fileInfo.typeIndicator();
if ((isLink = fileInfo.isLink())) { isLink = fileInfo.isLink();
if (isLink) {
string Link; string Link;
if (LyXReadLink(File, Link)) { if (LyXReadLink(File, Link)) {
Buffer += " -> "; buffer += " -> ";
Buffer += Link; buffer += Link;
// This gives the FileType of the file that // This gives the FileType of the file that
// is really pointed too after resolving all // is really pointed too after resolving all
@ -289,7 +289,7 @@ void FileDialog::Private::Reread()
// JV 199902 // JV 199902
fileInfo.newFile(File); fileInfo.newFile(File);
if (fileInfo.isOK()) if (fileInfo.isOK())
Buffer += fileInfo.typeIndicator(); buffer += fileInfo.typeIndicator();
else else
continue; continue;
} }
@ -308,14 +308,14 @@ void FileDialog::Private::Reread()
DirEntry tmp; DirEntry tmp;
// Note ls_entry_ is an string! // Note ls_entry_ is an string!
tmp.ls_entry_ = Buffer; tmp.ls_entry_ = buffer;
// creates used name // creates used name
string temp = fname; string temp = fname;
if (isDir) temp += '/'; if (isDir) temp += '/';
tmp.name_ = temp; tmp.name_ = temp;
// creates displayed name // creates displayed name
temp = pDirEntry->d_name; temp = entry->d_name;
if (isLink) if (isLink)
temp += '@'; temp += '@';
else else

View File

@ -90,7 +90,7 @@
namespace { namespace {
/// builds 'rwx' string describing file access rights // builds 'rwx' string describing file access rights
void flagRWX(mode_t i, char * str) void flagRWX(mode_t i, char * str)
{ {
str[0] = (i & S_IRUSR) ? 'r' : '-'; str[0] = (i & S_IRUSR) ? 'r' : '-';
@ -98,7 +98,7 @@ void flagRWX(mode_t i, char * str)
str[2] = (i & S_IXUSR) ? 'x' : '-'; str[2] = (i & S_IXUSR) ? 'x' : '-';
} }
/// updates mode string to match suid/sgid/sticky bits // updates mode string to match suid/sgid/sticky bits
void setSticky(mode_t i, char * str) void setSticky(mode_t i, char * str)
{ {
#ifdef S_ISUID #ifdef S_ISUID
@ -115,6 +115,34 @@ void setSticky(mode_t i, char * str)
#endif #endif
} }
// returns a letter describing a file type (ls style)
char typeLetter(mode_t i)
{
#ifdef S_ISBLK
if (S_ISBLK(i)) return 'b';
#endif
if (S_ISCHR(i)) return 'c';
if (S_ISDIR(i)) return 'd';
if (S_ISREG(i)) return '-';
#ifdef S_ISFIFO
if (S_ISFIFO(i)) return 'p';
#endif
#ifdef S_ISLNK
if (S_ISLNK(i)) return 'l';
#endif
#ifdef S_ISSOCK
if (S_ISSOCK(i)) return 's';
#endif
#ifdef S_ISMPC
if (S_ISMPC(i)) return 'm';
#endif
#ifdef S_ISNWK
if (S_ISNWK(i)) return 'n';
#endif
return '?';
}
} // namespace anon } // namespace anon
@ -181,26 +209,26 @@ FileInfo & FileInfo::newFile(int fildes)
// should not be in FileInfo // should not be in FileInfo
char const * FileInfo::typeIndicator() const char FileInfo::typeIndicator() const
{ {
lyx::Assert(isOK()); lyx::Assert(isOK());
if (S_ISDIR(buf_.st_mode)) if (S_ISDIR(buf_.st_mode))
return "/"; return '/';
#ifdef S_ISLNK #ifdef S_ISLNK
if (S_ISLNK(buf_.st_mode)) if (S_ISLNK(buf_.st_mode))
return "@"; return '@';
#endif #endif
#ifdef S_ISFIFO #ifdef S_ISFIFO
if (S_ISFIFO(buf_.st_mode)) if (S_ISFIFO(buf_.st_mode))
return "|"; return '|';
#endif #endif
#ifdef S_ISSOCK #ifdef S_ISSOCK
if (S_ISSOCK(buf_.st_mode)) if (S_ISSOCK(buf_.st_mode))
return "="; return '=';
#endif #endif
if (S_ISREG(buf_.st_mode) && (buf_.st_mode & (S_IEXEC | S_IXGRP | S_IXOTH))) if (S_ISREG(buf_.st_mode) && (buf_.st_mode & (S_IEXEC | S_IXGRP | S_IXOTH)))
return "*"; return '*';
return ""; return ' ';
} }
@ -212,45 +240,17 @@ mode_t FileInfo::getMode() const
// should not be in FileInfo // should not be in FileInfo
void FileInfo::modeString(char * str) const string FileInfo::modeString() const
{ {
str[0] = typeLetter(); lyx::Assert(isOK());
char str[11];
str[0] = typeLetter(buf_.st_mode);
flagRWX((buf_.st_mode & 0700) << 0, &str[1]); flagRWX((buf_.st_mode & 0700) << 0, &str[1]);
flagRWX((buf_.st_mode & 0070) << 3, &str[4]); flagRWX((buf_.st_mode & 0070) << 3, &str[4]);
flagRWX((buf_.st_mode & 0007) << 6, &str[7]); flagRWX((buf_.st_mode & 0007) << 6, &str[7]);
lyx::Assert(isOK());
setSticky(buf_.st_mode, str); setSticky(buf_.st_mode, str);
str[10] = 0; str[10] = 0;
} return str;
// should not be in FileInfo
char FileInfo::typeLetter() const
{
lyx::Assert(isOK());
#ifdef S_ISBLK
if (S_ISBLK(buf_.st_mode)) return 'b';
#endif
if (S_ISCHR(buf_.st_mode)) return 'c';
if (S_ISDIR(buf_.st_mode)) return 'd';
if (S_ISREG(buf_.st_mode)) return '-';
#ifdef S_ISFIFO
if (S_ISFIFO(buf_.st_mode)) return 'p';
#endif
#ifdef S_ISLNK
if (S_ISLNK(buf_.st_mode)) return 'l';
#endif
#ifdef S_ISSOCK
if (S_ISSOCK(buf_.st_mode)) return 's';
#endif
#ifdef S_ISMPC
if (S_ISMPC(buf_.st_mode)) return 'm';
#endif
#ifdef S_ISNWK
if (S_ISNWK(buf_.st_mode)) return 'n';
#endif
return '?';
} }

View File

@ -37,12 +37,10 @@ public:
/** Get information about file. /** Get information about file.
If link is true, the information is about the link itself, not If link is true, the information is about the link itself, not
the file that is obtained by tracing the links. */ the file that is obtained by tracing the links. */
explicit explicit FileInfo(string const & path, bool link = false);
FileInfo(string const & path, bool link = false);
/// File descriptor /// File descriptor
explicit explicit FileInfo(int fildes);
FileInfo(int fildes);
/// Query a new file /// Query a new file
FileInfo & newFile(string const & path, bool link = false); FileInfo & newFile(string const & path, bool link = false);
@ -51,16 +49,13 @@ public:
FileInfo & newFile(int fildes); FileInfo & newFile(int fildes);
/// Returns a character describing file type (ls -F) /// Returns a character describing file type (ls -F)
char const * typeIndicator() const; char typeIndicator() const;
/// File protection mode /// File protection mode
mode_t getMode() const; mode_t getMode() const;
/// Constructs standard mode string (ls style) /// Constructs standard mode string (ls style)
void modeString(char * str) const; string modeString() const;
/// returns a letter describing a file type (ls style)
char typeLetter() const;
/// ///
time_t getModificationTime() const; time_t getModificationTime() const;