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