1999-09-27 18:44:28 +00:00
|
|
|
/* This file is part of
|
2002-03-21 17:27:08 +00:00
|
|
|
* ======================================================
|
|
|
|
*
|
1999-10-02 16:21:10 +00:00
|
|
|
* LyX, The Document Processor
|
2002-03-21 17:27:08 +00:00
|
|
|
*
|
1999-10-02 16:21:10 +00:00
|
|
|
* Copyright 1995 Matthias Ettrich
|
2001-05-30 13:53:44 +00:00
|
|
|
* Copyright 1995-2001 The LyX Team.
|
1999-10-02 16:21:10 +00:00
|
|
|
*
|
1999-11-15 12:01:38 +00:00
|
|
|
* ====================================================== */
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2000-03-28 02:18:55 +00:00
|
|
|
#include "lastfiles.h"
|
|
|
|
#include "debug.h"
|
|
|
|
|
2002-01-14 15:29:40 +00:00
|
|
|
#include "support/FileInfo.h"
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iterator>
|
|
|
|
|
2002-02-16 15:59:55 +00:00
|
|
|
|
|
|
|
using std::ifstream;
|
|
|
|
using std::ofstream;
|
2000-05-04 10:57:00 +00:00
|
|
|
using std::getline;
|
2000-03-28 02:18:55 +00:00
|
|
|
using std::endl;
|
2002-02-16 15:59:55 +00:00
|
|
|
using std::find;
|
|
|
|
using std::copy;
|
|
|
|
using std::ostream_iterator;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-04-04 21:47:26 +00:00
|
|
|
|
1999-11-04 01:40:20 +00:00
|
|
|
LastFiles::LastFiles(string const & filename, bool st, unsigned int num)
|
1999-09-27 18:44:28 +00:00
|
|
|
: dostat(st)
|
|
|
|
{
|
|
|
|
setNumberOfFiles(num);
|
|
|
|
readFile(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-04 01:40:20 +00:00
|
|
|
void LastFiles::setNumberOfFiles(unsigned int no)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-11-04 01:40:20 +00:00
|
|
|
if (0 < no && no <= ABSOLUTEMAXLASTFILES)
|
1999-09-27 18:44:28 +00:00
|
|
|
num_files = no;
|
|
|
|
else {
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyX: lastfiles: too many files\n"
|
1999-12-01 10:46:58 +00:00
|
|
|
"\tdefault (=" << int(DEFAULTFILES)
|
1999-10-07 18:44:17 +00:00
|
|
|
<< ") used." << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
num_files = DEFAULTFILES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
void LastFiles::readFile(string const & filename)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
// we will not complain if we can't find filename nor will
|
1999-11-04 01:40:20 +00:00
|
|
|
// we issue a warning. (Lgb)
|
2002-02-16 15:59:55 +00:00
|
|
|
ifstream ifs(filename.c_str());
|
1999-10-02 16:21:10 +00:00
|
|
|
string tmp;
|
1999-09-27 18:44:28 +00:00
|
|
|
FileInfo fileInfo;
|
1999-11-04 01:40:20 +00:00
|
|
|
|
2001-12-05 08:04:20 +00:00
|
|
|
while (getline(ifs, tmp) && files.size() < num_files) {
|
1999-11-04 01:40:20 +00:00
|
|
|
if (dostat) {
|
|
|
|
if (!(fileInfo.newFile(tmp).exist() &&
|
|
|
|
fileInfo.isRegular()))
|
|
|
|
continue;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
1999-11-04 01:40:20 +00:00
|
|
|
files.push_back(tmp);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
void LastFiles::writeFile(string const & filename) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2002-02-16 15:59:55 +00:00
|
|
|
ofstream ofs(filename.c_str());
|
1999-11-04 01:40:20 +00:00
|
|
|
if (ofs) {
|
2002-02-16 15:59:55 +00:00
|
|
|
copy(files.begin(), files.end(),
|
|
|
|
ostream_iterator<string>(ofs, "\n"));
|
1999-09-27 18:44:28 +00:00
|
|
|
} else
|
1999-10-07 18:44:17 +00:00
|
|
|
lyxerr << "LyX: Warning: unable to save LastFiles: "
|
|
|
|
<< filename << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
void LastFiles::newFile(string const & file)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-11-04 01:40:20 +00:00
|
|
|
// If file already exist, delete it and reinsert at front.
|
2002-02-16 15:59:55 +00:00
|
|
|
Files::iterator it = find(files.begin(), files.end(), file);
|
1999-11-04 01:40:20 +00:00
|
|
|
if (it != files.end())
|
|
|
|
files.erase(it);
|
|
|
|
files.push_front(file);
|
|
|
|
if (files.size() > num_files)
|
|
|
|
files.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-05 05:17:18 +00:00
|
|
|
string const LastFiles::operator[](unsigned int i) const
|
1999-11-04 01:40:20 +00:00
|
|
|
{
|
|
|
|
if (i < files.size())
|
|
|
|
return files[i];
|
|
|
|
return string();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|