#include "lyx_gui_misc.h"
#include "FileInfo.h"
#include "support/path.h" // I know it's OS/2 specific (SMiyata)
-#include "support/syscall.h"
#include "gettext.h"
#include "lyxlib.h"
using std::ifstream;
using std::vector;
-#if 0
-using std::getenv;
-using std::isalpha;
-using std::isalnum;
-using std::popen;
-#endif
-
extern string system_lyxdir;
extern string build_lyxdir;
extern string user_lyxdir;
bool IsLyXFilename(string const & filename)
{
- return contains(filename, ".lyx");
+ return suffixIs(filename, ".lyx");
}
bool IsSGMLFilename(string const & filename)
{
- return contains(filename, ".sgml");
+ return suffixIs(filename, ".sgml");
}
// -1: error- couldn't find out
int IsDirWriteable (string const & path)
{
- string const tmpfl(lyx::tempName(path)); //TmpFileName(path));
-
+ string const tmpfl(lyx::tempName(path, "lyxwritetest"));
+ // We must unlink the tmpfl.
+ lyx::unlink(tmpfl);
+
if (tmpfl.empty()) {
WriteFSAlert(_("LyX Internal Error!"),
_("Could not test if directory is writeable"));
{
// This is a non-error checking C/system implementation
string extension(ext);
- if (extension[0] != '.')
+ if (!extension.empty() && extension[0] != '.')
extension.insert(0, ".");
vector<string> dirlist;
DIR * dirp = ::opendir(dir.c_str());
dirent * dire;
while ((dire = ::readdir(dirp))) {
string const fil = dire->d_name;
- if (prefixIs(fil, extension)) {
+ if (suffixIs(fil, extension)) {
dirlist.push_back(fil);
}
}
// safe because of the gap between unlink and mkdir. (Lgb)
lyx::unlink(tmpfl.c_str());
- if (tmpfl.empty() || lyx::mkdir(tmpfl, 0777)) {
+ if (tmpfl.empty() || lyx::mkdir(tmpfl, 0700)) {
WriteFSAlert(_("Error! Couldn't create temporary directory:"),
tempdir);
return string();
}
-// Strips filename from path
-string const BasePath(string const & fname)
-{
- if (fname.empty())
- return fname;
-
- string::size_type j = fname.rfind('/');
- if (j == string::npos) // no '/' in fname
- return string();
-
- // Strip to basename
- return fname.substr(0,j + 1);
-}
-
-
// Is a filename/path absolute?
bool AbsolutePath(string const & path)
{
buf += '/';
}
- if (!path2.empty()) {
- string::size_type const p2start = path2.find_first_not_of('/');
- string::size_type const p2end = path2.find_last_not_of('/');
- string const tmp = path2.substr(p2start, p2end - p2start + 1);
- buf += tmp + '/';
- }
+ if (!path2.empty())
+ buf += frontStrip(strip(path2, '/'), '/') + '/';
+
return buf;
}
}
}
}
-
-
-bool WriteableDir(string const & name, string & errorMessage)
-{
- errorMessage.erase();
-
- if (!AbsolutePath(name)) {
- errorMessage = N_("The absolute path is required.");
- return false;
- }
-
- FileInfo const tp(name);
- if (!tp.isDir()) {
- errorMessage = N_("Directory does not exist.");
- return false;
- }
-
- if (!tp.writable()) {
- errorMessage = N_("Cannot write to this directory.");
- return false;
- }
-
- return true;
-}
-
-
-bool ReadableDir(string const & name, string & errorMessage)
-{
- errorMessage.erase();
-
- if (!AbsolutePath(name)) {
- errorMessage = N_("The absolute path is required.");
- return false;
- }
-
- FileInfo const tp(name);
- if (!tp.isDir()) {
- errorMessage = N_("Directory does not exist.");
- return false;
- }
-
- if (!tp.readable()) {
- errorMessage = N_("Cannot read this directory.");
- return false;
- }
-
- return true;
-}
-
-
-bool WriteableFile(string const & name, string & errorMessage)
-{
- // A writeable file is either:
- // * An existing file to which we have write access, or
- // * A file that doesn't yet exist but that would exist in a writeable
- // directory.
-
- errorMessage.erase();
-
- if (name.empty()) {
- errorMessage = N_("No file input.");
- return false;
- }
-
- string const dir = OnlyPath(name);
- if (!AbsolutePath(dir)) {
- errorMessage = N_("The absolute path is required.");
- return false;
- }
-
- FileInfo d(name);
- if (!d.isDir()) {
- d.newFile(dir);
- }
-
- if (!d.isDir()) {
- errorMessage = N_("Directory does not exist.");
- return false;
- }
-
- if (!d.writable()) {
- errorMessage = N_("Cannot write to this directory.");
- return false;
- }
-
- FileInfo f(name);
- if (dir == name || f.isDir()) {
- errorMessage = N_("A file is required, not a directory.");
- return false;
- }
-
- if (f.exist() && !f.writable()) {
- errorMessage = N_("Cannot write to this file.");
- return false;
- }
-
- return true;
-}
-
-
-bool ReadableFile(string const & name, string & errorMessage)
-{
- errorMessage.erase();
-
- if (name.empty()) {
- errorMessage = N_("No file input.");
- return false;
- }
-
- string const dir = OnlyPath(name);
- if (!AbsolutePath(dir)) {
- errorMessage = N_("The absolute path is required.");
- return false;
- }
-
- FileInfo d(name);
- if (!d.isDir()) {
- d.newFile(dir);
- }
-
- if (!d.isDir()) {
- errorMessage = N_("Directory does not exist.");
- return false;
- }
-
- if (!d.readable()) {
- errorMessage = N_("Cannot read from this directory.");
- return false;
- }
-
- FileInfo f(name);
- if (dir == name || f.isDir()) {
- errorMessage = N_("A file is required, not a directory.");
- return false;
- }
-
- if (!f.exist()) {
- errorMessage = N_("File does not exist.");
- return false;
- }
-
- if (!f.readable()) {
- errorMessage = N_("Cannot read from this file.");
- return false;
- }
-
- return true;
-}