This commit was manufactured by cvs2svn to create tag 'lyx-1_3_7'.
[lyx.git] / src / lastfiles.C
blob5af4091202973b5db95d28671fe29278d1c4032a
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
11 #include <config.h>
13 #include "lastfiles.h"
14 #include "debug.h"
16 #include "support/FileInfo.h"
18 #include <fstream>
19 #include <algorithm>
20 #include <iterator>
22 using lyx::FileInfo;
24 using std::ifstream;
25 using std::ofstream;
26 using std::getline;
27 using std::endl;
28 using std::find;
29 using std::copy;
30 using std::ostream_iterator;
33 LastFiles::LastFiles(string const & filename, bool st, unsigned int num)
34         : dostat(st)
36         setNumberOfFiles(num);
37         readFile(filename);
41 void LastFiles::setNumberOfFiles(unsigned int no)
43         if (0 < no && no <= ABSOLUTEMAXLASTFILES)
44                 num_files = no;
45         else {
46                 lyxerr << "LyX: lastfiles: too many files\n"
47                         "\tdefault (=" << int(DEFAULTFILES)
48                        << ") used." << endl;
49                 num_files = DEFAULTFILES;
50         }
54 void LastFiles::readFile(string const & filename)
56         // we will not complain if we can't find filename nor will
57         // we issue a warning. (Lgb)
58         ifstream ifs(filename.c_str());
59         string tmp;
60         FileInfo fileInfo;
62         while (getline(ifs, tmp) && files.size() < num_files) {
63                 if (dostat) {
64                         if (!(fileInfo.newFile(tmp).exist() &&
65                               fileInfo.isRegular()))
66                                 continue;
67                 }
68                 files.push_back(tmp);
69         }
73 void LastFiles::writeFile(string const & filename) const
75         ofstream ofs(filename.c_str());
76         if (ofs) {
77                 copy(files.begin(), files.end(),
78                      ostream_iterator<string>(ofs, "\n"));
79         } else
80                 lyxerr << "LyX: Warning: unable to save LastFiles: "
81                        << filename << endl;
85 void LastFiles::newFile(string const & file)
87         // If file already exist, delete it and reinsert at front.
88         Files::iterator it = find(files.begin(), files.end(), file);
89         if (it != files.end())
90                 files.erase(it);
91         files.push_front(file);
92         if (files.size() > num_files)
93                 files.pop_back();
97 string const LastFiles::operator[](unsigned int i) const
99         if (i < files.size())
100                 return files[i];
101         return string();