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