Parts Copyright 1985, 1990, 1993 Free Software Foundation, Inc.
Parts Copyright 1996 Asger Alstrup
- See also filetools.H.
+ See also filetools.h.
lyx-filetool.C : tools functions for file/path handling
this file is part of LyX, the High Level Word Processor
#pragma implementation "filetools.h"
#endif
+#include <cstdlib>
+#include <fcntl.h>
+#include <cerrno>
+#include "debug.h"
+#include "support/lstrings.h"
+
#include "filetools.h"
#include "LSubstring.h"
#include "lyx_gui_misc.h"
bool IsLyXFilename(string const & filename)
{
- return suffixIs(filename, ".lyx");
+ return suffixIs(lowercase(filename), ".lyx");
}
bool IsSGMLFilename(string const & filename)
{
- return suffixIs(filename, ".sgml");
+ return suffixIs(lowercase(filename), ".sgml");
}
int IsFileWriteable (string const & path)
{
FileInfo fi(path);
+ //lyxerr << "fi : " << fi << endl;
+ //lyxerr << "fi.exists" << fi.exist() << endl;
if (fi.access(FileInfo::wperm|FileInfo::rperm)) // read-write
return 1;
if (fi.readable()) // read-only
}
-//returns 1: dir writeable
-// 0: not writeable
-// -1: error- couldn't find out
-int IsDirWriteable (string const & path)
+//returns true: dir writeable
+// false: not writeable
+bool IsDirWriteable (string const & path)
{
+ lyxerr[Debug::FILES] << "IsDirWriteable: " << path << endl;
+
string const tmpfl(lyx::tempName(path, "lyxwritetest"));
- // We must unlink the tmpfl.
+
+ if (tmpfl.empty())
+ return false;
+
lyx::unlink(tmpfl);
-
- if (tmpfl.empty()) {
- WriteFSAlert(_("LyX Internal Error!"),
- _("Could not test if directory is writeable"));
- return -1;
- } else {
- FileInfo fi(path);
- if (fi.writable()) return 1;
- return 0;
- }
+ return true;
}
vector<string> dirlist;
directory_iterator dit("dir");
while (dit != directory_iterator()) {
- string fil = (*dit).filename;
+ string fil = dit->filename;
if (prefixIs(fil, extension)) {
dirlist.push_back(fil);
}
i18nLibFileSearch(string const & dir, string const & name,
string const & ext)
{
- string const lang = token(string(GetEnv("LANG")), '_', 0);
+ // this comment is from intl/dcigettext.c. We try to mimick this
+ // behaviour here.
+ /* The highest priority value is the `LANGUAGE' environment
+ variable. But we don't use the value if the currently
+ selected locale is the C locale. This is a GNU extension. */
+
+ string const lc_all = GetEnv("LC_ALL");
+ string lang = GetEnv("LANGUAGE");
+ if (lang.empty() || lc_all == "C") {
+ lang = lc_all;
+ if (lang.empty()) {
+ lang = GetEnv("LANG");
+ }
+ }
+
+ lang = token(lang, '_', 0);
if (lang.empty() || lang == "C")
return LibFileSearch(dir, name, ext);
continue;
string const unlinkpath = AddName (path, temp);
- lyxerr.debug() << "Deleting file: " << unlinkpath << endl;
+ lyxerr[Debug::FILES] << "Deleting file: " << unlinkpath
+ << endl;
bool deleted = true;
if (FileInfo(unlinkpath).isDir())
string const MakeAbsPath(string const & RelPath, string const & BasePath)
{
// checks for already absolute path
- if (AbsolutePath(RelPath))
+ if (os::is_absolute_path(RelPath))
return RelPath;
// Copies given paths
string TempBase;
- if (AbsolutePath(BasePath))
+ if (os::is_absolute_path(BasePath))
TempBase = BasePath;
else
TempBase = AddPath(lyx::getcwd(), BasePath);
}
-// Is a filename/path absolute?
+/// Returns true is path is absolute
bool AbsolutePath(string const & path)
{
-#ifndef __EMX__
- return (!path.empty() && path[0] == '/');
-#else
- return (!path.empty() && isalpha(static_cast<unsigned char>(path[0])) && path.length() > 1 && path[1] == ':');
-#endif
+ return os::is_absolute_path(path);
}
+
// Create absolute path. If impossible, don't do anything
// Supports ./ and ~/. Later we can add support for ~logname/. (Asger)
string const ExpandPath(string const & path)
{
// checks for already absolute path
string RTemp(ReplaceEnvironmentPath(path));
- if (AbsolutePath(RTemp))
+ if (os::is_absolute_path(RTemp))
return RTemp;
string Temp;
string RTemp;
string Temp;
- if (AbsolutePath(path))
+ if (os::is_absolute_path(path))
RTemp = path;
else
// Make implicit current directory explicit
FileInfo finfo(fname);
if (finfo.exist()) {
ifstream ifs(fname.c_str());
- std::ostringstream ofs;
+ ostringstream ofs;
if (ifs && ofs) {
ofs << ifs.rdbuf();
ifs.close();
// If we backup from home or don't have a relative path,
// this try is no good
- if (prefixIs(relhome, "../") || AbsolutePath(relhome)) {
+ if (prefixIs(relhome, "../") || os::is_absolute_path(relhome)) {
// relative path was no good, just use the original path
relhome = path;
l2 = l1;
<< "kpse result = `" << strip(c.second, '\n')
<< "'" << endl;
if (c.first != -1)
- return strip(strip(c.second, '\n'), '\r');
+ return os::internal_path(strip(strip(c.second, '\n'), '\r'));
else
return string();
}