]> git.lyx.org Git - lyx.git/blob - src/lastfiles.C
The Gtk patch.
[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 <fstream>
19 #include <algorithm>
20 #include <iterator>
21
22
23 using namespace lyx::support;
24
25 using std::ifstream;
26 using std::ofstream;
27 using std::getline;
28 using std::endl;
29 using std::find;
30 using std::copy;
31 using std::ostream_iterator;
32
33
34 LastFiles::LastFiles(string const & filename, bool st, unsigned int num)
35         : dostat(st)
36 {
37         setNumberOfFiles(num);
38         readFile(filename);
39 }
40
41
42 void LastFiles::setNumberOfFiles(unsigned int no)
43 {
44         if (0 < no && no <= ABSOLUTEMAXLASTFILES)
45                 num_files = no;
46         else {
47                 lyxerr << "LyX: lastfiles: too many files\n"
48                         "\tdefault (=" << int(DEFAULTFILES)
49                        << ") used." << endl;
50                 num_files = DEFAULTFILES;
51         }
52 }
53
54
55 void LastFiles::readFile(string const & filename)
56 {
57         // we will not complain if we can't find filename nor will
58         // we issue a warning. (Lgb)
59         ifstream ifs(filename.c_str());
60         string tmp;
61         FileInfo fileInfo;
62
63         while (getline(ifs, tmp) && files.size() < num_files) {
64                 if (dostat) {
65                         if (!(fileInfo.newFile(tmp).exist() &&
66                               fileInfo.isRegular()))
67                                 continue;
68                 }
69                 files.push_back(tmp);
70         }
71 }
72
73
74 void LastFiles::writeFile(string const & filename) const
75 {
76         ofstream ofs(filename.c_str());
77         if (ofs) {
78                 copy(files.begin(), files.end(),
79                      ostream_iterator<string>(ofs, "\n"));
80         } else
81                 lyxerr << "LyX: Warning: unable to save LastFiles: "
82                        << filename << endl;
83 }
84
85
86 void LastFiles::newFile(string const & file)
87 {
88         // If file already exist, delete it and reinsert at front.
89         Files::iterator it = find(files.begin(), files.end(), file);
90         if (it != files.end())
91                 files.erase(it);
92         files.push_front(file);
93         if (files.size() > num_files)
94                 files.pop_back();
95 }
96
97
98 string const LastFiles::operator[](unsigned int i) const
99 {
100         if (i < files.size())
101                 return files[i];
102         return string();
103 }