1999-09-27 18:44:28 +00:00
|
|
|
/* This file is part of
|
1999-11-15 12:01:38 +00:00
|
|
|
* ======================================================
|
1999-10-02 16:21:10 +00:00
|
|
|
*
|
|
|
|
* LyX, The Document Processor
|
|
|
|
*
|
|
|
|
* Copyright 1995 Matthias Ettrich
|
2000-03-16 04:29:22 +00:00
|
|
|
* Copyright 1995-2000 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>
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
1999-11-04 01:40:20 +00:00
|
|
|
#include <fstream>
|
2000-01-27 01:29:01 +00:00
|
|
|
#include <algorithm>
|
2000-03-28 02:18:55 +00:00
|
|
|
|
|
|
|
#include "support/FileInfo.h"
|
|
|
|
#include "lastfiles.h"
|
|
|
|
#include "debug.h"
|
|
|
|
|
1999-11-08 13:54:04 +00:00
|
|
|
using std::ifstream;
|
2000-05-04 10:57:00 +00:00
|
|
|
using std::getline;
|
1999-11-08 13:54:04 +00:00
|
|
|
using std::ofstream;
|
2000-01-26 14:23:46 +00:00
|
|
|
using std::copy;
|
|
|
|
using std::ostream_iterator;
|
1999-11-08 13:54:04 +00:00
|
|
|
using std::find;
|
2000-03-28 02:18:55 +00:00
|
|
|
using std::endl;
|
1999-09-27 18:44:28 +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)
|
|
|
|
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
|
|
|
|
|
|
|
while(getline(ifs, tmp) && files.size() < num_files) {
|
|
|
|
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
|
|
|
{
|
1999-11-04 01:40:20 +00:00
|
|
|
ofstream ofs(filename.c_str());
|
|
|
|
if (ofs) {
|
2000-01-24 18:34:46 +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.
|
|
|
|
Files::iterator it = find(files.begin(), files.end(), file);
|
|
|
|
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
|
|
|
}
|