]> git.lyx.org Git - lyx.git/blob - src/lastfiles.C
Add missing headers. With thanks to Martin...
[lyx.git] / src / lastfiles.C
1 /**
2  * \file lastfiles.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "lastfiles.h"
14 #include "debug.h"
15
16 #include "support/FileInfo.h"
17
18 #include <algorithm>
19 #include <fstream>
20 #include <iterator>
21
22 using namespace lyx::support;
23
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;
31
32
33 LastFiles::LastFiles(string const & filename, bool st, unsigned int num)
34         : dostat(st)
35 {
36         setNumberOfFiles(num);
37         readFile(filename);
38 }
39
40
41 void LastFiles::setNumberOfFiles(unsigned int no)
42 {
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         }
51 }
52
53
54 void LastFiles::readFile(string const & filename)
55 {
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;
61
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         }
70 }
71
72
73 void LastFiles::writeFile(string const & filename) const
74 {
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;
82 }
83
84
85 void LastFiles::newFile(string const & file)
86 {
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();
94 }
95
96
97 string const LastFiles::operator[](unsigned int i) const
98 {
99         if (i < files.size())
100                 return files[i];
101         return string();
102 }