* moved FbTk/Directory.cc/hh over to FbTk/FileUtil.cc/hh which contain now

file and directory - helproutines.

* created the FileUtil-namespace which contains file-related functions, moved
  those functions out of Directory - code

* changes to the rest of the files to follow those changes
This commit is contained in:
mathias 2004-12-18 01:29:22 +00:00
parent 6458b1b485
commit b3fa5c2428
7 changed files with 171 additions and 129 deletions

View file

@ -1,5 +1,5 @@
// Directory.cc
// Copyright (c) 2002 - 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net)
// FileUtil.cc
// Copyright (c) 2002 - 2004 Henrik Kinnunen (fluxgen at users.sourceforge.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
@ -21,13 +21,70 @@
// $Id$
#include "Directory.hh"
#include "FileUtil.hh"
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
#include <fstream>
using std::ifstream;
using std::ofstream;
using std::cerr;
using std::endl;
namespace FbTk {
time_t FileUtil::getLastStatusChangeTimestamp(const char* filename) {
struct stat buf;
if (filename && !stat(filename, &buf)) {
return buf.st_ctime;
} else
return (time_t)-1;
}
bool FileUtil::isDirectory(const char* filename) {
struct stat buf;
if (!filename || stat(filename, &buf))
return false;
return S_ISDIR(buf.st_mode);
}
bool FileUtil::isRegularFile(const char* filename) {
struct stat buf;
if (!filename || stat(filename, &buf))
return false;
return S_ISREG(buf.st_mode);
}
bool FileUtil::isExecutable(const char* filename) {
struct stat buf;
if (!filename || !stat(filename, &buf))
return false;
return buf.st_mode & S_IXUSR ||
buf.st_mode & S_IXGRP ||
buf.st_mode & S_IXOTH;
}
bool FileUtil::copyFile(const char* from, const char* to) {
ifstream from_file(from);
ofstream to_file(to);
if (!to_file.good())
cerr << "Can't write file '"<<to<<"'."<<endl;
else if (from_file.good()) {
to_file<<from_file.rdbuf();
return true;
} else
cerr << "Can't copy from '"<<from<<"' to '"<<to<<"'."<<endl;
return false;
}
Directory::Directory(const char *dir):m_dir(0),
m_num_entries(0) {
if (dir != 0)
@ -89,30 +146,5 @@ bool Directory::open(const char *dir) {
return true;
}
bool Directory::isDirectory(const std::string &filename) {
struct stat statbuf;
if (stat(filename.c_str(), &statbuf) != 0)
return false;
return S_ISDIR(statbuf.st_mode);
}
bool Directory::isRegularFile(const std::string &filename) {
struct stat statbuf;
if (stat(filename.c_str(), &statbuf) != 0)
return false;
return S_ISREG(statbuf.st_mode);
}
bool Directory::isExecutable(const std::string &filename) {
struct stat statbuf;
if (stat(filename.c_str(), &statbuf) != 0)
return false;
return statbuf.st_mode & S_IXUSR ||
statbuf.st_mode & S_IXGRP ||
statbuf.st_mode & S_IXOTH;
}
}; // end namespace FbTk

View file

@ -1,5 +1,5 @@
// Directory.hh
// Copyright (c) 2002 - 2003 Henrik Kinnunen (fluxgen at users.sourceforge.net)
// FileUtil.hh
// Copyright (c) 2002 - 2004 Henrik Kinnunen (fluxgen at users.sourceforge.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
@ -21,17 +21,47 @@
// $Id$
#ifndef FBTK_DIRECTORY_HH
#define FBTK_DIRECTORY_HH
#ifndef FBTK_FILEUTIL_HH
#define FBTK_FILEUTIL_HH
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif // HAVE_CONFIG_H
#ifdef HAVE_CTIME
#include <ctime>
#else
#include <time.h>
#endif
#include <sys/types.h>
#include <dirent.h>
#include <string>
#include "NotCopyable.hh"
#include <sys/types.h>
#include <dirent.h>
#include <string>
namespace FbTk {
/// Wrapper for file routines
namespace FileUtil {
/// @return true if file is a directory
bool isDirectory(const char* filename);
/// @return true if a file is a regular file
bool isRegularFile(const char* filename);
/// @return true if a file executable for user
bool isExecutable(const char* filename);
/// gets timestamp of last status change
/// @return timestamp
/// @return -1 (failure)
time_t getLastStatusChangeTimestamp(const char* filename);
/// copies file 'from' to 'to'
bool copyFile(const char* from, const char* to);
}; // end of File namespace
/// Wrapper class for DIR * routines
class Directory : private FbTk::NotCopyable {
public:
@ -52,12 +82,6 @@ public:
bool open(const char *dir);
/// @return number of entries in the directory
size_t entries() const { return m_num_entries; }
/// @return true if file is a directory
static bool isDirectory(const std::string &filename);
/// @return true if a file is a regular file
static bool isRegularFile(const std::string &filename);
/// @return true if a file executable for user
static bool isExecutable(const std::string &filename);
private:
std::string m_name;
@ -67,4 +91,4 @@ private:
} // end namespace FbTk
#endif // FBTK_DIRECTORY_HH
#endif // FBTK_FILEUTIL_HH

View file

@ -13,7 +13,7 @@ xpm_SOURCE= ImageXPM.hh ImageXPM.cc
endif
libFbTk_a_SOURCES = App.hh App.cc Color.cc Color.hh Command.hh \
Directory.hh Directory.cc \
FileUtil.hh FileUtil.cc \
EventHandler.hh EventManager.hh EventManager.cc \
FbWindow.hh FbWindow.cc Font.cc Font.hh FontImp.hh \
I18n.cc I18n.hh \

View file

@ -26,7 +26,7 @@
#include "XrmDatabaseHelper.hh"
#include "App.hh"
#include "StringUtil.hh"
#include "Directory.hh"
#include "FileUtil.hh"
#include "I18n.hh"
#include "Image.hh"
@ -87,14 +87,14 @@ bool ThemeManager::load(const std::string &filename, int screen_num) {
std::string location = FbTk::StringUtil::expandFilename(filename);
std::string prefix = "";
if (Directory::isDirectory(filename)) {
if (FileUtil::isDirectory(filename.c_str())) {
prefix = location;
location.append("/theme.cfg");
if (!Directory::isRegularFile(location)) {
if (!FileUtil::isRegularFile(location.c_str())) {
location = prefix;
location.append("/style.cfg");
if (!Directory::isRegularFile(location)) {
if (!FileUtil::isRegularFile(location.c_str())) {
cerr<<"Error loading theme file "<<location<<": not a regular file"<<endl;
return false;
}

View file

@ -46,7 +46,7 @@
#include "FbTk/MacroCommand.hh"
#include "FbTk/SimpleCommand.hh"
#include "FbTk/StringUtil.hh"
#include "FbTk/Directory.hh"
#include "FbTk/FileUtil.hh"
#include "FbTk/MenuSeparator.hh"
#include "FbTk/MenuIcon.hh"
@ -63,7 +63,7 @@ static void createStyleMenu(FbTk::Menu &parent, const std::string &label,
// perform shell style ~ home directory expansion
string stylesdir(FbTk::StringUtil::expandFilename(directory));
if (!FbTk::Directory::isDirectory(stylesdir))
if (!FbTk::FileUtil::isDirectory(stylesdir.c_str()))
return;
FbTk::Directory dir(stylesdir.c_str());
@ -81,11 +81,11 @@ static void createStyleMenu(FbTk::Menu &parent, const std::string &label,
std::string style(stylesdir + '/' + filelist[file_index]);
// add to menu only if the file is a regular file, and not a
// .file or a backup~ file
if ((FbTk::Directory::isRegularFile(style) &&
if ((FbTk::FileUtil::isRegularFile(style.c_str()) &&
(filelist[file_index][0] != '.') &&
(style[style.length() - 1] != '~')) ||
FbTk::Directory::isRegularFile(style + "/theme.cfg") ||
FbTk::Directory::isRegularFile(style + "/style.cfg"))
FbTk::FileUtil::isRegularFile((style + "/theme.cfg").c_str()) ||
FbTk::FileUtil::isRegularFile((style + "/style.cfg").c_str()))
parent.insert(new StyleMenuItem(filelist[file_index], style));
}
// update menu graphics
@ -99,7 +99,7 @@ static void createRootCmdMenu(FbTk::Menu &parent, const string &label,
// perform shell style ~ home directory expansion
string rootcmddir(FbTk::StringUtil::expandFilename(directory));
if (!FbTk::Directory::isDirectory(rootcmddir))
if (!FbTk::FileUtil::isDirectory(rootcmddir.c_str()))
return;
FbTk::Directory dir(rootcmddir.c_str());
@ -118,7 +118,7 @@ static void createRootCmdMenu(FbTk::Menu &parent, const string &label,
string rootcmd(rootcmddir+ '/' + filelist[file_index]);
// add to menu only if the file is a regular file, and not a
// .file or a backup~ file
if ((FbTk::Directory::isRegularFile(rootcmd) &&
if ((FbTk::FileUtil::isRegularFile(rootcmd.c_str()) &&
(filelist[file_index][0] != '.') &&
(rootcmd[rootcmd.length() - 1] != '~')))
parent.insert(new RootCmdMenuItem(filelist[file_index], rootcmd, cmd));
@ -229,7 +229,7 @@ static void translateMenuItem(Parser &parse, ParseItem &pitem) {
safe_counter++;
string newfile = FbTk::StringUtil::expandFilename(str_label);
if (FbTk::Directory::isDirectory(newfile)) {
if (FbTk::FileUtil::isDirectory(newfile.c_str())) {
// inject every file in this directory into the current menu
FbTk::Directory dir(newfile.c_str());
@ -241,7 +241,7 @@ static void translateMenuItem(Parser &parse, ParseItem &pitem) {
for (size_t file_index = 0; file_index < dir.entries(); file_index++) {
std::string thisfile(newfile + '/' + filelist[file_index]);
if (FbTk::Directory::isRegularFile(thisfile) &&
if (FbTk::FileUtil::isRegularFile(thisfile.c_str()) &&
(filelist[file_index][0] != '.') &&
(thisfile[thisfile.length() - 1] != '~')) {
MenuCreator::createFromFile(thisfile, menu, false);

View file

@ -38,6 +38,7 @@
#include "FbTk/I18n.hh"
#include "FbTk/Image.hh"
#include "FbTk/FileUtil.hh"
#include "FbTk/KeyUtil.hh"
#include "FbTk/ImageControl.hh"
#include "FbTk/EventManager.hh"
@ -151,24 +152,11 @@ class Toolbar { };
using namespace std;
using namespace FbTk;
static Window last_bad_window = None;
namespace {
void copyFile(const std::string &from, const std::string &to) {
ifstream from_file(from.c_str());
ofstream to_file(to.c_str());
if (! to_file.good()) {
cerr<<"Can't write file: "<<to<<endl;
} else if (from_file.good()) {
to_file<<from_file.rdbuf(); //copy file
} else {
cerr<<"Can't copy from "<<from<<" to "<<to<<endl;
}
}
Window last_bad_window = None;
} // end anonymous
static int handleXErrors(Display *d, XErrorEvent *e) {
int handleXErrors(Display *d, XErrorEvent *e) {
if (e->error_code == BadWindow)
last_bad_window = e->resourceid;
#ifdef DEBUG
@ -183,10 +171,10 @@ static int handleXErrors(Display *d, XErrorEvent *e) {
}
#endif // !DEBUG
return False;
}
} // end anonymous
//static singleton var
Fluxbox *Fluxbox::s_singleton=0;
@ -573,15 +561,15 @@ void Fluxbox::setupConfigFiles() {
// copy key configuration
if (create_keys)
copyFile(DEFAULTKEYSFILE, keys_file);
FbTk::FileUtil::copyFile(DEFAULTKEYSFILE, keys_file.c_str());
// copy menu configuration
if (create_menu)
copyFile(DEFAULTMENU, menu_file);
FbTk::FileUtil::copyFile(DEFAULTMENU, menu_file.c_str());
// copy init file
if (create_init)
copyFile(DEFAULT_INITFILE, init_file);
FbTk::FileUtil::copyFile(DEFAULT_INITFILE, init_file.c_str());
}
@ -1644,10 +1632,11 @@ bool Fluxbox::menuTimestampsChanged() const {
std::list<MenuTimestamp *>::const_iterator it = m_menu_timestamps.begin();
std::list<MenuTimestamp *>::const_iterator it_end = m_menu_timestamps.end();
for (; it != it_end; ++it) {
struct stat buf;
if (! stat((*it)->filename.c_str(), &buf)) {
if ((*it)->timestamp != buf.st_ctime)
time_t timestamp = FbTk::FileUtil::getLastStatusChangeTimestamp((*it)->filename.c_str());
if (timestamp >= 0) {
if (timestamp != (*it)->timestamp)
return true;
} else
return true;
@ -1683,13 +1672,12 @@ void Fluxbox::rereadMenu(bool show_after_reread) {
void Fluxbox::real_rereadMenu() {
std::list<MenuTimestamp *>::iterator it = m_menu_timestamps.begin();
std::list<MenuTimestamp *>::iterator it_end = m_menu_timestamps.end();
for (; it != it_end; ++it)
delete *it;
m_menu_timestamps.erase(m_menu_timestamps.begin(), m_menu_timestamps.end());
for_each(m_screen_list.begin(), m_screen_list.end(), mem_fun(&BScreen::rereadMenu));
clearMenuFilenames();
for_each(m_screen_list.begin(),
m_screen_list.end(),
mem_fun(&BScreen::rereadMenu));
if(m_show_menu_after_reread) {
@ -1716,13 +1704,13 @@ void Fluxbox::saveMenuFilename(const char *filename) {
}
if (! found) {
struct stat buf;
time_t timestamp = FbTk::FileUtil::getLastStatusChangeTimestamp(filename);
if (! stat(filename, &buf)) {
if (timestamp >= 0) {
MenuTimestamp *ts = new MenuTimestamp;
ts->filename = filename;
ts->timestamp = buf.st_ctime;
ts->timestamp = timestamp;
m_menu_timestamps.push_back(ts);
}
@ -1730,12 +1718,10 @@ void Fluxbox::saveMenuFilename(const char *filename) {
}
void Fluxbox::clearMenuFilenames() {
std::list<MenuTimestamp *>::iterator it = m_menu_timestamps.begin();
std::list<MenuTimestamp *>::iterator it_end = m_menu_timestamps.end();
for (; it != it_end; ++it)
delete *it;
m_menu_timestamps.erase(m_menu_timestamps.begin(), m_menu_timestamps.end());
while(!m_menu_timestamps.empty()) {
delete m_menu_timestamps.back();
m_menu_timestamps.pop_back();
}
}
void Fluxbox::timed_reconfigure() {

View file

@ -27,7 +27,7 @@
#include "EventManager.hh"
#include "Color.hh"
#include "KeyUtil.hh"
#include "Directory.hh"
#include "FileUtil.hh"
#ifdef HAVE_CONFIG_H
#include "config.h"
@ -393,18 +393,18 @@ void FbRun::tabCompleteApps() {
filename;
// directories in dirmode ?
if (add_dirs && dir.isDirectory(fncomplete) &&
if (add_dirs && FbTk::FileUtil::isDirectory(fncomplete.c_str()) &&
filename != ".." && filename != ".") {
m_apps.push_back(fncomplete);
// executables in dirmode ?
} else if (add_dirs && dir.isRegularFile(fncomplete) &&
dir.isExecutable(fncomplete) &&
} else if (add_dirs && FbTk::FileUtil::isRegularFile(fncomplete.c_str()) &&
FbTk::FileUtil::isExecutable(fncomplete.c_str()) &&
(prefix == "" ||
fncomplete.substr(0, prefix.size()) == prefix)) {
m_apps.push_back(fncomplete);
// executables in $PATH ?
} else if (dir.isRegularFile(fncomplete) &&
dir.isExecutable(fncomplete) &&
} else if (FbTk::FileUtil::isRegularFile(fncomplete.c_str()) &&
FbTk::FileUtil::isExecutable(fncomplete.c_str()) &&
(prefix == "" ||
filename.substr(0, prefix.size()) == prefix)) {
m_apps.push_back(filename);
@ -440,7 +440,7 @@ void FbRun::tabCompleteApps() {
}
if (m_apps[apps_item].find(prefix) == 0) {
m_current_apps_item = apps_item;
if (add_dirs && FbTk::Directory::isDirectory(m_apps[m_current_apps_item]))
if (add_dirs && FbTk::FileUtil::isDirectory(m_apps[m_current_apps_item].c_str()))
setText(m_apps[m_current_apps_item] + "/");
else
setText(m_apps[m_current_apps_item]);