#include <config.h>
+#include "support/filetools.h"
+
#include "support/convert.h"
+#include "support/debug.h"
#include "support/environment.h"
-#include "support/filetools.h"
-#include "support/fs_extras.h"
+#include "support/gettext.h"
#include "support/lstrings.h"
#include "support/lyxlib.h"
#include "support/os.h"
#include "support/Package.h"
#include "support/Path.h"
#include "support/Systemcall.h"
+#include "support/qstring_helpers.h"
-// FIXME Interface violation
-#include "gettext.h"
-#include "debug.h"
+#include <QDir>
#include <boost/assert.hpp>
-#include <boost/filesystem/operations.hpp>
#include <boost/regex.hpp>
#include <fcntl.h>
#include <fstream>
#include <sstream>
-using std::endl;
-using std::getline;
-using std::make_pair;
-using std::string;
-using std::ifstream;
-using std::ostringstream;
-using std::vector;
-using std::pair;
-
-namespace fs = boost::filesystem;
+using namespace std;
namespace lyx {
namespace support {
// Substitutes spaces with underscores in filename (and path)
-string const makeLatexName(string const & file)
+FileName const makeLatexName(FileName const & file)
{
- string name = onlyFilename(file);
- string const path = onlyPath(file);
+ string name = file.onlyFileName();
+ string const path = file.onlyPath().absFilename() + "/";
// ok so we scan through the string twice, but who cares.
+ // FIXME: in Unicode time this will break for sure! There is
+ // a non-latin world out there...
string const keep = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"@!'()*+,-./0123456789:;<=>?[]`|";
while ((pos = name.find_first_not_of(keep, pos)) != string::npos)
name[pos++] = '_';
- return addName(path, name);
+ FileName latex_name(path + name);
+ latex_name.changeExtension(".tex");
+ return latex_name;
}
#endif
-/// Returns a vector of all files in directory dir having extension ext.
-vector<FileName> const dirList(FileName const & dir, string const & ext)
-{
- // EXCEPTIONS FIXME. Rewrite needed when we turn on exceptions. (Lgb)
- vector<FileName> dirlist;
-
- if (!(dir.exists() && dir.isDirectory())) {
- LYXERR(Debug::FILES)
- << "Directory \"" << dir
- << "\" does not exist to DirList." << endl;
- return dirlist;
- }
-
- string extension;
- if (!ext.empty() && ext[0] != '.')
- extension += '.';
- extension += ext;
-
- string const encoded_dir = dir.toFilesystemEncoding();
- fs::directory_iterator dit(encoded_dir);
- fs::directory_iterator end;
- for (; dit != end; ++dit) {
- string const & fil = dit->leaf();
- if (suffixIs(fil, extension))
- dirlist.push_back(FileName::fromFilesystemEncoding(
- encoded_dir + '/' + fil));
- }
- return dirlist;
-}
-
-
// Returns the real name of file name in directory path, with optional
// extension ext.
FileName const fileSearch(string const & path, string const & name,
string const tmpname = replaceEnvironmentPath(name);
FileName fullname(makeAbsPath(tmpname, path));
// search first without extension, then with it.
- if (fullname.isReadable())
+ if (fullname.isReadableFile())
return fullname;
if (ext.empty())
// We are done.
// fullname.
if (getExtension(fullname.absFilename()) != ext)
fullname = FileName(addExtension(fullname.absFilename(), ext));
- if (fullname.isReadable() || mode == allow_unreadable)
+ if (fullname.isReadableFile() || mode == allow_unreadable)
return fullname;
return FileName();
}
variable. But we don't use the value if the currently
selected locale is the C locale. This is a GNU extension.
- Otherwise, w use a trick to guess what gettext has done:
+ Otherwise, w use a trick to guess what support/gettext.has done:
each po file is able to tell us its name. (JMarc)
*/
static FileName createTmpDir(FileName const & tempdir, string const & mask)
{
- LYXERR(Debug::FILES)
- << "createTmpDir: tempdir=`" << tempdir << "'\n"
- << "createTmpDir: mask=`" << mask << '\'' << endl;
+ LYXERR(Debug::FILES, "createTmpDir: tempdir=`" << tempdir << "'\n"
+ << "createTmpDir: mask=`" << mask << '\'');
- FileName const tmpfl(tempName(tempdir, mask));
- // lyx::tempName actually creates a file to make sure that it
+ FileName const tmpfl = FileName::tempName(tempdir, mask);
+ // FileName::tempName actually creates a file to make sure that it
// stays unique. So we have to delete it before we can create
// a dir with the same name. Note also that we are not thread
// safe because of the gap between unlink and mkdir. (Lgb)
- unlink(tmpfl);
+ tmpfl.removeFile();
- if (tmpfl.empty() || mkdir(tmpfl, 0700)) {
+ if (tmpfl.empty() || !tmpfl.createDirectory(0700)) {
lyxerr << "LyX could not create the temporary directory '"
<< tmpfl << "'" << endl;
return FileName();
package().temp_dir().absFilename() + "/lyx_tmpbuf" +
convert<string>(count++);
- if (mkdir(FileName(tmpfl), 0777)) {
+ if (!FileName(tmpfl).createDirectory(0777)) {
lyxerr << "LyX could not create the temporary directory '"
<< tmpfl << "'" << endl;
return string();
FileName const createLyXTmpDir(FileName const & deflt)
{
- if (!deflt.empty() && deflt.absFilename() != "/tmp") {
- if (mkdir(deflt, 0777)) {
- if (deflt.isDirWritable()) {
- // deflt could not be created because it
- // did exist already, so let's create our own
- // dir inside deflt.
- return createTmpDir(deflt, "lyx_tmpdir");
- } else {
- // some other error occured.
- return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
- }
- } else
- return deflt;
+ if (deflt.empty() || deflt.absFilename() == "/tmp")
+ return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
+
+ if (deflt.createDirectory(0777))
+ return deflt;
+
+ if (deflt.isDirWritable()) {
+ // deflt could not be created because it
+ // did exist already, so let's create our own
+ // dir inside deflt.
+ return createTmpDir(deflt, "lyx_tmpdir");
} else {
+ // some other error occured.
return createTmpDir(FileName("/tmp"), "lyx_tmpdir");
}
}
return filename;
// Find last / or start of filename
- string::size_type j = filename.rfind('/');
+ size_t j = filename.rfind('/');
return j == string::npos ? "./" : filename.substr(0, j + 1);
}
}
// returns absolute path
- return FileName(os::internal_path(tempBase));
+ return FileName(tempBase);
}
}
-// Normalize a path. Constracts path/../path
-// Can't handle "../../" or "/../" (Asger)
-// Also converts paths like /foo//bar ==> /foo/bar
-string const normalizePath(string const & path)
-{
- // Normalize paths like /foo//bar ==> /foo/bar
- static boost::regex regex("/{2,}");
- string const tmppath = boost::regex_merge(path, regex, "/");
-
- fs::path const npath = fs::path(tmppath, fs::no_check).normalize();
-
- if (!npath.is_complete())
- return "./" + npath.string() + '/';
-
- return npath.string() + '/';
-}
-
-
// Search the string for ${VAR} and $VAR and replace VAR using getenv.
string const replaceEnvironmentPath(string const & path)
{
cmd_ret const c = runCommand(kpsecmd);
- LYXERR(Debug::LATEX) << "kpse status = " << c.first << '\n'
- << "kpse result = `" << rtrim(c.second, "\n\r")
- << '\'' << endl;
+ LYXERR(Debug::LATEX, "kpse status = " << c.first << '\n'
+ << "kpse result = `" << rtrim(c.second, "\n\r") << '\'');
if (c.first != -1)
- return FileName(os::internal_path(rtrim(to_utf8(from_filesystem8bit(c.second)),
- "\n\r")));
+ return FileName(rtrim(to_utf8(from_filesystem8bit(c.second)), "\n\r"));
else
return FileName();
}
a += '#';
FileName const autosave(a);
if (autosave.exists())
- unlink(autosave);
+ autosave.removeFile();
}
void readBB_lyxerrMessage(FileName const & file, bool & zipped,
string const & message)
{
- LYXERR(Debug::GRAPHICS) << "[readBB_from_PSFile] "
- << message << std::endl;
+ LYXERR(Debug::GRAPHICS, "[readBB_from_PSFile] " << message);
// FIXME: Why is this func deleting a file? (Lgb)
if (zipped)
- unlink(file);
+ file.removeFile();
}
// end of the file. Than we have in the header:
// %%BoundingBox: (atend)
// In this case we must check the end.
- bool zipped = zippedFile(file);
+ bool zipped = file.isZippedFile();
FileName const file_ = zipped ? unzipFile(file) : file;
- string const format = getFormatFromContents(file_);
+ string const format = file_.guessFormatFromContents();
if (format != "eps" && format != "ps") {
readBB_lyxerrMessage(file_, zipped,"no(e)ps-format");
static boost::regex bbox_re(
"^%%BoundingBox:\\s*([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)\\s+([[:digit:]]+)");
- std::ifstream is(file_.toFilesystemEncoding().c_str());
+ ifstream is(file_.toFilesystemEncoding().c_str());
while (is) {
string s;
getline(is,s);
return cmp;
}
+
} //namespace support
} // namespace lyx